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