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.net.wifi.WifiManager;
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 Wi-Fi scanning main switch.
31  */
32 public class WifiScanningMainSwitchPreferenceController extends TogglePreferenceController
33         implements OnMainSwitchChangeListener {
34 
35     private static final String KEY_WIFI_SCANNING_SWITCH = "wifi_always_scanning_switch";
36     private final WifiManager mWifiManager;
37 
WifiScanningMainSwitchPreferenceController(Context context)38     public WifiScanningMainSwitchPreferenceController(Context context) {
39         super(context, KEY_WIFI_SCANNING_SWITCH);
40         mWifiManager = context.getSystemService(WifiManager.class);
41     }
42 
43     @Override
displayPreference(PreferenceScreen screen)44     public void displayPreference(PreferenceScreen screen) {
45         super.displayPreference(screen);
46 
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                 ? AVAILABLE
56                 : UNSUPPORTED_ON_DEVICE;
57     }
58 
59     @Override
isChecked()60     public boolean isChecked() {
61         return mWifiManager.isScanAlwaysAvailable();
62     }
63 
64     @Override
setChecked(boolean isChecked)65     public boolean setChecked(boolean isChecked) {
66         mWifiManager.setScanAlwaysAvailable(isChecked);
67         // Returning true means the underlying setting is updated.
68         return true;
69     }
70 
71     @Override
getSliceHighlightMenuRes()72     public int getSliceHighlightMenuRes() {
73         return R.string.menu_key_location;
74     }
75 
76     @Override
onSwitchChanged(Switch switchView, boolean isChecked)77     public void onSwitchChanged(Switch switchView, boolean isChecked) {
78         if (isChecked != isChecked()) {
79             setChecked(isChecked);
80         }
81     }
82 }
83