diff options
author | Joe LaPenna <jlapenna@google.com> | 2017-02-27 18:18:47 -0800 |
---|---|---|
committer | Joe LaPenna <jlapenna@google.com> | 2017-02-27 18:18:47 -0800 |
commit | ff32635d4bf6547dda8bc191cae7ec5f59290a5c (patch) | |
tree | b5d24c39100f04a27c41656d36b925379f6569cf | |
parent | 0f602a6a0d486c19599dd0587bdcd6aa1006db44 (diff) | |
download | NetworkRecommendation-ff32635d4bf6547dda8bc191cae7ec5f59290a5c.tar.gz |
Update /notify/.
Bug: 35114358
Test: runtest --path packages/services/NetworkRecommendation/tests/src/com/android/networkrecommendation/wakeup/WifiWakeupNotificationHelperTest.java
Change-Id: I5b4759eb0ed1d5c3a98dd5d878163a11eee1fac4
8 files changed, 456 insertions, 108 deletions
diff --git a/robotests/src/android/net/RecommendationRequest.java b/robotests/src/android/net/RecommendationRequest.java index debf674..9f97c5a 100644 --- a/robotests/src/android/net/RecommendationRequest.java +++ b/robotests/src/android/net/RecommendationRequest.java @@ -1,35 +1,223 @@ /* - * Copyright (C) 2017 The Android Open Source Project + * Copyright (C) 2016 The Android Open Source Project * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License */ + package android.net; + +import android.annotation.SystemApi; +import android.net.wifi.ScanResult; +import android.net.wifi.WifiConfiguration; import android.os.Parcel; import android.os.Parcelable; +import com.android.internal.annotations.VisibleForTesting; + /** - * A placeholder class to prevent ClassNotFound exceptions caused by lack of visibility. + * A request for a network recommendation. + * + * @see {@link NetworkScoreManager#requestRecommendation(RecommendationRequest)}. + * @hide */ -public class RecommendationRequest implements Parcelable { +@SystemApi +public final class RecommendationRequest implements Parcelable { + private final ScanResult[] mScanResults; + private final WifiConfiguration mDefaultConfig; + private WifiConfiguration mConnectedConfig; + private WifiConfiguration[] mConnectableConfigs; + private final int mLastSelectedNetworkId; + private final long mLastSelectedNetworkTimestamp; + + /** + * Builder class for constructing {@link RecommendationRequest} instances. + * @hide + */ + @SystemApi public static final class Builder { + private ScanResult[] mScanResults; + private WifiConfiguration mDefaultConfig; + private WifiConfiguration mConnectedConfig; + private WifiConfiguration[] mConnectableConfigs; + private int mLastSelectedNetworkId = -1; + private long mLastSelectedTimestamp; + + public Builder setScanResults(ScanResult[] scanResults) { + mScanResults = scanResults; + return this; + } + + /** + * @param config the {@link WifiConfiguration} to return if no recommendation is available. + * @return this + */ + public Builder setDefaultWifiConfig(WifiConfiguration config) { + this.mDefaultConfig = config; + return this; + } + /** + * @param config the {@link WifiConfiguration} of the connected network at the time the + * this request was made. + * @return this + */ + public Builder setConnectedWifiConfig(WifiConfiguration config) { + this.mConnectedConfig = config; + return this; + } + + /** + * @param connectableConfigs the set of saved {@link WifiConfiguration}s that can be + * connected to based on the current set of {@link ScanResult}s. + * @return this + */ + public Builder setConnectableConfigs(WifiConfiguration[] connectableConfigs) { + this.mConnectableConfigs = connectableConfigs; + return this; + } + + /** + * @param networkId The {@link WifiConfiguration#networkId} of the last user selected + * network. + * @param timestamp The {@link android.os.SystemClock#elapsedRealtime()} when the user + * selected {@code networkId}. + * @return this + */ + public Builder setLastSelectedNetwork(int networkId, long timestamp) { + this.mLastSelectedNetworkId = networkId; + this.mLastSelectedTimestamp = timestamp; + return this; + } + + /** + * @return a new {@link RecommendationRequest} instance + */ public RecommendationRequest build() { - return null; + return new RecommendationRequest(mScanResults, mDefaultConfig, mConnectedConfig, + mConnectableConfigs, mLastSelectedNetworkId, mLastSelectedTimestamp); } } + /** + * @return the array of {@link ScanResult}s the recommendation must be constrained to i.e. if a + * non-null wifi config recommendation is returned then it must be able to connect to + * one of the networks in the results list. + * + * If the array is {@code null} or empty then there is no constraint. + */ + public ScanResult[] getScanResults() { + return mScanResults; + } + + /** + * @return the {@link WifiConfiguration} to return if no recommendation is available. + */ + public WifiConfiguration getDefaultWifiConfig() { + return mDefaultConfig; + } + + /** + * @return the {@link WifiConfiguration} of the connected network at the time the this request + * was made. + */ + public WifiConfiguration getConnectedConfig() { + return mConnectedConfig; + } + + /** + * @return the set of saved {@link WifiConfiguration}s that can be connected to based on the + * current set of {@link ScanResult}s. + */ + public WifiConfiguration[] getConnectableConfigs() { + return mConnectableConfigs; + } + + /** + * @param connectedConfig the {@link WifiConfiguration} of the connected network at the time + * the this request was made. + */ + public void setConnectedConfig(WifiConfiguration connectedConfig) { + mConnectedConfig = connectedConfig; + } + + /** + * @param connectableConfigs the set of saved {@link WifiConfiguration}s that can be connected + * to based on the current set of {@link ScanResult}s. + */ + public void setConnectableConfigs(WifiConfiguration[] connectableConfigs) { + mConnectableConfigs = connectableConfigs; + } + + /** + * @return The {@link WifiConfiguration#networkId} of the last user selected network. + * {@code -1} if not set. + */ + public int getLastSelectedNetworkId() { + return mLastSelectedNetworkId; + } + + /** + * @return The {@link android.os.SystemClock#elapsedRealtime()} when the user selected + * {@link #getLastSelectedNetworkId()}. {@code 0} if not set. + */ + public long getLastSelectedNetworkTimestamp() { + return mLastSelectedNetworkTimestamp; + } + + @VisibleForTesting + RecommendationRequest(ScanResult[] scanResults, + WifiConfiguration defaultWifiConfig, + WifiConfiguration connectedWifiConfig, + WifiConfiguration[] connectableConfigs, + int lastSelectedNetworkId, + long lastSelectedNetworkTimestamp) { + mScanResults = scanResults; + mDefaultConfig = defaultWifiConfig; + mConnectedConfig = connectedWifiConfig; + mConnectableConfigs = connectableConfigs; + mLastSelectedNetworkId = lastSelectedNetworkId; + mLastSelectedNetworkTimestamp = lastSelectedNetworkTimestamp; + } + protected RecommendationRequest(Parcel in) { + final int resultCount = in.readInt(); + if (resultCount > 0) { + mScanResults = new ScanResult[resultCount]; + final ClassLoader classLoader = ScanResult.class.getClassLoader(); + for (int i = 0; i < resultCount; i++) { + mScanResults[i] = in.readParcelable(classLoader); + } + } else { + mScanResults = null; + } + + mDefaultConfig = in.readParcelable(WifiConfiguration.class.getClassLoader()); + mConnectedConfig = in.readParcelable(WifiConfiguration.class.getClassLoader()); + + final int configCount = in.readInt(); + if (configCount > 0) { + mConnectableConfigs = new WifiConfiguration[configCount]; + final ClassLoader classLoader = WifiConfiguration.class.getClassLoader(); + for (int i = 0; i < configCount; i++) { + mConnectableConfigs[i] = in.readParcelable(classLoader); + } + } else { + mConnectableConfigs = null; + } + + mLastSelectedNetworkId = in.readInt(); + mLastSelectedNetworkTimestamp = in.readLong(); } @Override @@ -39,5 +227,41 @@ public class RecommendationRequest implements Parcelable { @Override public void writeToParcel(Parcel dest, int flags) { + if (mScanResults != null) { + dest.writeInt(mScanResults.length); + for (int i = 0; i < mScanResults.length; i++) { + dest.writeParcelable(mScanResults[i], flags); + } + } else { + dest.writeInt(0); + } + + dest.writeParcelable(mDefaultConfig, flags); + dest.writeParcelable(mConnectedConfig, flags); + + if (mConnectableConfigs != null) { + dest.writeInt(mConnectableConfigs.length); + for (int i = 0; i < mConnectableConfigs.length; i++) { + dest.writeParcelable(mConnectableConfigs[i], flags); + } + } else { + dest.writeInt(0); + } + + dest.writeInt(mLastSelectedNetworkId); + dest.writeLong(mLastSelectedNetworkTimestamp); } + + public static final Creator<RecommendationRequest> CREATOR = + new Creator<RecommendationRequest>() { + @Override + public RecommendationRequest createFromParcel(Parcel in) { + return new RecommendationRequest(in); + } + + @Override + public RecommendationRequest[] newArray(int size) { + return new RecommendationRequest[size]; + } + }; } diff --git a/robotests/src/android/net/RecommendationResult.java b/robotests/src/android/net/RecommendationResult.java index dafb1fc..70cf09c 100644 --- a/robotests/src/android/net/RecommendationResult.java +++ b/robotests/src/android/net/RecommendationResult.java @@ -1,31 +1,95 @@ /* - * Copyright (C) 2017 The Android Open Source Project + * Copyright (C) 2016 The Android Open Source Project * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License */ + package android.net; +import android.annotation.NonNull; import android.annotation.Nullable; +import android.annotation.SystemApi; import android.net.wifi.WifiConfiguration; import android.os.Parcel; import android.os.Parcelable; +import com.android.internal.annotations.VisibleForTesting; +import com.android.internal.util.Preconditions; + /** - * A placeholder class to prevent ClassNotFound exceptions caused by lack of visibility. + * The result of a network recommendation. + * + * @see {@link NetworkScoreManager#requestRecommendation(RecommendationRequest)}. + * @hide */ -public class RecommendationResult implements Parcelable { +@SystemApi +public final class RecommendationResult implements Parcelable { + private final WifiConfiguration mWifiConfiguration; + + /** + * Create a {@link RecommendationResult} that indicates that no network connection should be + * attempted at this time. + * + * @return a {@link RecommendationResult} + */ + public static RecommendationResult createDoNotConnectRecommendation() { + return new RecommendationResult((WifiConfiguration) null); + } + + /** + * Create a {@link RecommendationResult} that indicates that a connection attempt should be + * made for the given Wi-Fi network. + * + * @param wifiConfiguration {@link WifiConfiguration} with at least SSID and BSSID set. + * @return a {@link RecommendationResult} + */ + public static RecommendationResult createConnectRecommendation( + @NonNull WifiConfiguration wifiConfiguration) { + Preconditions.checkNotNull(wifiConfiguration, "wifiConfiguration must not be null"); + Preconditions.checkNotNull(wifiConfiguration.SSID, "SSID must not be null"); + Preconditions.checkNotNull(wifiConfiguration.BSSID, "BSSID must not be null"); + return new RecommendationResult(wifiConfiguration); + } + + private RecommendationResult(@Nullable WifiConfiguration wifiConfiguration) { + mWifiConfiguration = wifiConfiguration; + } + + private RecommendationResult(Parcel in) { + mWifiConfiguration = in.readParcelable(WifiConfiguration.class.getClassLoader()); + } + + /** + * @return {@code true} if a network recommendation exists. {@code false} indicates that + * no connection should be attempted at this time. + */ + public boolean hasRecommendation() { + return mWifiConfiguration != null; + } + + /** + * @return The recommended {@link WifiConfiguration} to connect to. A {@code null} value + * is returned if {@link #hasRecommendation} returns {@code false}. + */ + @Nullable public WifiConfiguration getWifiConfiguration() { + return mWifiConfiguration; + } - protected RecommendationResult(Parcel in) { + @Override + public String toString() { + return "RecommendationResult{" + + "mWifiConfiguration=" + mWifiConfiguration + + "}"; } @Override @@ -34,15 +98,20 @@ public class RecommendationResult implements Parcelable { } @Override - public void writeToParcel(Parcel dest, int flags) { + public void writeToParcel(Parcel out, int flags) { + out.writeParcelable(mWifiConfiguration, flags); } - public boolean hasRecommendation() { - return false; - } + public static final Creator<RecommendationResult> CREATOR = + new Creator<RecommendationResult>() { + @Override + public RecommendationResult createFromParcel(Parcel in) { + return new RecommendationResult(in); + } - @Nullable - public WifiConfiguration getWifiConfiguration() { - return null; - } + @Override + public RecommendationResult[] newArray(int size) { + return new RecommendationResult[size]; + } + }; } diff --git a/robotests/src/com/android/networkrecommendation/TestData.java b/robotests/src/com/android/networkrecommendation/TestData.java index aad9d5b..803f3b6 100644 --- a/robotests/src/com/android/networkrecommendation/TestData.java +++ b/robotests/src/com/android/networkrecommendation/TestData.java @@ -15,6 +15,9 @@ */ package com.android.networkrecommendation; +import static com.android.networkrecommendation.PlatformTestObjectFactory.createNetworkKey; + +import android.net.NetworkKey; import com.android.networkrecommendation.util.SsidUtil; /** @@ -36,6 +39,10 @@ public class TestData { public static final String BSSID_2 = "02:02:02:02:02:02"; public static final String BSSID_3 = "03:03:03:03:03:03"; + // Platform objects. + public static final NetworkKey NETWORK_KEY1 = createNetworkKey(SSID_1, BSSID_1); + public static final NetworkKey NETWORK_KEY2 = createNetworkKey(SSID_2, BSSID_2); + // Can't instantiate. private TestData() {} } diff --git a/robotests/src/com/android/networkrecommendation/TestUtil.java b/robotests/src/com/android/networkrecommendation/TestUtil.java index bcd5693..747e081 100644 --- a/robotests/src/com/android/networkrecommendation/TestUtil.java +++ b/robotests/src/com/android/networkrecommendation/TestUtil.java @@ -25,10 +25,4 @@ package com.android.networkrecommendation; public class TestUtil { private TestUtil() {} // do not instantiate - - /** @deprecated Use SsidUtil.quoteSsid rather. TODO(williamm): remove. */ - @Deprecated - public static String quoteSsid(String ssid) { - return "\"" + ssid + "\""; - } } diff --git a/robotests/src/com/android/networkrecommendation/notify/WifiNotificationControllerTest.java b/robotests/src/com/android/networkrecommendation/notify/WifiNotificationControllerTest.java index 30098ce..76766a3 100644 --- a/robotests/src/com/android/networkrecommendation/notify/WifiNotificationControllerTest.java +++ b/robotests/src/com/android/networkrecommendation/notify/WifiNotificationControllerTest.java @@ -49,7 +49,6 @@ import com.google.common.collect.Lists; import java.io.FileDescriptor; import java.io.PrintWriter; import java.io.StringWriter; -import java.util.ArrayList; import java.util.List; import org.junit.Before; import org.junit.Test; @@ -77,9 +76,8 @@ public class WifiNotificationControllerTest { @Mock private WifiNotificationHelper mWifiNotificationHelper; @Mock private SynchronousNetworkRecommendationProvider mNetworkRecommendationProvider; @Mock private NetworkInfo mNetworkInfo; - @Mock private RecommendationResult mRecommendationResult; @Mock private RoboCompatUtil mRoboCompatUtil; - @Captor private ArgumentCaptor<List<ScanResult>> mScanResultCaptor; + @Captor private ArgumentCaptor<RecommendationRequest> mRecommendationRequestCaptor; private ContentResolver mContentResolver; private Handler mHandler; private WifiNotificationController mWifiNotificationController; @@ -162,8 +160,7 @@ public class WifiNotificationControllerTest { createFakeBitmap(); when(mNetworkRecommendationProvider.requestRecommendation(any(RecommendationRequest.class))) - .thenReturn(mRecommendationResult); - when(mRecommendationResult.getWifiConfiguration()).thenReturn(createFakeConfig()); + .thenReturn(RecommendationResult.createConnectRecommendation(createFakeConfig())); // The notification should not be displayed after only two scan results. mBroadcastIntentTestHelper.sendScanResultsAvailable(); @@ -204,8 +201,7 @@ public class WifiNotificationControllerTest { // Recommendation result with no WifiConfiguration returned. when(mNetworkRecommendationProvider.requestRecommendation(any(RecommendationRequest.class))) - .thenReturn(mRecommendationResult); - when(mRecommendationResult.getWifiConfiguration()).thenReturn(null); + .thenReturn(RecommendationResult.createDoNotConnectRecommendation()); mBroadcastIntentTestHelper.sendScanResultsAvailable(); mBroadcastIntentTestHelper.sendScanResultsAvailable(); @@ -230,8 +226,7 @@ public class WifiNotificationControllerTest { createFakeBitmap(); when(mNetworkRecommendationProvider.requestRecommendation(any(RecommendationRequest.class))) - .thenReturn(mRecommendationResult); - when(mRecommendationResult.getWifiConfiguration()).thenReturn(createFakeConfig()); + .thenReturn(RecommendationResult.createConnectRecommendation(createFakeConfig())); mBroadcastIntentTestHelper.sendScanResultsAvailable(); mBroadcastIntentTestHelper.sendScanResultsAvailable(); @@ -277,8 +272,7 @@ public class WifiNotificationControllerTest { createFakeBitmap(); when(mNetworkRecommendationProvider.requestRecommendation(any(RecommendationRequest.class))) - .thenReturn(mRecommendationResult); - when(mRecommendationResult.getWifiConfiguration()).thenReturn(createFakeConfig()); + .thenReturn(RecommendationResult.createConnectRecommendation(createFakeConfig())); mBroadcastIntentTestHelper.sendScanResultsAvailable(); mBroadcastIntentTestHelper.sendScanResultsAvailable(); @@ -321,8 +315,7 @@ public class WifiNotificationControllerTest { createFakeBitmap(); when(mNetworkRecommendationProvider.requestRecommendation(any(RecommendationRequest.class))) - .thenReturn(mRecommendationResult); - when(mRecommendationResult.getWifiConfiguration()).thenReturn(createFakeConfig()); + .thenReturn(RecommendationResult.createConnectRecommendation(createFakeConfig())); mBroadcastIntentTestHelper.sendScanResultsAvailable(); mBroadcastIntentTestHelper.sendScanResultsAvailable(); @@ -338,6 +331,62 @@ public class WifiNotificationControllerTest { ShadowApplication.getInstance().sendBroadcast(intent); } + /** Verifies the flow where "Settings" button on notification is clicked. */ + @Test + public void verifyNotificationsFlowOnClickSettingsFromMainNotification() throws Exception { + when(mWifiManager.getWifiState()).thenReturn(WifiManager.WIFI_STATE_ENABLED); + + mBroadcastIntentTestHelper.sendWifiStateChanged(); + when(mNetworkInfo.getDetailedState()).thenReturn(DetailedState.DISCONNECTED); + mBroadcastIntentTestHelper.sendNetworkStateChanged(mNetworkInfo); + setOpenAccessPoints(); + createFakeBitmap(); + + when(mNetworkRecommendationProvider.requestRecommendation(any(RecommendationRequest.class))) + .thenReturn(RecommendationResult.createConnectRecommendation(createFakeConfig())); + + mBroadcastIntentTestHelper.sendScanResultsAvailable(); + mBroadcastIntentTestHelper.sendScanResultsAvailable(); + mBroadcastIntentTestHelper.sendScanResultsAvailable(); + + // Show main notification + verify(mWifiNotificationHelper) + .createMainNotification(any(WifiConfiguration.class), any(Bitmap.class)); + verify(mNotificationManager).notify(anyString(), anyInt(), any(Notification.class)); + + // Send click settings intent + Intent intent = new Intent(WifiNotificationController.ACTION_PICK_WIFI_NETWORK); + ShadowApplication.getInstance().sendBroadcast(intent); + } + + /** Verifies the flow when notification is reset on captive portal check. */ + @Test + public void verifyNotificationsFlowResetNotificationOnCaptivePortalCheck() throws Exception { + when(mWifiManager.getWifiState()).thenReturn(WifiManager.WIFI_STATE_ENABLED); + + mBroadcastIntentTestHelper.sendWifiStateChanged(); + when(mNetworkInfo.getDetailedState()).thenReturn(DetailedState.DISCONNECTED); + mBroadcastIntentTestHelper.sendNetworkStateChanged(mNetworkInfo); + setOpenAccessPoints(); + createFakeBitmap(); + + when(mNetworkRecommendationProvider.requestRecommendation(any(RecommendationRequest.class))) + .thenReturn(RecommendationResult.createConnectRecommendation(createFakeConfig())); + + mBroadcastIntentTestHelper.sendScanResultsAvailable(); + mBroadcastIntentTestHelper.sendScanResultsAvailable(); + mBroadcastIntentTestHelper.sendScanResultsAvailable(); + + // Show main notification + verify(mWifiNotificationHelper) + .createMainNotification(any(WifiConfiguration.class), any(Bitmap.class)); + verify(mNotificationManager).notify(anyString(), anyInt(), any(Notification.class)); + + when(mNetworkInfo.getDetailedState()).thenReturn(DetailedState.CAPTIVE_PORTAL_CHECK); + mBroadcastIntentTestHelper.sendNetworkStateChanged(mNetworkInfo); + verify(mNotificationManager).cancel(anyString(), anyInt()); + } + /** Verifies saved networks are skipped when getting network recommendations */ @Test public void verifyNotificationsFlowSkipSavedNetworks() throws Exception { @@ -356,9 +405,11 @@ public class WifiNotificationControllerTest { when(mWifiManager.getConfiguredNetworks()) .thenReturn(Lists.newArrayList(createFakeConfig())); mBroadcastIntentTestHelper.sendScanResultsAvailable(); - verify(mRoboCompatUtil).createRecommendationRequest(mScanResultCaptor.capture()); - assertEquals(new ArrayList<>(), mScanResultCaptor.getValue()); + verify(mNetworkRecommendationProvider) + .requestRecommendation(mRecommendationRequestCaptor.capture()); + + assertEquals(0, mRecommendationRequestCaptor.getValue().getScanResults().length); } /** Test dump() does not crash. */ diff --git a/src/com/android/networkrecommendation/notify/WifiNotificationController.java b/src/com/android/networkrecommendation/notify/WifiNotificationController.java index 28fafc2..459b547 100644 --- a/src/com/android/networkrecommendation/notify/WifiNotificationController.java +++ b/src/com/android/networkrecommendation/notify/WifiNotificationController.java @@ -15,6 +15,8 @@ */ package com.android.networkrecommendation.notify; +import static com.android.networkrecommendation.Constants.TAG; + import android.app.Notification; import android.app.NotificationManager; import android.content.BroadcastReceiver; @@ -37,6 +39,7 @@ import android.support.annotation.IntDef; import android.support.annotation.Nullable; import com.android.networkrecommendation.R; import com.android.networkrecommendation.SynchronousNetworkRecommendationProvider; +import com.android.networkrecommendation.util.Blog; import com.android.networkrecommendation.util.RoboCompatUtil; import com.android.networkrecommendation.util.ScanResultUtil; import java.io.FileDescriptor; @@ -196,6 +199,7 @@ public class WifiNotificationController { filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); filter.addAction(ACTION_CONNECT_TO_RECOMMENDED_NETWORK); filter.addAction(ACTION_NOTIFICATION_DELETED); + filter.addAction(ACTION_PICK_WIFI_NETWORK); mContext.registerReceiver( mBroadcastReceiver, filter, null /* broadcastPermission */, mHandler); @@ -215,48 +219,57 @@ public class WifiNotificationController { new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) { - mWifiState = mWifiManager.getWifiState(); - resetNotification(); - } else if (intent.getAction() - .equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { - mNetworkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); - NetworkInfo.DetailedState detailedState = mNetworkInfo.getDetailedState(); - if (detailedState != NetworkInfo.DetailedState.SCANNING - && detailedState != mDetailedState) { - mDetailedState = detailedState; - switch (mDetailedState) { - case CONNECTED: - updateNotificationOnConnect(); - break; - case DISCONNECTED: - case CAPTIVE_PORTAL_CHECK: - resetNotification(); - break; - - // TODO: figure out if these are failure cases when connecting - case IDLE: - case SCANNING: - case CONNECTING: - case DISCONNECTING: - case AUTHENTICATING: - case OBTAINING_IPADDR: - case SUSPENDED: - case FAILED: - case BLOCKED: - case VERIFYING_POOR_LINK: - break; + try { + if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) { + mWifiState = mWifiManager.getWifiState(); + resetNotification(); + } else if (intent.getAction() + .equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { + mNetworkInfo = + intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); + NetworkInfo.DetailedState detailedState = + mNetworkInfo.getDetailedState(); + if (detailedState != NetworkInfo.DetailedState.SCANNING + && detailedState != mDetailedState) { + mDetailedState = detailedState; + switch (mDetailedState) { + case CONNECTED: + updateNotificationOnConnect(); + break; + case DISCONNECTED: + case CAPTIVE_PORTAL_CHECK: + resetNotification(); + break; + + // TODO: figure out if these are failure cases when connecting + case IDLE: + case SCANNING: + case CONNECTING: + case DISCONNECTING: + case AUTHENTICATING: + case OBTAINING_IPADDR: + case SUSPENDED: + case FAILED: + case BLOCKED: + case VERIFYING_POOR_LINK: + break; + } } + } else if (intent.getAction() + .equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) { + checkAndSetNotification(mNetworkInfo, mWifiManager.getScanResults()); + } else if (intent.getAction() + .equals(ACTION_CONNECT_TO_RECOMMENDED_NETWORK)) { + connectToRecommendedNetwork(); + } else if (intent.getAction().equals(ACTION_NOTIFICATION_DELETED)) { + handleNotificationDeleted(); + } else if (intent.getAction().equals(ACTION_PICK_WIFI_NETWORK)) { + openWifiPicker(); } - } else if (intent.getAction() - .equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) { - checkAndSetNotification(mNetworkInfo, mWifiManager.getScanResults()); - } else if (intent.getAction().equals(ACTION_CONNECT_TO_RECOMMENDED_NETWORK)) { - connectToRecommendedNetwork(); - } else if (intent.getAction().equals(ACTION_NOTIFICATION_DELETED)) { - handleNotificationDeleted(); - } else if (intent.getAction().equals(ACTION_PICK_WIFI_NETWORK)) { - openWifiPicker(); + } catch (RuntimeException re) { + // TODO(b/35044022) Remove try/catch after a couple of releases when we are confident + // this is not going to throw. + Blog.e(TAG, re, "RuntimeException in broadcast receiver."); } } }; @@ -340,7 +353,9 @@ public class WifiNotificationController { } RecommendationRequest request = - RoboCompatUtil.getInstance().createRecommendationRequest(openNetworks); + new RecommendationRequest.Builder() + .setScanResults(openNetworks.toArray(new ScanResult[openNetworks.size()])) + .build(); return mNetworkRecommendationProvider.requestRecommendation(request); } diff --git a/src/com/android/networkrecommendation/notify/WifiNotificationHelper.java b/src/com/android/networkrecommendation/notify/WifiNotificationHelper.java index d50678d..564bea8 100644 --- a/src/com/android/networkrecommendation/notify/WifiNotificationHelper.java +++ b/src/com/android/networkrecommendation/notify/WifiNotificationHelper.java @@ -27,7 +27,6 @@ import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.Color; import android.graphics.drawable.Drawable; -import android.net.NetworkBadging; import android.net.NetworkKey; import android.net.ScoredNetwork; import android.net.wifi.ScanResult; @@ -218,12 +217,12 @@ public class WifiNotificationHelper { } private Bitmap getBadgedWifiBitmap(int badgeEnum, int rssi) { - if (badgeEnum == NetworkBadging.BADGING_NONE) { + if (badgeEnum == ScoredNetwork.BADGING_NONE) { return null; } if (Settings.Global.getInt(mContext.getContentResolver(), NETWORK_SCORING_UI_ENABLED, 0) == 0) { - badgeEnum = NetworkBadging.BADGING_NONE; + badgeEnum = ScoredNetwork.BADGING_NONE; } int signalLevel = WifiManager.calculateSignalLevel(rssi, 5); Drawable drawable = diff --git a/src/com/android/networkrecommendation/util/RoboCompatUtil.java b/src/com/android/networkrecommendation/util/RoboCompatUtil.java index 686c83a..06b48fe 100644 --- a/src/com/android/networkrecommendation/util/RoboCompatUtil.java +++ b/src/com/android/networkrecommendation/util/RoboCompatUtil.java @@ -18,15 +18,12 @@ package com.android.networkrecommendation.util; import android.content.res.Resources.Theme; import android.graphics.drawable.Drawable; import android.net.NetworkBadging; -import android.net.RecommendationRequest; import android.net.RssiCurve; import android.net.ScoredNetwork; -import android.net.wifi.ScanResult; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiManager; import android.os.Bundle; import android.support.annotation.VisibleForTesting; -import java.util.List; /** * This class provides access to @SystemApi methods that were added in Android O and not yet @@ -51,14 +48,6 @@ public class RoboCompatUtil { mRoboCompatUtil = roboCompatUtil; } - /** Wraps creating a RecommendationResult.Builder. */ - @SuppressWarnings("unchecked") - public RecommendationRequest createRecommendationRequest(List<ScanResult> scanResults) { - return new RecommendationRequest.Builder() - .setScanResults(scanResults.toArray(new ScanResult[scanResults.size()])) - .build(); - } - /** Wraps WifiManager.connect. */ public void connectToWifi(WifiManager wifiManager, WifiConfiguration wifiConfiguration) { wifiManager.connect(wifiConfiguration, null /* actionListener */); |