blob: 546f1e1e3996cd31551537e78f7d2eafde6a9380 [file] [log] [blame]
Yu-Han Yangd8df0d92021-03-08 20:25:49 -08001/*
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 */
16package com.android.settings.location;
17
18import android.content.Context;
19import android.net.wifi.WifiManager;
20import android.widget.Switch;
21
22import androidx.preference.PreferenceScreen;
23
24import com.android.settings.R;
25import com.android.settings.core.TogglePreferenceController;
26import com.android.settingslib.widget.MainSwitchPreference;
27import com.android.settingslib.widget.OnMainSwitchChangeListener;
28
29/**
30 * Preference controller for Wi-Fi scanning main switch.
31 */
32public 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
38 public WifiScanningMainSwitchPreferenceController(Context context) {
39 super(context, KEY_WIFI_SCANNING_SWITCH);
40 mWifiManager = context.getSystemService(WifiManager.class);
41 }
42
43 @Override
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
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
60 public boolean isChecked() {
61 return mWifiManager.isScanAlwaysAvailable();
62 }
63
64 @Override
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
Jason Chiu2989c502021-11-01 00:38:49 +080072 public int getSliceHighlightMenuRes() {
73 return R.string.menu_key_location;
74 }
75
76 @Override
Yu-Han Yangd8df0d92021-03-08 20:25:49 -080077 public void onSwitchChanged(Switch switchView, boolean isChecked) {
78 if (isChecked != isChecked()) {
79 setChecked(isChecked);
80 }
81 }
82}