Merge "[Network Connection] Implement new UI changing"
diff --git a/res/layout/network_request_dialog_title.xml b/res/layout/network_request_dialog_title.xml
index f643e0f..b61a7db 100644
--- a/res/layout/network_request_dialog_title.xml
+++ b/res/layout/network_request_dialog_title.xml
@@ -25,14 +25,12 @@
   <TextView
       android:id="@+id/network_request_title_text"
       android:layout_width="0dp"
-      android:layout_height="match_parent"
+      android:layout_height="wrap_content"
       android:paddingLeft="24dp"
       android:paddingTop="18dp"
       android:layout_weight="1"
       android:textSize="18sp"
       android:gravity="center_vertical"
-      android:maxLines="1"
-      android:ellipsize="end"
       style="@style/info_label"/>
 
   <ProgressBar
diff --git a/src/com/android/settings/wifi/NetworkRequestDialogFragment.java b/src/com/android/settings/wifi/NetworkRequestDialogFragment.java
index ba29e3a..09360e4 100644
--- a/src/com/android/settings/wifi/NetworkRequestDialogFragment.java
+++ b/src/com/android/settings/wifi/NetworkRequestDialogFragment.java
@@ -82,11 +82,15 @@
 
     @VisibleForTesting
     final static String EXTRA_APP_NAME = "com.android.settings.wifi.extra.APP_NAME";
+    final static String EXTRA_IS_SPECIFIED_SSID =
+            "com.android.settings.wifi.extra.REQUEST_IS_FOR_SINGLE_NETWORK";
 
     private List<AccessPoint> mAccessPointList;
     private FilterWifiTracker mFilterWifiTracker;
     private AccessPointAdapter mDialogAdapter;
     private NetworkRequestUserSelectionCallback mUserSelectionCallback;
+    private boolean mIsSpecifiedSsid;
+    private boolean mWaitingConnectCallback;
 
     public static NetworkRequestDialogFragment newInstance() {
         NetworkRequestDialogFragment dialogFragment = new NetworkRequestDialogFragment();
@@ -104,6 +108,11 @@
         final TextView title = customTitle.findViewById(R.id.network_request_title_text);
         title.setText(getTitle());
 
+        final Intent intent = getActivity().getIntent();
+        if (intent != null) {
+            mIsSpecifiedSsid = intent.getBooleanExtra(EXTRA_IS_SPECIFIED_SSID, false);
+        }
+
         final ProgressBar progressBar = customTitle.findViewById(
                 R.id.network_request_title_progress);
         progressBar.setVisibility(View.VISIBLE);
@@ -115,10 +124,13 @@
         final AlertDialog.Builder builder = new AlertDialog.Builder(context)
                 .setCustomTitle(customTitle)
                 .setAdapter(mDialogAdapter, this)
-                .setPositiveButton(R.string.cancel, (dialog, which) -> getActivity().finish())
+                .setNegativeButton(R.string.cancel, (dialog, which) -> getActivity().finish())
                 // Do nothings, will replace the onClickListener to avoid auto closing dialog.
                 .setNeutralButton(R.string.network_connection_request_dialog_showall,
                         null /* OnClickListener */);
+        if (mIsSpecifiedSsid) {
+            builder.setPositiveButton(R.string.wifi_connect, null /* OnClickListener */);
+        }
 
         // Clicking list item is to connect wifi ap.
         final AlertDialog dialog = builder.create();
@@ -136,8 +148,19 @@
                 notifyAdapterRefresh();
                 neutralBtn.setVisibility(View.GONE);
             });
-        });
 
+            // Replace Positive onClickListener to avoid closing dialog
+            if (mIsSpecifiedSsid) {
+                final Button positiveBtn = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
+                positiveBtn.setOnClickListener(v -> {
+                    // When clicking connect button, should connect to the first and the only one
+                    // list item.
+                    this.onClick(dialog, 0 /* position */);
+                });
+                // Disable button in first, and enable it after there are some accesspoints in list.
+                positiveBtn.setEnabled(false);
+            }
+        });
         return dialog;
     }
 
@@ -184,6 +207,9 @@
 
             if (wifiConfig != null) {
                 mUserSelectionCallback.select(wifiConfig);
+
+                mWaitingConnectCallback = true;
+                updateConnectButton(false);
             }
         }
     }
@@ -223,7 +249,7 @@
         }
     }
 
-    private void showNeutralButton() {
+    private void showAllButton() {
         final AlertDialog alertDialog = (AlertDialog) getDialog();
         if (alertDialog == null) {
             return;
@@ -235,6 +261,35 @@
         }
     }
 
+    private void updateConnectButton(boolean enabled) {
+        // The button is only showed in single SSID mode.
+        if (!mIsSpecifiedSsid) {
+            return;
+        }
+
+        final AlertDialog alertDialog = (AlertDialog) getDialog();
+        if (alertDialog == null) {
+            return;
+        }
+
+        final Button positiveBtn = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
+        if (positiveBtn != null) {
+            positiveBtn.setEnabled(enabled);
+        }
+    }
+
+    private void hideProgressIcon() {
+        final AlertDialog alertDialog = (AlertDialog) getDialog();
+        if (alertDialog == null) {
+            return;
+        }
+
+        final View progress = alertDialog.findViewById(R.id.network_request_title_progress);
+        if (progress != null) {
+            progress.setVisibility(View.GONE);
+        }
+    }
+
     @Override
     public void onResume() {
         super.onResume();
@@ -403,7 +458,9 @@
 
     @Override
     public void onUserSelectionConnectFailure(WifiConfiguration wificonfiguration) {
-        stopScanningAndPopErrorDialog(ERROR_DIALOG_TYPE.ABORT);
+        // Do nothing when selection is failed, let user could try again easily.
+        mWaitingConnectCallback = false;
+        updateConnectButton(true);
     }
 
     private final class FilterWifiTracker {
@@ -427,10 +484,6 @@
                     mAccessPointKeys.add(key);
                 }
             }
-
-            if (mShowLimitedItem && (mAccessPointKeys.size() > MAX_NUMBER_LIST_ITEM)) {
-                showNeutralButton();
-            }
         }
 
         /**
@@ -457,6 +510,21 @@
                 }
             }
 
+            // Update related UI buttons
+            if (mShowLimitedItem && (count >= MAX_NUMBER_LIST_ITEM)) {
+                showAllButton();
+            }
+            if (count > 0) {
+                hideProgressIcon();
+            }
+            // Enable connect button if there is Accesspoint item, except for the situation that
+            // user click but connected status doesn't come back yet.
+            if (count < 0) {
+                updateConnectButton(false);
+            } else if (!mWaitingConnectCallback) {
+                updateConnectButton(true);
+            }
+
             return result;
         }
 
diff --git a/tests/robotests/src/com/android/settings/wifi/NetworkRequestDialogFragmentTest.java b/tests/robotests/src/com/android/settings/wifi/NetworkRequestDialogFragmentTest.java
index 21b68f2..d2403b9 100644
--- a/tests/robotests/src/com/android/settings/wifi/NetworkRequestDialogFragmentTest.java
+++ b/tests/robotests/src/com/android/settings/wifi/NetworkRequestDialogFragmentTest.java
@@ -106,12 +106,12 @@
     }
 
     @Test
-    public void clickPositiveButton_shouldCloseTheDialog() {
+    public void clickNegativeButton_shouldCloseTheDialog() {
         networkRequestDialogFragment.show(mActivity.getSupportFragmentManager(), null);
         AlertDialog alertDialog = ShadowAlertDialogCompat.getLatestAlertDialog();
         assertThat(alertDialog.isShowing()).isTrue();
 
-        Button positiveButton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
+        Button positiveButton = alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
         assertThat(positiveButton).isNotNull();
 
         positiveButton.performClick();
@@ -186,26 +186,6 @@
     }
 
     @Test
-    public void updateAccessPointList_onUserSelectionConnectFailure_shouldCallAbortDialog() {
-        FakeNetworkRequestDialogFragment fakeFragment = new FakeNetworkRequestDialogFragment();
-        FakeNetworkRequestDialogFragment spyFakeFragment = spy(fakeFragment);
-        List<AccessPoint> accessPointList = createAccessPointList();
-        when(spyFakeFragment.getAccessPointList()).thenReturn(accessPointList);
-        spyFakeFragment.show(mActivity.getSupportFragmentManager(), null);
-
-        AlertDialog alertDialog = ShadowAlertDialogCompat.getLatestAlertDialog();
-        assertThat(alertDialog.isShowing()).isTrue();
-
-        // Test if config would update list.
-        WifiConfiguration config = new WifiConfiguration();
-        config.SSID = "Test AP 3";
-        fakeFragment.onUserSelectionConnectFailure(config);
-
-        assertThat(fakeFragment.bCalledStopAndPop).isTrue();
-        assertThat(fakeFragment.errorType).isEqualTo(ERROR_DIALOG_TYPE.ABORT);
-    }
-
-    @Test
     public void onUserSelectionCallbackRegistration_onClick_shouldCallSelect() {
         // Assert.
         final int indexClickItem = 3;
@@ -267,19 +247,27 @@
         Bundle bundle = new Bundle();
 
         bundle.putString(KEY_SSID, "Test AP 1");
-        bundle.putInt(KEY_SECURITY, 1);
+        bundle.putInt(KEY_SECURITY, 1 /* WEP */);
         accessPointList.add(new AccessPoint(mContext, bundle));
 
         bundle.putString(KEY_SSID, "Test AP 2");
-        bundle.putInt(KEY_SECURITY, 1);
+        bundle.putInt(KEY_SECURITY, 1 /* WEP */);
         accessPointList.add(new AccessPoint(mContext, bundle));
 
         bundle.putString(KEY_SSID, "Test AP 3");
-        bundle.putInt(KEY_SECURITY, 2);
+        bundle.putInt(KEY_SECURITY, 1 /* WEP */);
         accessPointList.add(new AccessPoint(mContext, bundle));
 
         bundle.putString(KEY_SSID, "Test AP 4");
-        bundle.putInt(KEY_SECURITY, 0);
+        bundle.putInt(KEY_SECURITY, 0 /* NONE */);
+        accessPointList.add(new AccessPoint(mContext, bundle));
+
+        bundle.putString(KEY_SSID, "Test AP 5");
+        bundle.putInt(KEY_SECURITY, 1 /* WEP */);
+        accessPointList.add(new AccessPoint(mContext, bundle));
+
+        bundle.putString(KEY_SSID, "Test AP 6");
+        bundle.putInt(KEY_SECURITY, 1 /* WEP */);
         accessPointList.add(new AccessPoint(mContext, bundle));
 
         return accessPointList;
@@ -300,9 +288,13 @@
         networkRequestDialogFragment.show(mActivity.getSupportFragmentManager(), /* tag */ null);
         final AlertDialog alertDialog = ShadowAlertDialogCompat.getLatestAlertDialog();
 
+
+        List<AccessPoint> accessPointList = createAccessPointList();
+        when(mWifiTracker.getAccessPoints()).thenReturn(accessPointList);
+
         final String SSID_AP = "Test AP ";
         final List<ScanResult> scanResults = new ArrayList<>();
-        for (int i = 0; i < 6 ; i ++) {
+        for (int i = 0; i < 7 ; i ++) {
             ScanResult scanResult = new ScanResult();
             scanResult.SSID = SSID_AP + i;
             scanResult.capabilities = "WEP";