1 /* 2 * Copyright 2021 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 package com.android.settings.location; 17 18 import android.content.Context; 19 import android.provider.Settings; 20 import android.widget.Switch; 21 22 import androidx.preference.PreferenceScreen; 23 24 import com.android.settings.R; 25 import com.android.settings.core.TogglePreferenceController; 26 import com.android.settingslib.widget.MainSwitchPreference; 27 import com.android.settingslib.widget.OnMainSwitchChangeListener; 28 29 /** 30 * Preference controller for Bluetooth scanning main switch. 31 */ 32 public class BluetoothScanningMainSwitchPreferenceController extends TogglePreferenceController 33 implements OnMainSwitchChangeListener { 34 35 private static final String KEY_BLUETOOTH_SCANNING_SWITCH = "bluetooth_always_scanning_switch"; 36 BluetoothScanningMainSwitchPreferenceController(Context context)37 public BluetoothScanningMainSwitchPreferenceController(Context context) { 38 super(context, KEY_BLUETOOTH_SCANNING_SWITCH); 39 } 40 41 @Override displayPreference(PreferenceScreen screen)42 public void displayPreference(PreferenceScreen screen) { 43 super.displayPreference(screen); 44 MainSwitchPreference pref = screen.findPreference(getPreferenceKey()); 45 pref.addOnSwitchChangeListener(this); 46 pref.updateStatus(isChecked()); 47 } 48 49 @Override getAvailabilityStatus()50 public int getAvailabilityStatus() { 51 return mContext.getResources().getBoolean(R.bool.config_show_location_scanning) 52 ? AVAILABLE 53 : UNSUPPORTED_ON_DEVICE; 54 } 55 56 @Override isChecked()57 public boolean isChecked() { 58 return Settings.Global.getInt(mContext.getContentResolver(), 59 Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE, 0) == 1; 60 } 61 62 @Override setChecked(boolean isChecked)63 public boolean setChecked(boolean isChecked) { 64 Settings.Global.putInt(mContext.getContentResolver(), 65 Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE, isChecked ? 1 : 0); 66 // Returning true means the underlying setting is updated. 67 return true; 68 } 69 70 @Override getSliceHighlightMenuRes()71 public int getSliceHighlightMenuRes() { 72 return R.string.menu_key_location; 73 } 74 75 @Override onSwitchChanged(Switch switchView, boolean isChecked)76 public void onSwitchChanged(Switch switchView, boolean isChecked) { 77 if (isChecked != isChecked()) { 78 setChecked(isChecked); 79 } 80 } 81 } 82