| Yu-Han Yang | d8df0d9 | 2021-03-08 20:25:49 -0800 | [diff] [blame] | 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 | |
| 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 Chiu | 2989c50 | 2021-11-01 00:38:49 +0800 | [diff] [blame^] | 72 | public int getSliceHighlightMenuRes() { |
| 73 | return R.string.menu_key_location; |
| 74 | } |
| 75 | |
| 76 | @Override |
| Yu-Han Yang | d8df0d9 | 2021-03-08 20:25:49 -0800 | [diff] [blame] | 77 | public void onSwitchChanged(Switch switchView, boolean isChecked) { |
| 78 | if (isChecked != isChecked()) { |
| 79 | setChecked(isChecked); |
| 80 | } |
| 81 | } |
| 82 | } |