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