diff options
| author | 2017-01-10 13:08:32 -0800 | |
|---|---|---|
| committer | 2017-01-10 16:41:42 -0800 | |
| commit | d7670f6d7c2fd0e0c6bf204613fbb33f68267679 (patch) | |
| tree | 9f22985353423cc6499f6fc56758d6141703425f | |
| parent | 9b442faa9193173753ea2669009bb5c2bed29079 (diff) | |
Move getActiveScorerPackage() to the score service.
Implemented getActiveScorerPackage() in the NetworkScoreService to
make it more efficient (no need to query PM).
Test: runtest frameworks-services -c com.android.server.NetworkScoreServiceTest
Bug: 33781558
Change-Id: Iee7610e4982b44aefa1ef8b6c208292b8f9adcf8
Merged-In: I2144351c2c09cad30f80399069364f3572e38445
4 files changed, 44 insertions, 4 deletions
diff --git a/core/java/android/net/INetworkScoreService.aidl b/core/java/android/net/INetworkScoreService.aidl index b097f12d460e..9573953e6e13 100644 --- a/core/java/android/net/INetworkScoreService.aidl +++ b/core/java/android/net/INetworkScoreService.aidl @@ -109,4 +109,12 @@ interface INetworkScoreService * @hide */ boolean isCallerActiveScorer(int callingUid); + + /** + * Obtain the package name of the current active network scorer. + * + * @return the full package name of the current active scorer, or null if there is no active + * scorer. + */ + String getActiveScorerPackage(); } diff --git a/core/java/android/net/NetworkScoreManager.java b/core/java/android/net/NetworkScoreManager.java index af0306e35aaa..a6854dcb8eb3 100644 --- a/core/java/android/net/NetworkScoreManager.java +++ b/core/java/android/net/NetworkScoreManager.java @@ -170,11 +170,11 @@ public class NetworkScoreManager { * scorer. */ public String getActiveScorerPackage() { - NetworkScorerAppData app = new NetworkScorerAppManager(mContext).getActiveScorer(); - if (app == null) { - return null; + try { + return mService.getActiveScorerPackage(); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); } - return app.packageName; } /** diff --git a/services/core/java/com/android/server/NetworkScoreService.java b/services/core/java/com/android/server/NetworkScoreService.java index 2f05d0f45399..e23844c3e633 100644 --- a/services/core/java/com/android/server/NetworkScoreService.java +++ b/services/core/java/com/android/server/NetworkScoreService.java @@ -432,6 +432,22 @@ public class NetworkScoreService extends INetworkScoreService.Stub { } } + /** + * Obtain the package name of the current active network scorer. + * + * @return the full package name of the current active scorer, or null if there is no active + * scorer. + */ + @Override + public String getActiveScorerPackage() { + synchronized (mServiceConnectionLock) { + if (mServiceConnection != null) { + return mServiceConnection.mComponentName.getPackageName(); + } + } + return null; + } + @Override public void disableScoring() { // Only the active scorer or the system should be allowed to disable scoring. diff --git a/services/tests/servicestests/src/com/android/server/NetworkScoreServiceTest.java b/services/tests/servicestests/src/com/android/server/NetworkScoreServiceTest.java index 9bb7bd1c5fd6..1189dae7b21c 100644 --- a/services/tests/servicestests/src/com/android/server/NetworkScoreServiceTest.java +++ b/services/tests/servicestests/src/com/android/server/NetworkScoreServiceTest.java @@ -23,6 +23,7 @@ import static android.net.NetworkScoreManager.CACHE_FILTER_NONE; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertNull; import static junit.framework.Assert.assertTrue; import static junit.framework.Assert.fail; @@ -470,6 +471,21 @@ public class NetworkScoreServiceTest { assertTrue(mNetworkScoreService.isCallerActiveScorer(Binder.getCallingUid())); } + @Test + public void testGetActiveScorerPackage_notActive() throws Exception { + mNetworkScoreService.systemRunning(); + + assertNull(mNetworkScoreService.getActiveScorerPackage()); + } + + @Test + public void testGetActiveScorerPackage_active() throws Exception { + when(mNetworkScorerAppManager.getActiveScorer()).thenReturn(NEW_SCORER); + mNetworkScoreService.systemRunning(); + + assertEquals(NEW_SCORER.packageName, mNetworkScoreService.getActiveScorerPackage()); + } + // "injects" the mock INetworkRecommendationProvider into the NetworkScoreService. private void injectProvider() { final ComponentName componentName = new ComponentName(NEW_SCORER.packageName, |