Move logic from SavedAccessPoint fragment to controller

Change-Id: Ic54e536f97490cec1746fa58c42ec55b60d17f0e
Fixes: 64806699
Test: robotests
This commit is contained in:
Fan Zhang
2018-07-18 17:01:53 -07:00
parent 499f71988f
commit 2ff710a911
10 changed files with 425 additions and 222 deletions

View File

@@ -17,18 +17,12 @@
package com.android.settings.wifi.savedaccesspoints;
import android.annotation.Nullable;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.internal.logging.nano.MetricsProto;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
@@ -39,68 +33,19 @@ import com.android.settings.wifi.WifiDialog;
import com.android.settings.wifi.WifiSettings;
import com.android.settingslib.wifi.AccessPoint;
import com.android.settingslib.wifi.AccessPointPreference;
import com.android.settingslib.wifi.WifiSavedConfigUtils;
import java.util.Collections;
import java.util.List;
/**
* UI to manage saved networks/access points.
*/
public class SavedAccessPointsWifiSettings extends DashboardFragment
implements WifiDialog.WifiDialogListener {
implements WifiDialog.WifiDialogListener, DialogInterface.OnCancelListener {
private static final String TAG = "SavedAccessPoints";
@VisibleForTesting
static final int MSG_UPDATE_PREFERENCES = 1;
@VisibleForTesting
final WifiManager.ActionListener mForgetListener = new WifiManager.ActionListener() {
@Override
public void onSuccess() {
postUpdatePreference();
}
@Override
public void onFailure(int reason) {
postUpdatePreference();
}
};
@VisibleForTesting
final Handler mHandler = new Handler() {
@Override
public void handleMessage(android.os.Message msg) {
if (msg.what == MSG_UPDATE_PREFERENCES) {
initPreferences();
}
}
};
private final WifiManager.ActionListener mSaveListener = new WifiManager.ActionListener() {
@Override
public void onSuccess() {
postUpdatePreference();
}
@Override
public void onFailure(int reason) {
Activity activity = getActivity();
if (activity != null) {
Toast.makeText(activity,
R.string.wifi_failed_save_message,
Toast.LENGTH_SHORT).show();
}
}
};
private WifiDialog mDialog;
private WifiManager mWifiManager;
private AccessPoint mDlgAccessPoint;
private Bundle mAccessPointSavedState;
private AccessPoint mSelectedAccessPoint;
private AccessPointPreference.UserBadgeCache mUserBadgeCache;
// Instance state key
private static final String SAVE_DIALOG_ACCESS_POINT_STATE = "wifi_ap_state";
@@ -122,21 +67,15 @@ public class SavedAccessPointsWifiSettings extends DashboardFragment
@Override
public void onAttach(Context context) {
super.onAttach(context);
mUserBadgeCache = new AccessPointPreference.UserBadgeCache(getPackageManager());
}
@Override
public void onResume() {
super.onResume();
initPreferences();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mWifiManager = (WifiManager) getContext()
.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
use(SavedAccessPointsPreferenceController.class)
.setHost(this);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
if (savedInstanceState.containsKey(SAVE_DIALOG_ACCESS_POINT_STATE)) {
mAccessPointSavedState =
@@ -145,55 +84,15 @@ public class SavedAccessPointsWifiSettings extends DashboardFragment
}
}
private void initPreferences() {
PreferenceScreen preferenceScreen = getPreferenceScreen();
final Context context = getPrefContext();
final List<AccessPoint> accessPoints =
WifiSavedConfigUtils.getAllConfigs(context, mWifiManager);
Collections.sort(accessPoints, SavedNetworkComparator.INSTANCE);
cacheRemoveAllPrefs(preferenceScreen);
final int accessPointsSize = accessPoints.size();
for (int i = 0; i < accessPointsSize; ++i) {
AccessPoint ap = accessPoints.get(i);
String key = ap.getKey();
AccessPointPreference preference =
(AccessPointPreference) getCachedPreference(key);
if (preference == null) {
preference = new AccessPointPreference(ap, context, mUserBadgeCache, true);
preference.setKey(key);
preference.setIcon(null);
preferenceScreen.addPreference(preference);
}
preference.setOrder(i);
}
removeCachedPrefs(preferenceScreen);
if (getPreferenceScreen().getPreferenceCount() < 1) {
Log.w(TAG, "Saved networks activity loaded, but there are no saved networks!");
}
}
private void postUpdatePreference() {
if (!mHandler.hasMessages(MSG_UPDATE_PREFERENCES)) {
mHandler.sendEmptyMessage(MSG_UPDATE_PREFERENCES);
}
}
private void showWifiDialog(@Nullable AccessPointPreference accessPoint) {
if (mDialog != null) {
removeDialog(WifiSettings.WIFI_DIALOG_ID);
mDialog = null;
}
public void showWifiDialog(@Nullable AccessPointPreference accessPoint) {
removeDialog(WifiSettings.WIFI_DIALOG_ID);
if (accessPoint != null) {
// Save the access point and edit mode
mDlgAccessPoint = accessPoint.getAccessPoint();
mSelectedAccessPoint = accessPoint.getAccessPoint();
} else {
// No access point is selected. Clear saved state.
mDlgAccessPoint = null;
mSelectedAccessPoint = null;
mAccessPointSavedState = null;
}
@@ -204,24 +103,18 @@ public class SavedAccessPointsWifiSettings extends DashboardFragment
public Dialog onCreateDialog(int dialogId) {
switch (dialogId) {
case WifiSettings.WIFI_DIALOG_ID:
if (mDlgAccessPoint == null && mAccessPointSavedState == null) {
// Add new network
mDialog = WifiDialog.createFullscreen(getActivity(), this, null,
WifiConfigUiBase.MODE_CONNECT);
} else {
// Modify network
if (mDlgAccessPoint == null) {
// Restore AP from save state
mDlgAccessPoint = new AccessPoint(getActivity(), mAccessPointSavedState);
// Reset the saved access point data
mAccessPointSavedState = null;
}
mDialog = WifiDialog.createModal(getActivity(), this, mDlgAccessPoint,
WifiConfigUiBase.MODE_VIEW);
// Modify network
if (mSelectedAccessPoint == null) {
// Restore AP from save state
mSelectedAccessPoint = new AccessPoint(getActivity(), mAccessPointSavedState);
// Reset the saved access point data
mAccessPointSavedState = null;
}
mSelectedAccessPoint = mDlgAccessPoint;
final WifiDialog dialog = WifiDialog.createModal(
getActivity(), this, mSelectedAccessPoint, WifiConfigUiBase.MODE_VIEW);
dialog.setOnCancelListener(this);
return mDialog;
return dialog;
}
return super.onCreateDialog(dialogId);
}
@@ -239,14 +132,12 @@ public class SavedAccessPointsWifiSettings extends DashboardFragment
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// If the dialog is showing, save its state.
if (mDialog != null && mDialog.isShowing()) {
if (mDlgAccessPoint != null) {
mAccessPointSavedState = new Bundle();
mDlgAccessPoint.saveWifiState(mAccessPointSavedState);
outState.putBundle(SAVE_DIALOG_ACCESS_POINT_STATE, mAccessPointSavedState);
}
// If the dialog is showing (indicated by the existence of mSelectedAccessPoint), then we
// save its state.
if (mSelectedAccessPoint != null) {
mAccessPointSavedState = new Bundle();
mSelectedAccessPoint.saveWifiState(mAccessPointSavedState);
outState.putBundle(SAVE_DIALOG_ACCESS_POINT_STATE, mAccessPointSavedState);
}
}
@@ -261,27 +152,19 @@ public class SavedAccessPointsWifiSettings extends DashboardFragment
Log.e(TAG, "Failed to remove Passpoint configuration for "
+ mSelectedAccessPoint.getConfigName());
}
postUpdatePreference();
use(SavedAccessPointsPreferenceController.class)
.postRefreshSavedAccessPoints();
} else {
// mForgetListener will call initPreferences upon completion
mWifiManager.forget(mSelectedAccessPoint.getConfig().networkId, mForgetListener);
mWifiManager.forget(mSelectedAccessPoint.getConfig().networkId,
use(SavedAccessPointsPreferenceController.class));
}
mSelectedAccessPoint = null;
}
}
@Override
public void onSubmit(WifiDialog dialog) {
mWifiManager.save(dialog.getController().getConfig(), mSaveListener);
}
@Override
public boolean onPreferenceTreeClick(Preference preference) {
if (preference instanceof AccessPointPreference) {
showWifiDialog((AccessPointPreference) preference);
return true;
} else {
return super.onPreferenceTreeClick(preference);
}
public void onCancel(DialogInterface dialog) {
mSelectedAccessPoint = null;
}
}