| Jong Wook Kim | 8c6d8da | 2018-01-05 18:42:36 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settings.development; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.provider.Settings; |
| Fan Zhang | de11704 | 2018-09-04 13:52:15 -0700 | [diff] [blame^] | 21 | |
| Aurimas Liutikas | 03fcde3 | 2018-04-17 11:22:43 -0700 | [diff] [blame] | 22 | import androidx.annotation.VisibleForTesting; |
| Aurimas Liutikas | 03fcde3 | 2018-04-17 11:22:43 -0700 | [diff] [blame] | 23 | import androidx.preference.Preference; |
| Fan Zhang | de11704 | 2018-09-04 13:52:15 -0700 | [diff] [blame^] | 24 | import androidx.preference.SwitchPreference; |
| Jong Wook Kim | 8c6d8da | 2018-01-05 18:42:36 -0800 | [diff] [blame] | 25 | |
| 26 | import com.android.settings.R; |
| 27 | import com.android.settings.core.PreferenceControllerMixin; |
| 28 | import com.android.settingslib.development.DeveloperOptionsPreferenceController; |
| 29 | |
| 30 | public 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 Kim | 8c6d8da | 2018-01-05 18:42:36 -0800 | [diff] [blame] | 42 | 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 Kim | 8c6d8da | 2018-01-05 18:42:36 -0800 | [diff] [blame] | 58 | 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 Ling | 4fbf04c | 2018-03-01 10:33:14 -0800 | [diff] [blame] | 70 | ((SwitchPreference) mPreference).setChecked(enableMode != SETTING_VALUE_OFF); |
| Jong Wook Kim | 8c6d8da | 2018-01-05 18:42:36 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
| 74 | protected void onDeveloperOptionsSwitchDisabled() { |
| Doris Ling | 4fbf04c | 2018-03-01 10:33:14 -0800 | [diff] [blame] | 75 | super.onDeveloperOptionsSwitchDisabled(); |
| Jong Wook Kim | 8c6d8da | 2018-01-05 18:42:36 -0800 | [diff] [blame] | 76 | Settings.Global.putInt(mContext.getContentResolver(), |
| 77 | Settings.Global.WIFI_CONNECTED_MAC_RANDOMIZATION_ENABLED, SETTING_VALUE_OFF); |
| Doris Ling | 4fbf04c | 2018-03-01 10:33:14 -0800 | [diff] [blame] | 78 | ((SwitchPreference) mPreference).setChecked(false); |
| Jong Wook Kim | 8c6d8da | 2018-01-05 18:42:36 -0800 | [diff] [blame] | 79 | } |
| 80 | } |