Implement persisting time for onUserSelectionConnectSuccess.

When onUserSelectionConnectSuccess callbacks, holding a second for user
to be aware of success connection. Then, go to destory network request dialog.

Bug: 117399926
Test: make RunSettingsRoboTests -j32 ROBOTEST_FILTER=com.android.settings.wifi.NetworkRequestDialogFragmentTest
Change-Id: I12d8fb2e8955cf24ddad43d1fe8b863682b6ae32
This commit is contained in:
cosmohsieh
2018-12-24 11:24:48 +08:00
parent e5e94b95dc
commit c1854cae9a
2 changed files with 76 additions and 33 deletions

View File

@@ -16,7 +16,6 @@
package com.android.settings.wifi; package com.android.settings.wifi;
import android.app.Activity;
import android.app.Dialog; import android.app.Dialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
@@ -67,9 +66,15 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp
/** Message sent to us to stop scanning wifi and pop up timeout dialog. */ /** Message sent to us to stop scanning wifi and pop up timeout dialog. */
private static final int MESSAGE_STOP_SCAN_WIFI_LIST = 0; private static final int MESSAGE_STOP_SCAN_WIFI_LIST = 0;
/** Message sent to us to finish activity. */
private static final int MESSAGE_FINISH_ACTIVITY = 1;
/** Spec defines there should be 5 wifi ap on the list at most. */ /** Spec defines there should be 5 wifi ap on the list at most. */
private static final int MAX_NUMBER_LIST_ITEM = 5; private static final int MAX_NUMBER_LIST_ITEM = 5;
/** Holding time to let user be aware that selected wifi ap is connected */
private static final int DELAY_TIME_USER_AWARE_CONNECTED_MS = 1 * 1000;
/** Delayed time to stop scanning wifi. */ /** Delayed time to stop scanning wifi. */
private static final int DELAY_TIME_STOP_SCAN_MS = 30 * 1000; private static final int DELAY_TIME_STOP_SCAN_MS = 30 * 1000;
@@ -155,7 +160,9 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp
public void onCancel(@NonNull DialogInterface dialog) { public void onCancel(@NonNull DialogInterface dialog) {
super.onCancel(dialog); super.onCancel(dialog);
// Finishes the activity when user clicks back key or outside of the dialog. // Finishes the activity when user clicks back key or outside of the dialog.
getActivity().finish(); if (getActivity() != null) {
getActivity().finish();
}
} }
@Override @Override
@@ -177,6 +184,8 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
mHandler.removeMessages(MESSAGE_FINISH_ACTIVITY);
if (mFilterWifiTracker != null) { if (mFilterWifiTracker != null) {
mFilterWifiTracker.onDestroy(); mFilterWifiTracker.onDestroy();
mFilterWifiTracker = null; mFilterWifiTracker = null;
@@ -207,7 +216,10 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp
switch (msg.what) { switch (msg.what) {
case MESSAGE_STOP_SCAN_WIFI_LIST: case MESSAGE_STOP_SCAN_WIFI_LIST:
removeMessages(MESSAGE_STOP_SCAN_WIFI_LIST); removeMessages(MESSAGE_STOP_SCAN_WIFI_LIST);
stopScanningAndPopErrorDialog(ERROR_DIALOG_TYPE.TIME_OUT); stopScanningAndMaybePopErrorDialog(ERROR_DIALOG_TYPE.TIME_OUT);
break;
case MESSAGE_FINISH_ACTIVITY:
stopScanningAndMaybePopErrorDialog(/* ERROR_DIALOG_TYPE */ null);
break; break;
default: default:
// Do nothing. // Do nothing.
@@ -216,18 +228,29 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp
} }
}; };
protected void stopScanningAndPopErrorDialog(ERROR_DIALOG_TYPE type) { protected void stopScanningAndMaybePopErrorDialog(ERROR_DIALOG_TYPE type) {
// Dismisses current dialog. // Dismisses current dialog.
dismiss(); final Dialog dialog = getDialog();
if (dialog != null && dialog.isShowing()) {
dismiss();
}
if (type == null) {
// If no error, finishes activity.
if (getActivity() != null) {
getActivity().finish();
}
} else {
// Throws error dialog.
final NetworkRequestErrorDialogFragment fragment = NetworkRequestErrorDialogFragment
.newInstance();
final Bundle bundle = new Bundle();
bundle.putSerializable(NetworkRequestErrorDialogFragment.DIALOG_TYPE, type);
fragment.setArguments(bundle);
fragment.show(getActivity().getSupportFragmentManager(),
NetworkRequestDialogFragment.class.getSimpleName());
}
// Throws new timeout dialog.
final NetworkRequestErrorDialogFragment fragment = NetworkRequestErrorDialogFragment
.newInstance();
final Bundle bundle = new Bundle();
bundle.putSerializable(NetworkRequestErrorDialogFragment.DIALOG_TYPE, type);
fragment.setArguments(bundle);
fragment.show(getActivity().getSupportFragmentManager(),
NetworkRequestDialogFragment.class.getSimpleName());
} }
@Override @Override
@@ -284,7 +307,7 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp
@Override @Override
public void onAbort() { public void onAbort() {
stopScanningAndPopErrorDialog(ERROR_DIALOG_TYPE.ABORT); stopScanningAndMaybePopErrorDialog(ERROR_DIALOG_TYPE.ABORT);
} }
@Override @Override
@@ -295,10 +318,13 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp
@Override @Override
public void onMatch(List<ScanResult> scanResults) { public void onMatch(List<ScanResult> scanResults) {
mHandler.removeMessages(MESSAGE_STOP_SCAN_WIFI_LIST); // Shouldn't need to renew cached list, since input result is empty.
renewAccessPointList(scanResults); if (scanResults != null && scanResults.size() > 0) {
mHandler.removeMessages(MESSAGE_STOP_SCAN_WIFI_LIST);
renewAccessPointList(scanResults);
notifyAdapterRefresh(); notifyAdapterRefresh();
}
} }
// Updates internal AccessPoint list from WifiTracker. scanResults are used to update key list // Updates internal AccessPoint list from WifiTracker. scanResults are used to update key list
@@ -329,17 +355,24 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp
@Override @Override
public void onUserSelectionConnectSuccess(WifiConfiguration wificonfiguration) { public void onUserSelectionConnectSuccess(WifiConfiguration wificonfiguration) {
// Dismisses current dialog and finishes Activity, since connection is success. // Removes the progress icon.
dismiss(); final Dialog dialog = getDialog();
final Activity activity = getActivity(); if (dialog != null) {
if (activity != null) { final View view = dialog.findViewById(R.id.network_request_title_progress);
activity.finish(); if (view != null) {
view.setVisibility(View.GONE);
}
} }
// Posts delay to finish self since connection is success.
mHandler.removeMessages(MESSAGE_STOP_SCAN_WIFI_LIST);
mHandler.sendEmptyMessageDelayed(MESSAGE_FINISH_ACTIVITY,
DELAY_TIME_USER_AWARE_CONNECTED_MS);
} }
@Override @Override
public void onUserSelectionConnectFailure(WifiConfiguration wificonfiguration) { public void onUserSelectionConnectFailure(WifiConfiguration wificonfiguration) {
stopScanningAndPopErrorDialog(ERROR_DIALOG_TYPE.ABORT); stopScanningAndMaybePopErrorDialog(ERROR_DIALOG_TYPE.ABORT);
} }
private final class FilterWifiTracker { private final class FilterWifiTracker {

View File

@@ -111,14 +111,17 @@ public class NetworkRequestDialogFragmentTest {
ShadowLooper.getShadowMainLooper().runToEndOfTasks(); ShadowLooper.getShadowMainLooper().runToEndOfTasks();
assertThat(fakeFragment.bCalledStopAndPop).isTrue(); assertThat(fakeFragment.bCalledStopAndPop).isTrue();
assertThat(fakeFragment.errorType).isEqualTo(ERROR_DIALOG_TYPE.TIME_OUT);
} }
class FakeNetworkRequestDialogFragment extends NetworkRequestDialogFragment { class FakeNetworkRequestDialogFragment extends NetworkRequestDialogFragment {
boolean bCalledStopAndPop = false; boolean bCalledStopAndPop = false;
ERROR_DIALOG_TYPE errorType = null;
@Override @Override
public void stopScanningAndPopErrorDialog(ERROR_DIALOG_TYPE type) { public void stopScanningAndMaybePopErrorDialog(ERROR_DIALOG_TYPE type) {
bCalledStopAndPop = true; bCalledStopAndPop = true;
errorType = type;
} }
} }
@@ -150,22 +153,28 @@ public class NetworkRequestDialogFragmentTest {
@Test @Test
public void updateAccessPointList_onUserSelectionConnectSuccess_shouldCloseTheDialog() { public void updateAccessPointList_onUserSelectionConnectSuccess_shouldCloseTheDialog() {
List<AccessPoint> accessPointList = createAccessPointList(); // Assert
when(networkRequestDialogFragment.getAccessPointList()).thenReturn(accessPointList); FakeNetworkRequestDialogFragment fakeFragment = new FakeNetworkRequestDialogFragment();
networkRequestDialogFragment.show(mActivity.getSupportFragmentManager(), null); FakeNetworkRequestDialogFragment spyFakeFragment = spy(fakeFragment);
AlertDialog alertDialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(alertDialog.isShowing()).isTrue();
// Test if config would update list. List<AccessPoint> accessPointList = createAccessPointList();
when(spyFakeFragment.getAccessPointList()).thenReturn(accessPointList);
spyFakeFragment.show(mActivity.getSupportFragmentManager(), null);
// Action
WifiConfiguration config = new WifiConfiguration(); WifiConfiguration config = new WifiConfiguration();
config.SSID = "Test AP 3"; config.SSID = "Test AP 3";
networkRequestDialogFragment.onUserSelectionConnectSuccess(config); spyFakeFragment.onUserSelectionConnectSuccess(config);
assertThat(alertDialog.isShowing()).isFalse(); // Check
ShadowLooper.getShadowMainLooper().runToEndOfTasks();
assertThat(fakeFragment.bCalledStopAndPop).isTrue();
assertThat(fakeFragment.errorType).isNull();
} }
@Test @Test
public void updateAccessPointList_onUserSelectionConnectFailure_shouldCallTimeoutDialog() { public void updateAccessPointList_onUserSelectionConnectFailure_shouldCallAbortDialog() {
FakeNetworkRequestDialogFragment fakeFragment = new FakeNetworkRequestDialogFragment(); FakeNetworkRequestDialogFragment fakeFragment = new FakeNetworkRequestDialogFragment();
FakeNetworkRequestDialogFragment spyFakeFragment = spy(fakeFragment); FakeNetworkRequestDialogFragment spyFakeFragment = spy(fakeFragment);
List<AccessPoint> accessPointList = createAccessPointList(); List<AccessPoint> accessPointList = createAccessPointList();
@@ -181,6 +190,7 @@ public class NetworkRequestDialogFragmentTest {
fakeFragment.onUserSelectionConnectFailure(config); fakeFragment.onUserSelectionConnectFailure(config);
assertThat(fakeFragment.bCalledStopAndPop).isTrue(); assertThat(fakeFragment.bCalledStopAndPop).isTrue();
assertThat(fakeFragment.errorType).isEqualTo(ERROR_DIALOG_TYPE.ABORT);
} }
@Test @Test