Merge "[Wi-Fi] Wrong MAC address shown in Settings."

This commit is contained in:
Goven Liu
2019-10-04 01:13:03 +00:00
committed by Android (Google) Code Review
11 changed files with 78 additions and 452 deletions

View File

@@ -20,8 +20,6 @@ import static android.content.Context.WIFI_SERVICE;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import com.android.settings.R;
@@ -39,7 +37,6 @@ public class ConfigureWifiSettings extends DashboardFragment {
private static final String TAG = "ConfigureWifiSettings";
public static final String KEY_IP_ADDRESS = "current_ip_address";
public static final int WIFI_WAKEUP_REQUEST_CODE = 600;
private WifiWakeupPreferenceController mWifiWakeupPreferenceController;
@@ -73,8 +70,6 @@ public class ConfigureWifiSettings extends DashboardFragment {
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
final WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
final List<AbstractPreferenceController> controllers = new ArrayList<>();
controllers.add(new WifiInfoPreferenceController(context, getSettingsLifecycle(),
wifiManager));
controllers.add(new WifiP2pPreferenceController(context, getSettingsLifecycle(),
wifiManager));
return controllers;
@@ -107,24 +102,6 @@ public class ConfigureWifiSettings extends DashboardFragment {
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.wifi_configure_settings) {
@Override
public List<String> getNonIndexableKeys(Context context) {
List<String> keys = super.getNonIndexableKeys(context);
// If connected to WiFi, this IP address will be the same as the Status IP.
// Or, if there is no connection they will say unavailable.
ConnectivityManager cm = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info == null
|| info.getType() == ConnectivityManager.TYPE_WIFI) {
keys.add(KEY_IP_ADDRESS);
}
return keys;
}
protected boolean isPageSearchEnabled(Context context) {
return context.getResources()
.getBoolean(R.bool.config_show_wifi_settings);

View File

@@ -1,132 +0,0 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.wifi;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.text.TextUtils;
import androidx.core.text.BidiFormatter;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnPause;
import com.android.settingslib.core.lifecycle.events.OnResume;
/**
* {@link PreferenceControllerMixin} that updates MAC/IP address.
*/
public class WifiInfoPreferenceController extends AbstractPreferenceController
implements PreferenceControllerMixin, LifecycleObserver, OnResume, OnPause {
private static final String KEY_CURRENT_IP_ADDRESS = "current_ip_address";
private static final String KEY_MAC_ADDRESS = "mac_address";
private final IntentFilter mFilter;
private final WifiManager mWifiManager;
private Preference mWifiMacAddressPref;
private Preference mWifiIpAddressPref;
public WifiInfoPreferenceController(Context context, Lifecycle lifecycle,
WifiManager wifiManager) {
super(context);
mWifiManager = wifiManager;
mFilter = new IntentFilter();
mFilter.addAction(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION);
mFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
lifecycle.addObserver(this);
}
@Override
public boolean isAvailable() {
return true;
}
@Override
public String getPreferenceKey() {
// Returns null because this controller contains more than 1 preference.
return null;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mWifiMacAddressPref = screen.findPreference(KEY_MAC_ADDRESS);
mWifiMacAddressPref.setSelectable(false);
mWifiIpAddressPref = screen.findPreference(KEY_CURRENT_IP_ADDRESS);
mWifiIpAddressPref.setSelectable(false);
}
@Override
public void onResume() {
mContext.registerReceiver(mReceiver, mFilter);
updateWifiInfo();
}
@Override
public void onPause() {
mContext.unregisterReceiver(mReceiver);
}
public void updateWifiInfo() {
if (mWifiMacAddressPref != null) {
final WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
final boolean macRandomizationSupported = mContext.getResources().getBoolean(
com.android.internal.R.bool.config_wifi_connected_mac_randomization_supported);
final String macAddress = wifiInfo == null ? null : wifiInfo.getMacAddress();
if (macRandomizationSupported && WifiInfo.DEFAULT_MAC_ADDRESS.equals(macAddress)) {
mWifiMacAddressPref.setSummary(R.string.wifi_status_mac_randomized);
} else if (TextUtils.isEmpty(macAddress)
|| WifiInfo.DEFAULT_MAC_ADDRESS.equals(macAddress)) {
mWifiMacAddressPref.setSummary(R.string.status_unavailable);
} else {
mWifiMacAddressPref.setSummary(macAddress);
}
}
if (mWifiIpAddressPref != null) {
final String ipAddress = Utils.getWifiIpAddresses(mContext);
mWifiIpAddressPref.setSummary(ipAddress == null
? mContext.getString(R.string.status_unavailable)
: BidiFormatter.getInstance().unicodeWrap(ipAddress));
}
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION) ||
action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
updateWifiInfo();
}
}
};
}

View File

@@ -687,6 +687,9 @@ public class WifiDetailPreferenceController extends AbstractPreferenceController
} else {
mMacAddressPref.setSummary(macAddress);
}
// MAC Address Pref Title
refreshMacTitle();
}
private String getMacAddress() {
@@ -1141,4 +1144,24 @@ public class WifiDetailPreferenceController extends AbstractPreferenceController
mTimer.cancel();
mTimer = null;
}
private void refreshMacTitle() {
if (mWifiConfig == null) {
return;
}
// For saved Passpoint network, framework doesn't have the field to keep the MAC choice
// persistently, so Passpoint network will always use the default value so far, which is
// randomized MAC address, so don't need to modify title.
if (mAccessPoint.isPasspoint() || mAccessPoint.isPasspointConfig()) {
return;
}
mMacAddressPref.setTitle(
(mWifiConfig.macRandomizationSetting
== WifiConfiguration.RANDOMIZATION_PERSISTENT)
? R.string.wifi_advanced_randomized_mac_address_title
: R.string.wifi_advanced_factory_mac_address_title);
}
}

View File

@@ -27,7 +27,6 @@ import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceScreen;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.utils.PreferenceGroupChildrenCache;
import com.android.settings.R;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
@@ -50,7 +49,6 @@ public class SavedAccessPointsPreferenceController extends BasePreferenceControl
private static final String TAG = "SavedAPPrefCtrl";
private final WifiManager mWifiManager;
private final PreferenceGroupChildrenCache mChildrenCache;
private final UserBadgeCache mUserBadgeCache;
private PreferenceGroup mPreferenceGroup;
@@ -61,7 +59,6 @@ public class SavedAccessPointsPreferenceController extends BasePreferenceControl
super(context, preferenceKey);
mUserBadgeCache = new AccessPointPreference.UserBadgeCache(context.getPackageManager());
mWifiManager = context.getSystemService(WifiManager.class);
mChildrenCache = new PreferenceGroupChildrenCache();
}
public SavedAccessPointsPreferenceController setHost(SavedAccessPointsWifiSettings host) {
@@ -91,8 +88,9 @@ public class SavedAccessPointsPreferenceController extends BasePreferenceControl
@Override
public boolean onPreferenceClick(Preference preference) {
final Preference preferenceInGroup = mPreferenceGroup.findPreference(preference.getKey());
if (mHost != null) {
mHost.showWifiPage((AccessPointPreference) preference);
mHost.showWifiPage((AccessPointPreference) preferenceInGroup);
}
return false;
}
@@ -118,7 +116,7 @@ public class SavedAccessPointsPreferenceController extends BasePreferenceControl
final List<AccessPoint> accessPoints =
WifiSavedConfigUtils.getAllConfigs(mContext, mWifiManager);
Collections.sort(accessPoints, SavedNetworkComparator.INSTANCE);
mChildrenCache.cacheRemoveAllPrefs(mPreferenceGroup);
mPreferenceGroup.removeAll();
final int accessPointsSize = accessPoints.size();
for (int i = 0; i < accessPointsSize; ++i) {
@@ -130,20 +128,15 @@ public class SavedAccessPointsPreferenceController extends BasePreferenceControl
}
String key = ap.getKey();
AccessPointPreference preference =
(AccessPointPreference) mChildrenCache.getCachedPreference(key);
if (preference == null) {
preference = new AccessPointPreference(ap, prefContext, mUserBadgeCache, true);
preference.setKey(key);
preference.setIcon(null);
preference.setOnPreferenceClickListener(this);
mPreferenceGroup.addPreference(preference);
}
AccessPointPreference preference = new AccessPointPreference(ap, prefContext,
mUserBadgeCache, true);
preference.setKey(key);
preference.setIcon(null);
preference.setOnPreferenceClickListener(this);
mPreferenceGroup.addPreference(preference);
preference.setOrder(i);
}
mChildrenCache.removeCachedPrefs(mPreferenceGroup);
if (mPreferenceGroup.getPreferenceCount() < 1) {
Log.w(TAG, "Saved networks activity loaded, but there are no saved networks!");
mPreferenceGroup.setVisible(false);