[Wi-Fi] Refine code for saved AccessPoints
SubscribedAccessPointsPreferenceController extends from SavedAccessPointsPreferenceController to remove duplicate code. Bug: 127206629 Test: make RunSettingsRoboTests ROBOTEST_FILTER=com.android.settings.wifi.savedaccesspoints Manual: Add and forget Wi-Fi accesspoints and observe UI display. Change-Id: Ia494117ae98f043ce65b2f9f6e4b4daccc8df543
This commit is contained in:
@@ -30,7 +30,6 @@ import com.android.settingslib.wifi.AccessPointPreference;
|
||||
import com.android.settingslib.wifi.AccessPointPreference.UserBadgeCache;
|
||||
import com.android.settingslib.wifi.WifiSavedConfigUtils;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -40,17 +39,14 @@ import java.util.stream.Collectors;
|
||||
public class SavedAccessPointsPreferenceController extends BasePreferenceController implements
|
||||
Preference.OnPreferenceClickListener {
|
||||
|
||||
private static final String TAG = "SavedApPrefCtrl";
|
||||
|
||||
private final WifiManager mWifiManager;
|
||||
protected final WifiManager mWifiManager;
|
||||
private final UserBadgeCache mUserBadgeCache;
|
||||
private PreferenceGroup mPreferenceGroup;
|
||||
private SavedAccessPointsWifiSettings mHost;
|
||||
@VisibleForTesting
|
||||
List<AccessPoint> mAccessPoints;
|
||||
|
||||
public SavedAccessPointsPreferenceController(Context context,
|
||||
String preferenceKey) {
|
||||
public SavedAccessPointsPreferenceController(Context context, String preferenceKey) {
|
||||
super(context, preferenceKey);
|
||||
mUserBadgeCache = new AccessPointPreference.UserBadgeCache(context.getPackageManager());
|
||||
mWifiManager = context.getSystemService(WifiManager.class);
|
||||
@@ -84,7 +80,7 @@ public class SavedAccessPointsPreferenceController extends BasePreferenceControl
|
||||
return false;
|
||||
}
|
||||
|
||||
private void refreshSavedAccessPoints() {
|
||||
protected void refreshSavedAccessPoints() {
|
||||
mAccessPoints = WifiSavedConfigUtils.getAllConfigs(mContext, mWifiManager).stream()
|
||||
.filter(accessPoint -> !accessPoint.isPasspointConfig())
|
||||
.sorted(SavedNetworkComparator.INSTANCE)
|
||||
|
@@ -17,92 +17,26 @@
|
||||
package com.android.settings.wifi.savedaccesspoints;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.wifi.WifiManager;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceGroup;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settingslib.wifi.AccessPoint;
|
||||
import com.android.settingslib.wifi.AccessPointPreference;
|
||||
import com.android.settingslib.wifi.AccessPointPreference.UserBadgeCache;
|
||||
import com.android.settingslib.wifi.WifiSavedConfigUtils;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Controller that manages a PreferenceGroup, which contains a list of subscribed access points.
|
||||
*/
|
||||
public class SubscribedAccessPointsPreferenceController extends BasePreferenceController implements
|
||||
Preference.OnPreferenceClickListener {
|
||||
public class SubscribedAccessPointsPreferenceController extends
|
||||
SavedAccessPointsPreferenceController {
|
||||
|
||||
private static final String TAG = "SubscribedApPrefCtrl";
|
||||
|
||||
private final WifiManager mWifiManager;
|
||||
private final UserBadgeCache mUserBadgeCache;
|
||||
private PreferenceGroup mPreferenceGroup;
|
||||
private SavedAccessPointsWifiSettings mHost;
|
||||
@VisibleForTesting
|
||||
List<AccessPoint> mAccessPoints;
|
||||
|
||||
public SubscribedAccessPointsPreferenceController(Context context,
|
||||
String preferenceKey) {
|
||||
public SubscribedAccessPointsPreferenceController(Context context, String preferenceKey) {
|
||||
super(context, preferenceKey);
|
||||
mUserBadgeCache = new AccessPointPreference.UserBadgeCache(context.getPackageManager());
|
||||
mWifiManager = context.getSystemService(WifiManager.class);
|
||||
}
|
||||
|
||||
public SubscribedAccessPointsPreferenceController setHost(SavedAccessPointsWifiSettings host) {
|
||||
mHost = host;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAvailabilityStatus() {
|
||||
return mAccessPoints.size() > 0 ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
mPreferenceGroup = screen.findPreference(getPreferenceKey());
|
||||
refreshSubscribedAccessPoints();
|
||||
updatePreference();
|
||||
super.displayPreference(screen);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
if (mHost != null) {
|
||||
final Preference preferenceInGroup =
|
||||
mPreferenceGroup.findPreference(preference.getKey());
|
||||
mHost.showWifiPage((AccessPointPreference) preferenceInGroup);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void refreshSubscribedAccessPoints() {
|
||||
protected void refreshSavedAccessPoints() {
|
||||
mAccessPoints = WifiSavedConfigUtils.getAllConfigs(mContext, mWifiManager).stream()
|
||||
.filter(accessPoint -> accessPoint.isPasspointConfig())
|
||||
.sorted(SavedNetworkComparator.INSTANCE)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private void updatePreference() {
|
||||
mPreferenceGroup.removeAll();
|
||||
for (AccessPoint accessPoint : mAccessPoints) {
|
||||
final String key = accessPoint.getKey();
|
||||
|
||||
final AccessPointPreference preference = new AccessPointPreference(accessPoint,
|
||||
mContext, mUserBadgeCache, true /* forSavedNetworks */);
|
||||
preference.setKey(key);
|
||||
preference.setIcon(null);
|
||||
preference.setOnPreferenceClickListener(this);
|
||||
|
||||
mPreferenceGroup.addPreference(preference);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -98,7 +98,7 @@ public class SavedAccessPointsPreferenceControllerTest {
|
||||
|
||||
@Test
|
||||
@Config(shadows = ShadowAccessPoint.class)
|
||||
public void refreshSavedAccessPoints_shouldListNonSubscribedAPs() {
|
||||
public void displayPreference_oneAccessPoint_shouldListNonSubscribedAPs() {
|
||||
final WifiConfiguration config = new WifiConfiguration();
|
||||
config.SSID = "SSID";
|
||||
config.BSSID = "BSSID";
|
||||
@@ -117,7 +117,7 @@ public class SavedAccessPointsPreferenceControllerTest {
|
||||
|
||||
@Test
|
||||
@Config(shadows = ShadowAccessPoint.class)
|
||||
public void refreshSavedAccessPoints_shouldNotListSubscribedAPs() {
|
||||
public void displayPreference_onePasspoint_shouldNotListSubscribedAPs() {
|
||||
mWifiManager.addOrUpdatePasspointConfiguration(
|
||||
SubscribedAccessPointsPreferenceControllerTest.createMockPasspointConfiguration());
|
||||
|
||||
|
@@ -16,9 +16,6 @@
|
||||
|
||||
package com.android.settings.wifi.savedaccesspoints;
|
||||
|
||||
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
|
||||
import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@@ -32,7 +29,6 @@ import android.net.wifi.WifiConfiguration;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.net.wifi.hotspot2.PasspointConfiguration;
|
||||
import android.net.wifi.hotspot2.pps.HomeSp;
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.preference.PreferenceCategory;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
@@ -42,9 +38,6 @@ import com.android.settings.testutils.shadow.ShadowWifiManager;
|
||||
import com.android.settingslib.wifi.AccessPoint;
|
||||
import com.android.settingslib.wifi.AccessPointPreference;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -83,24 +76,9 @@ public class SubscribedAccessPointsPreferenceControllerTest {
|
||||
when(mPreferenceCategory.getContext()).thenReturn(mContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailability_noSavedAccessPoint_shouldNotAvailable() {
|
||||
mController.mAccessPoints = new ArrayList<>();
|
||||
|
||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailability_oneSavedAccessPoint_shouldAvailable() {
|
||||
final AccessPoint accessPoint = new AccessPoint(mContext, new Bundle() /* savedState */);
|
||||
mController.mAccessPoints = new ArrayList<AccessPoint>(Arrays.asList(accessPoint));
|
||||
|
||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(shadows = ShadowAccessPoint.class)
|
||||
public void refreshSubscribedAccessPoints_shouldNotListNonSubscribedAPs() {
|
||||
public void displayPreference_oneAccessPoint_shouldNotListNonSubscribedAPs() {
|
||||
final WifiConfiguration config = new WifiConfiguration();
|
||||
config.SSID = "SSID";
|
||||
config.BSSID = "BSSID";
|
||||
@@ -114,7 +92,7 @@ public class SubscribedAccessPointsPreferenceControllerTest {
|
||||
|
||||
@Test
|
||||
@Config(shadows = ShadowAccessPoint.class)
|
||||
public void refreshSubscribedAccessPoints_shouldListSubscribedAPs() {
|
||||
public void displayPreference_onePasspoint_shouldListSubscribedAPs() {
|
||||
mWifiManager.addOrUpdatePasspointConfiguration(createMockPasspointConfiguration());
|
||||
|
||||
mController.displayPreference(mPreferenceScreen);
|
||||
|
Reference in New Issue
Block a user