1 /*
2  * Copyright (C) 2017 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.Build;
21 import android.os.SystemProperties;
22 import android.provider.Settings;
23 import android.text.TextUtils;
24 
25 import androidx.annotation.Nullable;
26 import androidx.annotation.VisibleForTesting;
27 import androidx.preference.ListPreference;
28 import androidx.preference.Preference;
29 
30 import com.android.settings.core.PreferenceControllerMixin;
31 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
32 
33 public class BluetoothSnoopLogPreferenceController extends DeveloperOptionsPreferenceController
34         implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
35 
36     private static final String PREFERENCE_KEY = "bt_hci_snoop_log";
37     @VisibleForTesting static final int BTSNOOP_LOG_MODE_DISABLED_INDEX = 0;
38     @VisibleForTesting static final int BTSNOOP_LOG_MODE_FILTERED_INDEX = 1;
39     @VisibleForTesting static final int BTSNOOP_LOG_MODE_FULL_INDEX = 2;
40 
41     @VisibleForTesting
42     static final String BLUETOOTH_BTSNOOP_LOG_MODE_PROPERTY = "persist.bluetooth.btsnooplogmode";
43 
44     private final String[] mListValues;
45     private final String[] mListEntries;
46     @Nullable private DevelopmentSettingsDashboardFragment mFragment;
47 
BluetoothSnoopLogPreferenceController( Context context, @Nullable DevelopmentSettingsDashboardFragment fragment)48     public BluetoothSnoopLogPreferenceController(
49             Context context, @Nullable DevelopmentSettingsDashboardFragment fragment) {
50         super(context);
51         mListValues = context.getResources()
52                 .getStringArray(com.android.settingslib.R.array.bt_hci_snoop_log_values);
53         mListEntries = context.getResources()
54                 .getStringArray(com.android.settingslib.R.array.bt_hci_snoop_log_entries);
55         mFragment = fragment;
56     }
57 
58     // Default mode is DISABLED. It can also be changed by modifying the global setting.
getDefaultModeIndex()59     public int getDefaultModeIndex() {
60         if (!Build.IS_DEBUGGABLE) {
61             return BTSNOOP_LOG_MODE_DISABLED_INDEX;
62         }
63 
64         final String default_mode =
65                 Settings.Global.getString(
66                         mContext.getContentResolver(),
67                         Settings.Global.BLUETOOTH_BTSNOOP_DEFAULT_MODE);
68 
69         for (int i = 0; i < mListValues.length; i++) {
70             if (TextUtils.equals(default_mode, mListValues[i])) {
71                 return i;
72             }
73         }
74 
75         return BTSNOOP_LOG_MODE_DISABLED_INDEX;
76     }
77 
78     @Override
getPreferenceKey()79     public String getPreferenceKey() {
80         return PREFERENCE_KEY;
81     }
82 
83     @Override
onPreferenceChange(Preference preference, Object newValue)84     public boolean onPreferenceChange(Preference preference, Object newValue) {
85         SystemProperties.set(BLUETOOTH_BTSNOOP_LOG_MODE_PROPERTY, newValue.toString());
86         updateState(mPreference);
87         if (mFragment != null) {
88             mFragment.onSettingChanged();
89         }
90         return true;
91     }
92 
93     @Override
updateState(Preference preference)94     public void updateState(Preference preference) {
95         final ListPreference listPreference = (ListPreference) preference;
96         final String currentValue = SystemProperties.get(BLUETOOTH_BTSNOOP_LOG_MODE_PROPERTY);
97 
98         int index = getDefaultModeIndex();
99         for (int i = 0; i < mListValues.length; i++) {
100             if (TextUtils.equals(currentValue, mListValues[i])) {
101                 index = i;
102                 break;
103             }
104         }
105         listPreference.setValue(mListValues[index]);
106         listPreference.setSummary(mListEntries[index]);
107     }
108 
109     @Override
onDeveloperOptionsSwitchDisabled()110     protected void onDeveloperOptionsSwitchDisabled() {
111         super.onDeveloperOptionsSwitchDisabled();
112         SystemProperties.set(BLUETOOTH_BTSNOOP_LOG_MODE_PROPERTY, null);
113         ((ListPreference) mPreference).setValue(mListValues[getDefaultModeIndex()]);
114         ((ListPreference) mPreference).setSummary(mListEntries[getDefaultModeIndex()]);
115     }
116 }
117