1 /* 2 * Copyright (C) 2018 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.wifi.details; 18 19 import android.content.Context; 20 import android.net.wifi.WifiConfiguration; 21 import android.net.wifi.WifiInfo; 22 import android.net.wifi.WifiManager; 23 24 import androidx.annotation.VisibleForTesting; 25 import androidx.preference.DropDownPreference; 26 import androidx.preference.Preference; 27 import androidx.preference.PreferenceScreen; 28 29 import com.android.settings.R; 30 import com.android.settings.core.BasePreferenceController; 31 import com.android.settings.wifi.WifiDialog; 32 import com.android.settingslib.core.AbstractPreferenceController; 33 34 /** 35 * {@link AbstractPreferenceController} that controls whether the wifi network is mac randomized 36 * or not. 37 * 38 * Migrating from Wi-Fi SettingsLib to to WifiTrackerLib, this object will be removed in the near 39 * future, please develop in 40 * {@link com.android.settings.wifi.details2.WifiPrivacyPreferenceController2}. 41 */ 42 public class WifiPrivacyPreferenceController extends BasePreferenceController implements 43 Preference.OnPreferenceChangeListener, WifiDialog.WifiDialogListener { 44 45 private static final String KEY_WIFI_PRIVACY = "privacy"; 46 private WifiConfiguration mWifiConfiguration; 47 private WifiManager mWifiManager; 48 private boolean mIsEphemeral = false; 49 private boolean mIsPasspoint = false; 50 private Preference mPreference; 51 WifiPrivacyPreferenceController(Context context)52 public WifiPrivacyPreferenceController(Context context) { 53 super(context, KEY_WIFI_PRIVACY); 54 mWifiConfiguration = null; 55 mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 56 } 57 setWifiConfiguration(WifiConfiguration wifiConfiguration)58 public void setWifiConfiguration(WifiConfiguration wifiConfiguration) { 59 mWifiConfiguration = wifiConfiguration; 60 } 61 setIsEphemeral(boolean isEphemeral)62 public void setIsEphemeral(boolean isEphemeral) { 63 mIsEphemeral = isEphemeral; 64 } 65 setIsPasspoint(boolean isPasspoint)66 public void setIsPasspoint(boolean isPasspoint) { 67 mIsPasspoint = isPasspoint; 68 } 69 70 @Override getAvailabilityStatus()71 public int getAvailabilityStatus() { 72 return mWifiManager.isConnectedMacRandomizationSupported() 73 ? AVAILABLE : CONDITIONALLY_UNAVAILABLE; 74 } 75 76 @Override displayPreference(PreferenceScreen screen)77 public void displayPreference(PreferenceScreen screen) { 78 super.displayPreference(screen); 79 mPreference = screen.findPreference(getPreferenceKey()); 80 } 81 82 @Override updateState(Preference preference)83 public void updateState(Preference preference) { 84 final DropDownPreference dropDownPreference = (DropDownPreference) preference; 85 final int randomizationLevel = getRandomizationValue(); 86 dropDownPreference.setValue(Integer.toString(randomizationLevel)); 87 updateSummary(dropDownPreference, randomizationLevel); 88 89 // Makes preference not selectable, when this is a ephemeral network. 90 if (mIsEphemeral || mIsPasspoint) { 91 preference.setSelectable(false); 92 dropDownPreference.setSummary(R.string.wifi_privacy_settings_ephemeral_summary); 93 } 94 } 95 96 @Override onPreferenceChange(Preference preference, Object newValue)97 public boolean onPreferenceChange(Preference preference, Object newValue) { 98 if (mWifiConfiguration != null) { 99 mWifiConfiguration.macRandomizationSetting = Integer.parseInt((String) newValue); 100 mWifiManager.updateNetwork(mWifiConfiguration); 101 102 // To activate changing, we need to reconnect network. WiFi will auto connect to 103 // current network after disconnect(). Only needed when this is connected network. 104 final WifiInfo wifiInfo = mWifiManager.getConnectionInfo(); 105 if (wifiInfo != null && wifiInfo.getNetworkId() == mWifiConfiguration.networkId) { 106 mWifiManager.disconnect(); 107 } 108 } 109 updateSummary((DropDownPreference) preference, Integer.parseInt((String) newValue)); 110 return true; 111 } 112 113 @VisibleForTesting getRandomizationValue()114 int getRandomizationValue() { 115 if (mWifiConfiguration != null) { 116 return mWifiConfiguration.macRandomizationSetting; 117 } 118 return WifiConfiguration.RANDOMIZATION_PERSISTENT; 119 } 120 121 private static final int PREF_RANDOMIZATION_PERSISTENT = 0; 122 private static final int PREF_RANDOMIZATION_NONE = 1; 123 124 /** 125 * Returns preference index value. 126 * 127 * @param macRandomized is mac randomized value 128 * @return index value of preference 129 */ translateMacRandomizedValueToPrefValue(int macRandomized)130 public static int translateMacRandomizedValueToPrefValue(int macRandomized) { 131 return (macRandomized == WifiConfiguration.RANDOMIZATION_PERSISTENT) 132 ? PREF_RANDOMIZATION_PERSISTENT : PREF_RANDOMIZATION_NONE; 133 } 134 135 /** 136 * Returns mac randomized value. 137 * 138 * @param prefMacRandomized is preference index value 139 * @return mac randomized value 140 */ translatePrefValueToMacRandomizedValue(int prefMacRandomized)141 public static int translatePrefValueToMacRandomizedValue(int prefMacRandomized) { 142 return (prefMacRandomized == PREF_RANDOMIZATION_PERSISTENT) 143 ? WifiConfiguration.RANDOMIZATION_PERSISTENT : WifiConfiguration.RANDOMIZATION_NONE; 144 } 145 updateSummary(DropDownPreference preference, int macRandomized)146 private void updateSummary(DropDownPreference preference, int macRandomized) { 147 // Translates value here to set RANDOMIZATION_PERSISTENT as first item in UI for better UX. 148 final int prefMacRandomized = translateMacRandomizedValueToPrefValue(macRandomized); 149 preference.setSummary(preference.getEntries()[prefMacRandomized]); 150 } 151 152 @Override onSubmit(WifiDialog dialog)153 public void onSubmit(WifiDialog dialog) { 154 if (dialog.getController() != null) { 155 final WifiConfiguration newConfig = dialog.getController().getConfig(); 156 if (newConfig == null || mWifiConfiguration == null) { 157 return; 158 } 159 160 if (newConfig.macRandomizationSetting != mWifiConfiguration.macRandomizationSetting) { 161 mWifiConfiguration = newConfig; 162 onPreferenceChange(mPreference, String.valueOf(newConfig.macRandomizationSetting)); 163 } 164 } 165 } 166 } 167