diff options
Diffstat (limited to 'libs')
5 files changed, 136 insertions, 84 deletions
diff --git a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/PasspointWifiEntry.java b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/PasspointWifiEntry.java index cda52adc2..a2d43c0f0 100644 --- a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/PasspointWifiEntry.java +++ b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/PasspointWifiEntry.java @@ -32,6 +32,7 @@ import android.net.wifi.WifiManager; import android.net.wifi.hotspot2.PasspointConfiguration; import android.net.wifi.hotspot2.pps.HomeSp; import android.os.Handler; +import android.text.TextUtils; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -56,13 +57,6 @@ class PasspointWifiEntry extends WifiEntry { @Nullable private WifiConfiguration mWifiConfig; private @Security int mSecurity; private boolean mIsRoaming = false; - @Nullable private NetworkInfo mNetworkInfo; - @Nullable private WifiInfo mWifiInfo; - @Nullable private ConnectCallback mConnectCallback; - @Nullable private DisconnectCallback mDisconnectCallback; - @Nullable private ForgetCallback mForgetCallback; - private boolean mCalledConnect = false; - private boolean mCalledDisconnect = false; private int mLevel = WIFI_LEVEL_UNREACHABLE; @@ -77,6 +71,7 @@ class PasspointWifiEntry extends WifiEntry { checkNotNull(passpointConfig, "Cannot construct with null PasspointConfiguration!"); mContext = context; + mPasspointConfig = passpointConfig; final HomeSp homeSp = passpointConfig.getHomeSp(); mKey = fqdnToPasspointWifiEntryKey(homeSp.getFqdn()); mFriendlyName = homeSp.getFriendlyName(); @@ -89,13 +84,6 @@ class PasspointWifiEntry extends WifiEntry { } @Override - @ConnectedState - public int getConnectedState() { - // TODO(b/70983952): Fill this method in - return CONNECTED_STATE_DISCONNECTED; - } - - @Override public String getTitle() { return mFriendlyName; } @@ -108,7 +96,7 @@ class PasspointWifiEntry extends WifiEntry { @Override public String getSummary(boolean concise) { // TODO(b/70983952): Fill this method in - return "Passpoint"; // Placeholder string + return "Passpoint (Placeholder Text)"; // Placeholder string } @Override @@ -157,20 +145,20 @@ class PasspointWifiEntry extends WifiEntry { } @Override - public ConnectedInfo getConnectedInfo() { - // TODO(b/70983952): Fill this method in - return null; - } - - @Override public boolean canConnect() { return mLevel != WIFI_LEVEL_UNREACHABLE - && getConnectedState() == CONNECTED_STATE_DISCONNECTED; + && getConnectedState() == CONNECTED_STATE_DISCONNECTED && mWifiConfig != null; } @Override public void connect(@Nullable ConnectCallback callback) { - // TODO(b/70983952): Fill this method in + mConnectCallback = callback; + + if (mWifiConfig == null) { + // We should not be able to call connect() if mWifiConfig is null + new ConnectActionListener().onFailure(0); + } + mWifiManager.connect(mWifiConfig, new ConnectActionListener()); } @Override @@ -185,53 +173,49 @@ class PasspointWifiEntry extends WifiEntry { @Override public boolean canForget() { - // TODO(b/70983952): Fill this method in - return false; + return true; } @Override public void forget(@Nullable ForgetCallback callback) { - // TODO(b/70983952): Fill this method in + mForgetCallback = callback; + mWifiManager.removePasspointConfiguration(mPasspointConfig.getHomeSp().getFqdn()); + new ForgetActionListener().onSuccess(); } @Override public boolean canSignIn() { - // TODO(b/70983952): Fill this method in return false; } @Override public void signIn(@Nullable SignInCallback callback) { - // TODO(b/70983952): Fill this method in + return; } @Override public boolean canShare() { - // TODO(b/70983952): Fill this method in return false; } @Override public boolean canEasyConnect() { - // TODO(b/70983952): Fill this method in return false; } @Override public String getQrCodeString() { - // TODO(b/70983952): Fill this method in return null; } @Override public boolean canSetPassword() { - // TODO(b/70983952): Fill this method in return false; } @Override public void setPassword(@NonNull String password) { - // TODO(b/70983952): Fill this method in + // Do nothing. } @Override @@ -342,7 +326,8 @@ class PasspointWifiEntry extends WifiEntry { return false; } - return mWifiConfig != null && mWifiConfig.networkId == wifiInfo.getNetworkId(); + return TextUtils.equals( + wifiInfo.getPasspointFqdn(), mPasspointConfig.getHomeSp().getFqdn()); } @NonNull diff --git a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardWifiEntry.java b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardWifiEntry.java index 1a04c3a9b..0db155243 100644 --- a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardWifiEntry.java +++ b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardWifiEntry.java @@ -344,8 +344,8 @@ class StandardWifiEntry extends WifiEntry { @Override public boolean isMetered() { - // TODO(b/70983952): Fill this method in - return false; + return getMeteredChoice() == METERED_CHOICE_METERED + || (mWifiConfig != null && mWifiConfig.meteredHint); } @Override @@ -765,52 +765,6 @@ class StandardWifiEntry extends WifiEntry { + getSecurityTypeFromWifiConfiguration(config); } - private class ConnectActionListener implements WifiManager.ActionListener { - @Override - public void onSuccess() { - mCalledConnect = true; - // If we aren't connected to the network after 10 seconds, trigger the failure callback - mCallbackHandler.postDelayed(() -> { - if (mConnectCallback != null && mCalledConnect - && getConnectedState() == CONNECTED_STATE_DISCONNECTED) { - mConnectCallback.onConnectResult( - ConnectCallback.CONNECT_STATUS_FAILURE_UNKNOWN); - mCalledConnect = false; - } - }, 10_000 /* delayMillis */); - } - - @Override - public void onFailure(int i) { - mCallbackHandler.post(() -> { - if (mConnectCallback != null) { - mConnectCallback.onConnectResult( - mConnectCallback.CONNECT_STATUS_FAILURE_UNKNOWN); - } - }); - } - } - - class ForgetActionListener implements WifiManager.ActionListener { - @Override - public void onSuccess() { - mCallbackHandler.post(() -> { - if (mForgetCallback != null) { - mForgetCallback.onForgetResult(ForgetCallback.FORGET_STATUS_SUCCESS); - } - }); - } - - @Override - public void onFailure(int i) { - mCallbackHandler.post(() -> { - if (mForgetCallback != null) { - mForgetCallback.onForgetResult(ForgetCallback.FORGET_STATUS_FAILURE_UNKNOWN); - } - }); - } - } - @Override String getScanResultDescription() { if (mCurrentScanResults.size() == 0) { diff --git a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/WifiEntry.java b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/WifiEntry.java index 9093716e7..52eb6eb2a 100644 --- a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/WifiEntry.java +++ b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/WifiEntry.java @@ -193,6 +193,7 @@ public abstract class WifiEntry implements Comparable<WifiEntry> { protected ConnectCallback mConnectCallback; protected DisconnectCallback mDisconnectCallback; + protected ForgetCallback mForgetCallback; protected boolean mCalledConnect = false; protected boolean mCalledDisconnect = false; @@ -625,6 +626,52 @@ public abstract class WifiEntry implements Comparable<WifiEntry> { return sj.toString(); } + protected class ConnectActionListener implements WifiManager.ActionListener { + @Override + public void onSuccess() { + mCalledConnect = true; + // If we aren't connected to the network after 10 seconds, trigger the failure callback + mCallbackHandler.postDelayed(() -> { + if (mConnectCallback != null && mCalledConnect + && getConnectedState() == CONNECTED_STATE_DISCONNECTED) { + mConnectCallback.onConnectResult( + ConnectCallback.CONNECT_STATUS_FAILURE_UNKNOWN); + mCalledConnect = false; + } + }, 10_000 /* delayMillis */); + } + + @Override + public void onFailure(int i) { + mCallbackHandler.post(() -> { + if (mConnectCallback != null) { + mConnectCallback.onConnectResult( + mConnectCallback.CONNECT_STATUS_FAILURE_UNKNOWN); + } + }); + } + } + + protected class ForgetActionListener implements WifiManager.ActionListener { + @Override + public void onSuccess() { + mCallbackHandler.post(() -> { + if (mForgetCallback != null) { + mForgetCallback.onForgetResult(ForgetCallback.FORGET_STATUS_SUCCESS); + } + }); + } + + @Override + public void onFailure(int i) { + mCallbackHandler.post(() -> { + if (mForgetCallback != null) { + mForgetCallback.onForgetResult(ForgetCallback.FORGET_STATUS_FAILURE_UNKNOWN); + } + }); + } + } + // TODO (b/70983952) Come up with a sorting scheme that does the right thing. @Override public int compareTo(@NonNull WifiEntry other) { diff --git a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/WifiPickerTracker.java b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/WifiPickerTracker.java index 34df6e67f..a54b60d38 100644 --- a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/WifiPickerTracker.java +++ b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/WifiPickerTracker.java @@ -18,6 +18,7 @@ package com.android.wifitrackerlib; import static androidx.core.util.Preconditions.checkNotNull; +import static com.android.wifitrackerlib.PasspointWifiEntry.fqdnToPasspointWifiEntryKey; import static com.android.wifitrackerlib.StandardWifiEntry.wifiConfigToStandardWifiEntryKey; import static com.android.wifitrackerlib.Utils.mapScanResultsToKey; import static com.android.wifitrackerlib.WifiEntry.CONNECTED_STATE_CONNECTED; @@ -173,6 +174,7 @@ public class WifiPickerTracker extends BaseWifiTracker { updateConnectionInfo(wifiInfo, networkInfo); // Create a StandardWifiEntry for the current connection if there are no scan results yet. conditionallyCreateConnectedStandardWifiEntry(wifiInfo, networkInfo); + conditionallyCreateConnectedPasspointWifiEntry(wifiInfo, networkInfo); handleLinkPropertiesChanged(mConnectivityManager.getLinkProperties( mWifiManager.getCurrentNetwork())); notifyOnNumSavedNetworksChanged(); @@ -252,6 +254,13 @@ public class WifiPickerTracker extends BaseWifiTracker { return connectedState == CONNECTED_STATE_CONNECTED || connectedState == CONNECTED_STATE_CONNECTING; }).findAny().orElse(null /* other */); + if (mConnectedWifiEntry == null) { + mConnectedWifiEntry = mPasspointWifiEntryCache.values().stream().filter(entry -> { + final @WifiEntry.ConnectedState int connectedState = entry.getConnectedState(); + return connectedState == CONNECTED_STATE_CONNECTED + || connectedState == CONNECTED_STATE_CONNECTING; + }).findAny().orElse(null /* other */); + } Collections.sort(mWifiEntries); if (isVerboseLoggingEnabled()) { Log.v(TAG, "Connected WifiEntry: " + mConnectedWifiEntry); @@ -313,7 +322,7 @@ public class WifiPickerTracker extends BaseWifiTracker { pair.second.get(WifiManager.PASSPOINT_HOME_NETWORK); final List<ScanResult> roamingScans = pair.second.get(WifiManager.PASSPOINT_ROAMING_NETWORK); - final String key = PasspointWifiEntry.fqdnToPasspointWifiEntryKey(wifiConfig.FQDN); + final String key = fqdnToPasspointWifiEntryKey(wifiConfig.FQDN); // Skip in case the returned if (!mPasspointConfigCache.containsKey(key)) { continue; @@ -430,6 +439,10 @@ public class WifiPickerTracker extends BaseWifiTracker { @WorkerThread private void conditionallyCreateConnectedStandardWifiEntry(@Nullable WifiInfo wifiInfo, @Nullable NetworkInfo networkInfo) { + if (wifiInfo.isPasspointAp()) { + return; + } + final int connectedNetId = wifiInfo.getNetworkId(); mWifiConfigCache.values().stream() .filter(config -> @@ -444,13 +457,40 @@ public class WifiPickerTracker extends BaseWifiTracker { }); } + /** + * Creates and caches a StandardWifiEntry representing the current connection using the current + * WifiInfo and NetworkInfo if there are no scans results available for the network yet. + * @param wifiInfo WifiInfo of the current connection + * @param networkInfo NetworkInfo of the current connection + */ + @WorkerThread + private void conditionallyCreateConnectedPasspointWifiEntry(@Nullable WifiInfo wifiInfo, + @Nullable NetworkInfo networkInfo) { + if (!wifiInfo.isPasspointAp()) { + return; + } + + final String connectedFqdn = wifiInfo.getPasspointFqdn(); + mPasspointConfigCache.values().stream() + .filter(config -> + config.getHomeSp().getFqdn() == connectedFqdn + && !mPasspointWifiEntryCache.containsKey( + fqdnToPasspointWifiEntryKey(connectedFqdn))) + .findAny().ifPresent(config -> { + final PasspointWifiEntry connectedEntry = + new PasspointWifiEntry(mContext, mMainHandler, config, mWifiManager); + connectedEntry.updateConnectionInfo(wifiInfo, networkInfo); + mPasspointWifiEntryCache.put(connectedEntry.getKey(), connectedEntry); + }); + } + @WorkerThread private void updatePasspointWifiEntryConfigs(@NonNull List<PasspointConfiguration> configs) { checkNotNull(configs, "Config list should not be null!"); mPasspointConfigCache.clear(); mPasspointConfigCache.putAll(configs.stream().collect( - toMap((config) -> PasspointWifiEntry.fqdnToPasspointWifiEntryKey( + toMap((config) -> fqdnToPasspointWifiEntryKey( config.getHomeSp().getFqdn()), Function.identity()))); // Iterate through current entries and update each entry's config or remove if no config diff --git a/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/WifiPickerTrackerTest.java b/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/WifiPickerTrackerTest.java index 3c7c1207c..280ec971d 100644 --- a/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/WifiPickerTrackerTest.java +++ b/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/WifiPickerTrackerTest.java @@ -531,4 +531,30 @@ public class WifiPickerTrackerTest { assertThat(wifiPickerTracker.getWifiEntries()).isNotEmpty(); assertThat(wifiPickerTracker.getWifiEntries().get(0).getTitle()).isEqualTo("friendlyName"); } + + @Test + public void testGetConnectedEntry_alreadyConnectedToPasspoint_returnsPasspointEntry() { + final String fqdn = "fqdn"; + final String friendlyName = "friendlyName"; + final WifiPickerTracker wifiPickerTracker = createTestWifiPickerTracker(); + final PasspointConfiguration config = new PasspointConfiguration(); + final HomeSp homeSp = new HomeSp(); + homeSp.setFqdn(fqdn); + homeSp.setFriendlyName(friendlyName); + config.setHomeSp(homeSp); + when(mMockWifiManager.getPasspointConfigurations()) + .thenReturn(Collections.singletonList(config)); + when(mMockWifiInfo.isPasspointAp()).thenReturn(true); + when(mMockWifiInfo.getPasspointFqdn()).thenReturn(fqdn); + when(mMockWifiInfo.getRssi()).thenReturn(-50); + when(mMockNetworkInfo.getDetailedState()).thenReturn(NetworkInfo.DetailedState.CONNECTED); + + wifiPickerTracker.onStart(); + verify(mMockContext).registerReceiver(mBroadcastReceiverCaptor.capture(), + any(), any(), any()); + mTestLooper.dispatchAll(); + + verify(mMockCallback, atLeastOnce()).onWifiEntriesChanged(); + assertThat(wifiPickerTracker.getConnectedWifiEntry().getTitle()).isEqualTo(friendlyName); + } } |