1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.development; 18 19 import android.content.Context; 20 import android.os.SystemProperties; 21 import android.util.Log; 22 23 import androidx.annotation.Nullable; 24 import androidx.annotation.VisibleForTesting; 25 import androidx.preference.Preference; 26 import androidx.preference.TwoStatePreference; 27 28 import com.android.settingslib.development.DeveloperOptionsPreferenceController; 29 30 /** 31 * Preference controller to control NFCSNOOP data payload 32 */ 33 public class NfcSnoopLogPreferenceController extends 34 DeveloperOptionsPreferenceController implements Preference.OnPreferenceChangeListener { 35 private static final String TAG = "NfcSnoopLog"; 36 private static final String NFC_NFCSNOOP_LOG_KEY = 37 "nfc_snoop_log"; 38 @VisibleForTesting 39 static final String NFC_NFCSNOOP_LOG_MODE_PROPERTY = 40 "persist.nfc.snoop_log_mode"; 41 @VisibleForTesting 42 static final String NFCSNOOP_MODE_FILTERED = "filtered"; 43 @VisibleForTesting 44 static final String NFCSNOOP_MODE_FULL = "full"; 45 46 @VisibleForTesting 47 boolean mChanged = false; 48 49 @Nullable private final DevelopmentSettingsDashboardFragment mFragment; 50 NfcSnoopLogPreferenceController(Context context, @Nullable DevelopmentSettingsDashboardFragment fragment)51 public NfcSnoopLogPreferenceController(Context context, 52 @Nullable DevelopmentSettingsDashboardFragment fragment) { 53 super(context); 54 mFragment = fragment; 55 } 56 57 @Override getPreferenceKey()58 public String getPreferenceKey() { 59 return NFC_NFCSNOOP_LOG_KEY; 60 } 61 62 @Override onPreferenceChange(Preference preference, Object newValue)63 public boolean onPreferenceChange(Preference preference, Object newValue) { 64 NfcRebootDialog.show(mFragment); 65 mChanged = true; 66 return false; 67 } 68 69 @Override updateState(Preference preference)70 public void updateState(Preference preference) { 71 try { 72 final String currentValue = SystemProperties.get(NFC_NFCSNOOP_LOG_MODE_PROPERTY); 73 ((TwoStatePreference) mPreference).setChecked(currentValue.equals(NFCSNOOP_MODE_FULL)); 74 } catch (RuntimeException e) { 75 Log.e(TAG, "Fail to get nfc system property: " + e.getMessage()); 76 } 77 } 78 79 @Override onDeveloperOptionsSwitchDisabled()80 protected void onDeveloperOptionsSwitchDisabled() { 81 super.onDeveloperOptionsSwitchDisabled(); 82 try { 83 SystemProperties.set(NFC_NFCSNOOP_LOG_MODE_PROPERTY, NFCSNOOP_MODE_FILTERED); 84 ((TwoStatePreference) mPreference).setChecked(false); 85 } catch (RuntimeException e) { 86 Log.e(TAG, "Fail to set nfc system property: " + e.getMessage()); 87 } 88 } 89 90 /** 91 * Check whether the current setting is the default value or not. 92 */ isDefaultValue()93 public boolean isDefaultValue() { 94 try { 95 final String currentValue = SystemProperties.get(NFC_NFCSNOOP_LOG_MODE_PROPERTY); 96 return !currentValue.equals(NFCSNOOP_MODE_FULL); 97 } catch (RuntimeException e) { 98 Log.e(TAG, "Fail to get nfc system property: " + e.getMessage()); 99 } 100 return true; 101 } 102 103 /** 104 * Called when the NfcRebootDialog confirm is clicked. 105 */ onNfcRebootDialogConfirmed()106 public void onNfcRebootDialogConfirmed() { 107 if (!mChanged) { 108 return; 109 } 110 try { 111 final String currentValue = 112 SystemProperties.get(NFC_NFCSNOOP_LOG_MODE_PROPERTY, NFCSNOOP_MODE_FILTERED); 113 if (currentValue.equals(NFCSNOOP_MODE_FILTERED)) { 114 SystemProperties.set(NFC_NFCSNOOP_LOG_MODE_PROPERTY, NFCSNOOP_MODE_FULL); 115 } else { 116 SystemProperties.set(NFC_NFCSNOOP_LOG_MODE_PROPERTY, NFCSNOOP_MODE_FILTERED); 117 } 118 } catch (RuntimeException e) { 119 Log.e(TAG, "Fail to set nfc system property: " + e.getMessage()); 120 } 121 122 } 123 124 /** 125 * Called when the NfcRebootDialog cancel is clicked. 126 */ onNfcRebootDialogCanceled()127 public void onNfcRebootDialogCanceled() { 128 mChanged = false; 129 } 130 } 131