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; |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 20 | import android.content.ComponentName; |
Jeff Davidson | 56f9f73 | 2014-08-14 16:47:23 -0700 | [diff] [blame] | 21 | import android.content.ContentResolver; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 22 | import android.content.Context; |
Jeff Davidson | b096bdc | 2014-07-01 12:29:11 -0700 | [diff] [blame] | 23 | import android.content.Intent; |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 24 | import android.content.ServiceConnection; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 25 | import android.content.pm.PackageManager; |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 26 | import android.net.INetworkScoreCache; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 27 | import android.net.INetworkScoreService; |
Jeff Davidson | b096bdc | 2014-07-01 12:29:11 -0700 | [diff] [blame] | 28 | import android.net.NetworkScoreManager; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 29 | import android.net.NetworkScorerAppManager; |
Jeff Davidson | c741553 | 2014-06-23 18:15:34 -0700 | [diff] [blame] | 30 | import android.net.NetworkScorerAppManager.NetworkScorerAppData; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 31 | import android.net.ScoredNetwork; |
Jeff Davidson | 26fd143 | 2014-07-29 09:39:52 -0700 | [diff] [blame] | 32 | import android.os.Binder; |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 33 | import android.os.IBinder; |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 34 | import android.os.RemoteException; |
Jeff Davidson | ac7285d | 2014-08-08 15:12:47 -0700 | [diff] [blame] | 35 | import android.os.UserHandle; |
Jeff Davidson | 56f9f73 | 2014-08-14 16:47:23 -0700 | [diff] [blame] | 36 | import android.provider.Settings; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 37 | import android.text.TextUtils; |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 38 | import android.util.Log; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 39 | |
| 40 | import com.android.internal.R; |
Jeff Davidson | 7842f64 | 2014-11-23 13:48:12 -0800 | [diff] [blame] | 41 | import com.android.internal.annotations.GuardedBy; |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame^] | 42 | import com.android.internal.content.PackageMonitor; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 43 | |
| 44 | import java.io.FileDescriptor; |
| 45 | import java.io.PrintWriter; |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 46 | import java.util.ArrayList; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 47 | import java.util.HashMap; |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 48 | import java.util.HashSet; |
| 49 | import java.util.List; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 50 | import java.util.Map; |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 51 | import java.util.Set; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Backing service for {@link android.net.NetworkScoreManager}. |
| 55 | * @hide |
| 56 | */ |
| 57 | public class NetworkScoreService extends INetworkScoreService.Stub { |
| 58 | private static final String TAG = "NetworkScoreService"; |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 59 | private static final boolean DBG = false; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 60 | |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 61 | private final Context mContext; |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 62 | private final Map<Integer, INetworkScoreCache> mScoreCaches; |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame^] | 63 | /** Lock used to update mPackageMonitor when scorer package changes occur. */ |
| 64 | private final Object mPackageMonitorLock = new Object[0]; |
Jeff Davidson | 7842f64 | 2014-11-23 13:48:12 -0800 | [diff] [blame] | 65 | |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame^] | 66 | @GuardedBy("mPackageMonitorLock") |
| 67 | private NetworkScorerPackageMonitor mPackageMonitor; |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 68 | private ScoringServiceConnection mServiceConnection; |
Jeff Davidson | 7842f64 | 2014-11-23 13:48:12 -0800 | [diff] [blame] | 69 | |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame^] | 70 | /** |
| 71 | * Clears scores when the active scorer package is no longer valid and |
| 72 | * manages the service connection. |
| 73 | */ |
| 74 | private class NetworkScorerPackageMonitor extends PackageMonitor { |
Jeff Davidson | 7842f64 | 2014-11-23 13:48:12 -0800 | [diff] [blame] | 75 | final String mRegisteredPackage; |
| 76 | |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame^] | 77 | private NetworkScorerPackageMonitor(String mRegisteredPackage) { |
| 78 | this.mRegisteredPackage = mRegisteredPackage; |
Jeff Davidson | 7842f64 | 2014-11-23 13:48:12 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Override |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame^] | 82 | public void onPackageAdded(String packageName, int uid) { |
| 83 | evaluateBinding(packageName, true /* forceUnbind */); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public void onPackageRemoved(String packageName, int uid) { |
| 88 | evaluateBinding(packageName, true /* forceUnbind */); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void onPackageModified(String packageName) { |
| 93 | evaluateBinding(packageName, false /* forceUnbind */); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) { |
| 98 | if (doit) { // "doit" means the force stop happened instead of just being queried for. |
| 99 | for (String packageName : packages) { |
| 100 | evaluateBinding(packageName, true /* forceUnbind */); |
| 101 | } |
| 102 | } |
| 103 | return super.onHandleForceStop(intent, packages, uid, doit); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void onPackageUpdateFinished(String packageName, int uid) { |
| 108 | evaluateBinding(packageName, true /* forceUnbind */); |
| 109 | } |
| 110 | |
| 111 | private void evaluateBinding(String scorerPackageName, boolean forceUnbind) { |
| 112 | if (mRegisteredPackage.equals(scorerPackageName)) { |
| 113 | if (DBG) { |
| 114 | Log.d(TAG, "Evaluating binding for: " + scorerPackageName |
| 115 | + ", forceUnbind=" + forceUnbind); |
| 116 | } |
| 117 | final NetworkScorerAppData activeScorer = |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 118 | NetworkScorerAppManager.getActiveScorer(mContext); |
| 119 | if (activeScorer == null) { |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame^] | 120 | // Package change has invalidated a scorer, this will also unbind any service |
| 121 | // connection. |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 122 | Log.i(TAG, "Package " + mRegisteredPackage + |
| 123 | " is no longer valid, disabling scoring."); |
| 124 | setScorerInternal(null); |
| 125 | } else if (activeScorer.mScoringServiceClassName == null) { |
| 126 | // The scoring service is not available, make sure it's unbound. |
| 127 | unbindFromScoringServiceIfNeeded(); |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame^] | 128 | } else { // The scoring service changed in some way. |
| 129 | if (forceUnbind) { |
| 130 | unbindFromScoringServiceIfNeeded(); |
| 131 | } |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 132 | bindToScoringServiceIfNeeded(activeScorer); |
| 133 | } |
Jeff Davidson | 7842f64 | 2014-11-23 13:48:12 -0800 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 138 | public NetworkScoreService(Context context) { |
| 139 | mContext = context; |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 140 | mScoreCaches = new HashMap<>(); |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /** Called when the system is ready to run third-party code but before it actually does so. */ |
| 144 | void systemReady() { |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 145 | if (DBG) Log.d(TAG, "systemReady"); |
Jeff Davidson | 56f9f73 | 2014-08-14 16:47:23 -0700 | [diff] [blame] | 146 | ContentResolver cr = mContext.getContentResolver(); |
| 147 | if (Settings.Global.getInt(cr, Settings.Global.NETWORK_SCORING_PROVISIONED, 0) == 0) { |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 148 | // On first run, we try to initialize the scorer to the one configured at build time. |
| 149 | // This will be a no-op if the scorer isn't actually valid. |
| 150 | String defaultPackage = mContext.getResources().getString( |
| 151 | R.string.config_defaultNetworkScorerPackageName); |
| 152 | if (!TextUtils.isEmpty(defaultPackage)) { |
| 153 | NetworkScorerAppManager.setActiveScorer(mContext, defaultPackage); |
| 154 | } |
Jeff Davidson | 56f9f73 | 2014-08-14 16:47:23 -0700 | [diff] [blame] | 155 | Settings.Global.putInt(cr, Settings.Global.NETWORK_SCORING_PROVISIONED, 1); |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 156 | } |
Jeff Davidson | 7842f64 | 2014-11-23 13:48:12 -0800 | [diff] [blame] | 157 | |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame^] | 158 | registerPackageMonitorIfNeeded(); |
Jeff Davidson | 7842f64 | 2014-11-23 13:48:12 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 161 | /** Called when the system is ready for us to start third-party code. */ |
| 162 | void systemRunning() { |
| 163 | if (DBG) Log.d(TAG, "systemRunning"); |
| 164 | bindToScoringServiceIfNeeded(); |
| 165 | } |
| 166 | |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame^] | 167 | private void registerPackageMonitorIfNeeded() { |
| 168 | if (DBG) Log.d(TAG, "registerPackageMonitorIfNeeded"); |
Jeff Davidson | 7842f64 | 2014-11-23 13:48:12 -0800 | [diff] [blame] | 169 | NetworkScorerAppData scorer = NetworkScorerAppManager.getActiveScorer(mContext); |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame^] | 170 | synchronized (mPackageMonitorLock) { |
| 171 | // Unregister the current monitor if needed. |
| 172 | if (mPackageMonitor != null) { |
| 173 | if (DBG) { |
| 174 | Log.d(TAG, "Unregistering package monitor for " |
| 175 | + mPackageMonitor.mRegisteredPackage); |
Jeff Davidson | 7842f64 | 2014-11-23 13:48:12 -0800 | [diff] [blame] | 176 | } |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame^] | 177 | mPackageMonitor.unregister(); |
| 178 | mPackageMonitor = null; |
Jeff Davidson | 7842f64 | 2014-11-23 13:48:12 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame^] | 181 | // Create and register the monitor if a scorer is active. |
Jeff Davidson | 7842f64 | 2014-11-23 13:48:12 -0800 | [diff] [blame] | 182 | if (scorer != null) { |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame^] | 183 | mPackageMonitor = new NetworkScorerPackageMonitor(scorer.mPackageName); |
Xiaohui Chen | e4de5a0 | 2015-09-22 15:33:31 -0700 | [diff] [blame] | 184 | // TODO: Need to update when we support per-user scorers. http://b/23422763 |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame^] | 185 | mPackageMonitor.register(mContext, null /* thread */, UserHandle.SYSTEM, |
| 186 | false /* externalStorage */); |
| 187 | if (DBG) { |
| 188 | Log.d(TAG, "Registered package monitor for " |
| 189 | + mPackageMonitor.mRegisteredPackage); |
Jeff Davidson | 7842f64 | 2014-11-23 13:48:12 -0800 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | } |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 195 | private void bindToScoringServiceIfNeeded() { |
| 196 | if (DBG) Log.d(TAG, "bindToScoringServiceIfNeeded"); |
| 197 | NetworkScorerAppData scorerData = NetworkScorerAppManager.getActiveScorer(mContext); |
| 198 | bindToScoringServiceIfNeeded(scorerData); |
| 199 | } |
| 200 | |
| 201 | private void bindToScoringServiceIfNeeded(NetworkScorerAppData scorerData) { |
| 202 | if (DBG) Log.d(TAG, "bindToScoringServiceIfNeeded(" + scorerData + ")"); |
| 203 | if (scorerData != null && scorerData.mScoringServiceClassName != null) { |
| 204 | ComponentName componentName = |
| 205 | new ComponentName(scorerData.mPackageName, scorerData.mScoringServiceClassName); |
| 206 | // If we're connected to a different component then drop it. |
| 207 | if (mServiceConnection != null |
| 208 | && !mServiceConnection.mComponentName.equals(componentName)) { |
| 209 | unbindFromScoringServiceIfNeeded(); |
| 210 | } |
| 211 | |
| 212 | // If we're not connected at all then create a new connection. |
| 213 | if (mServiceConnection == null) { |
| 214 | mServiceConnection = new ScoringServiceConnection(componentName); |
| 215 | } |
| 216 | |
| 217 | // Make sure the connection is connected (idempotent) |
| 218 | mServiceConnection.connect(mContext); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | private void unbindFromScoringServiceIfNeeded() { |
| 223 | if (DBG) Log.d(TAG, "unbindFromScoringServiceIfNeeded"); |
| 224 | if (mServiceConnection != null) { |
| 225 | mServiceConnection.disconnect(mContext); |
| 226 | } |
| 227 | mServiceConnection = null; |
| 228 | } |
| 229 | |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 230 | @Override |
| 231 | public boolean updateScores(ScoredNetwork[] networks) { |
| 232 | if (!NetworkScorerAppManager.isCallerActiveScorer(mContext, getCallingUid())) { |
| 233 | throw new SecurityException("Caller with UID " + getCallingUid() + |
| 234 | " is not the active scorer."); |
| 235 | } |
| 236 | |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 237 | // Separate networks by type. |
| 238 | Map<Integer, List<ScoredNetwork>> networksByType = new HashMap<>(); |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 239 | for (ScoredNetwork network : networks) { |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 240 | List<ScoredNetwork> networkList = networksByType.get(network.networkKey.type); |
| 241 | if (networkList == null) { |
| 242 | networkList = new ArrayList<>(); |
| 243 | networksByType.put(network.networkKey.type, networkList); |
| 244 | } |
| 245 | networkList.add(network); |
| 246 | } |
| 247 | |
| 248 | // Pass the scores of each type down to the appropriate network scorer. |
| 249 | for (Map.Entry<Integer, List<ScoredNetwork>> entry : networksByType.entrySet()) { |
| 250 | INetworkScoreCache scoreCache = mScoreCaches.get(entry.getKey()); |
| 251 | if (scoreCache != null) { |
| 252 | try { |
| 253 | scoreCache.updateScores(entry.getValue()); |
| 254 | } catch (RemoteException e) { |
| 255 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 256 | Log.v(TAG, "Unable to update scores of type " + entry.getKey(), e); |
| 257 | } |
| 258 | } |
| 259 | } else if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 260 | Log.v(TAG, "No scorer registered for type " + entry.getKey() + ", discarding"); |
| 261 | } |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | return true; |
| 265 | } |
| 266 | |
| 267 | @Override |
| 268 | public boolean clearScores() { |
Jeff Davidson | 1619779 | 2014-11-03 17:39:54 -0800 | [diff] [blame] | 269 | // Only the active scorer or the system (who can broadcast BROADCAST_NETWORK_PRIVILEGED) |
| 270 | // should be allowed to flush all scores. |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 271 | if (NetworkScorerAppManager.isCallerActiveScorer(mContext, getCallingUid()) || |
Jeff Davidson | 1619779 | 2014-11-03 17:39:54 -0800 | [diff] [blame] | 272 | mContext.checkCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED) == |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 273 | PackageManager.PERMISSION_GRANTED) { |
| 274 | clearInternal(); |
| 275 | return true; |
| 276 | } else { |
| 277 | throw new SecurityException( |
| 278 | "Caller is neither the active scorer nor the scorer manager."); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public boolean setActiveScorer(String packageName) { |
Jeff Davidson | e56f2bb | 2014-11-05 11:14:19 -0800 | [diff] [blame] | 284 | // TODO: For now, since SCORE_NETWORKS requires an app to be privileged, we allow such apps |
| 285 | // to directly set the scorer app rather than having to use the consent dialog. The |
| 286 | // assumption is that anyone bundling a scorer app with the system is trusted by the OEM to |
| 287 | // do the right thing and not enable this feature without explaining it to the user. |
| 288 | // In the future, should this API be opened to 3p apps, we will need to lock this down and |
| 289 | // figure out another way to streamline the UX. |
| 290 | |
| 291 | // mContext.enforceCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED, TAG); |
| 292 | mContext.enforceCallingOrSelfPermission(permission.SCORE_NETWORKS, TAG); |
| 293 | |
Jeff Davidson | 26fd143 | 2014-07-29 09:39:52 -0700 | [diff] [blame] | 294 | return setScorerInternal(packageName); |
| 295 | } |
| 296 | |
| 297 | @Override |
| 298 | public void disableScoring() { |
Jeff Davidson | 1619779 | 2014-11-03 17:39:54 -0800 | [diff] [blame] | 299 | // Only the active scorer or the system (who can broadcast BROADCAST_NETWORK_PRIVILEGED) |
| 300 | // should be allowed to disable scoring. |
Jeff Davidson | 26fd143 | 2014-07-29 09:39:52 -0700 | [diff] [blame] | 301 | if (NetworkScorerAppManager.isCallerActiveScorer(mContext, getCallingUid()) || |
Jeff Davidson | 1619779 | 2014-11-03 17:39:54 -0800 | [diff] [blame] | 302 | mContext.checkCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED) == |
Jeff Davidson | 26fd143 | 2014-07-29 09:39:52 -0700 | [diff] [blame] | 303 | PackageManager.PERMISSION_GRANTED) { |
| 304 | // The return value is discarded here because at this point, the call should always |
| 305 | // succeed. The only reason for failure is if the new package is not a valid scorer, but |
| 306 | // we're disabling scoring altogether here. |
| 307 | setScorerInternal(null /* packageName */); |
| 308 | } else { |
| 309 | throw new SecurityException( |
| 310 | "Caller is neither the active scorer nor the scorer manager."); |
Jeff Davidson | b096bdc | 2014-07-01 12:29:11 -0700 | [diff] [blame] | 311 | } |
Jeff Davidson | 26fd143 | 2014-07-29 09:39:52 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /** Set the active scorer. Callers are responsible for checking permissions as appropriate. */ |
| 315 | private boolean setScorerInternal(String packageName) { |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 316 | if (DBG) Log.d(TAG, "setScorerInternal(" + packageName + ")"); |
Jeff Davidson | 26fd143 | 2014-07-29 09:39:52 -0700 | [diff] [blame] | 317 | long token = Binder.clearCallingIdentity(); |
| 318 | try { |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 319 | unbindFromScoringServiceIfNeeded(); |
Jeff Davidson | 26fd143 | 2014-07-29 09:39:52 -0700 | [diff] [blame] | 320 | // Preemptively clear scores even though the set operation could fail. We do this for |
| 321 | // safety as scores should never be compared across apps; in practice, Settings should |
| 322 | // only be allowing valid apps to be set as scorers, so failure here should be rare. |
| 323 | clearInternal(); |
Jeremy Joslin | da11f5c | 2016-02-10 07:31:33 -0800 | [diff] [blame] | 324 | // Get the scorer that is about to be replaced, if any, so we can notify it directly. |
| 325 | NetworkScorerAppData prevScorer = NetworkScorerAppManager.getActiveScorer(mContext); |
Jeff Davidson | 26fd143 | 2014-07-29 09:39:52 -0700 | [diff] [blame] | 326 | boolean result = NetworkScorerAppManager.setActiveScorer(mContext, packageName); |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 327 | // Unconditionally attempt to bind to the current scorer. If setActiveScorer() failed |
| 328 | // then we'll attempt to restore the previous binding (if any), otherwise an attempt |
| 329 | // will be made to bind to the new scorer. |
| 330 | bindToScoringServiceIfNeeded(); |
Jeremy Joslin | da11f5c | 2016-02-10 07:31:33 -0800 | [diff] [blame] | 331 | if (result) { // new scorer successfully set |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame^] | 332 | registerPackageMonitorIfNeeded(); |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 333 | |
Jeff Davidson | 26fd143 | 2014-07-29 09:39:52 -0700 | [diff] [blame] | 334 | Intent intent = new Intent(NetworkScoreManager.ACTION_SCORER_CHANGED); |
Jeremy Joslin | da11f5c | 2016-02-10 07:31:33 -0800 | [diff] [blame] | 335 | if (prevScorer != null) { // Directly notify the old scorer. |
| 336 | intent.setPackage(prevScorer.mPackageName); |
| 337 | // TODO: Need to update when we support per-user scorers. http://b/23422763 |
| 338 | mContext.sendBroadcastAsUser(intent, UserHandle.SYSTEM); |
| 339 | } |
| 340 | |
| 341 | if (packageName != null) { // Then notify the new scorer |
| 342 | intent.putExtra(NetworkScoreManager.EXTRA_NEW_SCORER, packageName); |
| 343 | intent.setPackage(packageName); |
| 344 | // TODO: Need to update when we support per-user scorers. http://b/23422763 |
| 345 | mContext.sendBroadcastAsUser(intent, UserHandle.SYSTEM); |
| 346 | } |
Jeff Davidson | 26fd143 | 2014-07-29 09:39:52 -0700 | [diff] [blame] | 347 | } |
| 348 | return result; |
| 349 | } finally { |
| 350 | Binder.restoreCallingIdentity(token); |
| 351 | } |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /** Clear scores. Callers are responsible for checking permissions as appropriate. */ |
| 355 | private void clearInternal() { |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 356 | Set<INetworkScoreCache> cachesToClear = getScoreCaches(); |
| 357 | |
| 358 | for (INetworkScoreCache scoreCache : cachesToClear) { |
| 359 | try { |
| 360 | scoreCache.clearScores(); |
| 361 | } catch (RemoteException e) { |
| 362 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 363 | Log.v(TAG, "Unable to clear scores", e); |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | @Override |
| 370 | public void registerNetworkScoreCache(int networkType, INetworkScoreCache scoreCache) { |
Jeff Davidson | 1619779 | 2014-11-03 17:39:54 -0800 | [diff] [blame] | 371 | mContext.enforceCallingOrSelfPermission(permission.BROADCAST_NETWORK_PRIVILEGED, TAG); |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 372 | synchronized (mScoreCaches) { |
| 373 | if (mScoreCaches.containsKey(networkType)) { |
| 374 | throw new IllegalArgumentException( |
| 375 | "Score cache already registered for type " + networkType); |
| 376 | } |
| 377 | mScoreCaches.put(networkType, scoreCache); |
| 378 | } |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | @Override |
| 382 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
| 383 | mContext.enforceCallingOrSelfPermission(permission.DUMP, TAG); |
Jeff Davidson | c741553 | 2014-06-23 18:15:34 -0700 | [diff] [blame] | 384 | NetworkScorerAppData currentScorer = NetworkScorerAppManager.getActiveScorer(mContext); |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 385 | if (currentScorer == null) { |
| 386 | writer.println("Scoring is disabled."); |
| 387 | return; |
| 388 | } |
Jeff Davidson | c741553 | 2014-06-23 18:15:34 -0700 | [diff] [blame] | 389 | writer.println("Current scorer: " + currentScorer.mPackageName); |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 390 | |
| 391 | for (INetworkScoreCache scoreCache : getScoreCaches()) { |
| 392 | try { |
| 393 | scoreCache.asBinder().dump(fd, args); |
| 394 | } catch (RemoteException e) { |
| 395 | writer.println("Unable to dump score cache"); |
| 396 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 397 | Log.v(TAG, "Unable to dump score cache", e); |
| 398 | } |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 399 | } |
| 400 | } |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 401 | if (mServiceConnection != null) { |
| 402 | mServiceConnection.dump(fd, writer, args); |
| 403 | } else { |
| 404 | writer.println("ScoringServiceConnection: null"); |
| 405 | } |
| 406 | writer.flush(); |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 407 | } |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 408 | |
| 409 | /** |
| 410 | * Returns a set of all score caches that are currently active. |
| 411 | * |
| 412 | * <p>May be used to perform an action on all score caches without potentially strange behavior |
| 413 | * if a new scorer is registered during that action's execution. |
| 414 | */ |
| 415 | private Set<INetworkScoreCache> getScoreCaches() { |
| 416 | synchronized (mScoreCaches) { |
| 417 | return new HashSet<>(mScoreCaches.values()); |
| 418 | } |
| 419 | } |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 420 | |
| 421 | private static class ScoringServiceConnection implements ServiceConnection { |
| 422 | private final ComponentName mComponentName; |
| 423 | private boolean mBound = false; |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame^] | 424 | private boolean mConnected = false; |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 425 | |
| 426 | ScoringServiceConnection(ComponentName componentName) { |
| 427 | mComponentName = componentName; |
| 428 | } |
| 429 | |
| 430 | void connect(Context context) { |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 431 | if (!mBound) { |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame^] | 432 | Intent service = new Intent(); |
| 433 | service.setComponent(mComponentName); |
| 434 | mBound = context.bindServiceAsUser(service, this, |
| 435 | Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE, |
| 436 | UserHandle.SYSTEM); |
| 437 | if (!mBound) { |
| 438 | Log.w(TAG, "Bind call failed for " + service); |
| 439 | } else { |
| 440 | if (DBG) Log.d(TAG, "ScoringServiceConnection bound."); |
| 441 | } |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | |
| 445 | void disconnect(Context context) { |
| 446 | try { |
| 447 | if (mBound) { |
| 448 | mBound = false; |
| 449 | context.unbindService(this); |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame^] | 450 | if (DBG) Log.d(TAG, "ScoringServiceConnection unbound."); |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 451 | } |
| 452 | } catch (RuntimeException e) { |
| 453 | Log.e(TAG, "Unbind failed.", e); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | @Override |
| 458 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 459 | if (DBG) Log.d(TAG, "ScoringServiceConnection: " + name.flattenToString()); |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame^] | 460 | mConnected = true; |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | @Override |
| 464 | public void onServiceDisconnected(ComponentName name) { |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame^] | 465 | if (DBG) { |
| 466 | Log.d(TAG, "ScoringServiceConnection, disconnected: " + name.flattenToString()); |
| 467 | } |
| 468 | mConnected = false; |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | public void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame^] | 472 | writer.println("ScoringServiceConnection: " + mComponentName + ", bound: " + mBound |
| 473 | + ", connected: " + mConnected); |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 474 | } |
| 475 | } |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 476 | } |