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 NFC vendor verbose logging enable and disable
32  */
33 public class NfcVerboseVendorLogPreferenceController
34         extends DeveloperOptionsPreferenceController
35         implements Preference.OnPreferenceChangeListener {
36     private static final String TAG = "NfcVerboseVendorLog";
37     private static final String NFC_VERBOSE_VENDOR_LOG_KEY = "nfc_verbose_vendor_log";
38     @VisibleForTesting
39     static final String NFC_VERBOSE_VENDOR_LOG_PROPERTY =
40             "persist.nfc.vendor_debug_enabled";
41     @VisibleForTesting
42     static final String VERBOSE_VENDOR_LOG_ENABLED = "true";
43     @VisibleForTesting
44     static final String VERBOSE_VENDOR_LOG_DISABLED = "false";
45 
46     @VisibleForTesting
47     boolean mChanged = false;
48 
49     @Nullable private final DevelopmentSettingsDashboardFragment mFragment;
50 
NfcVerboseVendorLogPreferenceController(Context context, @Nullable DevelopmentSettingsDashboardFragment fragment)51     public NfcVerboseVendorLogPreferenceController(Context context,
52             @Nullable DevelopmentSettingsDashboardFragment fragment) {
53         super(context);
54         mFragment = fragment;
55     }
56 
57     @Override
getPreferenceKey()58     public String getPreferenceKey() {
59         return NFC_VERBOSE_VENDOR_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_VERBOSE_VENDOR_LOG_PROPERTY);
73             ((TwoStatePreference) mPreference)
74                     .setChecked(currentValue.equals(VERBOSE_VENDOR_LOG_ENABLED));
75         } catch (RuntimeException e) {
76             Log.e(TAG, "Fail to get nfc system property: " + e.getMessage());
77         }
78     }
79 
80     @Override
onDeveloperOptionsSwitchDisabled()81     protected void onDeveloperOptionsSwitchDisabled() {
82         super.onDeveloperOptionsSwitchDisabled();
83         try {
84             SystemProperties.set(NFC_VERBOSE_VENDOR_LOG_PROPERTY, VERBOSE_VENDOR_LOG_DISABLED);
85             ((TwoStatePreference) mPreference).setChecked(false);
86         } catch (RuntimeException e) {
87             Log.e(TAG, "Fail to set nfc system property: " + e.getMessage());
88         }
89     }
90 
91     /**
92      * Check whether the current setting is the default value or not.
93      */
isDefaultValue()94     public boolean isDefaultValue() {
95         try {
96             final String currentValue = SystemProperties.get(NFC_VERBOSE_VENDOR_LOG_PROPERTY);
97             return !currentValue.equals(VERBOSE_VENDOR_LOG_ENABLED);
98         } catch (RuntimeException e) {
99             Log.e(TAG, "Fail to get nfc system property: " + e.getMessage());
100         }
101         return true;
102     }
103 
104     /**
105      * Called when the NfcRebootDialog confirm is clicked.
106      */
onNfcRebootDialogConfirmed()107     public void onNfcRebootDialogConfirmed() {
108         if (!mChanged) {
109             return;
110         }
111         try {
112             final String currentValue = SystemProperties
113                     .get(NFC_VERBOSE_VENDOR_LOG_PROPERTY, VERBOSE_VENDOR_LOG_DISABLED);
114             if (currentValue.equals(VERBOSE_VENDOR_LOG_DISABLED)) {
115                 SystemProperties.set(NFC_VERBOSE_VENDOR_LOG_PROPERTY, VERBOSE_VENDOR_LOG_ENABLED);
116             } else {
117                 SystemProperties.set(NFC_VERBOSE_VENDOR_LOG_PROPERTY, VERBOSE_VENDOR_LOG_DISABLED);
118             }
119             updateState(mPreference);
120         } catch (RuntimeException e) {
121             Log.e(TAG, "Fail to set nfc system property: " + e.getMessage());
122         }
123     }
124 
125     /**
126      * Called when the NfcRebootDialog cancel is clicked.
127      */
onNfcRebootDialogCanceled()128     public void onNfcRebootDialogCanceled() {
129         mChanged = false;
130     }
131 }
132