[Wi-Fi] Apply WifiTrackerLib objects in NetworkRequestDialogFragment
Bug: 152571756 Test: make RunSettingsRoboTests ROBOTEST_FILTER=com.android.settings.wifi Change-Id: Ie6f63cb8aa81d61bfa41687c73f8c8764cb3cefe
This commit is contained in:
@@ -194,8 +194,7 @@ public class NetworkRequestDialogActivity extends FragmentActivity implements
|
|||||||
if (mIsSpecifiedSsid) {
|
if (mIsSpecifiedSsid) {
|
||||||
// Prevent from throwing same dialog, because onMatch() will be called many times.
|
// Prevent from throwing same dialog, because onMatch() will be called many times.
|
||||||
if (mMatchedConfig == null) {
|
if (mMatchedConfig == null) {
|
||||||
mMatchedConfig = WifiUtils.getWifiConfig(
|
mMatchedConfig = WifiUtils.getWifiConfig(null /* wifiEntry */, scanResults.get(0));
|
||||||
null /* accesspoint */, scanResults.get(0), null /* password */);
|
|
||||||
showSingleSsidRequestDialog(
|
showSingleSsidRequestDialog(
|
||||||
WifiInfo.sanitizeSsid(mMatchedConfig.SSID), false /* isTryAgain */);
|
WifiInfo.sanitizeSsid(mMatchedConfig.SSID), false /* isTryAgain */);
|
||||||
}
|
}
|
||||||
|
@@ -16,15 +16,28 @@
|
|||||||
|
|
||||||
package com.android.settings.wifi;
|
package com.android.settings.wifi;
|
||||||
|
|
||||||
|
import static com.android.settings.wifi.WifiUtils.getWifiEntrySecurity;
|
||||||
|
|
||||||
|
import static java.util.stream.Collectors.toList;
|
||||||
|
|
||||||
import android.app.Dialog;
|
import android.app.Dialog;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
|
import android.net.ConnectivityManager;
|
||||||
|
import android.net.NetworkScoreManager;
|
||||||
import android.net.wifi.ScanResult;
|
import android.net.wifi.ScanResult;
|
||||||
import android.net.wifi.WifiConfiguration;
|
import android.net.wifi.WifiConfiguration;
|
||||||
|
import android.net.wifi.WifiManager;
|
||||||
import android.net.wifi.WifiManager.NetworkRequestMatchCallback;
|
import android.net.wifi.WifiManager.NetworkRequestMatchCallback;
|
||||||
import android.net.wifi.WifiManager.NetworkRequestUserSelectionCallback;
|
import android.net.wifi.WifiManager.NetworkRequestUserSelectionCallback;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.HandlerThread;
|
||||||
|
import android.os.Looper;
|
||||||
|
import android.os.Process;
|
||||||
|
import android.os.SimpleClock;
|
||||||
|
import android.os.SystemClock;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
@@ -42,11 +55,11 @@ import androidx.preference.internal.PreferenceImageView;
|
|||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settingslib.Utils;
|
import com.android.settingslib.Utils;
|
||||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
import com.android.wifitrackerlib.WifiEntry;
|
||||||
import com.android.settingslib.wifi.AccessPoint;
|
import com.android.wifitrackerlib.WifiPickerTracker;
|
||||||
import com.android.settingslib.wifi.WifiTracker;
|
|
||||||
import com.android.settingslib.wifi.WifiTrackerFactory;
|
|
||||||
|
|
||||||
|
import java.time.Clock;
|
||||||
|
import java.time.ZoneOffset;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -56,7 +69,9 @@ import java.util.List;
|
|||||||
* happens, {@link NetworkRequestErrorDialogFragment} will be called to display error message.
|
* happens, {@link NetworkRequestErrorDialogFragment} will be called to display error message.
|
||||||
*/
|
*/
|
||||||
public class NetworkRequestDialogFragment extends NetworkRequestDialogBaseFragment implements
|
public class NetworkRequestDialogFragment extends NetworkRequestDialogBaseFragment implements
|
||||||
DialogInterface.OnClickListener{
|
DialogInterface.OnClickListener, WifiPickerTracker.WifiPickerTrackerCallback {
|
||||||
|
|
||||||
|
private static final String TAG = "NetworkRequestDialogFragment";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Spec defines there should be 5 wifi ap on the list at most or just show all if {@code
|
* Spec defines there should be 5 wifi ap on the list at most or just show all if {@code
|
||||||
@@ -65,17 +80,51 @@ public class NetworkRequestDialogFragment extends NetworkRequestDialogBaseFragme
|
|||||||
private static final int MAX_NUMBER_LIST_ITEM = 5;
|
private static final int MAX_NUMBER_LIST_ITEM = 5;
|
||||||
private boolean mShowLimitedItem = true;
|
private boolean mShowLimitedItem = true;
|
||||||
|
|
||||||
private List<AccessPoint> mAccessPointList;
|
@VisibleForTesting List<WifiEntry> mFilteredWifiEntries = new ArrayList<>();
|
||||||
@VisibleForTesting
|
@VisibleForTesting List<ScanResult> mMatchedScanResults = new ArrayList<>();
|
||||||
FilterWifiTracker mFilterWifiTracker;
|
private WifiEntryAdapter mDialogAdapter;
|
||||||
private AccessPointAdapter mDialogAdapter;
|
|
||||||
private NetworkRequestUserSelectionCallback mUserSelectionCallback;
|
private NetworkRequestUserSelectionCallback mUserSelectionCallback;
|
||||||
|
|
||||||
|
@VisibleForTesting WifiPickerTracker mWifiPickerTracker;
|
||||||
|
// Worker thread used for WifiPickerTracker work.
|
||||||
|
private HandlerThread mWorkerThread;
|
||||||
|
// Max age of tracked WifiEntries.
|
||||||
|
private static final long MAX_SCAN_AGE_MILLIS = 15_000;
|
||||||
|
// Interval between initiating WifiPickerTracker scans.
|
||||||
|
private static final long SCAN_INTERVAL_MILLIS = 10_000;
|
||||||
|
|
||||||
public static NetworkRequestDialogFragment newInstance() {
|
public static NetworkRequestDialogFragment newInstance() {
|
||||||
NetworkRequestDialogFragment dialogFragment = new NetworkRequestDialogFragment();
|
NetworkRequestDialogFragment dialogFragment = new NetworkRequestDialogFragment();
|
||||||
return dialogFragment;
|
return dialogFragment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
|
mWorkerThread = new HandlerThread(
|
||||||
|
TAG + "{" + Integer.toHexString(System.identityHashCode(this)) + "}",
|
||||||
|
Process.THREAD_PRIORITY_BACKGROUND);
|
||||||
|
mWorkerThread.start();
|
||||||
|
final Clock elapsedRealtimeClock = new SimpleClock(ZoneOffset.UTC) {
|
||||||
|
@Override
|
||||||
|
public long millis() {
|
||||||
|
return SystemClock.elapsedRealtime();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
final Context context = getContext();
|
||||||
|
mWifiPickerTracker = new WifiPickerTracker(getSettingsLifecycle(), context,
|
||||||
|
context.getSystemService(WifiManager.class),
|
||||||
|
context.getSystemService(ConnectivityManager.class),
|
||||||
|
context.getSystemService(NetworkScoreManager.class),
|
||||||
|
new Handler(Looper.getMainLooper()),
|
||||||
|
mWorkerThread.getThreadHandler(),
|
||||||
|
elapsedRealtimeClock,
|
||||||
|
MAX_SCAN_AGE_MILLIS,
|
||||||
|
SCAN_INTERVAL_MILLIS,
|
||||||
|
this);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||||
final Context context = getContext();
|
final Context context = getContext();
|
||||||
@@ -94,8 +143,8 @@ public class NetworkRequestDialogFragment extends NetworkRequestDialogBaseFragme
|
|||||||
progressBar.setVisibility(View.VISIBLE);
|
progressBar.setVisibility(View.VISIBLE);
|
||||||
|
|
||||||
// Prepares adapter.
|
// Prepares adapter.
|
||||||
mDialogAdapter = new AccessPointAdapter(context,
|
mDialogAdapter = new WifiEntryAdapter(context,
|
||||||
R.layout.preference_access_point, getAccessPointList());
|
R.layout.preference_access_point, mFilteredWifiEntries);
|
||||||
|
|
||||||
final AlertDialog.Builder builder = new AlertDialog.Builder(context)
|
final AlertDialog.Builder builder = new AlertDialog.Builder(context)
|
||||||
.setCustomTitle(customTitle)
|
.setCustomTitle(customTitle)
|
||||||
@@ -107,9 +156,8 @@ public class NetworkRequestDialogFragment extends NetworkRequestDialogBaseFragme
|
|||||||
|
|
||||||
// Clicking list item is to connect wifi ap.
|
// Clicking list item is to connect wifi ap.
|
||||||
final AlertDialog dialog = builder.create();
|
final AlertDialog dialog = builder.create();
|
||||||
dialog.getListView()
|
dialog.getListView().setOnItemClickListener(
|
||||||
.setOnItemClickListener(
|
(parent, view, position, id) -> this.onClick(dialog, position));
|
||||||
(parent, view, position, id) -> this.onClick(dialog, position));
|
|
||||||
|
|
||||||
// Don't dismiss dialog when touching outside. User reports it is easy to touch outside.
|
// Don't dismiss dialog when touching outside. User reports it is easy to touch outside.
|
||||||
// This causes dialog to close.
|
// This causes dialog to close.
|
||||||
@@ -121,76 +169,49 @@ public class NetworkRequestDialogFragment extends NetworkRequestDialogBaseFragme
|
|||||||
neutralBtn.setVisibility(View.GONE);
|
neutralBtn.setVisibility(View.GONE);
|
||||||
neutralBtn.setOnClickListener(v -> {
|
neutralBtn.setOnClickListener(v -> {
|
||||||
mShowLimitedItem = false;
|
mShowLimitedItem = false;
|
||||||
renewAccessPointList(null /* scanResults */);
|
updateWifiEntries();
|
||||||
notifyAdapterRefresh();
|
updateUi();
|
||||||
neutralBtn.setVisibility(View.GONE);
|
neutralBtn.setVisibility(View.GONE);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
return dialog;
|
return dialog;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
|
||||||
List<AccessPoint> getAccessPointList() {
|
|
||||||
// Initials list for adapter, in case of display crashing.
|
|
||||||
if (mAccessPointList == null) {
|
|
||||||
mAccessPointList = new ArrayList<>();
|
|
||||||
}
|
|
||||||
return mAccessPointList;
|
|
||||||
}
|
|
||||||
|
|
||||||
private BaseAdapter getDialogAdapter() {
|
private BaseAdapter getDialogAdapter() {
|
||||||
return mDialogAdapter;
|
return mDialogAdapter;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
final List<AccessPoint> accessPointList = getAccessPointList();
|
if (mFilteredWifiEntries.size() == 0 || which >= mFilteredWifiEntries.size()) {
|
||||||
if (accessPointList.size() == 0) {
|
|
||||||
return; // Invalid values.
|
return; // Invalid values.
|
||||||
}
|
}
|
||||||
if (mUserSelectionCallback == null) {
|
if (mUserSelectionCallback == null) {
|
||||||
return; // Callback is missing or not ready.
|
return; // Callback is missing or not ready.
|
||||||
}
|
}
|
||||||
|
|
||||||
if (which < accessPointList.size()) {
|
final WifiEntry wifiEntry = mFilteredWifiEntries.get(which);
|
||||||
final AccessPoint selectedAccessPoint = accessPointList.get(which);
|
WifiConfiguration config = wifiEntry.getWifiConfiguration();
|
||||||
WifiConfiguration wifiConfig = selectedAccessPoint.getConfig();
|
if (config == null) {
|
||||||
if (wifiConfig == null) {
|
config = WifiUtils.getWifiConfig(wifiEntry, null /* scanResult */);
|
||||||
wifiConfig = WifiUtils.getWifiConfig(selectedAccessPoint, /* scanResult */
|
|
||||||
null, /* password */ null);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (wifiConfig != null) {
|
|
||||||
mUserSelectionCallback.select(wifiConfig);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
mUserSelectionCallback.select(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCancel(@NonNull DialogInterface dialog) {
|
public void onCancel(@NonNull DialogInterface dialog) {
|
||||||
super.onCancel(dialog);
|
super.onCancel(dialog);
|
||||||
|
|
||||||
if (mUserSelectionCallback != null) {
|
if (mUserSelectionCallback != null) {
|
||||||
mUserSelectionCallback.reject();
|
mUserSelectionCallback.reject();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPause() {
|
|
||||||
super.onPause();
|
|
||||||
|
|
||||||
if (mFilterWifiTracker != null) {
|
|
||||||
mFilterWifiTracker.onPause();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
super.onDestroy();
|
mWorkerThread.quit();
|
||||||
|
|
||||||
if (mFilterWifiTracker != null) {
|
super.onDestroy();
|
||||||
mFilterWifiTracker.onDestroy();
|
|
||||||
mFilterWifiTracker = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void showAllButton() {
|
private void showAllButton() {
|
||||||
@@ -217,22 +238,65 @@ public class NetworkRequestDialogFragment extends NetworkRequestDialogBaseFragme
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Called when the state of Wifi has changed. */
|
||||||
@Override
|
@Override
|
||||||
public void onResume() {
|
public void onWifiStateChanged() {
|
||||||
super.onResume();
|
if (mMatchedScanResults.size() == 0) {
|
||||||
|
return;
|
||||||
if (mFilterWifiTracker == null) {
|
|
||||||
mFilterWifiTracker = new FilterWifiTracker(getContext(), getSettingsLifecycle());
|
|
||||||
}
|
}
|
||||||
mFilterWifiTracker.onResume();
|
updateWifiEntries();
|
||||||
|
updateUi();
|
||||||
}
|
}
|
||||||
|
|
||||||
private class AccessPointAdapter extends ArrayAdapter<AccessPoint> {
|
/**
|
||||||
|
* Update the results when data changes
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void onWifiEntriesChanged() {
|
||||||
|
if (mMatchedScanResults.size() == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
updateWifiEntries();
|
||||||
|
updateUi();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onNumSavedSubscriptionsChanged() {
|
||||||
|
// Do nothing.
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onNumSavedNetworksChanged() {
|
||||||
|
// Do nothing.
|
||||||
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
void updateWifiEntries() {
|
||||||
|
final List<WifiEntry> wifiEntries = new ArrayList<>();
|
||||||
|
if (mWifiPickerTracker.getConnectedWifiEntry() != null) {
|
||||||
|
wifiEntries.add(mWifiPickerTracker.getConnectedWifiEntry());
|
||||||
|
}
|
||||||
|
wifiEntries.addAll(mWifiPickerTracker.getWifiEntries());
|
||||||
|
|
||||||
|
mFilteredWifiEntries.clear();
|
||||||
|
mFilteredWifiEntries.addAll(wifiEntries.stream().filter(entry -> {
|
||||||
|
for (ScanResult matchedScanResult : mMatchedScanResults) {
|
||||||
|
if (TextUtils.equals(entry.getSsid(), matchedScanResult.SSID)
|
||||||
|
&& entry.getSecurity() == getWifiEntrySecurity(matchedScanResult)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}).limit(mShowLimitedItem ? MAX_NUMBER_LIST_ITEM : Long.MAX_VALUE)
|
||||||
|
.collect(toList()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private class WifiEntryAdapter extends ArrayAdapter<WifiEntry> {
|
||||||
|
|
||||||
private final int mResourceId;
|
private final int mResourceId;
|
||||||
private final LayoutInflater mInflater;
|
private final LayoutInflater mInflater;
|
||||||
|
|
||||||
public AccessPointAdapter(Context context, int resourceId, List<AccessPoint> objects) {
|
WifiEntryAdapter(Context context, int resourceId, List<WifiEntry> objects) {
|
||||||
super(context, resourceId, objects);
|
super(context, resourceId, objects);
|
||||||
mResourceId = resourceId;
|
mResourceId = resourceId;
|
||||||
mInflater = LayoutInflater.from(context);
|
mInflater = LayoutInflater.from(context);
|
||||||
@@ -248,18 +312,18 @@ public class NetworkRequestDialogFragment extends NetworkRequestDialogBaseFragme
|
|||||||
divider.setVisibility(View.GONE);
|
divider.setVisibility(View.GONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
final AccessPoint accessPoint = getItem(position);
|
final WifiEntry wifiEntry = getItem(position);
|
||||||
|
|
||||||
final TextView titleView = view.findViewById(android.R.id.title);
|
final TextView titleView = view.findViewById(android.R.id.title);
|
||||||
if (titleView != null) {
|
if (titleView != null) {
|
||||||
// Shows whole SSID for better UX.
|
// Shows whole SSID for better UX.
|
||||||
titleView.setSingleLine(false);
|
titleView.setSingleLine(false);
|
||||||
titleView.setText(accessPoint.getTitle());
|
titleView.setText(wifiEntry.getTitle());
|
||||||
}
|
}
|
||||||
|
|
||||||
final TextView summary = view.findViewById(android.R.id.summary);
|
final TextView summary = view.findViewById(android.R.id.summary);
|
||||||
if (summary != null) {
|
if (summary != null) {
|
||||||
final String summaryString = accessPoint.getSettingsSummary();
|
final String summaryString = wifiEntry.getSummary();
|
||||||
if (TextUtils.isEmpty(summaryString)) {
|
if (TextUtils.isEmpty(summaryString)) {
|
||||||
summary.setVisibility(View.GONE);
|
summary.setVisibility(View.GONE);
|
||||||
} else {
|
} else {
|
||||||
@@ -269,7 +333,7 @@ public class NetworkRequestDialogFragment extends NetworkRequestDialogBaseFragme
|
|||||||
}
|
}
|
||||||
|
|
||||||
final PreferenceImageView imageView = view.findViewById(android.R.id.icon);
|
final PreferenceImageView imageView = view.findViewById(android.R.id.icon);
|
||||||
final int level = accessPoint.getLevel();
|
final int level = wifiEntry.getLevel();
|
||||||
if (imageView != null) {
|
if (imageView != null) {
|
||||||
final Drawable drawable = getContext().getDrawable(
|
final Drawable drawable = getContext().getDrawable(
|
||||||
Utils.getWifiIconResource(level));
|
Utils.getWifiIconResource(level));
|
||||||
@@ -290,137 +354,23 @@ public class NetworkRequestDialogFragment extends NetworkRequestDialogBaseFragme
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onMatch(List<ScanResult> scanResults) {
|
public void onMatch(List<ScanResult> scanResults) {
|
||||||
// Shouldn't need to renew cached list, since input result is empty.
|
mMatchedScanResults = scanResults;
|
||||||
if (scanResults != null && scanResults.size() > 0) {
|
updateWifiEntries();
|
||||||
renewAccessPointList(scanResults);
|
updateUi();
|
||||||
|
|
||||||
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.
|
|
||||||
private void renewAccessPointList(List<ScanResult> scanResults) {
|
|
||||||
if (mFilterWifiTracker == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO(b/119846365): Checks if we could escalate the converting effort.
|
|
||||||
// Updates keys of scanResults into FilterWifiTracker for updating matched AccessPoints.
|
|
||||||
if (scanResults != null) {
|
|
||||||
mFilterWifiTracker.updateKeys(scanResults);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Re-gets matched AccessPoints from WifiTracker.
|
|
||||||
final List<AccessPoint> list = getAccessPointList();
|
|
||||||
list.clear();
|
|
||||||
list.addAll(mFilterWifiTracker.getAccessPoints());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
void notifyAdapterRefresh() {
|
void updateUi() {
|
||||||
|
// Update related UI buttons
|
||||||
|
if (mShowLimitedItem && mFilteredWifiEntries.size() >= MAX_NUMBER_LIST_ITEM) {
|
||||||
|
showAllButton();
|
||||||
|
}
|
||||||
|
if (mFilteredWifiEntries.size() > 0) {
|
||||||
|
hideProgressIcon();
|
||||||
|
}
|
||||||
|
|
||||||
if (getDialogAdapter() != null) {
|
if (getDialogAdapter() != null) {
|
||||||
getDialogAdapter().notifyDataSetChanged();
|
getDialogAdapter().notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
|
||||||
final class FilterWifiTracker {
|
|
||||||
private final List<String> mAccessPointKeys;
|
|
||||||
private final WifiTracker mWifiTracker;
|
|
||||||
private final Context mContext;
|
|
||||||
|
|
||||||
public FilterWifiTracker(Context context, Lifecycle lifecycle) {
|
|
||||||
mWifiTracker = WifiTrackerFactory.create(context, mWifiListener,
|
|
||||||
lifecycle, /* includeSaved */ true, /* includeScans */ true);
|
|
||||||
mAccessPointKeys = new ArrayList<>();
|
|
||||||
mContext = context;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Updates key list from input. {@code onMatch()} may be called in multi-times according
|
|
||||||
* wifi scanning result, so needs patchwork here.
|
|
||||||
*/
|
|
||||||
public void updateKeys(List<ScanResult> scanResults) {
|
|
||||||
for (ScanResult scanResult : scanResults) {
|
|
||||||
final String key = AccessPoint.getKey(mContext, scanResult);
|
|
||||||
if (!mAccessPointKeys.contains(key)) {
|
|
||||||
mAccessPointKeys.add(key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns only AccessPoints whose key is in {@code mAccessPointKeys}.
|
|
||||||
*
|
|
||||||
* @return List of matched AccessPoints.
|
|
||||||
*/
|
|
||||||
public List<AccessPoint> getAccessPoints() {
|
|
||||||
final List<AccessPoint> allAccessPoints = mWifiTracker.getAccessPoints();
|
|
||||||
final List<AccessPoint> result = new ArrayList<>();
|
|
||||||
|
|
||||||
// The order should be kept, because order means wifi score (sorting in WifiTracker).
|
|
||||||
int count = 0;
|
|
||||||
for (AccessPoint accessPoint : allAccessPoints) {
|
|
||||||
final String key = accessPoint.getKey();
|
|
||||||
if (mAccessPointKeys.contains(key)) {
|
|
||||||
result.add(accessPoint);
|
|
||||||
|
|
||||||
count++;
|
|
||||||
// Limits how many count of items could show.
|
|
||||||
if (mShowLimitedItem && count >= MAX_NUMBER_LIST_ITEM) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update related UI buttons
|
|
||||||
if (mShowLimitedItem && (count >= MAX_NUMBER_LIST_ITEM)) {
|
|
||||||
showAllButton();
|
|
||||||
}
|
|
||||||
if (count > 0) {
|
|
||||||
hideProgressIcon();
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@VisibleForTesting
|
|
||||||
WifiTracker.WifiListener mWifiListener = new WifiTracker.WifiListener() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onWifiStateChanged(int state) {
|
|
||||||
notifyAdapterRefresh();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onConnectedChanged() {
|
|
||||||
notifyAdapterRefresh();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onAccessPointsChanged() {
|
|
||||||
renewAccessPointList(null /* scanResults */);
|
|
||||||
notifyAdapterRefresh();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public void onDestroy() {
|
|
||||||
if (mWifiTracker != null) {
|
|
||||||
mWifiTracker.onDestroy();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void onResume() {
|
|
||||||
if (mWifiTracker != null) {
|
|
||||||
mWifiTracker.onStart();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void onPause() {
|
|
||||||
if (mWifiTracker != null) {
|
|
||||||
mWifiTracker.onStop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -31,6 +31,7 @@ import android.text.TextUtils;
|
|||||||
|
|
||||||
import com.android.settings.Utils;
|
import com.android.settings.Utils;
|
||||||
import com.android.settingslib.wifi.AccessPoint;
|
import com.android.settingslib.wifi.AccessPoint;
|
||||||
|
import com.android.wifitrackerlib.WifiEntry;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
@@ -132,136 +133,102 @@ public class WifiUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides a simple way to generate a new {@link WifiConfiguration} obj from
|
* Provides a simple way to generate a new {@link WifiConfiguration} obj from
|
||||||
* {@link ScanResult} or {@link AccessPoint}. Either {@code accessPoint} or {@code scanResult
|
* {@link ScanResult} or {@link WifiEntry}. Either {@code wifiEntry} or {@code scanResult
|
||||||
* } input should be not null for retrieving information, otherwise will throw
|
* } input should be not null for retrieving information, otherwise will throw
|
||||||
* IllegalArgumentException.
|
* IllegalArgumentException.
|
||||||
* This method prefers to take {@link AccessPoint} input in priority. Therefore this method
|
* This method prefers to take {@link WifiEntry} input in priority. Therefore this method
|
||||||
* will take {@link AccessPoint} input as preferred data extraction source when you input
|
* will take {@link WifiEntry} input as preferred data extraction source when you input
|
||||||
* both {@link AccessPoint} and {@link ScanResult}, and ignore {@link ScanResult} input.
|
* both {@link WifiEntry} and {@link ScanResult}, and ignore {@link ScanResult} input.
|
||||||
*
|
*
|
||||||
* Duplicated and simplified method from {@link WifiConfigController#getConfig()}.
|
* Duplicated and simplified method from {@link WifiConfigController#getConfig()}.
|
||||||
* TODO(b/120827021): Should be removed if the there is have a common one in shared place (e.g.
|
* TODO(b/120827021): Should be removed if the there is have a common one in shared place (e.g.
|
||||||
* SettingsLib).
|
* SettingsLib).
|
||||||
*
|
*
|
||||||
* @param accessPoint Input data for retrieving WifiConfiguration.
|
* @param wifiEntry Input data for retrieving WifiConfiguration.
|
||||||
* @param scanResult Input data for retrieving WifiConfiguration.
|
* @param scanResult Input data for retrieving WifiConfiguration.
|
||||||
* @return WifiConfiguration obj based on input.
|
* @return WifiConfiguration obj based on input.
|
||||||
*/
|
*/
|
||||||
public static WifiConfiguration getWifiConfig(AccessPoint accessPoint, ScanResult scanResult,
|
public static WifiConfiguration getWifiConfig(WifiEntry wifiEntry, ScanResult scanResult) {
|
||||||
String password) {
|
if (wifiEntry == null && scanResult == null) {
|
||||||
if (accessPoint == null && scanResult == null) {
|
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
"At least one of AccessPoint and ScanResult input is required.");
|
"At least one of WifiEntry and ScanResult input is required.");
|
||||||
}
|
}
|
||||||
|
|
||||||
final WifiConfiguration config = new WifiConfiguration();
|
final WifiConfiguration config = new WifiConfiguration();
|
||||||
final int security;
|
final int security;
|
||||||
|
|
||||||
if (accessPoint == null) {
|
if (wifiEntry == null) {
|
||||||
config.SSID = AccessPoint.convertToQuotedString(scanResult.SSID);
|
config.SSID = AccessPoint.convertToQuotedString(scanResult.SSID);
|
||||||
security = getAccessPointSecurity(scanResult);
|
security = getWifiEntrySecurity(scanResult);
|
||||||
} else {
|
} else {
|
||||||
if (!accessPoint.isSaved()) {
|
if (wifiEntry.getWifiConfiguration() == null) {
|
||||||
config.SSID = AccessPoint.convertToQuotedString(
|
config.SSID = AccessPoint.convertToQuotedString(wifiEntry.getSsid());
|
||||||
accessPoint.getSsidStr());
|
|
||||||
} else {
|
} else {
|
||||||
config.networkId = accessPoint.getConfig().networkId;
|
config.networkId = wifiEntry.getWifiConfiguration().networkId;
|
||||||
config.hiddenSSID = accessPoint.getConfig().hiddenSSID;
|
config.hiddenSSID = wifiEntry.getWifiConfiguration().hiddenSSID;
|
||||||
}
|
}
|
||||||
security = accessPoint.getSecurity();
|
security = wifiEntry.getSecurity();
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (security) {
|
switch (security) {
|
||||||
case AccessPoint.SECURITY_NONE:
|
case WifiEntry.SECURITY_NONE:
|
||||||
config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_OPEN);
|
config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_OPEN);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AccessPoint.SECURITY_WEP:
|
case WifiEntry.SECURITY_WEP:
|
||||||
config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_WEP);
|
config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_WEP);
|
||||||
if (!TextUtils.isEmpty(password)) {
|
|
||||||
int length = password.length();
|
|
||||||
// WEP-40, WEP-104, and 256-bit WEP (WEP-232?)
|
|
||||||
if ((length == 10 || length == 26 || length == 58)
|
|
||||||
&& password.matches("[0-9A-Fa-f]*")) {
|
|
||||||
config.wepKeys[0] = password;
|
|
||||||
} else {
|
|
||||||
config.wepKeys[0] = '"' + password + '"';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AccessPoint.SECURITY_PSK:
|
case WifiEntry.SECURITY_PSK:
|
||||||
config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_PSK);
|
config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_PSK);
|
||||||
if (!TextUtils.isEmpty(password)) {
|
|
||||||
if (password.matches("[0-9A-Fa-f]{64}")) {
|
|
||||||
config.preSharedKey = password;
|
|
||||||
} else {
|
|
||||||
config.preSharedKey = '"' + password + '"';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AccessPoint.SECURITY_EAP:
|
case WifiEntry.SECURITY_EAP_SUITE_B:
|
||||||
case AccessPoint.SECURITY_EAP_SUITE_B:
|
config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_EAP_SUITE_B);
|
||||||
if (security == AccessPoint.SECURITY_EAP_SUITE_B) {
|
|
||||||
// allowedSuiteBCiphers will be set according to certificate type
|
|
||||||
config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_EAP_SUITE_B);
|
|
||||||
} else {
|
|
||||||
config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_EAP);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!TextUtils.isEmpty(password)) {
|
|
||||||
config.enterpriseConfig.setPassword(password);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case AccessPoint.SECURITY_SAE:
|
|
||||||
|
case WifiEntry.SECURITY_EAP:
|
||||||
|
config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_EAP);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case WifiEntry.SECURITY_SAE:
|
||||||
config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_SAE);
|
config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_SAE);
|
||||||
if (!TextUtils.isEmpty(password)) {
|
|
||||||
config.preSharedKey = '"' + password + '"';
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AccessPoint.SECURITY_OWE:
|
case WifiEntry.SECURITY_OWE:
|
||||||
config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_OWE);
|
config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_OWE);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets security value from ScanResult.
|
* Gets security value from ScanResult.
|
||||||
*
|
*
|
||||||
* Duplicated method from {@link AccessPoint#getSecurity(ScanResult)}.
|
|
||||||
* TODO(b/120827021): Should be removed if the there is have a common one in shared place (e.g.
|
|
||||||
* SettingsLib).
|
|
||||||
*
|
|
||||||
* @param result ScanResult
|
* @param result ScanResult
|
||||||
* @return Related security value based on {@link AccessPoint}.
|
* @return Related security value based on {@link WifiEntry}.
|
||||||
*/
|
*/
|
||||||
public static int getAccessPointSecurity(ScanResult result) {
|
public static int getWifiEntrySecurity(ScanResult result) {
|
||||||
if (result.capabilities.contains("WEP")) {
|
if (result.capabilities.contains("WEP")) {
|
||||||
return AccessPoint.SECURITY_WEP;
|
return WifiEntry.SECURITY_WEP;
|
||||||
} else if (result.capabilities.contains("SAE")) {
|
} else if (result.capabilities.contains("SAE")) {
|
||||||
return AccessPoint.SECURITY_SAE;
|
return WifiEntry.SECURITY_SAE;
|
||||||
} else if (result.capabilities.contains("PSK")) {
|
} else if (result.capabilities.contains("PSK")) {
|
||||||
return AccessPoint.SECURITY_PSK;
|
return WifiEntry.SECURITY_PSK;
|
||||||
} else if (result.capabilities.contains("EAP_SUITE_B_192")) {
|
} else if (result.capabilities.contains("EAP_SUITE_B_192")) {
|
||||||
return AccessPoint.SECURITY_EAP_SUITE_B;
|
return WifiEntry.SECURITY_EAP_SUITE_B;
|
||||||
} else if (result.capabilities.contains("EAP")) {
|
} else if (result.capabilities.contains("EAP")) {
|
||||||
return AccessPoint.SECURITY_EAP;
|
return WifiEntry.SECURITY_EAP;
|
||||||
} else if (result.capabilities.contains("OWE")) {
|
} else if (result.capabilities.contains("OWE")) {
|
||||||
return AccessPoint.SECURITY_OWE;
|
return WifiEntry.SECURITY_OWE;
|
||||||
}
|
}
|
||||||
|
|
||||||
return AccessPoint.SECURITY_NONE;
|
return WifiEntry.SECURITY_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static final int CONNECT_TYPE_OTHERS = 0;
|
public static final int CONNECT_TYPE_OTHERS = 0;
|
||||||
public static final int CONNECT_TYPE_OPEN_NETWORK = 1;
|
public static final int CONNECT_TYPE_OPEN_NETWORK = 1;
|
||||||
public static final int CONNECT_TYPE_SAVED_NETWORK = 2;
|
public static final int CONNECT_TYPE_SAVED_NETWORK = 2;
|
||||||
|
@@ -18,19 +18,18 @@ package com.android.settings.wifi;
|
|||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.inOrder;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.spy;
|
import static org.mockito.Mockito.spy;
|
||||||
import static org.mockito.Mockito.times;
|
import static org.mockito.Mockito.times;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.net.wifi.ScanResult;
|
import android.net.wifi.ScanResult;
|
||||||
import android.net.wifi.WifiManager;
|
import android.net.wifi.WifiConfiguration;
|
||||||
import android.net.wifi.WifiManager.NetworkRequestUserSelectionCallback;
|
import android.net.wifi.WifiManager.NetworkRequestUserSelectionCallback;
|
||||||
import android.os.Bundle;
|
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
@@ -40,13 +39,13 @@ import androidx.fragment.app.FragmentActivity;
|
|||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
|
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
|
||||||
import com.android.settingslib.wifi.AccessPoint;
|
import com.android.wifitrackerlib.WifiEntry;
|
||||||
import com.android.settingslib.wifi.WifiTracker;
|
import com.android.wifitrackerlib.WifiPickerTracker;
|
||||||
import com.android.settingslib.wifi.WifiTrackerFactory;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.InOrder;
|
||||||
import org.robolectric.Robolectric;
|
import org.robolectric.Robolectric;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
@@ -65,8 +64,6 @@ public class NetworkRequestDialogFragmentTest {
|
|||||||
|
|
||||||
private FragmentActivity mActivity;
|
private FragmentActivity mActivity;
|
||||||
private NetworkRequestDialogFragment networkRequestDialogFragment;
|
private NetworkRequestDialogFragment networkRequestDialogFragment;
|
||||||
private Context mContext;
|
|
||||||
private WifiTracker mWifiTracker;
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
@@ -74,10 +71,7 @@ public class NetworkRequestDialogFragmentTest {
|
|||||||
new Intent().putExtra(NetworkRequestDialogFragment.EXTRA_APP_NAME,
|
new Intent().putExtra(NetworkRequestDialogFragment.EXTRA_APP_NAME,
|
||||||
TEST_APP_NAME)).setup().get();
|
TEST_APP_NAME)).setup().get();
|
||||||
networkRequestDialogFragment = spy(NetworkRequestDialogFragment.newInstance());
|
networkRequestDialogFragment = spy(NetworkRequestDialogFragment.newInstance());
|
||||||
mContext = spy(RuntimeEnvironment.application);
|
networkRequestDialogFragment.mWifiPickerTracker = mock(WifiPickerTracker.class);
|
||||||
|
|
||||||
mWifiTracker = mock(WifiTracker.class);
|
|
||||||
WifiTrackerFactory.setTestingWifiTracker(mWifiTracker);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -93,7 +87,7 @@ public class NetworkRequestDialogFragmentTest {
|
|||||||
networkRequestDialogFragment.show(mActivity.getSupportFragmentManager(), /* tag */ null);
|
networkRequestDialogFragment.show(mActivity.getSupportFragmentManager(), /* tag */ null);
|
||||||
final AlertDialog alertDialog = ShadowAlertDialogCompat.getLatestAlertDialog();
|
final AlertDialog alertDialog = ShadowAlertDialogCompat.getLatestAlertDialog();
|
||||||
|
|
||||||
final String targetTitle = mContext.getString(
|
final String targetTitle = RuntimeEnvironment.application.getString(
|
||||||
R.string.network_connection_request_dialog_title, TEST_APP_NAME);
|
R.string.network_connection_request_dialog_title, TEST_APP_NAME);
|
||||||
final TextView view = alertDialog.findViewById(R.id.network_request_title_text);
|
final TextView view = alertDialog.findViewById(R.id.network_request_title_text);
|
||||||
assertThat(view.getText()).isEqualTo(targetTitle);
|
assertThat(view.getText()).isEqualTo(targetTitle);
|
||||||
@@ -113,124 +107,95 @@ public class NetworkRequestDialogFragmentTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onUserSelectionCallbackRegistration_onClick_shouldCallSelect() {
|
public void onClick_validSelection_shouldCallSelect() {
|
||||||
final int indexClickItem = 3;
|
final int indexClickItem = 3;
|
||||||
List<AccessPoint> accessPointList = createAccessPointList();
|
final List<WifiEntry> wifiEntryList = createWifiEntryList();
|
||||||
AccessPoint clickedAccessPoint = accessPointList.get(indexClickItem);
|
final WifiEntry clickedWifiEntry = wifiEntryList.get(indexClickItem);
|
||||||
clickedAccessPoint.generateOpenNetworkConfig();
|
final WifiConfiguration wifiConfig = new WifiConfiguration();
|
||||||
when(networkRequestDialogFragment.getAccessPointList()).thenReturn(accessPointList);
|
when(clickedWifiEntry.getWifiConfiguration()).thenReturn(wifiConfig);
|
||||||
|
networkRequestDialogFragment.mFilteredWifiEntries = wifiEntryList;
|
||||||
NetworkRequestUserSelectionCallback selectionCallback = mock(
|
final NetworkRequestUserSelectionCallback selectionCallback = mock(
|
||||||
NetworkRequestUserSelectionCallback.class);
|
NetworkRequestUserSelectionCallback.class);
|
||||||
AlertDialog dialog = mock(AlertDialog.class);
|
final AlertDialog dialog = mock(AlertDialog.class);
|
||||||
networkRequestDialogFragment.onUserSelectionCallbackRegistration(selectionCallback);
|
networkRequestDialogFragment.onUserSelectionCallbackRegistration(selectionCallback);
|
||||||
|
|
||||||
// Act.
|
|
||||||
networkRequestDialogFragment.onClick(dialog, indexClickItem);
|
networkRequestDialogFragment.onClick(dialog, indexClickItem);
|
||||||
|
|
||||||
// Check.
|
verify(selectionCallback, times(1)).select(wifiConfig);
|
||||||
verify(selectionCallback, times(1)).select(clickedAccessPoint.getConfig());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onMatch_shouldUpdatedList() {
|
public void onMatch_shouldUpdateWifiEntries() {
|
||||||
when(networkRequestDialogFragment.getContext()).thenReturn(mContext);
|
final InOrder inOrder = inOrder(networkRequestDialogFragment);
|
||||||
Context applicationContext = spy(RuntimeEnvironment.application.getApplicationContext());
|
|
||||||
when(mContext.getApplicationContext()).thenReturn(applicationContext);
|
|
||||||
WifiManager wifiManager = mock(WifiManager.class);
|
|
||||||
when(applicationContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(wifiManager);
|
|
||||||
networkRequestDialogFragment.onResume();
|
|
||||||
|
|
||||||
List<AccessPoint> accessPointList = createAccessPointList();
|
networkRequestDialogFragment.onMatch(new ArrayList<ScanResult>());
|
||||||
when(mWifiTracker.getAccessPoints()).thenReturn(accessPointList);
|
|
||||||
|
|
||||||
final String ssidAp1 = "Test AP 1";
|
inOrder.verify(networkRequestDialogFragment).updateWifiEntries();
|
||||||
final String ssidAp2 = "Test AP 2";
|
inOrder.verify(networkRequestDialogFragment).updateUi();
|
||||||
List<ScanResult> scanResults = new ArrayList<>();
|
|
||||||
ScanResult scanResult = mock(ScanResult.class);
|
|
||||||
scanResult.SSID = ssidAp1;
|
|
||||||
scanResult.capabilities = "WEP";
|
|
||||||
scanResults.add(scanResult);
|
|
||||||
scanResult = mock(ScanResult.class);
|
|
||||||
scanResult.SSID = ssidAp2;
|
|
||||||
scanResult.capabilities = "WEP";
|
|
||||||
scanResults.add(scanResult);
|
|
||||||
|
|
||||||
// Act.
|
|
||||||
networkRequestDialogFragment.onMatch(scanResults);
|
|
||||||
|
|
||||||
// Check.
|
|
||||||
List<AccessPoint> returnList = networkRequestDialogFragment.getAccessPointList();
|
|
||||||
assertThat(returnList).isNotEmpty();
|
|
||||||
assertThat(returnList.size()).isEqualTo(2);
|
|
||||||
assertThat(returnList.get(0).getSsid()).isEqualTo(ssidAp1);
|
|
||||||
assertThat(returnList.get(1).getSsid()).isEqualTo(ssidAp2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onAccessPointsChanged_shouldUpdatedList() {
|
public void onWifiStateChanged_nonEmptyMatchedScanResults_shouldUpdateWifiEntries() {
|
||||||
when(networkRequestDialogFragment.getContext()).thenReturn(mContext);
|
final InOrder inOrder = inOrder(networkRequestDialogFragment);
|
||||||
Context applicationContext = spy(RuntimeEnvironment.application.getApplicationContext());
|
|
||||||
when(mContext.getApplicationContext()).thenReturn(applicationContext);
|
|
||||||
WifiManager wifiManager = mock(WifiManager.class);
|
|
||||||
when(applicationContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(wifiManager);
|
|
||||||
networkRequestDialogFragment.onResume();
|
|
||||||
|
|
||||||
List<AccessPoint> accessPointList = new ArrayList<>();
|
final List<ScanResult> scanResults = new ArrayList<>();
|
||||||
when(mWifiTracker.getAccessPoints()).thenReturn(accessPointList);
|
networkRequestDialogFragment.mMatchedScanResults = scanResults;
|
||||||
|
|
||||||
final String ssidAp1 = "Test AP 1";
|
|
||||||
List<ScanResult> scanResults = new ArrayList<>();
|
|
||||||
ScanResult scanResult = mock(ScanResult.class);
|
ScanResult scanResult = mock(ScanResult.class);
|
||||||
scanResult.SSID = ssidAp1;
|
networkRequestDialogFragment.mMatchedScanResults.add(scanResult);
|
||||||
scanResult.capabilities = "WEP";
|
|
||||||
scanResults.add(scanResult);
|
|
||||||
|
|
||||||
// Act.
|
|
||||||
networkRequestDialogFragment.onMatch(scanResults);
|
networkRequestDialogFragment.onMatch(scanResults);
|
||||||
|
|
||||||
accessPointList = createAccessPointList();
|
inOrder.verify(networkRequestDialogFragment).updateWifiEntries();
|
||||||
when(mWifiTracker.getAccessPoints()).thenReturn(accessPointList);
|
inOrder.verify(networkRequestDialogFragment).updateUi();
|
||||||
|
|
||||||
// Act.
|
|
||||||
networkRequestDialogFragment.mFilterWifiTracker.mWifiListener.onAccessPointsChanged();
|
|
||||||
|
|
||||||
// Check.
|
|
||||||
List<AccessPoint> returnList = networkRequestDialogFragment.getAccessPointList();
|
|
||||||
assertThat(returnList).isNotEmpty();
|
|
||||||
assertThat(returnList.size()).isEqualTo(1);
|
|
||||||
assertThat(returnList.get(0).getSsid()).isEqualTo(ssidAp1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<AccessPoint> createAccessPointList() {
|
@Test
|
||||||
List<AccessPoint> accessPointList = spy(new ArrayList<>());
|
public void onWifiEntriesChanged_nonEmptyMatchedScanResults_shouldUpdateWifiEntries() {
|
||||||
Bundle bundle = new Bundle();
|
final InOrder inOrder = inOrder(networkRequestDialogFragment);
|
||||||
|
|
||||||
bundle.putString(KEY_SSID, "Test AP 1");
|
final List<ScanResult> scanResults = new ArrayList<>();
|
||||||
bundle.putInt(KEY_SECURITY, 1 /* WEP */);
|
networkRequestDialogFragment.mMatchedScanResults = scanResults;
|
||||||
accessPointList.add(new AccessPoint(mContext, bundle));
|
ScanResult scanResult = mock(ScanResult.class);
|
||||||
|
networkRequestDialogFragment.mMatchedScanResults.add(scanResult);
|
||||||
|
networkRequestDialogFragment.onMatch(scanResults);
|
||||||
|
|
||||||
bundle.putString(KEY_SSID, "Test AP 2");
|
inOrder.verify(networkRequestDialogFragment).updateWifiEntries();
|
||||||
bundle.putInt(KEY_SECURITY, 1 /* WEP */);
|
inOrder.verify(networkRequestDialogFragment).updateUi();
|
||||||
accessPointList.add(new AccessPoint(mContext, bundle));
|
}
|
||||||
|
|
||||||
bundle.putString(KEY_SSID, "Test AP 3");
|
private List<WifiEntry> createWifiEntryList() {
|
||||||
bundle.putInt(KEY_SECURITY, 1 /* WEP */);
|
List<WifiEntry> wifiEntryList = spy(new ArrayList<>());
|
||||||
accessPointList.add(new AccessPoint(mContext, bundle));
|
|
||||||
|
|
||||||
bundle.putString(KEY_SSID, "Test AP 4");
|
final WifiEntry wifiEntry1 = mock(WifiEntry.class);
|
||||||
bundle.putInt(KEY_SECURITY, 0 /* NONE */);
|
when(wifiEntry1.getSsid()).thenReturn("Test AP 1");
|
||||||
accessPointList.add(new AccessPoint(mContext, bundle));
|
when(wifiEntry1.getSecurity()).thenReturn(WifiEntry.SECURITY_WEP);
|
||||||
|
wifiEntryList.add(wifiEntry1);
|
||||||
|
|
||||||
bundle.putString(KEY_SSID, "Test AP 5");
|
final WifiEntry wifiEntry2 = mock(WifiEntry.class);
|
||||||
bundle.putInt(KEY_SECURITY, 1 /* WEP */);
|
when(wifiEntry2.getSsid()).thenReturn("Test AP 2");
|
||||||
accessPointList.add(new AccessPoint(mContext, bundle));
|
when(wifiEntry2.getSecurity()).thenReturn(WifiEntry.SECURITY_WEP);
|
||||||
|
wifiEntryList.add(wifiEntry2);
|
||||||
|
|
||||||
bundle.putString(KEY_SSID, "Test AP 6");
|
final WifiEntry wifiEntry3 = mock(WifiEntry.class);
|
||||||
bundle.putInt(KEY_SECURITY, 1 /* WEP */);
|
when(wifiEntry3.getSsid()).thenReturn("Test AP 3");
|
||||||
accessPointList.add(new AccessPoint(mContext, bundle));
|
when(wifiEntry3.getSecurity()).thenReturn(WifiEntry.SECURITY_WEP);
|
||||||
|
wifiEntryList.add(wifiEntry3);
|
||||||
|
|
||||||
return accessPointList;
|
final WifiEntry wifiEntry4 = mock(WifiEntry.class);
|
||||||
|
when(wifiEntry4.getSsid()).thenReturn("Test AP 4");
|
||||||
|
when(wifiEntry4.getSecurity()).thenReturn(WifiEntry.SECURITY_NONE);
|
||||||
|
wifiEntryList.add(wifiEntry4);
|
||||||
|
|
||||||
|
final WifiEntry wifiEntry5 = mock(WifiEntry.class);
|
||||||
|
when(wifiEntry5.getSsid()).thenReturn("Test AP 5");
|
||||||
|
when(wifiEntry5.getSecurity()).thenReturn(WifiEntry.SECURITY_WEP);
|
||||||
|
wifiEntryList.add(wifiEntry5);
|
||||||
|
|
||||||
|
final WifiEntry wifiEntry6 = mock(WifiEntry.class);
|
||||||
|
when(wifiEntry6.getSsid()).thenReturn("Test AP 6");
|
||||||
|
when(wifiEntry6.getSecurity()).thenReturn(WifiEntry.SECURITY_WEP);
|
||||||
|
wifiEntryList.add(wifiEntry6);
|
||||||
|
|
||||||
|
return wifiEntryList;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -247,8 +212,10 @@ public class NetworkRequestDialogFragmentTest {
|
|||||||
public void onMatchManyResult_showNeutralButton() {
|
public void onMatchManyResult_showNeutralButton() {
|
||||||
networkRequestDialogFragment.show(mActivity.getSupportFragmentManager(), /* tag */ null);
|
networkRequestDialogFragment.show(mActivity.getSupportFragmentManager(), /* tag */ null);
|
||||||
final AlertDialog alertDialog = ShadowAlertDialogCompat.getLatestAlertDialog();
|
final AlertDialog alertDialog = ShadowAlertDialogCompat.getLatestAlertDialog();
|
||||||
List<AccessPoint> accessPointList = createAccessPointList();
|
List<WifiEntry> wifiEntryList = createWifiEntryList();
|
||||||
when(mWifiTracker.getAccessPoints()).thenReturn(accessPointList);
|
final WifiPickerTracker wifiPickerTracker = mock(WifiPickerTracker.class);
|
||||||
|
when(wifiPickerTracker.getWifiEntries()).thenReturn(wifiEntryList);
|
||||||
|
networkRequestDialogFragment.mWifiPickerTracker = wifiPickerTracker;
|
||||||
|
|
||||||
final String ssidAp = "Test AP ";
|
final String ssidAp = "Test AP ";
|
||||||
final List<ScanResult> scanResults = new ArrayList<>();
|
final List<ScanResult> scanResults = new ArrayList<>();
|
||||||
|
@@ -18,20 +18,18 @@ package com.android.settings.wifi;
|
|||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import android.net.wifi.WifiConfiguration;
|
||||||
|
|
||||||
|
import com.android.settingslib.wifi.AccessPoint;
|
||||||
|
import com.android.wifitrackerlib.WifiEntry;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
|
|
||||||
import static org.mockito.Mockito.spy;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.net.wifi.WifiConfiguration;
|
|
||||||
import android.os.Bundle;
|
|
||||||
|
|
||||||
import com.android.settingslib.wifi.AccessPoint;
|
|
||||||
|
|
||||||
import org.robolectric.RuntimeEnvironment;
|
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
public class WifiUtilsTest {
|
public class WifiUtilsTest {
|
||||||
|
|
||||||
@@ -56,14 +54,12 @@ public class WifiUtilsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getWifiConfigByAccessPoint_shouldReturnCorrectConfig() {
|
public void getWifiConfigByWifiEntry_shouldReturnCorrectConfig() {
|
||||||
String testSSID = "WifiUtilsTest";
|
final String testSSID = "WifiUtilsTest";
|
||||||
Bundle bundle = new Bundle();
|
final WifiEntry wifiEntry = mock(WifiEntry.class);
|
||||||
bundle.putString("key_ssid", testSSID);
|
when(wifiEntry.getSsid()).thenReturn(testSSID);
|
||||||
Context context = spy(RuntimeEnvironment.application);
|
|
||||||
AccessPoint accessPoint = new AccessPoint(context, bundle);
|
|
||||||
|
|
||||||
WifiConfiguration config = WifiUtils.getWifiConfig(accessPoint, null, null);
|
final WifiConfiguration config = WifiUtils.getWifiConfig(wifiEntry, null /* scanResult */);
|
||||||
|
|
||||||
assertThat(config).isNotNull();
|
assertThat(config).isNotNull();
|
||||||
assertThat(config.SSID).isEqualTo(AccessPoint.convertToQuotedString(testSSID));
|
assertThat(config.SSID).isEqualTo(AccessPoint.convertToQuotedString(testSSID));
|
||||||
@@ -71,6 +67,7 @@ public class WifiUtilsTest {
|
|||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void getWifiConfigWithNullInput_ThrowIllegalArgumentException() {
|
public void getWifiConfigWithNullInput_ThrowIllegalArgumentException() {
|
||||||
WifiConfiguration config = WifiUtils.getWifiConfig(null, null, null);
|
WifiConfiguration config = WifiUtils.getWifiConfig(null /* wifiEntry */,
|
||||||
|
null /* scanResult */);
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user