OSDN Git Service

[Network Connection] Implement toasting after wifi is connected
authorcosmohsieh <cosmohsieh@google.com>
Tue, 22 Jan 2019 02:24:20 +0000 (10:24 +0800)
committercosmohsieh <cosmohsieh@google.com>
Tue, 22 Jan 2019 05:49:10 +0000 (13:49 +0800)
When wifi is connected (onUserSelectionConnectSuccess), making a toast
infos user and closed the dialog.

Bug: 123160958
Test: atest NetworkRequestDialogFragmentTest
Change-Id: I85ab648188018773e1f76541d18903c733bc3e69

res/values/strings.xml
src/com/android/settings/wifi/NetworkRequestDialogFragment.java
tests/robotests/src/com/android/settings/wifi/NetworkRequestDialogFragmentTest.java

index a082074..51f49ca 100644 (file)
     <string name="network_connection_timeout_dialog_ok">Try again</string>
     <!-- Message for Network connection error state Dialog [CHAR LIMIT=NONE] -->
     <string name="network_connection_errorstate_dialog_message">Something came up. The application has cancelled the request to choose a device.</string>
+    <!-- Toast message when connection is successful [CHAR LIMIT=30] -->
+    <string name="network_connection_connect_successful">Connection successful</string>
 
     <!-- Summary for bluetooth devices count in Bluetooth devices slice. [CHAR LIMIT=NONE] -->
     <plurals name="show_bluetooth_devices">
index 0ed8544..e3428f8 100644 (file)
@@ -16,6 +16,7 @@
 
 package com.android.settings.wifi;
 
+import android.app.Activity;
 import android.app.Dialog;
 import android.app.settings.SettingsEnums;
 import android.content.Context;
@@ -36,6 +37,7 @@ import android.widget.ArrayAdapter;
 import android.widget.BaseAdapter;
 import android.widget.ProgressBar;
 import android.widget.TextView;
+import android.widget.Toast;
 
 import androidx.annotation.NonNull;
 import androidx.annotation.VisibleForTesting;
@@ -65,15 +67,9 @@ 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;
 
@@ -184,7 +180,6 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp
     public void onDestroy() {
         super.onDestroy();
 
-        mHandler.removeMessages(MESSAGE_FINISH_ACTIVITY);
         if (mFilterWifiTracker != null) {
             mFilterWifiTracker.onDestroy();
             mFilterWifiTracker = null;
@@ -215,10 +210,7 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp
             switch (msg.what) {
                 case MESSAGE_STOP_SCAN_WIFI_LIST:
                     removeMessages(MESSAGE_STOP_SCAN_WIFI_LIST);
-                    stopScanningAndMaybePopErrorDialog(ERROR_DIALOG_TYPE.TIME_OUT);
-                    break;
-                case MESSAGE_FINISH_ACTIVITY:
-                    stopScanningAndMaybePopErrorDialog(/* ERROR_DIALOG_TYPE */ null);
+                    stopScanningAndPopErrorDialog(ERROR_DIALOG_TYPE.TIME_OUT);
                     break;
                 default:
                     // Do nothing.
@@ -227,29 +219,21 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp
         }
     };
 
-    protected void stopScanningAndMaybePopErrorDialog(ERROR_DIALOG_TYPE type) {
+    protected void stopScanningAndPopErrorDialog(ERROR_DIALOG_TYPE type) {
         // Dismisses current dialog.
         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 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());
     }
 
     @Override
@@ -306,7 +290,7 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp
 
     @Override
     public void onAbort() {
-        stopScanningAndMaybePopErrorDialog(ERROR_DIALOG_TYPE.ABORT);
+        stopScanningAndPopErrorDialog(ERROR_DIALOG_TYPE.ABORT);
     }
 
     @Override
@@ -354,24 +338,17 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp
 
     @Override
     public void onUserSelectionConnectSuccess(WifiConfiguration wificonfiguration) {
-        // 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);
-            }
+        final Activity activity = getActivity();
+        if (activity != null) {
+            Toast.makeText(activity, R.string.network_connection_connect_successful,
+                    Toast.LENGTH_SHORT).show();
+            activity.finish();
         }
-
-        // 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) {
-        stopScanningAndMaybePopErrorDialog(ERROR_DIALOG_TYPE.ABORT);
+        stopScanningAndPopErrorDialog(ERROR_DIALOG_TYPE.ABORT);
     }
 
     private final class FilterWifiTracker {
index 17516e9..d84d665 100644 (file)
@@ -119,7 +119,7 @@ public class NetworkRequestDialogFragmentTest {
         ERROR_DIALOG_TYPE errorType = null;
 
         @Override
-        public void stopScanningAndMaybePopErrorDialog(ERROR_DIALOG_TYPE type) {
+        public void stopScanningAndPopErrorDialog(ERROR_DIALOG_TYPE type) {
             bCalledStopAndPop = true;
             errorType = type;
         }
@@ -152,25 +152,19 @@ public class NetworkRequestDialogFragmentTest {
     }
 
     @Test
-    public void updateAccessPointList_onUserSelectionConnectSuccess_shouldCloseTheDialog() {
+    public void updateAccessPointList_onUserSelectionConnectSuccess_shouldFinishActivity() {
         // Assert
-        FakeNetworkRequestDialogFragment fakeFragment = new FakeNetworkRequestDialogFragment();
-        FakeNetworkRequestDialogFragment spyFakeFragment = spy(fakeFragment);
-
-        List<AccessPoint> accessPointList = createAccessPointList();
-        when(spyFakeFragment.getAccessPointList()).thenReturn(accessPointList);
-
-        spyFakeFragment.show(mActivity.getSupportFragmentManager(), null);
+        final FragmentActivity spyActivity = spy(mActivity);
+        when(networkRequestDialogFragment.getActivity()).thenReturn(spyActivity);
+        networkRequestDialogFragment.show(spyActivity.getSupportFragmentManager(), "onUserSelectionConnectSuccess");
 
         // Action
-        WifiConfiguration config = new WifiConfiguration();
+        final WifiConfiguration config = new WifiConfiguration();
         config.SSID = "Test AP 3";
-        spyFakeFragment.onUserSelectionConnectSuccess(config);
+        networkRequestDialogFragment.onUserSelectionConnectSuccess(config);
 
         // Check
-        ShadowLooper.getShadowMainLooper().runToEndOfTasks();
-        assertThat(fakeFragment.bCalledStopAndPop).isTrue();
-        assertThat(fakeFragment.errorType).isNull();
+        verify(spyActivity).finish();
     }
 
     @Test