Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import android.Manifest.permission; |
Jeff Davidson | 56f9f73 | 2014-08-14 16:47:23 -0700 | [diff] [blame] | 20 | import android.content.ContentResolver; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Jeff Davidson | b096bdc | 2014-07-01 12:29:11 -0700 | [diff] [blame] | 22 | import android.content.Intent; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 23 | import android.content.pm.PackageManager; |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 24 | import android.net.INetworkScoreCache; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 25 | import android.net.INetworkScoreService; |
Jeff Davidson | b096bdc | 2014-07-01 12:29:11 -0700 | [diff] [blame] | 26 | import android.net.NetworkScoreManager; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 27 | import android.net.NetworkScorerAppManager; |
Jeff Davidson | c741553 | 2014-06-23 18:15:34 -0700 | [diff] [blame] | 28 | import android.net.NetworkScorerAppManager.NetworkScorerAppData; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 29 | import android.net.ScoredNetwork; |
Jeff Davidson | 26fd143 | 2014-07-29 09:39:52 -0700 | [diff] [blame] | 30 | import android.os.Binder; |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 31 | import android.os.RemoteException; |
Jeff Davidson | ac7285d | 2014-08-08 15:12:47 -0700 | [diff] [blame] | 32 | import android.os.UserHandle; |
Jeff Davidson | 56f9f73 | 2014-08-14 16:47:23 -0700 | [diff] [blame] | 33 | import android.provider.Settings; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 34 | import android.text.TextUtils; |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 35 | import android.util.Log; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 36 | |
| 37 | import com.android.internal.R; |
| 38 | |
| 39 | import java.io.FileDescriptor; |
| 40 | import java.io.PrintWriter; |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 41 | import java.util.ArrayList; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 42 | import java.util.HashMap; |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 43 | import java.util.HashSet; |
| 44 | import java.util.List; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 45 | import java.util.Map; |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 46 | import java.util.Set; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Backing service for {@link android.net.NetworkScoreManager}. |
| 50 | * @hide |
| 51 | */ |
| 52 | public class NetworkScoreService extends INetworkScoreService.Stub { |
| 53 | private static final String TAG = "NetworkScoreService"; |
| 54 | |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 55 | private final Context mContext; |
| 56 | |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 57 | private final Map<Integer, INetworkScoreCache> mScoreCaches; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 58 | |
| 59 | public NetworkScoreService(Context context) { |
| 60 | mContext = context; |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 61 | mScoreCaches = new HashMap<>(); |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /** Called when the system is ready to run third-party code but before it actually does so. */ |
| 65 | void systemReady() { |
Jeff Davidson | 56f9f73 | 2014-08-14 16:47:23 -0700 | [diff] [blame] | 66 | ContentResolver cr = mContext.getContentResolver(); |
| 67 | if (Settings.Global.getInt(cr, Settings.Global.NETWORK_SCORING_PROVISIONED, 0) == 0) { |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 68 | // On first run, we try to initialize the scorer to the one configured at build time. |
| 69 | // This will be a no-op if the scorer isn't actually valid. |
| 70 | String defaultPackage = mContext.getResources().getString( |
| 71 | R.string.config_defaultNetworkScorerPackageName); |
| 72 | if (!TextUtils.isEmpty(defaultPackage)) { |
| 73 | NetworkScorerAppManager.setActiveScorer(mContext, defaultPackage); |
| 74 | } |
Jeff Davidson | 56f9f73 | 2014-08-14 16:47:23 -0700 | [diff] [blame] | 75 | Settings.Global.putInt(cr, Settings.Global.NETWORK_SCORING_PROVISIONED, 1); |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public boolean updateScores(ScoredNetwork[] networks) { |
| 81 | if (!NetworkScorerAppManager.isCallerActiveScorer(mContext, getCallingUid())) { |
| 82 | throw new SecurityException("Caller with UID " + getCallingUid() + |
| 83 | " is not the active scorer."); |
| 84 | } |
| 85 | |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 86 | // Separate networks by type. |
| 87 | Map<Integer, List<ScoredNetwork>> networksByType = new HashMap<>(); |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 88 | for (ScoredNetwork network : networks) { |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 89 | List<ScoredNetwork> networkList = networksByType.get(network.networkKey.type); |
| 90 | if (networkList == null) { |
| 91 | networkList = new ArrayList<>(); |
| 92 | networksByType.put(network.networkKey.type, networkList); |
| 93 | } |
| 94 | networkList.add(network); |
| 95 | } |
| 96 | |
| 97 | // Pass the scores of each type down to the appropriate network scorer. |
| 98 | for (Map.Entry<Integer, List<ScoredNetwork>> entry : networksByType.entrySet()) { |
| 99 | INetworkScoreCache scoreCache = mScoreCaches.get(entry.getKey()); |
| 100 | if (scoreCache != null) { |
| 101 | try { |
| 102 | scoreCache.updateScores(entry.getValue()); |
| 103 | } catch (RemoteException e) { |
| 104 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 105 | Log.v(TAG, "Unable to update scores of type " + entry.getKey(), e); |
| 106 | } |
| 107 | } |
| 108 | } else if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 109 | Log.v(TAG, "No scorer registered for type " + entry.getKey() + ", discarding"); |
| 110 | } |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public boolean clearScores() { |
Jeff Davidson | 1619779 | 2014-11-03 17:39:54 -0800 | [diff] [blame^] | 118 | // Only the active scorer or the system (who can broadcast BROADCAST_NETWORK_PRIVILEGED) |
| 119 | // should be allowed to flush all scores. |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 120 | if (NetworkScorerAppManager.isCallerActiveScorer(mContext, getCallingUid()) || |
Jeff Davidson | 1619779 | 2014-11-03 17:39:54 -0800 | [diff] [blame^] | 121 | mContext.checkCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED) == |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 122 | PackageManager.PERMISSION_GRANTED) { |
| 123 | clearInternal(); |
| 124 | return true; |
| 125 | } else { |
| 126 | throw new SecurityException( |
| 127 | "Caller is neither the active scorer nor the scorer manager."); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public boolean setActiveScorer(String packageName) { |
Jeff Davidson | 1619779 | 2014-11-03 17:39:54 -0800 | [diff] [blame^] | 133 | mContext.enforceCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED, TAG); |
Jeff Davidson | 26fd143 | 2014-07-29 09:39:52 -0700 | [diff] [blame] | 134 | return setScorerInternal(packageName); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void disableScoring() { |
Jeff Davidson | 1619779 | 2014-11-03 17:39:54 -0800 | [diff] [blame^] | 139 | // Only the active scorer or the system (who can broadcast BROADCAST_NETWORK_PRIVILEGED) |
| 140 | // should be allowed to disable scoring. |
Jeff Davidson | 26fd143 | 2014-07-29 09:39:52 -0700 | [diff] [blame] | 141 | if (NetworkScorerAppManager.isCallerActiveScorer(mContext, getCallingUid()) || |
Jeff Davidson | 1619779 | 2014-11-03 17:39:54 -0800 | [diff] [blame^] | 142 | mContext.checkCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED) == |
Jeff Davidson | 26fd143 | 2014-07-29 09:39:52 -0700 | [diff] [blame] | 143 | PackageManager.PERMISSION_GRANTED) { |
| 144 | // The return value is discarded here because at this point, the call should always |
| 145 | // succeed. The only reason for failure is if the new package is not a valid scorer, but |
| 146 | // we're disabling scoring altogether here. |
| 147 | setScorerInternal(null /* packageName */); |
| 148 | } else { |
| 149 | throw new SecurityException( |
| 150 | "Caller is neither the active scorer nor the scorer manager."); |
Jeff Davidson | b096bdc | 2014-07-01 12:29:11 -0700 | [diff] [blame] | 151 | } |
Jeff Davidson | 26fd143 | 2014-07-29 09:39:52 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** Set the active scorer. Callers are responsible for checking permissions as appropriate. */ |
| 155 | private boolean setScorerInternal(String packageName) { |
| 156 | long token = Binder.clearCallingIdentity(); |
| 157 | try { |
| 158 | // Preemptively clear scores even though the set operation could fail. We do this for |
| 159 | // safety as scores should never be compared across apps; in practice, Settings should |
| 160 | // only be allowing valid apps to be set as scorers, so failure here should be rare. |
| 161 | clearInternal(); |
| 162 | boolean result = NetworkScorerAppManager.setActiveScorer(mContext, packageName); |
| 163 | if (result) { |
| 164 | Intent intent = new Intent(NetworkScoreManager.ACTION_SCORER_CHANGED); |
| 165 | intent.putExtra(NetworkScoreManager.EXTRA_NEW_SCORER, packageName); |
Jeff Davidson | ac7285d | 2014-08-08 15:12:47 -0700 | [diff] [blame] | 166 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
Jeff Davidson | 26fd143 | 2014-07-29 09:39:52 -0700 | [diff] [blame] | 167 | } |
| 168 | return result; |
| 169 | } finally { |
| 170 | Binder.restoreCallingIdentity(token); |
| 171 | } |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** Clear scores. Callers are responsible for checking permissions as appropriate. */ |
| 175 | private void clearInternal() { |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 176 | Set<INetworkScoreCache> cachesToClear = getScoreCaches(); |
| 177 | |
| 178 | for (INetworkScoreCache scoreCache : cachesToClear) { |
| 179 | try { |
| 180 | scoreCache.clearScores(); |
| 181 | } catch (RemoteException e) { |
| 182 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 183 | Log.v(TAG, "Unable to clear scores", e); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public void registerNetworkScoreCache(int networkType, INetworkScoreCache scoreCache) { |
Jeff Davidson | 1619779 | 2014-11-03 17:39:54 -0800 | [diff] [blame^] | 191 | mContext.enforceCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED, TAG); |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 192 | synchronized (mScoreCaches) { |
| 193 | if (mScoreCaches.containsKey(networkType)) { |
| 194 | throw new IllegalArgumentException( |
| 195 | "Score cache already registered for type " + networkType); |
| 196 | } |
| 197 | mScoreCaches.put(networkType, scoreCache); |
| 198 | } |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | @Override |
| 202 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
| 203 | mContext.enforceCallingOrSelfPermission(permission.DUMP, TAG); |
Jeff Davidson | c741553 | 2014-06-23 18:15:34 -0700 | [diff] [blame] | 204 | NetworkScorerAppData currentScorer = NetworkScorerAppManager.getActiveScorer(mContext); |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 205 | if (currentScorer == null) { |
| 206 | writer.println("Scoring is disabled."); |
| 207 | return; |
| 208 | } |
Jeff Davidson | c741553 | 2014-06-23 18:15:34 -0700 | [diff] [blame] | 209 | writer.println("Current scorer: " + currentScorer.mPackageName); |
Jeff Davidson | 68c46fd | 2014-05-09 15:51:34 -0700 | [diff] [blame] | 210 | writer.flush(); |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 211 | |
| 212 | for (INetworkScoreCache scoreCache : getScoreCaches()) { |
| 213 | try { |
| 214 | scoreCache.asBinder().dump(fd, args); |
| 215 | } catch (RemoteException e) { |
| 216 | writer.println("Unable to dump score cache"); |
| 217 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 218 | Log.v(TAG, "Unable to dump score cache", e); |
| 219 | } |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | } |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 223 | |
| 224 | /** |
| 225 | * Returns a set of all score caches that are currently active. |
| 226 | * |
| 227 | * <p>May be used to perform an action on all score caches without potentially strange behavior |
| 228 | * if a new scorer is registered during that action's execution. |
| 229 | */ |
| 230 | private Set<INetworkScoreCache> getScoreCaches() { |
| 231 | synchronized (mScoreCaches) { |
| 232 | return new HashSet<>(mScoreCaches.values()); |
| 233 | } |
| 234 | } |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 235 | } |