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