Loading src/com/android/settings/wifi/NetworkRequestDialogFragment.java +57 −24 Original line number Diff line number Diff line Loading @@ -16,7 +16,6 @@ package com.android.settings.wifi; import android.app.Activity; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; Loading Loading @@ -67,9 +66,15 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp /** Message sent to us to stop scanning wifi and pop up timeout dialog. */ 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. */ 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. */ private static final int DELAY_TIME_STOP_SCAN_MS = 30 * 1000; Loading Loading @@ -155,8 +160,10 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp public void onCancel(@NonNull DialogInterface dialog) { super.onCancel(dialog); // Finishes the activity when user clicks back key or outside of the dialog. if (getActivity() != null) { getActivity().finish(); } } @Override public void onPause() { Loading @@ -177,6 +184,8 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp @Override public void onDestroy() { super.onDestroy(); mHandler.removeMessages(MESSAGE_FINISH_ACTIVITY); if (mFilterWifiTracker != null) { mFilterWifiTracker.onDestroy(); mFilterWifiTracker = null; Loading Loading @@ -207,7 +216,10 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp switch (msg.what) { case 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; default: // Do nothing. Loading @@ -216,11 +228,20 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp } }; protected void stopScanningAndPopErrorDialog(ERROR_DIALOG_TYPE type) { protected void stopScanningAndMaybePopErrorDialog(ERROR_DIALOG_TYPE type) { // Dismisses current dialog. final Dialog dialog = getDialog(); if (dialog != null && dialog.isShowing()) { dismiss(); } // Throws new timeout dialog. 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(); Loading @@ -230,6 +251,8 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp NetworkRequestDialogFragment.class.getSimpleName()); } } @Override public int getMetricsCategory() { return MetricsProto.MetricsEvent.WIFI_SCANNING_NEEDED_DIALOG; Loading Loading @@ -284,7 +307,7 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp @Override public void onAbort() { stopScanningAndPopErrorDialog(ERROR_DIALOG_TYPE.ABORT); stopScanningAndMaybePopErrorDialog(ERROR_DIALOG_TYPE.ABORT); } @Override Loading @@ -295,11 +318,14 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp @Override public void onMatch(List<ScanResult> scanResults) { // Shouldn't need to renew cached list, since input result is empty. if (scanResults != null && scanResults.size() > 0) { mHandler.removeMessages(MESSAGE_STOP_SCAN_WIFI_LIST); renewAccessPointList(scanResults); notifyAdapterRefresh(); } } // Updates internal AccessPoint list from WifiTracker. scanResults are used to update key list // of AccessPoint, and could be null if there is no necessary to update key list. Loading Loading @@ -329,17 +355,24 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp @Override public void onUserSelectionConnectSuccess(WifiConfiguration wificonfiguration) { // Dismisses current dialog and finishes Activity, since connection is success. dismiss(); final Activity activity = getActivity(); if (activity != null) { activity.finish(); // Removes the progress icon. final Dialog dialog = getDialog(); if (dialog != null) { final View view = dialog.findViewById(R.id.network_request_title_progress); 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 public void onUserSelectionConnectFailure(WifiConfiguration wificonfiguration) { stopScanningAndPopErrorDialog(ERROR_DIALOG_TYPE.ABORT); stopScanningAndMaybePopErrorDialog(ERROR_DIALOG_TYPE.ABORT); } private final class FilterWifiTracker { Loading tests/robotests/src/com/android/settings/wifi/NetworkRequestDialogFragmentTest.java +19 −9 Original line number Diff line number Diff line Loading @@ -111,14 +111,17 @@ public class NetworkRequestDialogFragmentTest { ShadowLooper.getShadowMainLooper().runToEndOfTasks(); assertThat(fakeFragment.bCalledStopAndPop).isTrue(); assertThat(fakeFragment.errorType).isEqualTo(ERROR_DIALOG_TYPE.TIME_OUT); } class FakeNetworkRequestDialogFragment extends NetworkRequestDialogFragment { boolean bCalledStopAndPop = false; ERROR_DIALOG_TYPE errorType = null; @Override public void stopScanningAndPopErrorDialog(ERROR_DIALOG_TYPE type) { public void stopScanningAndMaybePopErrorDialog(ERROR_DIALOG_TYPE type) { bCalledStopAndPop = true; errorType = type; } } Loading Loading @@ -150,22 +153,28 @@ public class NetworkRequestDialogFragmentTest { @Test public void updateAccessPointList_onUserSelectionConnectSuccess_shouldCloseTheDialog() { // Assert FakeNetworkRequestDialogFragment fakeFragment = new FakeNetworkRequestDialogFragment(); FakeNetworkRequestDialogFragment spyFakeFragment = spy(fakeFragment); List<AccessPoint> accessPointList = createAccessPointList(); when(networkRequestDialogFragment.getAccessPointList()).thenReturn(accessPointList); networkRequestDialogFragment.show(mActivity.getSupportFragmentManager(), null); AlertDialog alertDialog = ShadowAlertDialogCompat.getLatestAlertDialog(); assertThat(alertDialog.isShowing()).isTrue(); when(spyFakeFragment.getAccessPointList()).thenReturn(accessPointList); // Test if config would update list. spyFakeFragment.show(mActivity.getSupportFragmentManager(), null); // Action WifiConfiguration config = new WifiConfiguration(); 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 public void updateAccessPointList_onUserSelectionConnectFailure_shouldCallTimeoutDialog() { public void updateAccessPointList_onUserSelectionConnectFailure_shouldCallAbortDialog() { FakeNetworkRequestDialogFragment fakeFragment = new FakeNetworkRequestDialogFragment(); FakeNetworkRequestDialogFragment spyFakeFragment = spy(fakeFragment); List<AccessPoint> accessPointList = createAccessPointList(); Loading @@ -181,6 +190,7 @@ public class NetworkRequestDialogFragmentTest { fakeFragment.onUserSelectionConnectFailure(config); assertThat(fakeFragment.bCalledStopAndPop).isTrue(); assertThat(fakeFragment.errorType).isEqualTo(ERROR_DIALOG_TYPE.ABORT); } @Test Loading Loading
src/com/android/settings/wifi/NetworkRequestDialogFragment.java +57 −24 Original line number Diff line number Diff line Loading @@ -16,7 +16,6 @@ package com.android.settings.wifi; import android.app.Activity; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; Loading Loading @@ -67,9 +66,15 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp /** Message sent to us to stop scanning wifi and pop up timeout dialog. */ 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. */ 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. */ private static final int DELAY_TIME_STOP_SCAN_MS = 30 * 1000; Loading Loading @@ -155,8 +160,10 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp public void onCancel(@NonNull DialogInterface dialog) { super.onCancel(dialog); // Finishes the activity when user clicks back key or outside of the dialog. if (getActivity() != null) { getActivity().finish(); } } @Override public void onPause() { Loading @@ -177,6 +184,8 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp @Override public void onDestroy() { super.onDestroy(); mHandler.removeMessages(MESSAGE_FINISH_ACTIVITY); if (mFilterWifiTracker != null) { mFilterWifiTracker.onDestroy(); mFilterWifiTracker = null; Loading Loading @@ -207,7 +216,10 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp switch (msg.what) { case 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; default: // Do nothing. Loading @@ -216,11 +228,20 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp } }; protected void stopScanningAndPopErrorDialog(ERROR_DIALOG_TYPE type) { protected void stopScanningAndMaybePopErrorDialog(ERROR_DIALOG_TYPE type) { // Dismisses current dialog. final Dialog dialog = getDialog(); if (dialog != null && dialog.isShowing()) { dismiss(); } // Throws new timeout dialog. 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(); Loading @@ -230,6 +251,8 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp NetworkRequestDialogFragment.class.getSimpleName()); } } @Override public int getMetricsCategory() { return MetricsProto.MetricsEvent.WIFI_SCANNING_NEEDED_DIALOG; Loading Loading @@ -284,7 +307,7 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp @Override public void onAbort() { stopScanningAndPopErrorDialog(ERROR_DIALOG_TYPE.ABORT); stopScanningAndMaybePopErrorDialog(ERROR_DIALOG_TYPE.ABORT); } @Override Loading @@ -295,11 +318,14 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp @Override public void onMatch(List<ScanResult> scanResults) { // Shouldn't need to renew cached list, since input result is empty. if (scanResults != null && scanResults.size() > 0) { mHandler.removeMessages(MESSAGE_STOP_SCAN_WIFI_LIST); renewAccessPointList(scanResults); notifyAdapterRefresh(); } } // Updates internal AccessPoint list from WifiTracker. scanResults are used to update key list // of AccessPoint, and could be null if there is no necessary to update key list. Loading Loading @@ -329,17 +355,24 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp @Override public void onUserSelectionConnectSuccess(WifiConfiguration wificonfiguration) { // Dismisses current dialog and finishes Activity, since connection is success. dismiss(); final Activity activity = getActivity(); if (activity != null) { activity.finish(); // Removes the progress icon. final Dialog dialog = getDialog(); if (dialog != null) { final View view = dialog.findViewById(R.id.network_request_title_progress); 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 public void onUserSelectionConnectFailure(WifiConfiguration wificonfiguration) { stopScanningAndPopErrorDialog(ERROR_DIALOG_TYPE.ABORT); stopScanningAndMaybePopErrorDialog(ERROR_DIALOG_TYPE.ABORT); } private final class FilterWifiTracker { Loading
tests/robotests/src/com/android/settings/wifi/NetworkRequestDialogFragmentTest.java +19 −9 Original line number Diff line number Diff line Loading @@ -111,14 +111,17 @@ public class NetworkRequestDialogFragmentTest { ShadowLooper.getShadowMainLooper().runToEndOfTasks(); assertThat(fakeFragment.bCalledStopAndPop).isTrue(); assertThat(fakeFragment.errorType).isEqualTo(ERROR_DIALOG_TYPE.TIME_OUT); } class FakeNetworkRequestDialogFragment extends NetworkRequestDialogFragment { boolean bCalledStopAndPop = false; ERROR_DIALOG_TYPE errorType = null; @Override public void stopScanningAndPopErrorDialog(ERROR_DIALOG_TYPE type) { public void stopScanningAndMaybePopErrorDialog(ERROR_DIALOG_TYPE type) { bCalledStopAndPop = true; errorType = type; } } Loading Loading @@ -150,22 +153,28 @@ public class NetworkRequestDialogFragmentTest { @Test public void updateAccessPointList_onUserSelectionConnectSuccess_shouldCloseTheDialog() { // Assert FakeNetworkRequestDialogFragment fakeFragment = new FakeNetworkRequestDialogFragment(); FakeNetworkRequestDialogFragment spyFakeFragment = spy(fakeFragment); List<AccessPoint> accessPointList = createAccessPointList(); when(networkRequestDialogFragment.getAccessPointList()).thenReturn(accessPointList); networkRequestDialogFragment.show(mActivity.getSupportFragmentManager(), null); AlertDialog alertDialog = ShadowAlertDialogCompat.getLatestAlertDialog(); assertThat(alertDialog.isShowing()).isTrue(); when(spyFakeFragment.getAccessPointList()).thenReturn(accessPointList); // Test if config would update list. spyFakeFragment.show(mActivity.getSupportFragmentManager(), null); // Action WifiConfiguration config = new WifiConfiguration(); 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 public void updateAccessPointList_onUserSelectionConnectFailure_shouldCallTimeoutDialog() { public void updateAccessPointList_onUserSelectionConnectFailure_shouldCallAbortDialog() { FakeNetworkRequestDialogFragment fakeFragment = new FakeNetworkRequestDialogFragment(); FakeNetworkRequestDialogFragment spyFakeFragment = spy(fakeFragment); List<AccessPoint> accessPointList = createAccessPointList(); Loading @@ -181,6 +190,7 @@ public class NetworkRequestDialogFragmentTest { fakeFragment.onUserSelectionConnectFailure(config); assertThat(fakeFragment.bCalledStopAndPop).isTrue(); assertThat(fakeFragment.errorType).isEqualTo(ERROR_DIALOG_TYPE.ABORT); } @Test Loading