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 android.util.Log; 26 27 import androidx.annotation.VisibleForTesting; 28 import androidx.preference.ListPreference; 29 import androidx.preference.Preference; 30 import androidx.preference.SwitchPreference; 31 32 import com.android.settings.R; 33 import com.android.settings.core.PreferenceControllerMixin; 34 import com.android.settingslib.development.DeveloperOptionsPreferenceController; 35 36 public class BluetoothSnoopLogPreferenceController extends DeveloperOptionsPreferenceController 37 implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin { 38 39 private static final String PREFERENCE_KEY = "bt_hci_snoop_log"; 40 @VisibleForTesting 41 static final int BTSNOOP_LOG_MODE_DISABLED_INDEX = 0; 42 @VisibleForTesting 43 static final int BTSNOOP_LOG_MODE_FILTERED_INDEX = 1; 44 @VisibleForTesting 45 static final int BTSNOOP_LOG_MODE_FULL_INDEX = 2; 46 @VisibleForTesting 47 static final String BLUETOOTH_BTSNOOP_LOG_MODE_PROPERTY = "persist.bluetooth.btsnooplogmode"; 48 49 private final String[] mListValues; 50 private final String[] mListEntries; 51 BluetoothSnoopLogPreferenceController(Context context)52 public BluetoothSnoopLogPreferenceController(Context context) { 53 super(context); 54 mListValues = context.getResources().getStringArray(R.array.bt_hci_snoop_log_values); 55 mListEntries = context.getResources().getStringArray(R.array.bt_hci_snoop_log_entries); 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 = Settings.Global.getString(mContext.getContentResolver(), 65 Settings.Global.BLUETOOTH_BTSNOOP_DEFAULT_MODE); 66 67 for (int i = 0; i < mListValues.length; i++) { 68 if (TextUtils.equals(default_mode, mListValues[i])) { 69 return i; 70 } 71 } 72 73 return BTSNOOP_LOG_MODE_DISABLED_INDEX; 74 } 75 76 @Override getPreferenceKey()77 public String getPreferenceKey() { 78 return PREFERENCE_KEY; 79 } 80 81 @Override onPreferenceChange(Preference preference, Object newValue)82 public boolean onPreferenceChange(Preference preference, Object newValue) { 83 SystemProperties.set(BLUETOOTH_BTSNOOP_LOG_MODE_PROPERTY, newValue.toString()); 84 updateState(mPreference); 85 return true; 86 } 87 88 @Override updateState(Preference preference)89 public void updateState(Preference preference) { 90 final ListPreference listPreference = (ListPreference) preference; 91 final String currentValue = SystemProperties.get(BLUETOOTH_BTSNOOP_LOG_MODE_PROPERTY); 92 93 int index = getDefaultModeIndex(); 94 for (int i = 0; i < mListValues.length; i++) { 95 if (TextUtils.equals(currentValue, mListValues[i])) { 96 index = i; 97 break; 98 } 99 } 100 listPreference.setValue(mListValues[index]); 101 listPreference.setSummary(mListEntries[index]); 102 } 103 104 @Override onDeveloperOptionsSwitchDisabled()105 protected void onDeveloperOptionsSwitchDisabled() { 106 super.onDeveloperOptionsSwitchDisabled(); 107 SystemProperties.set(BLUETOOTH_BTSNOOP_LOG_MODE_PROPERTY, null); 108 ((ListPreference) mPreference).setValue(mListValues[getDefaultModeIndex()]); 109 ((ListPreference) mPreference).setSummary(mListEntries[getDefaultModeIndex()]); 110 } 111 } 112