blob: fed07f654361b8f1e5bda8dfca1fe63b63062bdd [file] [log] [blame]
Jong Wook Kim8c6d8da2018-01-05 18:42:36 -08001/*
2 * Copyright (C) 2017 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
17package com.android.settings.development;
18
19import android.content.Context;
20import android.provider.Settings;
Fan Zhangde117042018-09-04 13:52:15 -070021
Aurimas Liutikas03fcde32018-04-17 11:22:43 -070022import androidx.annotation.VisibleForTesting;
Aurimas Liutikas03fcde32018-04-17 11:22:43 -070023import androidx.preference.Preference;
Fan Zhangde117042018-09-04 13:52:15 -070024import androidx.preference.SwitchPreference;
Jong Wook Kim8c6d8da2018-01-05 18:42:36 -080025
26import com.android.settings.R;
27import com.android.settings.core.PreferenceControllerMixin;
28import com.android.settingslib.development.DeveloperOptionsPreferenceController;
29
30public class WifiConnectedMacRandomizationPreferenceController extends
31 DeveloperOptionsPreferenceController implements Preference.OnPreferenceChangeListener,
32 PreferenceControllerMixin {
33
34 private static final String WIFI_CONNECTED_MAC_RANDOMIZATION_KEY =
35 "wifi_connected_mac_randomization";
36
37 @VisibleForTesting
38 static final int SETTING_VALUE_ON = 1;
39 @VisibleForTesting
40 static final int SETTING_VALUE_OFF = 0;
41
Jong Wook Kim8c6d8da2018-01-05 18:42:36 -080042 public WifiConnectedMacRandomizationPreferenceController(Context context) {
43 super(context);
44 }
45
46 @Override
47 public boolean isAvailable() {
48 return mContext.getResources().getBoolean(
49 R.bool.config_wifi_support_connected_mac_randomization);
50 }
51
52 @Override
53 public String getPreferenceKey() {
54 return WIFI_CONNECTED_MAC_RANDOMIZATION_KEY;
55 }
56
57 @Override
Jong Wook Kim8c6d8da2018-01-05 18:42:36 -080058 public boolean onPreferenceChange(Preference preference, Object newValue) {
59 final boolean isEnabled = (Boolean) newValue;
60 Settings.Global.putInt(mContext.getContentResolver(),
61 Settings.Global.WIFI_CONNECTED_MAC_RANDOMIZATION_ENABLED,
62 isEnabled ? SETTING_VALUE_ON : SETTING_VALUE_OFF);
63 return true;
64 }
65
66 @Override
67 public void updateState(Preference preference) {
68 final int enableMode = Settings.Global.getInt(mContext.getContentResolver(),
69 Settings.Global.WIFI_CONNECTED_MAC_RANDOMIZATION_ENABLED, SETTING_VALUE_OFF);
Doris Ling4fbf04c2018-03-01 10:33:14 -080070 ((SwitchPreference) mPreference).setChecked(enableMode != SETTING_VALUE_OFF);
Jong Wook Kim8c6d8da2018-01-05 18:42:36 -080071 }
72
73 @Override
74 protected void onDeveloperOptionsSwitchDisabled() {
Doris Ling4fbf04c2018-03-01 10:33:14 -080075 super.onDeveloperOptionsSwitchDisabled();
Jong Wook Kim8c6d8da2018-01-05 18:42:36 -080076 Settings.Global.putInt(mContext.getContentResolver(),
77 Settings.Global.WIFI_CONNECTED_MAC_RANDOMIZATION_ENABLED, SETTING_VALUE_OFF);
Doris Ling4fbf04c2018-03-01 10:33:14 -080078 ((SwitchPreference) mPreference).setChecked(false);
Jong Wook Kim8c6d8da2018-01-05 18:42:36 -080079 }
80}