Fix automatically directing the user to the captive portal in Wi-Fi Slice

The feature failed after the CL "Force the adapter to rebind cards with
a toggle".

Because toggle slices have been forced to rebind after starting another
activity and when any slice is updating. This unpins Wi-Fi slice and
stops WifiScanWorker and then clears the saved clicked network.

Solution:
1. Change ConnectToWifiHandler from activity to receiver and send
   broadcasts to it with FLAG_RECEIVER_FOREGROUND, so Wi-Fi slice won't
   be forced to rebind.
2. Seperate Wi-Fi scan worker and contextual Wi-Fi scan worker. Keep the
   original logic for the generic one, and then add the logic below to
   the contextual one.
3. Do not clear the saved clicked network when slice is unppined because
   it happens frequently in contextual homepage.
4. Introduce a static long in ContextualWifiScanWorker that updates once
   in every visible UI session. A session is when the screen is visible
   to user.
5. Use session token to determine whether auto-starting captive portal
   is needed.

Fixes: 128056349
Test: robotest, visual in homepage and network panel
Change-Id: I9e03c379806e124fa7253b2a635574b2433f6afc
This commit is contained in:
Jason Chiu
2019-04-30 14:41:19 +08:00
parent 42aac32823
commit 2c3e6c6434
13 changed files with 303 additions and 103 deletions

View File

@@ -94,6 +94,7 @@ public class WifiSlice implements CustomSliceable {
final boolean isWifiEnabled = isWifiEnabled();
ListBuilder listBuilder = getHeaderRow(isWifiEnabled);
if (!isWifiEnabled) {
WifiScanWorker.clearClickedWifi();
return listBuilder.build();
}
@@ -133,6 +134,17 @@ public class WifiSlice implements CustomSliceable {
return listBuilder.build();
}
private void handleNetworkCallback(WifiScanWorker worker, boolean isFirstApActive) {
if (worker == null) {
return;
}
if (isFirstApActive) {
worker.registerNetworkCallback(mWifiManager.getCurrentNetwork());
} else {
worker.unregisterNetworkCallback();
}
}
private ListBuilder getHeaderRow(boolean isWifiEnabled) {
final IconCompat icon = IconCompat.createWithResource(mContext,
R.drawable.ic_settings_wireless);
@@ -155,17 +167,6 @@ public class WifiSlice implements CustomSliceable {
.setPrimaryAction(primarySliceAction));
}
private void handleNetworkCallback(WifiScanWorker worker, boolean isFirstApActive) {
if (worker == null) {
return;
}
if (isFirstApActive) {
worker.registerNetworkCallback(mWifiManager.getCurrentNetwork());
} else {
worker.unregisterNetworkCallback();
}
}
private ListBuilder.RowBuilder getAccessPointRow(AccessPoint accessPoint) {
final boolean isCaptivePortal = accessPoint.isActive() && isCaptivePortal();
final CharSequence title = accessPoint.getTitle();
@@ -175,9 +176,8 @@ public class WifiSlice implements CustomSliceable {
.setTitleItem(levelIcon, ListBuilder.ICON_IMAGE)
.setTitle(title)
.setSubtitle(summary)
.setPrimaryAction(SliceAction.createDeeplink(
getAccessPointAction(accessPoint, isCaptivePortal), levelIcon,
ListBuilder.ICON_IMAGE, title));
.setPrimaryAction(getAccessPointAction(accessPoint, isCaptivePortal, levelIcon,
title));
if (isCaptivePortal) {
rowBuilder.addEndItem(getCaptivePortalEndAction(accessPoint, title));
@@ -203,7 +203,7 @@ public class WifiSlice implements CustomSliceable {
final Drawable d = mContext.getDrawable(
com.android.settingslib.Utils.getWifiIconResource(accessPoint.getLevel()));
@ColorInt int color;
final @ColorInt int color;
if (accessPoint.isActive()) {
final NetworkInfo.State state = accessPoint.getNetworkInfo().getState();
if (state == NetworkInfo.State.CONNECTED) {
@@ -232,36 +232,54 @@ public class WifiSlice implements CustomSliceable {
}
private SliceAction getCaptivePortalEndAction(AccessPoint accessPoint, CharSequence title) {
return SliceAction.createDeeplink(
getAccessPointAction(accessPoint, false /* isCaptivePortal */),
IconCompat.createWithResource(mContext, R.drawable.ic_settings_accent),
ListBuilder.ICON_IMAGE, title);
return getAccessPointAction(accessPoint, false /* isCaptivePortal */,
IconCompat.createWithResource(mContext, R.drawable.ic_settings_accent), title);
}
private PendingIntent getAccessPointAction(AccessPoint accessPoint, boolean isCaptivePortal) {
private SliceAction getAccessPointAction(AccessPoint accessPoint, boolean isCaptivePortal,
IconCompat icon, CharSequence title) {
final int requestCode = accessPoint.hashCode();
if (isCaptivePortal) {
final Intent intent = new Intent(mContext, ConnectToWifiHandler.class)
.putExtra(ConnectivityManager.EXTRA_NETWORK, mWifiManager.getCurrentNetwork());
return getBroadcastAction(requestCode, intent, icon, title);
}
final Bundle extras = new Bundle();
accessPoint.saveWifiState(extras);
Intent intent;
if (isCaptivePortal) {
intent = new Intent(mContext, ConnectToWifiHandler.class);
intent.putExtra(ConnectivityManager.EXTRA_NETWORK, mWifiManager.getCurrentNetwork());
} else if (accessPoint.isActive()) {
intent = new SubSettingLauncher(mContext)
if (accessPoint.isActive()) {
final Intent intent = new SubSettingLauncher(mContext)
.setTitleRes(R.string.pref_title_network_details)
.setDestination(WifiNetworkDetailsFragment.class.getName())
.setArguments(extras)
.setSourceMetricsCategory(SettingsEnums.WIFI)
.toIntent();
return getActivityAction(requestCode, intent, icon, title);
} else if (WifiUtils.getConnectingType(accessPoint) != WifiUtils.CONNECT_TYPE_OTHERS) {
intent = new Intent(mContext, ConnectToWifiHandler.class);
intent.putExtra(WifiDialogActivity.KEY_ACCESS_POINT_STATE, extras);
final Intent intent = new Intent(mContext, ConnectToWifiHandler.class)
.putExtra(WifiDialogActivity.KEY_ACCESS_POINT_STATE, extras);
return getBroadcastAction(requestCode, intent, icon, title);
} else {
intent = new Intent(mContext, WifiDialogActivity.class);
intent.putExtra(WifiDialogActivity.KEY_ACCESS_POINT_STATE, extras);
final Intent intent = new Intent(mContext, WifiDialogActivity.class)
.putExtra(WifiDialogActivity.KEY_ACCESS_POINT_STATE, extras);
return getActivityAction(requestCode, intent, icon, title);
}
return PendingIntent.getActivity(mContext, accessPoint.hashCode() /* requestCode */,
intent, 0 /* flags */);
}
private SliceAction getActivityAction(int requestCode, Intent intent, IconCompat icon,
CharSequence title) {
final PendingIntent pi = PendingIntent.getActivity(mContext, requestCode, intent,
0 /* flags */);
return SliceAction.createDeeplink(pi, icon, ListBuilder.ICON_IMAGE, title);
}
private SliceAction getBroadcastAction(int requestCode, Intent intent, IconCompat icon,
CharSequence title) {
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
final PendingIntent pi = PendingIntent.getBroadcast(mContext, requestCode, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
return SliceAction.create(pi, icon, ListBuilder.ICON_IMAGE, title);
}
private ListBuilder.RowBuilder getLoadingRow(CharSequence placeholder) {
@@ -277,7 +295,7 @@ public class WifiSlice implements CustomSliceable {
.setSubtitle(title);
}
protected boolean isCaptivePortal() {
private boolean isCaptivePortal() {
final NetworkCapabilities nc = mConnectivityManager.getNetworkCapabilities(
mWifiManager.getCurrentNetwork());
return WifiUtils.canSignIntoNetwork(nc);