Search in sources :

Example 1 with RemoteCallbackList

use of android.os.RemoteCallbackList in project platform_frameworks_base by android.

the class NetworkScoreService method updateScores.

@Override
public boolean updateScores(ScoredNetwork[] networks) {
    if (!isCallerActiveScorer(getCallingUid())) {
        throw new SecurityException("Caller with UID " + getCallingUid() + " 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) {
            List<ScoredNetwork> networkList = networksByType.get(network.networkKey.type);
            if (networkList == null) {
                networkList = new ArrayList<>();
                networksByType.put(network.networkKey.type, networkList);
            }
            networkList.add(network);
        }
        // Pass the scores of each type down to the appropriate network scorer.
        for (final Map.Entry<Integer, List<ScoredNetwork>> entry : networksByType.entrySet()) {
            final RemoteCallbackList<INetworkScoreCache> callbackList;
            final boolean isEmpty;
            synchronized (mScoreCaches) {
                callbackList = mScoreCaches.get(entry.getKey());
                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");
                }
                continue;
            }
            sendCallback(new Consumer<INetworkScoreCache>() {

                @Override
                public void accept(INetworkScoreCache networkScoreCache) {
                    try {
                        networkScoreCache.updateScores(entry.getValue());
                    } catch (RemoteException e) {
                        if (Log.isLoggable(TAG, Log.VERBOSE)) {
                            Log.v(TAG, "Unable to update scores of type " + entry.getKey(), e);
                        }
                    }
                }
            }, Collections.singleton(callbackList));
        }
        return true;
    } finally {
        Binder.restoreCallingIdentity(token);
    }
}
Also used : INetworkScoreCache(android.net.INetworkScoreCache) ScoredNetwork(android.net.ScoredNetwork) ArrayMap(android.util.ArrayMap) RemoteCallbackList(android.os.RemoteCallbackList) List(java.util.List) ArrayList(java.util.ArrayList) RemoteException(android.os.RemoteException) Map(java.util.Map) ArrayMap(android.util.ArrayMap)

Aggregations

INetworkScoreCache (android.net.INetworkScoreCache)1 ScoredNetwork (android.net.ScoredNetwork)1 RemoteCallbackList (android.os.RemoteCallbackList)1 RemoteException (android.os.RemoteException)1 ArrayMap (android.util.ArrayMap)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1