Loading services/core/java/com/android/server/NetworkScoreService.java +114 −81 Original line number Diff line number Diff line Loading @@ -40,7 +40,7 @@ import android.net.RecommendationRequest; import android.net.RecommendationResult; import android.net.ScoredNetwork; import android.net.Uri; import android.net.wifi.WifiConfiguration; import android.os.Binder; import android.os.Bundle; import android.os.IBinder; import android.os.IRemoteCallback; Loading Loading @@ -319,6 +319,8 @@ public class NetworkScoreService extends INetworkScoreService.Stub { " is not the active scorer."); } final long token = Binder.clearCallingIdentity(); try { // Separate networks by type. Map<Integer, List<ScoredNetwork>> networksByType = new ArrayMap<>(); for (ScoredNetwork network : networks) { Loading @@ -336,11 +338,13 @@ public class NetworkScoreService extends INetworkScoreService.Stub { final boolean isEmpty; synchronized (mScoreCaches) { callbackList = mScoreCaches.get(entry.getKey()); isEmpty = callbackList == null || callbackList.getRegisteredCallbackCount() == 0; isEmpty = callbackList == null || callbackList.getRegisteredCallbackCount() == 0; } if (isEmpty) { if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "No scorer registered for type " + entry.getKey() + ", discarding"); Log.v(TAG, "No scorer registered for type " + entry.getKey() + ", discarding"); } continue; } Loading @@ -360,6 +364,9 @@ public class NetworkScoreService extends INetworkScoreService.Stub { } return true; } finally { Binder.restoreCallingIdentity(token); } } @Override Loading @@ -369,8 +376,13 @@ public class NetworkScoreService extends INetworkScoreService.Stub { if (mNetworkScorerAppManager.isCallerActiveScorer(getCallingUid()) || mContext.checkCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED) == PackageManager.PERMISSION_GRANTED) { final long token = Binder.clearCallingIdentity(); try { clearInternal(); return true; } finally { Binder.restoreCallingIdentity(token); } } else { throw new SecurityException( "Caller is neither the active scorer nor the scorer manager."); Loading Loading @@ -428,6 +440,8 @@ public class NetworkScoreService extends INetworkScoreService.Stub { INetworkScoreCache scoreCache, int filterType) { mContext.enforceCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED, TAG); final long token = Binder.clearCallingIdentity(); try { synchronized (mScoreCaches) { RemoteCallbackList<INetworkScoreCache> callbackList = mScoreCaches.get(networkType); if (callbackList == null) { Loading @@ -443,27 +457,38 @@ public class NetworkScoreService extends INetworkScoreService.Stub { } } } } finally { Binder.restoreCallingIdentity(token); } } @Override public void unregisterNetworkScoreCache(int networkType, INetworkScoreCache scoreCache) { mContext.enforceCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED, TAG); final long token = Binder.clearCallingIdentity(); try { synchronized (mScoreCaches) { RemoteCallbackList<INetworkScoreCache> callbackList = mScoreCaches.get(networkType); if (callbackList == null || !callbackList.unregister(scoreCache)) { if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "Unable to unregister NetworkScoreCache for type " + networkType); Log.v(TAG, "Unable to unregister NetworkScoreCache for type " + networkType); } } else if (callbackList.getRegisteredCallbackCount() == 0) { mScoreCaches.remove(networkType); } } } finally { Binder.restoreCallingIdentity(token); } } @Override public RecommendationResult requestRecommendation(RecommendationRequest request) { mContext.enforceCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED, TAG); throwIfCalledOnMainThread(); final long token = Binder.clearCallingIdentity(); try { final INetworkRecommendationProvider provider = getRecommendationProvider(); if (provider != null) { try { Loading @@ -483,17 +508,22 @@ public class NetworkScoreService extends INetworkScoreService.Stub { request.getCurrentSelectedConfig()); } return RecommendationResult.createDoNotConnectRecommendation(); } finally { Binder.restoreCallingIdentity(token); } } @Override public boolean requestScores(NetworkKey[] networks) { mContext.enforceCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED, TAG); final long token = Binder.clearCallingIdentity(); try { final INetworkRecommendationProvider provider = getRecommendationProvider(); if (provider != null) { try { provider.requestScores(networks); // TODO(jjoslin): 12/15/16 - Consider pushing null scores into the cache to prevent // repeated requests for the same scores. // TODO(jjoslin): 12/15/16 - Consider pushing null scores into the cache to // prevent repeated requests for the same scores. return true; } catch (RemoteException e) { Log.w(TAG, "Failed to request scores.", e); Loading @@ -501,6 +531,9 @@ public class NetworkScoreService extends INetworkScoreService.Stub { } } return false; } finally { Binder.restoreCallingIdentity(token); } } @Override Loading services/tests/servicestests/src/com/android/server/NetworkScoreServiceTest.java +2 −0 Original line number Diff line number Diff line Loading @@ -122,6 +122,8 @@ public class NetworkScoreServiceTest { when(mContext.getResources()).thenReturn(mResources); mNetworkScoreService = new NetworkScoreService(mContext, mNetworkScorerAppManager); WifiConfiguration configuration = new WifiConfiguration(); configuration.SSID = "NetworkScoreServiceTest_SSID"; configuration.BSSID = "NetworkScoreServiceTest_BSSID"; mRecommendationRequest = new RecommendationRequest.Builder() .setCurrentRecommendedWifiConfig(configuration).build(); } Loading Loading
services/core/java/com/android/server/NetworkScoreService.java +114 −81 Original line number Diff line number Diff line Loading @@ -40,7 +40,7 @@ import android.net.RecommendationRequest; import android.net.RecommendationResult; import android.net.ScoredNetwork; import android.net.Uri; import android.net.wifi.WifiConfiguration; import android.os.Binder; import android.os.Bundle; import android.os.IBinder; import android.os.IRemoteCallback; Loading Loading @@ -319,6 +319,8 @@ public class NetworkScoreService extends INetworkScoreService.Stub { " is not the active scorer."); } final long token = Binder.clearCallingIdentity(); try { // Separate networks by type. Map<Integer, List<ScoredNetwork>> networksByType = new ArrayMap<>(); for (ScoredNetwork network : networks) { Loading @@ -336,11 +338,13 @@ public class NetworkScoreService extends INetworkScoreService.Stub { final boolean isEmpty; synchronized (mScoreCaches) { callbackList = mScoreCaches.get(entry.getKey()); isEmpty = callbackList == null || callbackList.getRegisteredCallbackCount() == 0; isEmpty = callbackList == null || callbackList.getRegisteredCallbackCount() == 0; } if (isEmpty) { if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "No scorer registered for type " + entry.getKey() + ", discarding"); Log.v(TAG, "No scorer registered for type " + entry.getKey() + ", discarding"); } continue; } Loading @@ -360,6 +364,9 @@ public class NetworkScoreService extends INetworkScoreService.Stub { } return true; } finally { Binder.restoreCallingIdentity(token); } } @Override Loading @@ -369,8 +376,13 @@ public class NetworkScoreService extends INetworkScoreService.Stub { if (mNetworkScorerAppManager.isCallerActiveScorer(getCallingUid()) || mContext.checkCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED) == PackageManager.PERMISSION_GRANTED) { final long token = Binder.clearCallingIdentity(); try { clearInternal(); return true; } finally { Binder.restoreCallingIdentity(token); } } else { throw new SecurityException( "Caller is neither the active scorer nor the scorer manager."); Loading Loading @@ -428,6 +440,8 @@ public class NetworkScoreService extends INetworkScoreService.Stub { INetworkScoreCache scoreCache, int filterType) { mContext.enforceCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED, TAG); final long token = Binder.clearCallingIdentity(); try { synchronized (mScoreCaches) { RemoteCallbackList<INetworkScoreCache> callbackList = mScoreCaches.get(networkType); if (callbackList == null) { Loading @@ -443,27 +457,38 @@ public class NetworkScoreService extends INetworkScoreService.Stub { } } } } finally { Binder.restoreCallingIdentity(token); } } @Override public void unregisterNetworkScoreCache(int networkType, INetworkScoreCache scoreCache) { mContext.enforceCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED, TAG); final long token = Binder.clearCallingIdentity(); try { synchronized (mScoreCaches) { RemoteCallbackList<INetworkScoreCache> callbackList = mScoreCaches.get(networkType); if (callbackList == null || !callbackList.unregister(scoreCache)) { if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "Unable to unregister NetworkScoreCache for type " + networkType); Log.v(TAG, "Unable to unregister NetworkScoreCache for type " + networkType); } } else if (callbackList.getRegisteredCallbackCount() == 0) { mScoreCaches.remove(networkType); } } } finally { Binder.restoreCallingIdentity(token); } } @Override public RecommendationResult requestRecommendation(RecommendationRequest request) { mContext.enforceCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED, TAG); throwIfCalledOnMainThread(); final long token = Binder.clearCallingIdentity(); try { final INetworkRecommendationProvider provider = getRecommendationProvider(); if (provider != null) { try { Loading @@ -483,17 +508,22 @@ public class NetworkScoreService extends INetworkScoreService.Stub { request.getCurrentSelectedConfig()); } return RecommendationResult.createDoNotConnectRecommendation(); } finally { Binder.restoreCallingIdentity(token); } } @Override public boolean requestScores(NetworkKey[] networks) { mContext.enforceCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED, TAG); final long token = Binder.clearCallingIdentity(); try { final INetworkRecommendationProvider provider = getRecommendationProvider(); if (provider != null) { try { provider.requestScores(networks); // TODO(jjoslin): 12/15/16 - Consider pushing null scores into the cache to prevent // repeated requests for the same scores. // TODO(jjoslin): 12/15/16 - Consider pushing null scores into the cache to // prevent repeated requests for the same scores. return true; } catch (RemoteException e) { Log.w(TAG, "Failed to request scores.", e); Loading @@ -501,6 +531,9 @@ public class NetworkScoreService extends INetworkScoreService.Stub { } } return false; } finally { Binder.restoreCallingIdentity(token); } } @Override Loading
services/tests/servicestests/src/com/android/server/NetworkScoreServiceTest.java +2 −0 Original line number Diff line number Diff line Loading @@ -122,6 +122,8 @@ public class NetworkScoreServiceTest { when(mContext.getResources()).thenReturn(mResources); mNetworkScoreService = new NetworkScoreService(mContext, mNetworkScorerAppManager); WifiConfiguration configuration = new WifiConfiguration(); configuration.SSID = "NetworkScoreServiceTest_SSID"; configuration.BSSID = "NetworkScoreServiceTest_BSSID"; mRecommendationRequest = new RecommendationRequest.Builder() .setCurrentRecommendedWifiConfig(configuration).build(); } Loading