• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.details2;
18 
19 import android.content.Context;
20 import android.net.wifi.WifiConfiguration;
21 import android.net.wifi.WifiManager;
22 
23 import androidx.annotation.NonNull;
24 import androidx.annotation.VisibleForTesting;
25 import androidx.preference.ListPreference;
26 import androidx.preference.Preference;
27 
28 import com.android.settings.R;
29 import com.android.settings.core.BasePreferenceController;
30 import com.android.wifi.flags.Flags;
31 import com.android.wifitrackerlib.WifiEntry;
32 
33 /**
34  * A controller that controls whether the Wi-Fi network is mac randomized or not.
35  */
36 public class WifiPrivacyPreferenceController2 extends BasePreferenceController implements
37         Preference.OnPreferenceChangeListener {
38 
39     private static final String KEY_WIFI_PRIVACY = "privacy";
40     private final WifiManager mWifiManager;
41     private WifiEntry mWifiEntry;
42 
WifiPrivacyPreferenceController2(Context context)43     public WifiPrivacyPreferenceController2(Context context) {
44         super(context, KEY_WIFI_PRIVACY);
45 
46         mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
47     }
48 
setWifiEntry(WifiEntry wifiEntry)49     public void setWifiEntry(WifiEntry wifiEntry) {
50         mWifiEntry = wifiEntry;
51     }
52 
53     @Override
getAvailabilityStatus()54     public int getAvailabilityStatus() {
55         return (!Flags.androidVWifiApi() && mWifiManager.isConnectedMacRandomizationSupported())
56                 ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
57     }
58 
59     @Override
updateState(Preference preference)60     public void updateState(Preference preference) {
61         final ListPreference listPreference = (ListPreference) preference;
62         final int randomizationLevel = getRandomizationValue();
63         final boolean isSelectable = mWifiEntry.canSetPrivacy();
64         preference.setSelectable(isSelectable);
65         listPreference.setValue(Integer.toString(randomizationLevel));
66         updateSummary(listPreference, randomizationLevel);
67 
68         // If the preference cannot be selectable, display a temporary network in the summary.
69         if (!isSelectable) {
70             listPreference.setSummary(R.string.wifi_privacy_settings_ephemeral_summary);
71         }
72     }
73 
74     @Override
onPreferenceChange(@onNull Preference preference, Object newValue)75     public boolean onPreferenceChange(@NonNull Preference preference, Object newValue) {
76         final int privacy = Integer.parseInt((String) newValue);
77         if (mWifiEntry.getPrivacy() == privacy) {
78             // Prevent disconnection + reconnection if settings not changed.
79             return true;
80         }
81         mWifiEntry.setPrivacy(privacy);
82 
83         // To activate changing, we need to reconnect network. WiFi will auto connect to
84         // current network after disconnect(). Only needed when this is connected network.
85         if (mWifiEntry.getConnectedState() == WifiEntry.CONNECTED_STATE_CONNECTED) {
86             mWifiEntry.disconnect(null /* callback */);
87             mWifiEntry.connect(null /* callback */);
88         }
89         updateSummary((ListPreference) preference, privacy);
90         return true;
91     }
92 
93     @VisibleForTesting
getRandomizationValue()94     int getRandomizationValue() {
95         return mWifiEntry.getPrivacy();
96     }
97 
98     private static final int PREF_RANDOMIZATION_PERSISTENT = 0;
99     private static final int PREF_RANDOMIZATION_NONE = 1;
100 
101     /**
102      * Translates a WifiEntry.Privacy value to the matching preference index value.
103      *
104      * @param privacy WifiEntry.Privacy value
105      * @return index value of preference
106      */
translateWifiEntryPrivacyToPrefValue(@ifiEntry.Privacy int privacy)107     public static int translateWifiEntryPrivacyToPrefValue(@WifiEntry.Privacy int privacy) {
108         return (privacy == WifiEntry.PRIVACY_RANDOMIZED_MAC)
109             ? PREF_RANDOMIZATION_PERSISTENT : PREF_RANDOMIZATION_NONE;
110     }
111 
112     /**
113      * Translates the pref value to WifiConfiguration.MacRandomizationSetting value
114      *
115      * @param prefMacRandomized is preference index value
116      * @return WifiConfiguration.MacRandomizationSetting value
117      */
translatePrefValueToWifiConfigSetting(int prefMacRandomized)118     public static int translatePrefValueToWifiConfigSetting(int prefMacRandomized) {
119         return (prefMacRandomized == PREF_RANDOMIZATION_PERSISTENT)
120             ? WifiConfiguration.RANDOMIZATION_AUTO : WifiConfiguration.RANDOMIZATION_NONE;
121     }
122 
updateSummary(ListPreference preference, int macRandomized)123     private void updateSummary(ListPreference preference, int macRandomized) {
124         // Translates value here to set RANDOMIZATION_PERSISTENT as first item in UI for better UX.
125         final int prefMacRandomized = translateWifiEntryPrivacyToPrefValue(macRandomized);
126         preference.setSummary(preference.getEntries()[prefMacRandomized]);
127     }
128 }
129