[Wi-Fi] Apply WifiTrackerLib objects in WifiDppQrCodeScannerFragment
Bug: 152571756 Bug: 145100890 Test: manual Scan QR code and check connection. Change-Id: I21b9fadc83ab185df5710f4bf777afec3dfbbc8d
This commit is contained in:
@@ -16,6 +16,8 @@
|
||||
|
||||
package com.android.settings.wifi.dpp;
|
||||
|
||||
import static android.net.wifi.WifiInfo.sanitizeSsid;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.content.Context;
|
||||
@@ -23,12 +25,19 @@ import android.content.Intent;
|
||||
import android.graphics.Matrix;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.SurfaceTexture;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkScoreManager;
|
||||
import android.net.wifi.EasyConnectStatusCallback;
|
||||
import android.net.wifi.WifiConfiguration;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.HandlerThread;
|
||||
import android.os.Looper;
|
||||
import android.os.Message;
|
||||
import android.os.Process;
|
||||
import android.os.SimpleClock;
|
||||
import android.os.SystemClock;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.util.Size;
|
||||
@@ -51,18 +60,17 @@ import com.android.settings.R;
|
||||
import com.android.settings.wifi.WifiDialogActivity;
|
||||
import com.android.settings.wifi.qrcode.QrCamera;
|
||||
import com.android.settings.wifi.qrcode.QrDecorateView;
|
||||
import com.android.wifitrackerlib.WifiEntry;
|
||||
import com.android.wifitrackerlib.WifiPickerTracker;
|
||||
|
||||
import com.android.settingslib.wifi.AccessPoint;
|
||||
import com.android.settingslib.wifi.WifiTracker;
|
||||
import com.android.settingslib.wifi.WifiTrackerFactory;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.List;
|
||||
|
||||
public class WifiDppQrCodeScannerFragment extends WifiDppQrCodeBaseFragment implements
|
||||
SurfaceTextureListener,
|
||||
QrCamera.ScannerCallback,
|
||||
WifiManager.ActionListener,
|
||||
WifiTracker.WifiListener {
|
||||
WifiManager.ActionListener {
|
||||
private static final String TAG = "WifiDppQrCodeScanner";
|
||||
|
||||
/** Message sent to hide error message */
|
||||
@@ -87,6 +95,11 @@ public class WifiDppQrCodeScannerFragment extends WifiDppQrCodeBaseFragment impl
|
||||
|
||||
private static final int ARG_RESTART_CAMERA = 1;
|
||||
|
||||
// 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;
|
||||
|
||||
private QrCamera mCamera;
|
||||
private TextureView mTextureView;
|
||||
private QrDecorateView mDecorateView;
|
||||
@@ -106,7 +119,8 @@ public class WifiDppQrCodeScannerFragment extends WifiDppQrCodeBaseFragment impl
|
||||
|
||||
private int mLatestStatusCode = WifiDppUtils.EASY_CONNECT_EVENT_FAILURE_NONE;
|
||||
|
||||
private WifiTracker mWifiTracker;
|
||||
private WifiPickerTracker mWifiPickerTracker;
|
||||
private HandlerThread mWorkerThread;
|
||||
|
||||
private final Handler mHandler = new Handler() {
|
||||
@Override
|
||||
@@ -219,11 +233,27 @@ public class WifiDppQrCodeScannerFragment extends WifiDppQrCodeBaseFragment impl
|
||||
}
|
||||
|
||||
private boolean isReachableWifiNetwork(WifiConfiguration wifiConfiguration) {
|
||||
final List<AccessPoint> scannedAccessPoints = mWifiTracker.getAccessPoints();
|
||||
final List<WifiEntry> wifiEntries = mWifiPickerTracker.getWifiEntries();
|
||||
final WifiEntry connectedWifiEntry = mWifiPickerTracker.getConnectedWifiEntry();
|
||||
if (connectedWifiEntry != null) {
|
||||
// Add connected WifiEntry to prevent fail toast to users when it's connected.
|
||||
wifiEntries.add(connectedWifiEntry);
|
||||
}
|
||||
|
||||
for (AccessPoint scannedAccessPoint : scannedAccessPoints) {
|
||||
if (scannedAccessPoint.matches(wifiConfiguration) &&
|
||||
scannedAccessPoint.isReachable()) {
|
||||
for (WifiEntry wifiEntry : wifiEntries) {
|
||||
if (!TextUtils.equals(wifiEntry.getSsid(), sanitizeSsid(wifiConfiguration.SSID))) {
|
||||
continue;
|
||||
}
|
||||
final int security =
|
||||
WifiDppUtils.getSecurityTypeFromWifiConfiguration(wifiConfiguration);
|
||||
if (security == wifiEntry.getSecurity()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Default security type of PSK/SAE transition mode WifiEntry is SECURITY_PSK and
|
||||
// there is no way to know if a WifiEntry is of transition mode. Give it a chance.
|
||||
if (security == WifiEntry.SECURITY_SAE
|
||||
&& wifiEntry.getSecurity() == WifiEntry.SECURITY_PSK) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -323,8 +353,27 @@ public class WifiDppQrCodeScannerFragment extends WifiDppQrCodeBaseFragment impl
|
||||
public void onActivityCreated(Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
|
||||
mWifiTracker = WifiTrackerFactory.create(getActivity(), /* wifiListener */ this,
|
||||
getSettingsLifecycle(), /* includeSaved */ false, /* includeScans */ true);
|
||||
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,
|
||||
null /* listener */);
|
||||
|
||||
// setTitle for TalkBack
|
||||
if (mIsConfiguratorMode) {
|
||||
@@ -348,6 +397,13 @@ public class WifiDppQrCodeScannerFragment extends WifiDppQrCodeBaseFragment impl
|
||||
super.onDetach();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
mWorkerThread.quit();
|
||||
|
||||
super.onDestroyView();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
@@ -690,27 +746,6 @@ public class WifiDppQrCodeScannerFragment extends WifiDppQrCodeBaseFragment impl
|
||||
}
|
||||
}
|
||||
|
||||
/** Called when the state of Wifi has changed. */
|
||||
@Override
|
||||
public void onWifiStateChanged(int state) {
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
/** Called when the connection state of wifi has changed. */
|
||||
@Override
|
||||
public void onConnectedChanged() {
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
/**
|
||||
* Called to indicate the list of AccessPoints has been updated and
|
||||
* getAccessPoints should be called to get the latest information.
|
||||
*/
|
||||
@Override
|
||||
public void onAccessPointsChanged() {
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
protected boolean isDecodeTaskAlive() {
|
||||
return mCamera != null && mCamera.isDecodeTaskAlive();
|
||||
|
@@ -534,4 +534,25 @@ public class WifiDppUtils {
|
||||
VIBRATE_DURATION_QR_CODE_RECOGNITION.toMillis(),
|
||||
VibrationEffect.DEFAULT_AMPLITUDE));
|
||||
}
|
||||
|
||||
@WifiEntry.Security
|
||||
static int getSecurityTypeFromWifiConfiguration(WifiConfiguration config) {
|
||||
if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.SAE)) {
|
||||
return WifiEntry.SECURITY_SAE;
|
||||
}
|
||||
if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_PSK)) {
|
||||
return WifiEntry.SECURITY_PSK;
|
||||
}
|
||||
if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.SUITE_B_192)) {
|
||||
return WifiEntry.SECURITY_EAP_SUITE_B;
|
||||
}
|
||||
if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_EAP)
|
||||
|| config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.IEEE8021X)) {
|
||||
return WifiEntry.SECURITY_EAP;
|
||||
}
|
||||
if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.OWE)) {
|
||||
return WifiEntry.SECURITY_OWE;
|
||||
}
|
||||
return (config.wepKeys[0] != null) ? WifiEntry.SECURITY_WEP : WifiEntry.SECURITY_NONE;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user