diff options
author | Stephen Chen <stewchen@google.com> | 2017-01-12 09:41:44 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2017-01-12 09:41:44 +0000 |
commit | f83c2eeb337a5c145b8bdae1e993ccca5c7c3718 (patch) | |
tree | 98581aeb0004da3cd9f3fc1337d32bf91727ffb2 | |
parent | 9973bc5b31cbc90bb03336c5dd0da6516ae1c54b (diff) | |
parent | 534074986a68b197f3c4f3fb881102f1fb99f3d0 (diff) | |
download | NetworkRecommendation-f83c2eeb337a5c145b8bdae1e993ccca5c7c3718.tar.gz |
Request recommendations for WifiNotificationController via provider. am: 69631bf8a6
am: 534074986a
Change-Id: Ic6c57b2ec060358b608b87936f0524be0b19cb43
-rw-r--r-- | src/com/android/networkrecommendation/DefaultNetworkRecommendationProvider.java | 15 | ||||
-rw-r--r-- | src/com/android/networkrecommendation/DefaultNetworkRecommendationService.java | 2 | ||||
-rw-r--r-- | src/com/android/networkrecommendation/SynchronousNetworkRecommendationProvider.java (renamed from src/com/android/networkrecommendation/CachedScoredNetworkProvider.java) | 9 | ||||
-rw-r--r-- | src/com/android/networkrecommendation/WifiNotificationController.java | 17 | ||||
-rw-r--r-- | src/com/android/networkrecommendation/WifiNotificationHelper.java | 21 | ||||
-rw-r--r-- | tests/src/com/android/networkrecommendation/WifiNotificationControllerTest.java | 42 |
6 files changed, 70 insertions, 36 deletions
diff --git a/src/com/android/networkrecommendation/DefaultNetworkRecommendationProvider.java b/src/com/android/networkrecommendation/DefaultNetworkRecommendationProvider.java index 1b58ace..1f78df2 100644 --- a/src/com/android/networkrecommendation/DefaultNetworkRecommendationProvider.java +++ b/src/com/android/networkrecommendation/DefaultNetworkRecommendationProvider.java @@ -83,7 +83,7 @@ import javax.annotation.concurrent.GuardedBy; */ @VisibleForTesting public class DefaultNetworkRecommendationProvider - extends NetworkRecommendationProvider implements CachedScoredNetworkProvider { + extends NetworkRecommendationProvider implements SynchronousNetworkRecommendationProvider { static final String TAG = "DefaultNetRecProvider"; static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE); @@ -139,10 +139,19 @@ public class DefaultNetworkRecommendationProvider mStorage = storage; } - /** Recommend the wireless network with the highest RSSI. */ + /** + * Recommend the wireless network with the highest RSSI and run + * {@link ResultCallback#onResult(RecommendationResult)}. + */ @Override public void onRequestRecommendation(RecommendationRequest request, ResultCallback callback) { + callback.onResult(requestRecommendation(request)); + } + + @Override + /** Recommend the wireless network with the highest RSSI. */ + public RecommendationResult requestRecommendation(RecommendationRequest request) { ScanResult recommendedScanResult = null; int recommendedScore = Integer.MIN_VALUE; @@ -210,7 +219,7 @@ public class DefaultNetworkRecommendationProvider mRecommendationCounter++; if (DEBUG) Log.d(TAG, "Recommending network: " + configToString(mLastRecommended)); } - callback.onResult(recommendationResult); + return recommendationResult; } /** Score networks based on a few properties ... */ diff --git a/src/com/android/networkrecommendation/DefaultNetworkRecommendationService.java b/src/com/android/networkrecommendation/DefaultNetworkRecommendationService.java index 7b6585a..b35f8d8 100644 --- a/src/com/android/networkrecommendation/DefaultNetworkRecommendationService.java +++ b/src/com/android/networkrecommendation/DefaultNetworkRecommendationService.java @@ -48,7 +48,7 @@ public class DefaultNetworkRecommendationService extends Service { mProvider = new DefaultNetworkRecommendationProvider(mHandler, networkScoreManager, new DefaultNetworkRecommendationProvider.ScoreStorage()); mWifiNotificationController = new WifiNotificationController( - this, getContentResolver(), mHandler, networkScoreManager, + this, getContentResolver(), mHandler, mProvider, getSystemService(WifiManager.class), getSystemService(NotificationManager.class), new WifiNotificationHelper(this, mProvider)); mWifiWakeupController = new WifiWakeupController(this, getContentResolver(), diff --git a/src/com/android/networkrecommendation/CachedScoredNetworkProvider.java b/src/com/android/networkrecommendation/SynchronousNetworkRecommendationProvider.java index 154fba4..44a7e0d 100644 --- a/src/com/android/networkrecommendation/CachedScoredNetworkProvider.java +++ b/src/com/android/networkrecommendation/SynchronousNetworkRecommendationProvider.java @@ -17,13 +17,18 @@ package com.android.networkrecommendation; import android.net.NetworkKey; +import android.net.RecommendationRequest; +import android.net.RecommendationResult; import android.net.ScoredNetwork; /** * Provider to return {@link ScoredNetwork} from cached scores in NetworkRecommendationProvider. */ -interface CachedScoredNetworkProvider { +interface SynchronousNetworkRecommendationProvider { - /** Returns a {@link ScoredNetwork} if present in the cache. Otherwise, return null */ + /** Returns a {@link ScoredNetwork} if present in the cache. Otherwise, return null. */ ScoredNetwork getCachedScoredNetwork(NetworkKey networkKey); + + /** Returns a {@link RecommendationResult} using the internal NetworkRecommendationProvider. */ + RecommendationResult requestRecommendation(RecommendationRequest request); } diff --git a/src/com/android/networkrecommendation/WifiNotificationController.java b/src/com/android/networkrecommendation/WifiNotificationController.java index 11bb8ac..76302a5 100644 --- a/src/com/android/networkrecommendation/WifiNotificationController.java +++ b/src/com/android/networkrecommendation/WifiNotificationController.java @@ -134,10 +134,12 @@ public class WifiNotificationController { showFailedToConnectNotification(); }; + private static final String TAG = "WifiNotification"; + private final Context mContext; private final Handler mHandler; private final ContentResolver mContentResolver; - private final NetworkScoreManager mScoreManager; + private final SynchronousNetworkRecommendationProvider mNetworkRecommendationProvider; private final WifiManager mWifiManager; private final NotificationManager mNotificationManager; private final WifiNotificationHelper mWifiNotificationHelper; @@ -146,11 +148,12 @@ public class WifiNotificationController { private volatile int mWifiState; WifiNotificationController(Context context, ContentResolver contentResolver, Handler handler, - NetworkScoreManager networkScoreManager, WifiManager wifiManager, - NotificationManager notificationManager, WifiNotificationHelper helper) { + SynchronousNetworkRecommendationProvider networkRecommendationProvider, + WifiManager wifiManager, NotificationManager notificationManager, + WifiNotificationHelper helper) { mContext = context; mContentResolver = contentResolver; - mScoreManager = networkScoreManager; + mNetworkRecommendationProvider = networkRecommendationProvider; mWifiManager = wifiManager; mNotificationManager = notificationManager; mHandler = handler; @@ -250,12 +253,14 @@ public class WifiNotificationController { // don't bother doing any of the following if (!mNotificationEnabled) return; if (mWifiState != WifiManager.WIFI_STATE_ENABLED) return; + if (scanResults == null || scanResults.isEmpty()) return; NetworkInfo.State state = NetworkInfo.State.DISCONNECTED; if (networkInfo != null) { state = networkInfo.getState(); } + if (state == NetworkInfo.State.DISCONNECTED || state == NetworkInfo.State.UNKNOWN) { RecommendationResult result = getOpenNetworkRecommendation(scanResults); @@ -285,6 +290,7 @@ public class WifiNotificationController { /** * Uses {@link NetworkScoreManager} to choose a qualified network out of the list of + * {@link ScanResult}s. * * @return returns the best qualified open networks, if any. */ @@ -304,7 +310,8 @@ public class WifiNotificationController { RecommendationRequest request = new RecommendationRequest.Builder() .setScanResults(openNetworks.toArray(new ScanResult[openNetworks.size()])) .build(); - return mScoreManager.requestRecommendation(request); + + return mNetworkRecommendationProvider.requestRecommendation(request); } /** diff --git a/src/com/android/networkrecommendation/WifiNotificationHelper.java b/src/com/android/networkrecommendation/WifiNotificationHelper.java index 4408804..0bf4cb8 100644 --- a/src/com/android/networkrecommendation/WifiNotificationHelper.java +++ b/src/com/android/networkrecommendation/WifiNotificationHelper.java @@ -47,10 +47,10 @@ import java.util.List; */ public class WifiNotificationHelper { private final Context mContext; - private final CachedScoredNetworkProvider mCachedScoredNetworkProvider; + private final SynchronousNetworkRecommendationProvider mCachedScoredNetworkProvider; - WifiNotificationHelper( - Context context, CachedScoredNetworkProvider cachedScoredNetworkProvider) { + public WifiNotificationHelper( + Context context, SynchronousNetworkRecommendationProvider cachedScoredNetworkProvider) { mContext = context; mCachedScoredNetworkProvider = cachedScoredNetworkProvider; } @@ -60,7 +60,7 @@ public class WifiNotificationHelper { * Wi-Fi picker activity, and "Connect" prompts {@link WifiNotificationController} * to connect to the recommended network. */ - Notification createMainNotification(WifiConfiguration config, Bitmap badge) { + public Notification createMainNotification(WifiConfiguration config, Bitmap badge) { PendingIntent optionsIntent = PendingIntent.getActivity( mContext, 0, new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK), FLAG_UPDATE_CURRENT); Action optionsAction = new Action.Builder( @@ -88,7 +88,7 @@ public class WifiNotificationHelper { * Creates the notification that indicates the controller is attempting to connect * to the recommended network. */ - Notification createConnectingNotification(WifiConfiguration config, Bitmap badge) { + public Notification createConnectingNotification(WifiConfiguration config, Bitmap badge) { Action connecting = new Action.Builder( null /* icon */, mContext.getText(R.string.wifi_available_connecting), @@ -104,7 +104,7 @@ public class WifiNotificationHelper { * Creates the notification that indicates the controller successfully connected * to the recommended network. */ - Notification createConnectedNotification(WifiConfiguration config, Bitmap badge) { + public Notification createConnectedNotification(WifiConfiguration config, Bitmap badge) { Action connected = new Action.Builder( null /* icon */, mContext.getText(R.string.wifi_available_connected), @@ -119,7 +119,7 @@ public class WifiNotificationHelper { * Creates the notification that indicates the controller failed to connect to * the recommended network. */ - Notification createFailedToConnectNotification(WifiConfiguration config) { + public Notification createFailedToConnectNotification(WifiConfiguration config) { Spannable failedText = new SpannableString(mContext.getText(R.string.wifi_available_failed)); Resources resources = mContext.getResources(); @@ -172,6 +172,8 @@ public class WifiNotificationHelper { private int getWifiBadgeResourceForEnum(int badgeEnum) { switch (badgeEnum) { + case ScoredNetwork.BADGING_NONE: + return 0; case ScoredNetwork.BADGING_SD: return R.drawable.ic_signal_wifi_badged_sd; case ScoredNetwork.BADGING_HD: @@ -187,7 +189,7 @@ public class WifiNotificationHelper { * Creates a Wi-Fi badge for the notification using matching {@link ScanResult}'s RSSI * and badging from {@link CachedScoredNetworkProvider}. */ - Bitmap createNotificationBadgeBitmap( + public Bitmap createNotificationBadgeBitmap( @NonNull WifiConfiguration config, @NonNull List<ScanResult> scanResults) { ScanResult matchingScanResult = findMatchingScanResult(scanResults, config); @@ -205,8 +207,9 @@ public class WifiNotificationHelper { } private Bitmap getBadgedWifiBitmap(int badgeEnum, int rssi) { + int signalLevel = WifiManager.calculateSignalLevel(rssi, 5); LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{ - mContext.getDrawable(WIFI_PIE_FOR_BADGING[rssi]), + mContext.getDrawable(WIFI_PIE_FOR_BADGING[signalLevel]), mContext.getDrawable(getWifiBadgeResourceForEnum(badgeEnum))}); layerDrawable.setTint(mContext.getColor(R.color.color_tint)); Resources resources = mContext.getResources(); diff --git a/tests/src/com/android/networkrecommendation/WifiNotificationControllerTest.java b/tests/src/com/android/networkrecommendation/WifiNotificationControllerTest.java index 23bee68..83fb850 100644 --- a/tests/src/com/android/networkrecommendation/WifiNotificationControllerTest.java +++ b/tests/src/com/android/networkrecommendation/WifiNotificationControllerTest.java @@ -34,7 +34,6 @@ import android.content.Intent; import android.content.IntentFilter; import android.graphics.Bitmap; import android.net.NetworkInfo; -import android.net.NetworkScoreManager; import android.net.RecommendationRequest; import android.net.RecommendationResult; import android.net.wifi.ScanResult; @@ -46,11 +45,12 @@ import android.os.Looper; import android.os.UserHandle; import android.provider.Settings; import android.support.test.InstrumentationRegistry; -import android.test.suitebuilder.annotation.SmallTest; +import android.support.test.runner.AndroidJUnit4; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -61,25 +61,23 @@ import java.util.List; /** * Unit tests for {@link com.android.networkrecommendation.WifiNotificationController}. */ -@SmallTest +@RunWith(AndroidJUnit4.class) public class WifiNotificationControllerTest { - public static final String TAG = "WifiScanningServiceTest"; - @Mock private Context mContext; - @Mock private NetworkScoreManager mScoreManager; @Mock private WifiManager mWifiManager; @Mock private NotificationManager mNotificationManager; @Mock private WifiNotificationHelper mWifiNotificationHelper; - @Mock private CachedScoredNetworkProvider mCachedScoredNetworkProvider; + @Mock private SynchronousNetworkRecommendationProvider mNetworkRecommendationProvider; private ContentResolver mContentResolver; private Handler mHandler; - WifiNotificationController mWifiNotificationController; + private WifiNotificationController mWifiNotificationController; + private int mNotificationsEnabledOriginalValue; /** * Internal BroadcastReceiver that WifiNotificationController uses to listen for broadcasts * this is initialized by calling startServiceAndLoadDriver */ - BroadcastReceiver mBroadcastReceiver; + private BroadcastReceiver mBroadcastReceiver; /** Initialize objects before each test run. */ @Before @@ -88,12 +86,15 @@ public class WifiNotificationControllerTest { // Needed for the NotificationEnabledSettingObserver. mContentResolver = InstrumentationRegistry.getTargetContext().getContentResolver(); + mNotificationsEnabledOriginalValue = + Settings.Global.getInt(mContentResolver, + Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 0); Settings.Global.putInt(mContentResolver, Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 1); mHandler = new Handler(Looper.getMainLooper()); mWifiNotificationController = new WifiNotificationController( - mContext, mContentResolver, mHandler, mScoreManager, + mContext, mContentResolver, mHandler, mNetworkRecommendationProvider, mWifiManager, mNotificationManager, mWifiNotificationHelper); mWifiNotificationController.start(); @@ -107,7 +108,9 @@ public class WifiNotificationControllerTest { @After public void tearDown() throws Exception { - + Settings.Global.putInt(mContentResolver, + Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, + mNotificationsEnabledOriginalValue); } private void setOpenAccessPoints(int numAccessPoints) { @@ -135,6 +138,11 @@ public class WifiNotificationControllerTest { return config; } + private void createFakeBitmap() { + when(mWifiNotificationHelper.createNotificationBadgeBitmap(any(), any())) + .thenReturn(Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888)); + } + /** Verifies that a notification is displayed (and retracted) given system events. */ @Test public void verifyNotificationDisplayedWhenNetworkRecommended() throws Exception { @@ -142,8 +150,9 @@ public class WifiNotificationControllerTest { TestUtil.sendNetworkStateChanged(mBroadcastReceiver, mContext, NetworkInfo.DetailedState.DISCONNECTED); setOpenAccessPoints(3); + createFakeBitmap(); - when(mScoreManager.requestRecommendation(any(RecommendationRequest.class))) + when(mNetworkRecommendationProvider.requestRecommendation(any(RecommendationRequest.class))) .thenReturn( RecommendationResult.createConnectRecommendation(createFakeConfig())); @@ -162,7 +171,6 @@ public class WifiNotificationControllerTest { verify(mNotificationManager, never()) .notifyAsUser(any(String.class), anyInt(), any(Notification.class), any(UserHandle.class)); - // The third scan result notification will trigger the notification. TestUtil.sendScanResultsAvailable(mBroadcastReceiver, mContext); verify(mWifiNotificationHelper).createMainNotification(any(WifiConfiguration.class), @@ -181,9 +189,10 @@ public class WifiNotificationControllerTest { TestUtil.sendNetworkStateChanged(mBroadcastReceiver, mContext, NetworkInfo.DetailedState.DISCONNECTED); setOpenAccessPoints(3); + createFakeBitmap(); // Recommendation result with no WifiConfiguration returned. - when(mScoreManager.requestRecommendation(any(RecommendationRequest.class))) + when(mNetworkRecommendationProvider.requestRecommendation(any(RecommendationRequest.class))) .thenReturn(RecommendationResult.createDoNotConnectRecommendation()); TestUtil.sendScanResultsAvailable(mBroadcastReceiver, mContext); @@ -195,7 +204,7 @@ public class WifiNotificationControllerTest { any(UserHandle.class)); // DoNotConnect Recommendation result. - when(mScoreManager.requestRecommendation(any(RecommendationRequest.class))) + when(mNetworkRecommendationProvider.requestRecommendation(any(RecommendationRequest.class))) .thenReturn(RecommendationResult.createDoNotConnectRecommendation()); TestUtil.sendScanResultsAvailable(mBroadcastReceiver, mContext); verify(mNotificationManager, never()) @@ -213,7 +222,8 @@ public class WifiNotificationControllerTest { TestUtil.sendNetworkStateChanged(mBroadcastReceiver, mContext, NetworkInfo.DetailedState.DISCONNECTED); setOpenAccessPoints(3); - when(mScoreManager.requestRecommendation(any(RecommendationRequest.class))) + createFakeBitmap(); + when(mNetworkRecommendationProvider.requestRecommendation(any(RecommendationRequest.class))) .thenReturn( RecommendationResult.createConnectRecommendation(createFakeConfig())); |