Merge "[Network Connection] Implement "show all" button for NetworkRequestDialog"
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 365bda8..b787473 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -10562,6 +10562,8 @@
     <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>
+    <!-- Neutral button for Network connection request Dialog [CHAR LIMIT=30] -->
+    <string name="network_connection_request_dialog_showall">Show all</string>
 
     <!-- Summary for bluetooth devices count in Bluetooth devices slice. [CHAR LIMIT=NONE] -->
     <plurals name="show_bluetooth_devices">
diff --git a/src/com/android/settings/wifi/NetworkRequestDialogFragment.java b/src/com/android/settings/wifi/NetworkRequestDialogFragment.java
index d1df7f0..ba29e3a 100644
--- a/src/com/android/settings/wifi/NetworkRequestDialogFragment.java
+++ b/src/com/android/settings/wifi/NetworkRequestDialogFragment.java
@@ -37,6 +37,7 @@
 import android.view.ViewGroup;
 import android.widget.ArrayAdapter;
 import android.widget.BaseAdapter;
+import android.widget.Button;
 import android.widget.ProgressBar;
 import android.widget.TextView;
 import android.widget.Toast;
@@ -69,8 +70,12 @@
     /** Message sent to us to stop scanning wifi and pop up timeout dialog. */
     private static final int MESSAGE_STOP_SCAN_WIFI_LIST = 0;
 
-    /** 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 or just show all if {@code
+     * mShowLimitedItem} is false.
+     */
     private static final int MAX_NUMBER_LIST_ITEM = 5;
+    private boolean mShowLimitedItem = true;
 
     /** Delayed time to stop scanning wifi. */
     private static final int DELAY_TIME_STOP_SCAN_MS = 30 * 1000;
@@ -110,13 +115,29 @@
         final AlertDialog.Builder builder = new AlertDialog.Builder(context)
                 .setCustomTitle(customTitle)
                 .setAdapter(mDialogAdapter, this)
-                .setPositiveButton(R.string.cancel, (dialog, which) -> getActivity().finish());
+                .setPositiveButton(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 */);
 
         // Clicking list item is to connect wifi ap.
         final AlertDialog dialog = builder.create();
         dialog.getListView()
                 .setOnItemClickListener(
                         (parent, view, position, id) -> this.onClick(dialog, position));
+
+        dialog.setOnShowListener((dialogInterface) -> {
+            // Replace NeutralButton onClickListener to avoid closing dialog
+            final Button neutralBtn = dialog.getButton(AlertDialog.BUTTON_NEUTRAL);
+            neutralBtn.setVisibility(View.GONE);
+            neutralBtn.setOnClickListener(v -> {
+                mShowLimitedItem = false;
+                renewAccessPointList(null /* List<ScanResult> */);
+                notifyAdapterRefresh();
+                neutralBtn.setVisibility(View.GONE);
+            });
+        });
+
         return dialog;
     }
 
@@ -202,6 +223,18 @@
         }
     }
 
+    private void showNeutralButton() {
+        final AlertDialog alertDialog = (AlertDialog) getDialog();
+        if (alertDialog == null) {
+            return;
+        }
+
+        final Button neutralBtn = alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL);
+        if (neutralBtn != null) {
+            neutralBtn.setVisibility(View.VISIBLE);
+        }
+    }
+
     @Override
     public void onResume() {
         super.onResume();
@@ -394,6 +427,10 @@
                     mAccessPointKeys.add(key);
                 }
             }
+
+            if (mShowLimitedItem && (mAccessPointKeys.size() > MAX_NUMBER_LIST_ITEM)) {
+                showNeutralButton();
+            }
         }
 
         /**
@@ -414,7 +451,7 @@
 
                     count++;
                     // Limits how many count of items could show.
-                    if (count >= MAX_NUMBER_LIST_ITEM) {
+                    if (mShowLimitedItem && count >= MAX_NUMBER_LIST_ITEM) {
                         break;
                     }
                 }
diff --git a/tests/robotests/src/com/android/settings/wifi/NetworkRequestDialogFragmentTest.java b/tests/robotests/src/com/android/settings/wifi/NetworkRequestDialogFragmentTest.java
index 0286d07..21b68f2 100644
--- a/tests/robotests/src/com/android/settings/wifi/NetworkRequestDialogFragmentTest.java
+++ b/tests/robotests/src/com/android/settings/wifi/NetworkRequestDialogFragmentTest.java
@@ -33,6 +33,7 @@
 import android.net.wifi.WifiManager;
 import android.net.wifi.WifiManager.NetworkRequestUserSelectionCallback;
 import android.os.Bundle;
+import android.view.View;
 import android.widget.Button;
 import android.widget.TextView;
 
@@ -264,20 +265,77 @@
     private List<AccessPoint> createAccessPointList() {
         List<AccessPoint> accessPointList = spy(new ArrayList<>());
         Bundle bundle = new Bundle();
+
         bundle.putString(KEY_SSID, "Test AP 1");
         bundle.putInt(KEY_SECURITY, 1);
         accessPointList.add(new AccessPoint(mContext, bundle));
+
         bundle.putString(KEY_SSID, "Test AP 2");
         bundle.putInt(KEY_SECURITY, 1);
         accessPointList.add(new AccessPoint(mContext, bundle));
+
         bundle.putString(KEY_SSID, "Test AP 3");
         bundle.putInt(KEY_SECURITY, 2);
-        AccessPoint clickedAccessPoint = new AccessPoint(mContext, bundle);
-        accessPointList.add(clickedAccessPoint);
+        accessPointList.add(new AccessPoint(mContext, bundle));
+
         bundle.putString(KEY_SSID, "Test AP 4");
         bundle.putInt(KEY_SECURITY, 0);
         accessPointList.add(new AccessPoint(mContext, bundle));
 
         return accessPointList;
     }
+
+    @Test
+    public void display_shouldNotShowNeutralButton() {
+        networkRequestDialogFragment.show(mActivity.getSupportFragmentManager(), /* tag */ null);
+        final AlertDialog alertDialog = ShadowAlertDialogCompat.getLatestAlertDialog();
+
+        final Button button = alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL);
+        assertThat(button).isNotNull();
+        assertThat(button.getVisibility()).isEqualTo(View.GONE);
+    }
+
+    @Test
+    public void onMatchManyResult_showNeutralButton() {
+        networkRequestDialogFragment.show(mActivity.getSupportFragmentManager(), /* tag */ null);
+        final AlertDialog alertDialog = ShadowAlertDialogCompat.getLatestAlertDialog();
+
+        final String SSID_AP = "Test AP ";
+        final List<ScanResult> scanResults = new ArrayList<>();
+        for (int i = 0; i < 6 ; i ++) {
+            ScanResult scanResult = new ScanResult();
+            scanResult.SSID = SSID_AP + i;
+            scanResult.capabilities = "WEP";
+            scanResults.add(scanResult);
+        }
+        networkRequestDialogFragment.onMatch(scanResults);
+
+        final Button button = alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL);
+        assertThat(button).isNotNull();
+        assertThat(button.getVisibility()).isEqualTo(View.VISIBLE);
+    }
+
+    @Test
+    public void clickNeutralButton_hideNeutralButton() {
+        // Assert
+        networkRequestDialogFragment.show(mActivity.getSupportFragmentManager(), /* tag */ null);
+        final AlertDialog alertDialog = ShadowAlertDialogCompat.getLatestAlertDialog();
+
+        final String SSID_AP = "Test AP ";
+        final List<ScanResult> scanResults = new ArrayList<>();
+        for (int i = 0; i < 6 ; i ++) {
+            ScanResult scanResult = new ScanResult();
+            scanResult.SSID = SSID_AP + i;
+            scanResult.capabilities = "WEP";
+            scanResults.add(scanResult);
+        }
+        networkRequestDialogFragment.onMatch(scanResults);
+
+        // Action
+        final Button button = alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL);
+        button.performClick();
+
+        // Check
+        assertThat(button.getVisibility()).isEqualTo(View.GONE);
+    }
 }