[Wi-Fi] Apply WifiTrackerLib objects in Wi-Fi Slice
This change uses WifiTrackerLib's WifiPickerTracker & WifiEntry to replace SettingLib's WifiTracker & AccessPoint. This change includes 1. WifiScanWorker has the callbacks similar to a lifecycle component but it's not a lifecycle component. Let WifiScanWorker implements LifecycleOwner and provides #getLifecycle() for WifiPickerTracker. 2. Remove captive portal related code because WifiEntry#connect will handle captive portal login if it's necessary. 3. Create WifiSliceItem to wrap WifiEntry because WifiEntry is an abstract object and it does not provide copy constructor. Without copy construcor, Wi-Fi Slice may show unexpected information when a WifiEntry is updated. 4. Use WifiTrackerLib's NetworkDetailsTracker & WifiEntry in WifiDialogActivity because it gets a WifiEntry key from Wi-Fi Slice. NetworkDetailsTracker can get the WifiEntry of th key. Bug: 155613549 Bug: 152571756 Test: make RunSettingsRoboTests ROBOTEST_FILTER=com.android.settings.wifi.slice make RunSettingsRoboTests ROBOTEST_FILTER=com.android.settings.wifi Change-Id: I0718f4647cea007a9b701922f3121a388dd43918
This commit is contained in:
@@ -16,51 +16,56 @@
|
||||
|
||||
package com.android.settings.wifi;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.net.NetworkInfo;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkScoreManager;
|
||||
import android.net.wifi.WifiConfiguration;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.net.wifi.WifiManager.ActionListener;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import android.os.Handler;
|
||||
import android.os.HandlerThread;
|
||||
import android.os.Looper;
|
||||
import android.os.Process;
|
||||
import android.os.SimpleClock;
|
||||
import android.os.SystemClock;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SetupWizardUtils;
|
||||
import com.android.settings.wifi.dpp.WifiDppUtils;
|
||||
import com.android.settingslib.wifi.AccessPoint;
|
||||
import com.android.settingslib.core.lifecycle.ObservableActivity;
|
||||
import com.android.wifitrackerlib.NetworkDetailsTracker;
|
||||
import com.android.wifitrackerlib.WifiEntry;
|
||||
|
||||
import com.google.android.setupcompat.util.WizardManagerHelper;
|
||||
|
||||
public class WifiDialogActivity extends Activity implements WifiDialog.WifiDialogListener,
|
||||
DialogInterface.OnDismissListener {
|
||||
import java.time.Clock;
|
||||
import java.time.ZoneOffset;
|
||||
|
||||
/**
|
||||
* The activity shows a CONNECT_MODE Wi-fi editor dialog.
|
||||
*/
|
||||
public class WifiDialogActivity extends ObservableActivity implements
|
||||
WifiDialog2.WifiDialog2Listener, DialogInterface.OnDismissListener {
|
||||
|
||||
private static final String TAG = "WifiDialogActivity";
|
||||
|
||||
public static final String KEY_ACCESS_POINT_STATE = "access_point_state";
|
||||
|
||||
/**
|
||||
* Boolean extra indicating whether this activity should connect to an access point on the
|
||||
* caller's behalf. If this is set to false, the caller should check
|
||||
* {@link #KEY_WIFI_CONFIGURATION} in the result data and save that using
|
||||
* {@link WifiManager#connect(WifiConfiguration, ActionListener)}. Default is true.
|
||||
*/
|
||||
@VisibleForTesting
|
||||
static final String KEY_CONNECT_FOR_CALLER = "connect_for_caller";
|
||||
|
||||
public static final String KEY_WIFI_CONFIGURATION = "wifi_configuration";
|
||||
public static final String KEY_CHOSEN_WIFIENTRY_KEY = "key_chosen_wifientry_key";
|
||||
|
||||
private static final int RESULT_CONNECTED = RESULT_FIRST_USER;
|
||||
private static final int RESULT_FORGET = RESULT_FIRST_USER + 1;
|
||||
|
||||
private static final int REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER = 0;
|
||||
|
||||
private WifiDialog mDialog;
|
||||
// Max age of tracked WifiEntries.
|
||||
private static final long MAX_SCAN_AGE_MILLIS = 15_000;
|
||||
// Interval between initiating NetworkDetailsTracker scans.
|
||||
private static final long SCAN_INTERVAL_MILLIS = 10_000;
|
||||
|
||||
private WifiDialog2 mDialog;
|
||||
private Intent mIntent;
|
||||
private NetworkDetailsTracker mNetworkDetailsTracker;
|
||||
private HandlerThread mWorkerThread;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -71,18 +76,43 @@ public class WifiDialogActivity extends Activity implements WifiDialog.WifiDialo
|
||||
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
final Bundle accessPointState = mIntent.getBundleExtra(KEY_ACCESS_POINT_STATE);
|
||||
AccessPoint accessPoint = null;
|
||||
if (accessPointState != null) {
|
||||
accessPoint = new AccessPoint(this, accessPointState);
|
||||
mWorkerThread = new HandlerThread(
|
||||
TAG + "{" + Integer.toHexString(System.identityHashCode(this)) + "}",
|
||||
Process.THREAD_PRIORITY_BACKGROUND);
|
||||
mWorkerThread.start();
|
||||
final Clock elapsedRealtimeClock = new SimpleClock(ZoneOffset.UTC) {
|
||||
@Override
|
||||
public long millis() {
|
||||
return SystemClock.elapsedRealtime();
|
||||
}
|
||||
};
|
||||
mNetworkDetailsTracker = NetworkDetailsTracker.createNetworkDetailsTracker(
|
||||
getLifecycle(),
|
||||
this,
|
||||
getSystemService(WifiManager.class),
|
||||
getSystemService(ConnectivityManager.class),
|
||||
getSystemService(NetworkScoreManager.class),
|
||||
new Handler(Looper.getMainLooper()),
|
||||
mWorkerThread.getThreadHandler(),
|
||||
elapsedRealtimeClock,
|
||||
MAX_SCAN_AGE_MILLIS,
|
||||
SCAN_INTERVAL_MILLIS,
|
||||
mIntent.getStringExtra(KEY_CHOSEN_WIFIENTRY_KEY));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
if (mDialog != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (WizardManagerHelper.isAnySetupWizard(getIntent())) {
|
||||
mDialog = WifiDialog.createModal(this, this, accessPoint,
|
||||
WifiConfigUiBase.MODE_CONNECT, R.style.SuwAlertDialogThemeCompat_Light);
|
||||
mDialog = WifiDialog2.createModal(this, this, mNetworkDetailsTracker.getWifiEntry(),
|
||||
WifiConfigUiBase2.MODE_CONNECT, R.style.SuwAlertDialogThemeCompat_Light);
|
||||
} else {
|
||||
mDialog = WifiDialog.createModal(
|
||||
this, this, accessPoint, WifiConfigUiBase.MODE_CONNECT);
|
||||
mDialog = WifiDialog2.createModal(this, this, mNetworkDetailsTracker.getWifiEntry(),
|
||||
WifiConfigUiBase2.MODE_CONNECT);
|
||||
}
|
||||
mDialog.show();
|
||||
mDialog.setOnDismissListener(this);
|
||||
@@ -90,82 +120,44 @@ public class WifiDialogActivity extends Activity implements WifiDialog.WifiDialo
|
||||
|
||||
@Override
|
||||
public void finish() {
|
||||
super.finish();
|
||||
overridePendingTransition(0, 0);
|
||||
|
||||
super.finish();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
if (mDialog != null && mDialog.isShowing()) {
|
||||
mDialog.dismiss();
|
||||
mDialog = null;
|
||||
}
|
||||
mWorkerThread.quit();
|
||||
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onForget(WifiDialog dialog) {
|
||||
final WifiManager wifiManager = getSystemService(WifiManager.class);
|
||||
final AccessPoint accessPoint = dialog.getController().getAccessPoint();
|
||||
if (accessPoint != null) {
|
||||
if (!accessPoint.isSaved()) {
|
||||
if (accessPoint.getNetworkInfo() != null &&
|
||||
accessPoint.getNetworkInfo().getState() != NetworkInfo.State.DISCONNECTED) {
|
||||
// Network is active but has no network ID - must be ephemeral.
|
||||
wifiManager.disableEphemeralNetwork(
|
||||
AccessPoint.convertToQuotedString(accessPoint.getSsidStr()));
|
||||
} else {
|
||||
// Should not happen, but a monkey seems to trigger it
|
||||
Log.e(TAG, "Failed to forget invalid network " + accessPoint.getConfig());
|
||||
}
|
||||
} else {
|
||||
wifiManager.forget(accessPoint.getConfig().networkId, null /* listener */);
|
||||
}
|
||||
public void onForget(WifiDialog2 dialog) {
|
||||
final WifiEntry wifiEntry = dialog.getController().getWifiEntry();
|
||||
if (wifiEntry != null && wifiEntry.canForget()) {
|
||||
wifiEntry.forget(null /* callback */);
|
||||
}
|
||||
|
||||
Intent resultData = new Intent();
|
||||
if (accessPoint != null) {
|
||||
Bundle accessPointState = new Bundle();
|
||||
accessPoint.saveWifiState(accessPointState);
|
||||
resultData.putExtra(KEY_ACCESS_POINT_STATE, accessPointState);
|
||||
}
|
||||
setResult(RESULT_FORGET);
|
||||
finish();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSubmit(WifiDialog dialog) {
|
||||
public void onSubmit(WifiDialog2 dialog) {
|
||||
final WifiEntry wifiEntry = dialog.getController().getWifiEntry();
|
||||
final WifiConfiguration config = dialog.getController().getConfig();
|
||||
final AccessPoint accessPoint = dialog.getController().getAccessPoint();
|
||||
final WifiManager wifiManager = getSystemService(WifiManager.class);
|
||||
|
||||
if (getIntent().getBooleanExtra(KEY_CONNECT_FOR_CALLER, true)) {
|
||||
if (config == null) {
|
||||
if (accessPoint != null && accessPoint.isSaved()) {
|
||||
wifiManager.connect(accessPoint.getConfig(), null /* listener */);
|
||||
}
|
||||
} else {
|
||||
wifiManager.save(config, null /* listener */);
|
||||
if (accessPoint != null) {
|
||||
// accessPoint is null for "Add network"
|
||||
NetworkInfo networkInfo = accessPoint.getNetworkInfo();
|
||||
if (networkInfo == null || !networkInfo.isConnected()) {
|
||||
wifiManager.connect(config, null /* listener */);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (config == null && wifiEntry != null && wifiEntry.canConnect()) {
|
||||
wifiEntry.connect(null /* callback */);
|
||||
} else {
|
||||
getSystemService(WifiManager.class).connect(config, null /* listener */);
|
||||
}
|
||||
|
||||
Intent resultData = new Intent();
|
||||
if (accessPoint != null) {
|
||||
Bundle accessPointState = new Bundle();
|
||||
accessPoint.saveWifiState(accessPointState);
|
||||
resultData.putExtra(KEY_ACCESS_POINT_STATE, accessPointState);
|
||||
}
|
||||
if (config != null) {
|
||||
resultData.putExtra(KEY_WIFI_CONFIGURATION, config);
|
||||
}
|
||||
setResult(RESULT_CONNECTED, resultData);
|
||||
setResult(RESULT_CONNECTED);
|
||||
finish();
|
||||
}
|
||||
|
||||
@@ -176,7 +168,7 @@ public class WifiDialogActivity extends Activity implements WifiDialog.WifiDialo
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onScan(WifiDialog dialog, String ssid) {
|
||||
public void onScan(WifiDialog2 dialog, String ssid) {
|
||||
Intent intent = WifiDppUtils.getEnrolleeQrCodeScannerIntent(ssid);
|
||||
WizardManagerHelper.copyWizardManagerExtras(mIntent, intent);
|
||||
|
||||
|
Reference in New Issue
Block a user