Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4a04c07d authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge changes I40f055df,I562713df

* changes:
  Stay bound to the most current active scorer.
  Clear and restore the calling ID.
parents febd982c b1365852
Loading
Loading
Loading
Loading
+123 −82
Original line number Diff line number Diff line
@@ -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;
@@ -159,15 +159,23 @@ public class NetworkScoreService extends INetworkScoreService.Stub {
                    if (DBG) Log.d(TAG, "No active scorers available.");
                    unbindFromScoringServiceIfNeeded();
                } else if (activeScorer.packageName.equals(scorerPackageName)) {
                    // The active scoring service changed in some way.
                    if (DBG) {
                        Log.d(TAG, "Possible change to the active scorer: "
                            + activeScorer.packageName);
                    }
                    // The scoring service changed in some way.
                    if (forceUnbind) {
                        unbindFromScoringServiceIfNeeded();
                    }
                    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.");
        }

        final long token = Binder.clearCallingIdentity();
        try {
            // Separate networks by type.
            Map<Integer, List<ScoredNetwork>> networksByType = new ArrayMap<>();
            for (ScoredNetwork network : networks) {
@@ -336,11 +346,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;
                }
@@ -360,6 +372,9 @@ public class NetworkScoreService extends INetworkScoreService.Stub {
            }

            return true;
        } finally {
            Binder.restoreCallingIdentity(token);
        }
    }

    @Override
@@ -369,8 +384,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.");
@@ -428,6 +448,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) {
@@ -443,27 +465,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 {
@@ -483,17 +516,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);
@@ -501,6 +539,9 @@ public class NetworkScoreService extends INetworkScoreService.Stub {
                }
            }
            return false;
        } finally {
            Binder.restoreCallingIdentity(token);
        }
    }

    @Override
+2 −0
Original line number Diff line number Diff line
@@ -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();
    }