Merge changes I40f055df,I562713df

* changes:
  Stay bound to the most current active scorer.
  Clear and restore the calling ID.
This commit is contained in:
Treehugger Robot
2016-12-21 22:58:40 +00:00
committed by Gerrit Code Review
2 changed files with 126 additions and 83 deletions

View File

@@ -40,7 +40,7 @@ import android.net.RecommendationRequest;
import android.net.RecommendationResult; import android.net.RecommendationResult;
import android.net.ScoredNetwork; import android.net.ScoredNetwork;
import android.net.Uri; import android.net.Uri;
import android.net.wifi.WifiConfiguration; import android.os.Binder;
import android.os.Bundle; import android.os.Bundle;
import android.os.IBinder; import android.os.IBinder;
import android.os.IRemoteCallback; import android.os.IRemoteCallback;
@@ -159,15 +159,23 @@ public class NetworkScoreService extends INetworkScoreService.Stub {
if (DBG) Log.d(TAG, "No active scorers available."); if (DBG) Log.d(TAG, "No active scorers available.");
unbindFromScoringServiceIfNeeded(); unbindFromScoringServiceIfNeeded();
} else if (activeScorer.packageName.equals(scorerPackageName)) { } else if (activeScorer.packageName.equals(scorerPackageName)) {
// The active scoring service changed in some way.
if (DBG) { if (DBG) {
Log.d(TAG, "Possible change to the active scorer: " Log.d(TAG, "Possible change to the active scorer: "
+ activeScorer.packageName); + activeScorer.packageName);
} }
// The scoring service changed in some way.
if (forceUnbind) { if (forceUnbind) {
unbindFromScoringServiceIfNeeded(); unbindFromScoringServiceIfNeeded();
} }
bindToScoringServiceIfNeeded(activeScorer); bindToScoringServiceIfNeeded(activeScorer);
} else {
// One of the scoring apps on the device has changed and we may no longer be
// bound to the correct scoring app. The logic in bindToScoringServiceIfNeeded()
// will sort that out to leave us bound to the most recent active scorer.
if (DBG) {
Log.d(TAG, "Binding to " + activeScorer.packageName + " if needed.");
}
bindToScoringServiceIfNeeded(activeScorer);
} }
} }
} }
@@ -319,6 +327,8 @@ public class NetworkScoreService extends INetworkScoreService.Stub {
" is not the active scorer."); " is not the active scorer.");
} }
final long token = Binder.clearCallingIdentity();
try {
// Separate networks by type. // Separate networks by type.
Map<Integer, List<ScoredNetwork>> networksByType = new ArrayMap<>(); Map<Integer, List<ScoredNetwork>> networksByType = new ArrayMap<>();
for (ScoredNetwork network : networks) { for (ScoredNetwork network : networks) {
@@ -336,11 +346,13 @@ public class NetworkScoreService extends INetworkScoreService.Stub {
final boolean isEmpty; final boolean isEmpty;
synchronized (mScoreCaches) { synchronized (mScoreCaches) {
callbackList = mScoreCaches.get(entry.getKey()); callbackList = mScoreCaches.get(entry.getKey());
isEmpty = callbackList == null || callbackList.getRegisteredCallbackCount() == 0; isEmpty = callbackList == null
|| callbackList.getRegisteredCallbackCount() == 0;
} }
if (isEmpty) { if (isEmpty) {
if (Log.isLoggable(TAG, Log.VERBOSE)) { 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; continue;
} }
@@ -360,6 +372,9 @@ public class NetworkScoreService extends INetworkScoreService.Stub {
} }
return true; return true;
} finally {
Binder.restoreCallingIdentity(token);
}
} }
@Override @Override
@@ -369,8 +384,13 @@ public class NetworkScoreService extends INetworkScoreService.Stub {
if (mNetworkScorerAppManager.isCallerActiveScorer(getCallingUid()) || if (mNetworkScorerAppManager.isCallerActiveScorer(getCallingUid()) ||
mContext.checkCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED) == mContext.checkCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED) ==
PackageManager.PERMISSION_GRANTED) { PackageManager.PERMISSION_GRANTED) {
final long token = Binder.clearCallingIdentity();
try {
clearInternal(); clearInternal();
return true; return true;
} finally {
Binder.restoreCallingIdentity(token);
}
} else { } else {
throw new SecurityException( throw new SecurityException(
"Caller is neither the active scorer nor the scorer manager."); "Caller is neither the active scorer nor the scorer manager.");
@@ -428,6 +448,8 @@ public class NetworkScoreService extends INetworkScoreService.Stub {
INetworkScoreCache scoreCache, INetworkScoreCache scoreCache,
int filterType) { int filterType) {
mContext.enforceCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED, TAG); mContext.enforceCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED, TAG);
final long token = Binder.clearCallingIdentity();
try {
synchronized (mScoreCaches) { synchronized (mScoreCaches) {
RemoteCallbackList<INetworkScoreCache> callbackList = mScoreCaches.get(networkType); RemoteCallbackList<INetworkScoreCache> callbackList = mScoreCaches.get(networkType);
if (callbackList == null) { if (callbackList == null) {
@@ -443,27 +465,38 @@ public class NetworkScoreService extends INetworkScoreService.Stub {
} }
} }
} }
} finally {
Binder.restoreCallingIdentity(token);
}
} }
@Override @Override
public void unregisterNetworkScoreCache(int networkType, INetworkScoreCache scoreCache) { public void unregisterNetworkScoreCache(int networkType, INetworkScoreCache scoreCache) {
mContext.enforceCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED, TAG); mContext.enforceCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED, TAG);
final long token = Binder.clearCallingIdentity();
try {
synchronized (mScoreCaches) { synchronized (mScoreCaches) {
RemoteCallbackList<INetworkScoreCache> callbackList = mScoreCaches.get(networkType); RemoteCallbackList<INetworkScoreCache> callbackList = mScoreCaches.get(networkType);
if (callbackList == null || !callbackList.unregister(scoreCache)) { if (callbackList == null || !callbackList.unregister(scoreCache)) {
if (Log.isLoggable(TAG, Log.VERBOSE)) { 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) { } else if (callbackList.getRegisteredCallbackCount() == 0) {
mScoreCaches.remove(networkType); mScoreCaches.remove(networkType);
} }
} }
} finally {
Binder.restoreCallingIdentity(token);
}
} }
@Override @Override
public RecommendationResult requestRecommendation(RecommendationRequest request) { public RecommendationResult requestRecommendation(RecommendationRequest request) {
mContext.enforceCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED, TAG); mContext.enforceCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED, TAG);
throwIfCalledOnMainThread(); throwIfCalledOnMainThread();
final long token = Binder.clearCallingIdentity();
try {
final INetworkRecommendationProvider provider = getRecommendationProvider(); final INetworkRecommendationProvider provider = getRecommendationProvider();
if (provider != null) { if (provider != null) {
try { try {
@@ -483,17 +516,22 @@ public class NetworkScoreService extends INetworkScoreService.Stub {
request.getCurrentSelectedConfig()); request.getCurrentSelectedConfig());
} }
return RecommendationResult.createDoNotConnectRecommendation(); return RecommendationResult.createDoNotConnectRecommendation();
} finally {
Binder.restoreCallingIdentity(token);
}
} }
@Override @Override
public boolean requestScores(NetworkKey[] networks) { public boolean requestScores(NetworkKey[] networks) {
mContext.enforceCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED, TAG); mContext.enforceCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED, TAG);
final long token = Binder.clearCallingIdentity();
try {
final INetworkRecommendationProvider provider = getRecommendationProvider(); final INetworkRecommendationProvider provider = getRecommendationProvider();
if (provider != null) { if (provider != null) {
try { try {
provider.requestScores(networks); provider.requestScores(networks);
// TODO(jjoslin): 12/15/16 - Consider pushing null scores into the cache to prevent // TODO(jjoslin): 12/15/16 - Consider pushing null scores into the cache to
// repeated requests for the same scores. // prevent repeated requests for the same scores.
return true; return true;
} catch (RemoteException e) { } catch (RemoteException e) {
Log.w(TAG, "Failed to request scores.", e); Log.w(TAG, "Failed to request scores.", e);
@@ -501,6 +539,9 @@ public class NetworkScoreService extends INetworkScoreService.Stub {
} }
} }
return false; return false;
} finally {
Binder.restoreCallingIdentity(token);
}
} }
@Override @Override

View File

@@ -122,6 +122,8 @@ public class NetworkScoreServiceTest {
when(mContext.getResources()).thenReturn(mResources); when(mContext.getResources()).thenReturn(mResources);
mNetworkScoreService = new NetworkScoreService(mContext, mNetworkScorerAppManager); mNetworkScoreService = new NetworkScoreService(mContext, mNetworkScorerAppManager);
WifiConfiguration configuration = new WifiConfiguration(); WifiConfiguration configuration = new WifiConfiguration();
configuration.SSID = "NetworkScoreServiceTest_SSID";
configuration.BSSID = "NetworkScoreServiceTest_BSSID";
mRecommendationRequest = new RecommendationRequest.Builder() mRecommendationRequest = new RecommendationRequest.Builder()
.setCurrentRecommendedWifiConfig(configuration).build(); .setCurrentRecommendedWifiConfig(configuration).build();
} }