Merge "Move logic from SavedAccessPoint fragment to controller"
This commit is contained in:
committed by
Android (Google) Code Review
commit
6cf7a6e564
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright (C) 2018 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.utils;
|
||||
|
||||
import android.text.TextUtils;
|
||||
import android.util.ArrayMap;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceGroup;
|
||||
|
||||
/**
|
||||
* Class that helps track which {@link Preference}s in a {@link PreferenceGroup} are still being
|
||||
* used, and remove unused ones.
|
||||
*/
|
||||
public class PreferenceGroupChildrenCache {
|
||||
|
||||
private ArrayMap<String, Preference> mPreferenceCache;
|
||||
|
||||
public void cacheRemoveAllPrefs(PreferenceGroup group) {
|
||||
mPreferenceCache = new ArrayMap<>();
|
||||
final int N = group.getPreferenceCount();
|
||||
for (int i = 0; i < N; i++) {
|
||||
Preference p = group.getPreference(i);
|
||||
if (TextUtils.isEmpty(p.getKey())) {
|
||||
continue;
|
||||
}
|
||||
mPreferenceCache.put(p.getKey(), p);
|
||||
}
|
||||
}
|
||||
|
||||
public void removeCachedPrefs(PreferenceGroup group) {
|
||||
for (Preference p : mPreferenceCache.values()) {
|
||||
group.removePreference(p);
|
||||
}
|
||||
mPreferenceCache = null;
|
||||
}
|
||||
|
||||
public Preference getCachedPreference(String key) {
|
||||
return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
|
||||
}
|
||||
|
||||
public int getCachedCount() {
|
||||
return mPreferenceCache != null ? mPreferenceCache.size() : 0;
|
||||
}
|
||||
}
|
@@ -27,11 +27,15 @@ import com.android.settings.R;
|
||||
import com.android.settingslib.RestrictedLockUtils;
|
||||
import com.android.settingslib.wifi.AccessPoint;
|
||||
|
||||
public class WifiDialog extends AlertDialog implements WifiConfigUiBase, DialogInterface.OnClickListener {
|
||||
public class WifiDialog extends AlertDialog implements WifiConfigUiBase,
|
||||
DialogInterface.OnClickListener {
|
||||
|
||||
public interface WifiDialogListener {
|
||||
void onForget(WifiDialog dialog);
|
||||
void onSubmit(WifiDialog dialog);
|
||||
default void onForget(WifiDialog dialog) {
|
||||
}
|
||||
|
||||
default void onSubmit(WifiDialog dialog) {
|
||||
}
|
||||
}
|
||||
|
||||
private static final int BUTTON_SUBMIT = DialogInterface.BUTTON_POSITIVE;
|
||||
@@ -64,7 +68,7 @@ public class WifiDialog extends AlertDialog implements WifiConfigUiBase, DialogI
|
||||
}
|
||||
|
||||
/* package */ WifiDialog(Context context, WifiDialogListener listener, AccessPoint accessPoint,
|
||||
int mode, int style, boolean hideSubmitButton) {
|
||||
int mode, int style, boolean hideSubmitButton) {
|
||||
super(context, style);
|
||||
mMode = mode;
|
||||
mListener = listener;
|
||||
@@ -99,8 +103,8 @@ public class WifiDialog extends AlertDialog implements WifiConfigUiBase, DialogI
|
||||
}
|
||||
|
||||
public void onRestoreInstanceState(Bundle savedInstanceState) {
|
||||
super.onRestoreInstanceState(savedInstanceState);
|
||||
mController.updatePassword();
|
||||
super.onRestoreInstanceState(savedInstanceState);
|
||||
mController.updatePassword();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -541,11 +541,6 @@ public class WifiDetailPreferenceController extends AbstractPreferenceController
|
||||
mConnectivityManager.startCaptivePortalApp(mNetwork);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onForget(WifiDialog dialog) {
|
||||
// can't forget network from a 'modify' dialog
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSubmit(WifiDialog dialog) {
|
||||
if (dialog.getController() != null) {
|
||||
|
@@ -18,21 +18,127 @@ package com.android.settings.wifi.savedaccesspoints;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.util.Log;
|
||||
|
||||
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.settings.utils.PreferenceGroupChildrenCache;
|
||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settingslib.core.lifecycle.events.OnStart;
|
||||
import com.android.settingslib.utils.ThreadUtils;
|
||||
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;
|
||||
|
||||
/**
|
||||
* Controller that manages a PrferenceGroup, which contains a list of saved access points.
|
||||
* Controller that manages a PreferenceGroup, which contains a list of saved access points.
|
||||
*/
|
||||
public class SavedAccessPointsPreferenceController extends BasePreferenceController {
|
||||
public class SavedAccessPointsPreferenceController extends BasePreferenceController implements
|
||||
LifecycleObserver, OnStart, Preference.OnPreferenceClickListener,
|
||||
WifiManager.ActionListener {
|
||||
|
||||
private static final String TAG = "SavedAPPrefCtrl";
|
||||
|
||||
private final WifiManager mWifiManager;
|
||||
private final PreferenceGroupChildrenCache mChildrenCache;
|
||||
|
||||
private final UserBadgeCache mUserBadgeCache;
|
||||
private PreferenceGroup mPreferenceGroup;
|
||||
private SavedAccessPointsWifiSettings mHost;
|
||||
|
||||
public SavedAccessPointsPreferenceController(Context context,
|
||||
String preferenceKey) {
|
||||
super(context, preferenceKey);
|
||||
mUserBadgeCache = new AccessPointPreference.UserBadgeCache(context.getPackageManager());
|
||||
mWifiManager = context.getSystemService(WifiManager.class);
|
||||
mChildrenCache = new PreferenceGroupChildrenCache();
|
||||
}
|
||||
|
||||
public SavedAccessPointsPreferenceController setHost(SavedAccessPointsWifiSettings host) {
|
||||
mHost = host;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAvailabilityStatus() {
|
||||
return AVAILABLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mPreferenceGroup = (PreferenceGroup) screen.findPreference(getPreferenceKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
refreshSavedAccessPoints();
|
||||
}
|
||||
|
||||
public void postRefreshSavedAccessPoints() {
|
||||
ThreadUtils.postOnMainThread(() -> refreshSavedAccessPoints());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
if (mHost != null) {
|
||||
mHost.showWifiDialog((AccessPointPreference) preference);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
postRefreshSavedAccessPoints();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(int reason) {
|
||||
postRefreshSavedAccessPoints();
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void refreshSavedAccessPoints() {
|
||||
if (mPreferenceGroup == null) {
|
||||
Log.w(TAG, "PreferenceGroup is null, skipping.");
|
||||
return;
|
||||
}
|
||||
final Context prefContext = mPreferenceGroup.getContext();
|
||||
|
||||
final List<AccessPoint> accessPoints =
|
||||
WifiSavedConfigUtils.getAllConfigs(mContext, mWifiManager);
|
||||
Collections.sort(accessPoints, SavedNetworkComparator.INSTANCE);
|
||||
mChildrenCache.cacheRemoveAllPrefs(mPreferenceGroup);
|
||||
|
||||
final int accessPointsSize = accessPoints.size();
|
||||
for (int i = 0; i < accessPointsSize; ++i) {
|
||||
AccessPoint ap = accessPoints.get(i);
|
||||
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);
|
||||
}
|
||||
preference.setOrder(i);
|
||||
}
|
||||
|
||||
mChildrenCache.removeCachedPrefs(mPreferenceGroup);
|
||||
|
||||
if (mPreferenceGroup.getPreferenceCount() < 1) {
|
||||
Log.w(TAG, "Saved networks activity loaded, but there are no saved networks!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user