[Network Connection] Implement "show all" button for

NetworkRequestDialog

Add "show all" button to NetworkRequest dialog to let user be able to
show all AccessPoints of onMatch() callback.

Bug: 117985692
Test: atest NetworkRequestDialogFragmentTest
Change-Id: I604083fd0f3ea98208d860a327733699cd4664d7
This commit is contained in:
cosmohsieh
2019-02-13 17:11:16 +08:00
parent 29934110dd
commit 1e687606dd
3 changed files with 102 additions and 5 deletions

View File

@@ -10560,6 +10560,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">

View File

@@ -37,6 +37,7 @@ import android.view.View;
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 @@ 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;
/** 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 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp
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 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp
}
}
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 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp
mAccessPointKeys.add(key);
}
}
if (mShowLimitedItem && (mAccessPointKeys.size() > MAX_NUMBER_LIST_ITEM)) {
showNeutralButton();
}
}
/**
@@ -414,7 +451,7 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp
count++;
// Limits how many count of items could show.
if (count >= MAX_NUMBER_LIST_ITEM) {
if (mShowLimitedItem && count >= MAX_NUMBER_LIST_ITEM) {
break;
}
}

View File

@@ -33,6 +33,7 @@ import android.net.wifi.WifiConfiguration;
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 @@ public class NetworkRequestDialogFragmentTest {
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);
}
}