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 | |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 19 | import static android.net.NetworkRecommendationProvider.EXTRA_RECOMMENDATION_RESULT; |
| 20 | import static android.net.NetworkRecommendationProvider.EXTRA_SEQUENCE; |
| 21 | |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 22 | import android.Manifest.permission; |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 23 | import android.annotation.Nullable; |
Jeremy Joslin | 967b581 | 2016-06-02 07:58:14 -0700 | [diff] [blame] | 24 | import android.content.BroadcastReceiver; |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 25 | import android.content.ComponentName; |
Jeff Davidson | 56f9f73 | 2014-08-14 16:47:23 -0700 | [diff] [blame] | 26 | import android.content.ContentResolver; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 27 | import android.content.Context; |
Jeff Davidson | b096bdc | 2014-07-01 12:29:11 -0700 | [diff] [blame] | 28 | import android.content.Intent; |
Jeremy Joslin | 967b581 | 2016-06-02 07:58:14 -0700 | [diff] [blame] | 29 | import android.content.IntentFilter; |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 30 | import android.content.ServiceConnection; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 31 | import android.content.pm.PackageManager; |
Jeremy Joslin | fa4f08e | 2016-12-06 07:42:38 -0800 | [diff] [blame] | 32 | import android.database.ContentObserver; |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 33 | import android.net.INetworkRecommendationProvider; |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 34 | import android.net.INetworkScoreCache; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 35 | import android.net.INetworkScoreService; |
Jeremy Joslin | b2087a1 | 2016-12-13 16:11:51 -0800 | [diff] [blame] | 36 | import android.net.NetworkKey; |
Jeremy Joslin | 5519d7c | 2017-01-06 14:36:54 -0800 | [diff] [blame] | 37 | import android.net.NetworkScoreManager; |
Jeremy Joslin | f621bc9 | 2017-02-16 11:11:57 -0800 | [diff] [blame] | 38 | import android.net.NetworkScorerAppData; |
Jeremy Joslin | d1daf6d | 2016-11-28 17:47:35 -0800 | [diff] [blame] | 39 | import android.net.RecommendationRequest; |
| 40 | import android.net.RecommendationResult; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 41 | import android.net.ScoredNetwork; |
Jeremy Joslin | fa4f08e | 2016-12-06 07:42:38 -0800 | [diff] [blame] | 42 | import android.net.Uri; |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 43 | import android.net.wifi.ScanResult; |
| 44 | import android.net.wifi.WifiInfo; |
| 45 | import android.net.wifi.WifiManager; |
| 46 | import android.net.wifi.WifiScanner; |
Jeremy Joslin | 8f5521a | 2016-12-20 14:36:20 -0800 | [diff] [blame] | 47 | import android.os.Binder; |
Jeremy Joslin | ce73c6f | 2016-12-29 14:49:38 -0800 | [diff] [blame] | 48 | import android.os.Build; |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 49 | import android.os.Bundle; |
Jeremy Joslin | ce73c6f | 2016-12-29 14:49:38 -0800 | [diff] [blame] | 50 | import android.os.Handler; |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 51 | import android.os.IBinder; |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 52 | import android.os.IRemoteCallback; |
Jeremy Joslin | ce73c6f | 2016-12-29 14:49:38 -0800 | [diff] [blame] | 53 | import android.os.Looper; |
| 54 | import android.os.Message; |
Jeremy Joslin | a5172f6 | 2017-02-02 14:27:05 -0800 | [diff] [blame] | 55 | import android.os.Process; |
Jeremy Joslin | 998d7ca | 2016-12-28 15:56:46 -0800 | [diff] [blame] | 56 | import android.os.RemoteCallback; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 57 | import android.os.RemoteCallbackList; |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 58 | import android.os.RemoteException; |
Jeff Davidson | ac7285d | 2014-08-08 15:12:47 -0700 | [diff] [blame] | 59 | import android.os.UserHandle; |
Jeremy Joslin | cb594f3 | 2017-01-03 17:31:23 -0800 | [diff] [blame] | 60 | import android.provider.Settings; |
Jeremy Joslin | fa4f08e | 2016-12-06 07:42:38 -0800 | [diff] [blame] | 61 | import android.provider.Settings.Global; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 62 | import android.util.ArrayMap; |
Jeremy Joslin | e71fe2b | 2017-01-25 11:40:08 -0800 | [diff] [blame] | 63 | import android.util.ArraySet; |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 64 | import android.util.Log; |
Jeremy Joslin | ce73c6f | 2016-12-29 14:49:38 -0800 | [diff] [blame] | 65 | import android.util.Pair; |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 66 | import android.util.TimedRemoteCaller; |
| 67 | |
Jeff Davidson | 7842f64 | 2014-11-23 13:48:12 -0800 | [diff] [blame] | 68 | import com.android.internal.annotations.GuardedBy; |
Amin Shaikh | aa09aa0 | 2016-11-21 17:27:53 -0800 | [diff] [blame] | 69 | import com.android.internal.annotations.VisibleForTesting; |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame] | 70 | import com.android.internal.content.PackageMonitor; |
Jeff Sharkey | ba6f8c8 | 2016-11-09 12:25:44 -0700 | [diff] [blame] | 71 | import com.android.internal.os.TransferPipe; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame^] | 72 | import com.android.internal.util.DumpUtils; |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 73 | |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 74 | import java.io.FileDescriptor; |
Jeff Sharkey | ba6f8c8 | 2016-11-09 12:25:44 -0700 | [diff] [blame] | 75 | import java.io.IOException; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 76 | import java.io.PrintWriter; |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 77 | import java.util.ArrayList; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 78 | import java.util.Collection; |
| 79 | import java.util.Collections; |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 80 | import java.util.List; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 81 | import java.util.Map; |
Jeremy Joslin | e71fe2b | 2017-01-25 11:40:08 -0800 | [diff] [blame] | 82 | import java.util.Set; |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 83 | import java.util.concurrent.TimeoutException; |
Jeremy Joslin | ce73c6f | 2016-12-29 14:49:38 -0800 | [diff] [blame] | 84 | import java.util.concurrent.atomic.AtomicBoolean; |
Jeremy Joslin | 3452b69 | 2017-01-17 15:48:13 -0800 | [diff] [blame] | 85 | import java.util.concurrent.atomic.AtomicReference; |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 86 | import java.util.function.BiConsumer; |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 87 | import java.util.function.Supplier; |
Jeremy Joslin | e71fe2b | 2017-01-25 11:40:08 -0800 | [diff] [blame] | 88 | import java.util.function.UnaryOperator; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * Backing service for {@link android.net.NetworkScoreManager}. |
| 92 | * @hide |
| 93 | */ |
| 94 | public class NetworkScoreService extends INetworkScoreService.Stub { |
| 95 | private static final String TAG = "NetworkScoreService"; |
Jeremy Joslin | ce73c6f | 2016-12-29 14:49:38 -0800 | [diff] [blame] | 96 | private static final boolean DBG = Build.IS_DEBUGGABLE && Log.isLoggable(TAG, Log.DEBUG); |
Jeremy Joslin | e71fe2b | 2017-01-25 11:40:08 -0800 | [diff] [blame] | 97 | private static final boolean VERBOSE = Build.IS_DEBUGGABLE && Log.isLoggable(TAG, Log.VERBOSE); |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 98 | |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 99 | private final Context mContext; |
Amin Shaikh | aa09aa0 | 2016-11-21 17:27:53 -0800 | [diff] [blame] | 100 | private final NetworkScorerAppManager mNetworkScorerAppManager; |
Jeremy Joslin | 3452b69 | 2017-01-17 15:48:13 -0800 | [diff] [blame] | 101 | private final AtomicReference<RequestRecommendationCaller> mReqRecommendationCallerRef; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 102 | @GuardedBy("mScoreCaches") |
| 103 | private final Map<Integer, RemoteCallbackList<INetworkScoreCache>> mScoreCaches; |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame] | 104 | /** Lock used to update mPackageMonitor when scorer package changes occur. */ |
Jeremy Joslin | ce73c6f | 2016-12-29 14:49:38 -0800 | [diff] [blame] | 105 | private final Object mPackageMonitorLock = new Object(); |
| 106 | private final Object mServiceConnectionLock = new Object(); |
| 107 | private final Handler mHandler; |
Jeremy Joslin | cb594f3 | 2017-01-03 17:31:23 -0800 | [diff] [blame] | 108 | private final DispatchingContentObserver mContentObserver; |
Jeff Davidson | 7842f64 | 2014-11-23 13:48:12 -0800 | [diff] [blame] | 109 | |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame] | 110 | @GuardedBy("mPackageMonitorLock") |
| 111 | private NetworkScorerPackageMonitor mPackageMonitor; |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 112 | @GuardedBy("mServiceConnectionLock") |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 113 | private ScoringServiceConnection mServiceConnection; |
Jeremy Joslin | cb594f3 | 2017-01-03 17:31:23 -0800 | [diff] [blame] | 114 | private volatile long mRecommendationRequestTimeoutMs; |
Jeff Davidson | 7842f64 | 2014-11-23 13:48:12 -0800 | [diff] [blame] | 115 | |
Jeremy Joslin | 967b581 | 2016-06-02 07:58:14 -0700 | [diff] [blame] | 116 | private BroadcastReceiver mUserIntentReceiver = new BroadcastReceiver() { |
| 117 | @Override |
| 118 | public void onReceive(Context context, Intent intent) { |
| 119 | final String action = intent.getAction(); |
| 120 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); |
| 121 | if (DBG) Log.d(TAG, "Received " + action + " for userId " + userId); |
| 122 | if (userId == UserHandle.USER_NULL) return; |
| 123 | |
| 124 | if (Intent.ACTION_USER_UNLOCKED.equals(action)) { |
| 125 | onUserUnlocked(userId); |
| 126 | } |
| 127 | } |
| 128 | }; |
| 129 | |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame] | 130 | /** |
| 131 | * Clears scores when the active scorer package is no longer valid and |
| 132 | * manages the service connection. |
| 133 | */ |
| 134 | private class NetworkScorerPackageMonitor extends PackageMonitor { |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 135 | final String mPackageToWatch; |
Jeff Davidson | 7842f64 | 2014-11-23 13:48:12 -0800 | [diff] [blame] | 136 | |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 137 | private NetworkScorerPackageMonitor(String packageToWatch) { |
| 138 | mPackageToWatch = packageToWatch; |
Jeff Davidson | 7842f64 | 2014-11-23 13:48:12 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Override |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame] | 142 | public void onPackageAdded(String packageName, int uid) { |
| 143 | evaluateBinding(packageName, true /* forceUnbind */); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void onPackageRemoved(String packageName, int uid) { |
| 148 | evaluateBinding(packageName, true /* forceUnbind */); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public void onPackageModified(String packageName) { |
| 153 | evaluateBinding(packageName, false /* forceUnbind */); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) { |
| 158 | if (doit) { // "doit" means the force stop happened instead of just being queried for. |
| 159 | for (String packageName : packages) { |
| 160 | evaluateBinding(packageName, true /* forceUnbind */); |
| 161 | } |
| 162 | } |
| 163 | return super.onHandleForceStop(intent, packages, uid, doit); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public void onPackageUpdateFinished(String packageName, int uid) { |
| 168 | evaluateBinding(packageName, true /* forceUnbind */); |
| 169 | } |
| 170 | |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 171 | private void evaluateBinding(String changedPackageName, boolean forceUnbind) { |
| 172 | if (!mPackageToWatch.equals(changedPackageName)) { |
Jeremy Joslin | 86c2a5e | 2016-12-21 13:35:02 -0800 | [diff] [blame] | 173 | // Early exit when we don't care about the package that has changed. |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | if (DBG) { |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 178 | Log.d(TAG, "Evaluating binding for: " + changedPackageName |
Jeremy Joslin | 86c2a5e | 2016-12-21 13:35:02 -0800 | [diff] [blame] | 179 | + ", forceUnbind=" + forceUnbind); |
| 180 | } |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 181 | |
Jeremy Joslin | 86c2a5e | 2016-12-21 13:35:02 -0800 | [diff] [blame] | 182 | final NetworkScorerAppData activeScorer = mNetworkScorerAppManager.getActiveScorer(); |
| 183 | if (activeScorer == null) { |
| 184 | // Package change has invalidated a scorer, this will also unbind any service |
| 185 | // connection. |
| 186 | if (DBG) Log.d(TAG, "No active scorers available."); |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 187 | refreshBinding(); |
| 188 | } else { // The scoring service changed in some way. |
Jeremy Joslin | 86c2a5e | 2016-12-21 13:35:02 -0800 | [diff] [blame] | 189 | if (forceUnbind) { |
| 190 | unbindFromScoringServiceIfNeeded(); |
| 191 | } |
Jeremy Joslin | 86c2a5e | 2016-12-21 13:35:02 -0800 | [diff] [blame] | 192 | if (DBG) { |
Jeremy Joslin | 37e877b | 2017-02-02 11:06:14 -0800 | [diff] [blame] | 193 | Log.d(TAG, "Binding to " + activeScorer.getRecommendationServiceComponent() |
| 194 | + " if needed."); |
Jeremy Joslin | 86c2a5e | 2016-12-21 13:35:02 -0800 | [diff] [blame] | 195 | } |
| 196 | bindToScoringServiceIfNeeded(activeScorer); |
Jeff Davidson | 7842f64 | 2014-11-23 13:48:12 -0800 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
Jeremy Joslin | fa4f08e | 2016-12-06 07:42:38 -0800 | [diff] [blame] | 201 | /** |
Jeremy Joslin | cb594f3 | 2017-01-03 17:31:23 -0800 | [diff] [blame] | 202 | * Dispatches observed content changes to a handler for further processing. |
Jeremy Joslin | fa4f08e | 2016-12-06 07:42:38 -0800 | [diff] [blame] | 203 | */ |
Jeremy Joslin | cb594f3 | 2017-01-03 17:31:23 -0800 | [diff] [blame] | 204 | @VisibleForTesting |
| 205 | public static class DispatchingContentObserver extends ContentObserver { |
| 206 | final private Map<Uri, Integer> mUriEventMap; |
| 207 | final private Context mContext; |
| 208 | final private Handler mHandler; |
Jeremy Joslin | fa4f08e | 2016-12-06 07:42:38 -0800 | [diff] [blame] | 209 | |
Jeremy Joslin | cb594f3 | 2017-01-03 17:31:23 -0800 | [diff] [blame] | 210 | public DispatchingContentObserver(Context context, Handler handler) { |
| 211 | super(handler); |
| 212 | mContext = context; |
| 213 | mHandler = handler; |
| 214 | mUriEventMap = new ArrayMap<>(); |
| 215 | } |
| 216 | |
| 217 | void observe(Uri uri, int what) { |
| 218 | mUriEventMap.put(uri, what); |
| 219 | final ContentResolver resolver = mContext.getContentResolver(); |
| 220 | resolver.registerContentObserver(uri, false /*notifyForDescendants*/, this); |
Jeremy Joslin | fa4f08e | 2016-12-06 07:42:38 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public void onChange(boolean selfChange) { |
| 225 | onChange(selfChange, null); |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public void onChange(boolean selfChange, Uri uri) { |
| 230 | if (DBG) Log.d(TAG, String.format("onChange(%s, %s)", selfChange, uri)); |
Jeremy Joslin | cb594f3 | 2017-01-03 17:31:23 -0800 | [diff] [blame] | 231 | final Integer what = mUriEventMap.get(uri); |
| 232 | if (what != null) { |
| 233 | mHandler.obtainMessage(what).sendToTarget(); |
| 234 | } else { |
| 235 | Log.w(TAG, "No matching event to send for URI = " + uri); |
| 236 | } |
Jeremy Joslin | fa4f08e | 2016-12-06 07:42:38 -0800 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 240 | public NetworkScoreService(Context context) { |
Jeremy Joslin | ce73c6f | 2016-12-29 14:49:38 -0800 | [diff] [blame] | 241 | this(context, new NetworkScorerAppManager(context), Looper.myLooper()); |
Amin Shaikh | aa09aa0 | 2016-11-21 17:27:53 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | @VisibleForTesting |
Jeremy Joslin | ce73c6f | 2016-12-29 14:49:38 -0800 | [diff] [blame] | 245 | NetworkScoreService(Context context, NetworkScorerAppManager networkScoreAppManager, |
| 246 | Looper looper) { |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 247 | mContext = context; |
Amin Shaikh | aa09aa0 | 2016-11-21 17:27:53 -0800 | [diff] [blame] | 248 | mNetworkScorerAppManager = networkScoreAppManager; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 249 | mScoreCaches = new ArrayMap<>(); |
Jeremy Joslin | 967b581 | 2016-06-02 07:58:14 -0700 | [diff] [blame] | 250 | IntentFilter filter = new IntentFilter(Intent.ACTION_USER_UNLOCKED); |
| 251 | // TODO: Need to update when we support per-user scorers. http://b/23422763 |
| 252 | mContext.registerReceiverAsUser( |
| 253 | mUserIntentReceiver, UserHandle.SYSTEM, filter, null /* broadcastPermission*/, |
| 254 | null /* scheduler */); |
Jeremy Joslin | 3452b69 | 2017-01-17 15:48:13 -0800 | [diff] [blame] | 255 | mReqRecommendationCallerRef = new AtomicReference<>( |
| 256 | new RequestRecommendationCaller(TimedRemoteCaller.DEFAULT_CALL_TIMEOUT_MILLIS)); |
Jeremy Joslin | ce73c6f | 2016-12-29 14:49:38 -0800 | [diff] [blame] | 257 | mRecommendationRequestTimeoutMs = TimedRemoteCaller.DEFAULT_CALL_TIMEOUT_MILLIS; |
| 258 | mHandler = new ServiceHandler(looper); |
Jeremy Joslin | cb594f3 | 2017-01-03 17:31:23 -0800 | [diff] [blame] | 259 | mContentObserver = new DispatchingContentObserver(context, mHandler); |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /** Called when the system is ready to run third-party code but before it actually does so. */ |
| 263 | void systemReady() { |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 264 | if (DBG) Log.d(TAG, "systemReady"); |
Jeremy Joslin | cb594f3 | 2017-01-03 17:31:23 -0800 | [diff] [blame] | 265 | registerRecommendationSettingsObserver(); |
Jeff Davidson | 7842f64 | 2014-11-23 13:48:12 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 268 | /** Called when the system is ready for us to start third-party code. */ |
| 269 | void systemRunning() { |
| 270 | if (DBG) Log.d(TAG, "systemRunning"); |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 273 | @VisibleForTesting |
| 274 | void onUserUnlocked(int userId) { |
| 275 | if (DBG) Log.d(TAG, "onUserUnlocked(" + userId + ")"); |
| 276 | refreshBinding(); |
| 277 | } |
| 278 | |
| 279 | private void refreshBinding() { |
| 280 | if (DBG) Log.d(TAG, "refreshBinding()"); |
Jeremy Joslin | 9925c6a | 2017-03-06 10:39:35 -0800 | [diff] [blame] | 281 | // Make sure the scorer is up-to-date |
| 282 | mNetworkScorerAppManager.updateState(); |
Jeremy Joslin | 967b581 | 2016-06-02 07:58:14 -0700 | [diff] [blame] | 283 | registerPackageMonitorIfNeeded(); |
| 284 | bindToScoringServiceIfNeeded(); |
| 285 | } |
| 286 | |
Jeremy Joslin | cb594f3 | 2017-01-03 17:31:23 -0800 | [diff] [blame] | 287 | private void registerRecommendationSettingsObserver() { |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 288 | final Uri packageNameUri = Global.getUriFor(Global.NETWORK_RECOMMENDATIONS_PACKAGE); |
| 289 | mContentObserver.observe(packageNameUri, |
| 290 | ServiceHandler.MSG_RECOMMENDATIONS_PACKAGE_CHANGED); |
Jeremy Joslin | cb594f3 | 2017-01-03 17:31:23 -0800 | [diff] [blame] | 291 | |
| 292 | final Uri timeoutUri = Global.getUriFor(Global.NETWORK_RECOMMENDATION_REQUEST_TIMEOUT_MS); |
| 293 | mContentObserver.observe(timeoutUri, |
| 294 | ServiceHandler.MSG_RECOMMENDATION_REQUEST_TIMEOUT_CHANGED); |
Jeremy Joslin | 9925c6a | 2017-03-06 10:39:35 -0800 | [diff] [blame] | 295 | |
| 296 | final Uri settingUri = Global.getUriFor(Global.NETWORK_RECOMMENDATIONS_ENABLED); |
| 297 | mContentObserver.observe(settingUri, |
| 298 | ServiceHandler.MSG_RECOMMENDATION_ENABLED_SETTING_CHANGED); |
Jeremy Joslin | fa4f08e | 2016-12-06 07:42:38 -0800 | [diff] [blame] | 299 | } |
| 300 | |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 301 | /** |
| 302 | * Ensures the package manager is registered to monitor the current active scorer. |
| 303 | * If a discrepancy is found any previous monitor will be cleaned up |
| 304 | * and a new monitor will be created. |
| 305 | * |
| 306 | * This method is idempotent. |
| 307 | */ |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame] | 308 | private void registerPackageMonitorIfNeeded() { |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 309 | if (DBG) Log.d(TAG, "registerPackageMonitorIfNeeded()"); |
| 310 | final NetworkScorerAppData appData = mNetworkScorerAppManager.getActiveScorer(); |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame] | 311 | synchronized (mPackageMonitorLock) { |
| 312 | // Unregister the current monitor if needed. |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 313 | if (mPackageMonitor != null && (appData == null |
| 314 | || !appData.getRecommendationServicePackageName().equals( |
| 315 | mPackageMonitor.mPackageToWatch))) { |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame] | 316 | if (DBG) { |
| 317 | Log.d(TAG, "Unregistering package monitor for " |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 318 | + mPackageMonitor.mPackageToWatch); |
Jeff Davidson | 7842f64 | 2014-11-23 13:48:12 -0800 | [diff] [blame] | 319 | } |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame] | 320 | mPackageMonitor.unregister(); |
| 321 | mPackageMonitor = null; |
Jeff Davidson | 7842f64 | 2014-11-23 13:48:12 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 324 | // Create and register the monitor if a scorer is active. |
| 325 | if (appData != null && mPackageMonitor == null) { |
| 326 | mPackageMonitor = new NetworkScorerPackageMonitor( |
| 327 | appData.getRecommendationServicePackageName()); |
Xiaohui Chen | e4de5a0 | 2015-09-22 15:33:31 -0700 | [diff] [blame] | 328 | // 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] | 329 | mPackageMonitor.register(mContext, null /* thread */, UserHandle.SYSTEM, |
| 330 | false /* externalStorage */); |
| 331 | if (DBG) { |
| 332 | Log.d(TAG, "Registered package monitor for " |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 333 | + mPackageMonitor.mPackageToWatch); |
Jeff Davidson | 7842f64 | 2014-11-23 13:48:12 -0800 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | } |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 339 | private void bindToScoringServiceIfNeeded() { |
| 340 | if (DBG) Log.d(TAG, "bindToScoringServiceIfNeeded"); |
Amin Shaikh | aa09aa0 | 2016-11-21 17:27:53 -0800 | [diff] [blame] | 341 | NetworkScorerAppData scorerData = mNetworkScorerAppManager.getActiveScorer(); |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 342 | bindToScoringServiceIfNeeded(scorerData); |
| 343 | } |
| 344 | |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 345 | /** |
| 346 | * Ensures the service connection is bound to the current active scorer. |
| 347 | * If a discrepancy is found any previous connection will be cleaned up |
| 348 | * and a new connection will be created. |
| 349 | * |
| 350 | * This method is idempotent. |
| 351 | */ |
Jeremy Joslin | 37e877b | 2017-02-02 11:06:14 -0800 | [diff] [blame] | 352 | private void bindToScoringServiceIfNeeded(NetworkScorerAppData appData) { |
| 353 | if (DBG) Log.d(TAG, "bindToScoringServiceIfNeeded(" + appData + ")"); |
| 354 | if (appData != null) { |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 355 | synchronized (mServiceConnectionLock) { |
| 356 | // If we're connected to a different component then drop it. |
| 357 | if (mServiceConnection != null |
Jeremy Joslin | 37e877b | 2017-02-02 11:06:14 -0800 | [diff] [blame] | 358 | && !mServiceConnection.mAppData.equals(appData)) { |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 359 | unbindFromScoringServiceIfNeeded(); |
| 360 | } |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 361 | |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 362 | // If we're not connected at all then create a new connection. |
| 363 | if (mServiceConnection == null) { |
Jeremy Joslin | 37e877b | 2017-02-02 11:06:14 -0800 | [diff] [blame] | 364 | mServiceConnection = new ScoringServiceConnection(appData); |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 365 | } |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 366 | |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 367 | // Make sure the connection is connected (idempotent) |
| 368 | mServiceConnection.connect(mContext); |
| 369 | } |
Jeremy Joslin | 967b581 | 2016-06-02 07:58:14 -0700 | [diff] [blame] | 370 | } else { // otherwise make sure it isn't bound. |
| 371 | unbindFromScoringServiceIfNeeded(); |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
| 375 | private void unbindFromScoringServiceIfNeeded() { |
| 376 | if (DBG) Log.d(TAG, "unbindFromScoringServiceIfNeeded"); |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 377 | synchronized (mServiceConnectionLock) { |
| 378 | if (mServiceConnection != null) { |
| 379 | mServiceConnection.disconnect(mContext); |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 380 | if (DBG) Log.d(TAG, "Disconnected from: " |
| 381 | + mServiceConnection.mAppData.getRecommendationServiceComponent()); |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 382 | } |
| 383 | mServiceConnection = null; |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 384 | } |
Jeremy Joslin | fa4f08e | 2016-12-06 07:42:38 -0800 | [diff] [blame] | 385 | clearInternal(); |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 388 | @Override |
| 389 | public boolean updateScores(ScoredNetwork[] networks) { |
Jeremy Joslin | 134c9d3 | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 390 | if (!isCallerActiveScorer(getCallingUid())) { |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 391 | throw new SecurityException("Caller with UID " + getCallingUid() + |
| 392 | " is not the active scorer."); |
| 393 | } |
| 394 | |
Jeremy Joslin | 8f5521a | 2016-12-20 14:36:20 -0800 | [diff] [blame] | 395 | final long token = Binder.clearCallingIdentity(); |
| 396 | try { |
| 397 | // Separate networks by type. |
| 398 | Map<Integer, List<ScoredNetwork>> networksByType = new ArrayMap<>(); |
| 399 | for (ScoredNetwork network : networks) { |
| 400 | List<ScoredNetwork> networkList = networksByType.get(network.networkKey.type); |
| 401 | if (networkList == null) { |
| 402 | networkList = new ArrayList<>(); |
| 403 | networksByType.put(network.networkKey.type, networkList); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 404 | } |
Jeremy Joslin | 8f5521a | 2016-12-20 14:36:20 -0800 | [diff] [blame] | 405 | networkList.add(network); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 406 | } |
| 407 | |
Jeremy Joslin | 8f5521a | 2016-12-20 14:36:20 -0800 | [diff] [blame] | 408 | // Pass the scores of each type down to the appropriate network scorer. |
| 409 | for (final Map.Entry<Integer, List<ScoredNetwork>> entry : networksByType.entrySet()) { |
| 410 | final RemoteCallbackList<INetworkScoreCache> callbackList; |
| 411 | final boolean isEmpty; |
| 412 | synchronized (mScoreCaches) { |
| 413 | callbackList = mScoreCaches.get(entry.getKey()); |
| 414 | isEmpty = callbackList == null |
| 415 | || callbackList.getRegisteredCallbackCount() == 0; |
| 416 | } |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 417 | |
Jeremy Joslin | 8f5521a | 2016-12-20 14:36:20 -0800 | [diff] [blame] | 418 | if (isEmpty) { |
| 419 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 420 | Log.v(TAG, "No scorer registered for type " + entry.getKey() |
| 421 | + ", discarding"); |
| 422 | } |
| 423 | continue; |
| 424 | } |
| 425 | |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 426 | final BiConsumer<INetworkScoreCache, Object> consumer = |
Jeremy Joslin | e71fe2b | 2017-01-25 11:40:08 -0800 | [diff] [blame] | 427 | FilteringCacheUpdatingConsumer.create(mContext, entry.getValue(), |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 428 | entry.getKey()); |
| 429 | sendCacheUpdateCallback(consumer, Collections.singleton(callbackList)); |
Jeremy Joslin | 8f5521a | 2016-12-20 14:36:20 -0800 | [diff] [blame] | 430 | } |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 431 | |
Jeremy Joslin | 8f5521a | 2016-12-20 14:36:20 -0800 | [diff] [blame] | 432 | return true; |
| 433 | } finally { |
| 434 | Binder.restoreCallingIdentity(token); |
| 435 | } |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 438 | /** |
| 439 | * A {@link BiConsumer} implementation that filters the given {@link ScoredNetwork} |
| 440 | * list (if needed) before invoking {@link INetworkScoreCache#updateScores(List)} on the |
| 441 | * accepted {@link INetworkScoreCache} implementation. |
| 442 | */ |
| 443 | @VisibleForTesting |
Jeremy Joslin | e71fe2b | 2017-01-25 11:40:08 -0800 | [diff] [blame] | 444 | static class FilteringCacheUpdatingConsumer |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 445 | implements BiConsumer<INetworkScoreCache, Object> { |
| 446 | private final Context mContext; |
| 447 | private final List<ScoredNetwork> mScoredNetworkList; |
| 448 | private final int mNetworkType; |
Jeremy Joslin | e71fe2b | 2017-01-25 11:40:08 -0800 | [diff] [blame] | 449 | // TODO: 1/23/17 - Consider a Map if we implement more filters. |
| 450 | // These are created on-demand to defer the construction cost until |
| 451 | // an instance is actually needed. |
| 452 | private UnaryOperator<List<ScoredNetwork>> mCurrentNetworkFilter; |
| 453 | private UnaryOperator<List<ScoredNetwork>> mScanResultsFilter; |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 454 | |
Jeremy Joslin | e71fe2b | 2017-01-25 11:40:08 -0800 | [diff] [blame] | 455 | static FilteringCacheUpdatingConsumer create(Context context, |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 456 | List<ScoredNetwork> scoredNetworkList, int networkType) { |
Jeremy Joslin | e71fe2b | 2017-01-25 11:40:08 -0800 | [diff] [blame] | 457 | return new FilteringCacheUpdatingConsumer(context, scoredNetworkList, networkType, |
| 458 | null, null); |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | @VisibleForTesting |
Jeremy Joslin | e71fe2b | 2017-01-25 11:40:08 -0800 | [diff] [blame] | 462 | FilteringCacheUpdatingConsumer(Context context, |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 463 | List<ScoredNetwork> scoredNetworkList, int networkType, |
Jeremy Joslin | e71fe2b | 2017-01-25 11:40:08 -0800 | [diff] [blame] | 464 | UnaryOperator<List<ScoredNetwork>> currentNetworkFilter, |
| 465 | UnaryOperator<List<ScoredNetwork>> scanResultsFilter) { |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 466 | mContext = context; |
| 467 | mScoredNetworkList = scoredNetworkList; |
| 468 | mNetworkType = networkType; |
| 469 | mCurrentNetworkFilter = currentNetworkFilter; |
| 470 | mScanResultsFilter = scanResultsFilter; |
| 471 | } |
| 472 | |
| 473 | @Override |
| 474 | public void accept(INetworkScoreCache networkScoreCache, Object cookie) { |
| 475 | int filterType = NetworkScoreManager.CACHE_FILTER_NONE; |
| 476 | if (cookie instanceof Integer) { |
| 477 | filterType = (Integer) cookie; |
| 478 | } |
| 479 | |
| 480 | try { |
| 481 | final List<ScoredNetwork> filteredNetworkList = |
| 482 | filterScores(mScoredNetworkList, filterType); |
| 483 | if (!filteredNetworkList.isEmpty()) { |
Jeremy Joslin | e71fe2b | 2017-01-25 11:40:08 -0800 | [diff] [blame] | 484 | networkScoreCache.updateScores(filteredNetworkList); |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 485 | } |
| 486 | } catch (RemoteException e) { |
Jeremy Joslin | e71fe2b | 2017-01-25 11:40:08 -0800 | [diff] [blame] | 487 | if (VERBOSE) { |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 488 | Log.v(TAG, "Unable to update scores of type " + mNetworkType, e); |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * Applies the appropriate filter and returns the filtered results. |
| 495 | */ |
| 496 | private List<ScoredNetwork> filterScores(List<ScoredNetwork> scoredNetworkList, |
| 497 | int filterType) { |
| 498 | switch (filterType) { |
| 499 | case NetworkScoreManager.CACHE_FILTER_NONE: |
| 500 | return scoredNetworkList; |
| 501 | |
| 502 | case NetworkScoreManager.CACHE_FILTER_CURRENT_NETWORK: |
| 503 | if (mCurrentNetworkFilter == null) { |
| 504 | mCurrentNetworkFilter = |
| 505 | new CurrentNetworkScoreCacheFilter(new WifiInfoSupplier(mContext)); |
| 506 | } |
| 507 | return mCurrentNetworkFilter.apply(scoredNetworkList); |
| 508 | |
| 509 | case NetworkScoreManager.CACHE_FILTER_SCAN_RESULTS: |
| 510 | if (mScanResultsFilter == null) { |
| 511 | mScanResultsFilter = new ScanResultsScoreCacheFilter( |
| 512 | new ScanResultsSupplier(mContext)); |
| 513 | } |
| 514 | return mScanResultsFilter.apply(scoredNetworkList); |
| 515 | |
| 516 | default: |
| 517 | Log.w(TAG, "Unknown filter type: " + filterType); |
| 518 | return scoredNetworkList; |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Helper class that improves the testability of the cache filter Functions. |
| 525 | */ |
| 526 | private static class WifiInfoSupplier implements Supplier<WifiInfo> { |
| 527 | private final Context mContext; |
| 528 | |
| 529 | WifiInfoSupplier(Context context) { |
| 530 | mContext = context; |
| 531 | } |
| 532 | |
| 533 | @Override |
| 534 | public WifiInfo get() { |
| 535 | WifiManager wifiManager = mContext.getSystemService(WifiManager.class); |
| 536 | if (wifiManager != null) { |
| 537 | return wifiManager.getConnectionInfo(); |
| 538 | } |
| 539 | Log.w(TAG, "WifiManager is null, failed to return the WifiInfo."); |
| 540 | return null; |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * Helper class that improves the testability of the cache filter Functions. |
| 546 | */ |
| 547 | private static class ScanResultsSupplier implements Supplier<List<ScanResult>> { |
| 548 | private final Context mContext; |
| 549 | |
| 550 | ScanResultsSupplier(Context context) { |
| 551 | mContext = context; |
| 552 | } |
| 553 | |
| 554 | @Override |
| 555 | public List<ScanResult> get() { |
| 556 | WifiScanner wifiScanner = mContext.getSystemService(WifiScanner.class); |
| 557 | if (wifiScanner != null) { |
| 558 | return wifiScanner.getSingleScanResults(); |
| 559 | } |
| 560 | Log.w(TAG, "WifiScanner is null, failed to return scan results."); |
| 561 | return Collections.emptyList(); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * Filters the given set of {@link ScoredNetwork}s and returns a new List containing only the |
| 567 | * {@link ScoredNetwork} associated with the current network. If no network is connected the |
| 568 | * returned list will be empty. |
| 569 | * <p> |
| 570 | * Note: this filter performs some internal caching for consistency and performance. The |
| 571 | * current network is determined at construction time and never changed. Also, the |
| 572 | * last filtered list is saved so if the same input is provided multiple times in a row |
| 573 | * the computation is only done once. |
| 574 | */ |
| 575 | @VisibleForTesting |
Jeremy Joslin | e71fe2b | 2017-01-25 11:40:08 -0800 | [diff] [blame] | 576 | static class CurrentNetworkScoreCacheFilter implements UnaryOperator<List<ScoredNetwork>> { |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 577 | private final NetworkKey mCurrentNetwork; |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 578 | |
| 579 | CurrentNetworkScoreCacheFilter(Supplier<WifiInfo> wifiInfoSupplier) { |
| 580 | mCurrentNetwork = NetworkKey.createFromWifiInfo(wifiInfoSupplier.get()); |
| 581 | } |
| 582 | |
| 583 | @Override |
| 584 | public List<ScoredNetwork> apply(List<ScoredNetwork> scoredNetworks) { |
| 585 | if (mCurrentNetwork == null || scoredNetworks.isEmpty()) { |
| 586 | return Collections.emptyList(); |
| 587 | } |
| 588 | |
Jeremy Joslin | e71fe2b | 2017-01-25 11:40:08 -0800 | [diff] [blame] | 589 | for (int i = 0; i < scoredNetworks.size(); i++) { |
| 590 | final ScoredNetwork scoredNetwork = scoredNetworks.get(i); |
| 591 | if (scoredNetwork.networkKey.equals(mCurrentNetwork)) { |
| 592 | return Collections.singletonList(scoredNetwork); |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
Jeremy Joslin | e71fe2b | 2017-01-25 11:40:08 -0800 | [diff] [blame] | 596 | return Collections.emptyList(); |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | |
| 600 | /** |
| 601 | * Filters the given set of {@link ScoredNetwork}s and returns a new List containing only the |
| 602 | * {@link ScoredNetwork} associated with the current set of {@link ScanResult}s. |
| 603 | * If there are no {@link ScanResult}s the returned list will be empty. |
| 604 | * <p> |
| 605 | * Note: this filter performs some internal caching for consistency and performance. The |
| 606 | * current set of ScanResults is determined at construction time and never changed. |
| 607 | * Also, the last filtered list is saved so if the same input is provided multiple |
| 608 | * times in a row the computation is only done once. |
| 609 | */ |
| 610 | @VisibleForTesting |
Jeremy Joslin | e71fe2b | 2017-01-25 11:40:08 -0800 | [diff] [blame] | 611 | static class ScanResultsScoreCacheFilter implements UnaryOperator<List<ScoredNetwork>> { |
| 612 | private final Set<NetworkKey> mScanResultKeys; |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 613 | |
| 614 | ScanResultsScoreCacheFilter(Supplier<List<ScanResult>> resultsSupplier) { |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 615 | List<ScanResult> scanResults = resultsSupplier.get(); |
Jeremy Joslin | e71fe2b | 2017-01-25 11:40:08 -0800 | [diff] [blame] | 616 | final int size = scanResults.size(); |
| 617 | mScanResultKeys = new ArraySet<>(size); |
| 618 | for (int i = 0; i < size; i++) { |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 619 | ScanResult scanResult = scanResults.get(i); |
Stephen Chen | fde900d | 2017-02-14 16:40:21 -0800 | [diff] [blame] | 620 | NetworkKey key = NetworkKey.createFromScanResult(scanResult); |
| 621 | if (key != null) { |
| 622 | mScanResultKeys.add(key); |
| 623 | } |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
| 627 | @Override |
| 628 | public List<ScoredNetwork> apply(List<ScoredNetwork> scoredNetworks) { |
| 629 | if (mScanResultKeys.isEmpty() || scoredNetworks.isEmpty()) { |
| 630 | return Collections.emptyList(); |
| 631 | } |
| 632 | |
Jeremy Joslin | e71fe2b | 2017-01-25 11:40:08 -0800 | [diff] [blame] | 633 | List<ScoredNetwork> filteredScores = new ArrayList<>(); |
| 634 | for (int i = 0; i < scoredNetworks.size(); i++) { |
| 635 | final ScoredNetwork scoredNetwork = scoredNetworks.get(i); |
| 636 | if (mScanResultKeys.contains(scoredNetwork.networkKey)) { |
| 637 | filteredScores.add(scoredNetwork); |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 638 | } |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 639 | } |
| 640 | |
Jeremy Joslin | e71fe2b | 2017-01-25 11:40:08 -0800 | [diff] [blame] | 641 | return filteredScores; |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | |
Jeremy Joslin | 35f34ea | 2017-02-03 09:13:51 -0800 | [diff] [blame] | 645 | private boolean callerCanRequestScores() { |
Jeremy Joslin | 5519d7c | 2017-01-06 14:36:54 -0800 | [diff] [blame] | 646 | // REQUEST_NETWORK_SCORES is a signature only permission. |
| 647 | return mContext.checkCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES) == |
| 648 | PackageManager.PERMISSION_GRANTED; |
| 649 | } |
| 650 | |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 651 | @Override |
| 652 | public boolean clearScores() { |
Jeremy Joslin | 5519d7c | 2017-01-06 14:36:54 -0800 | [diff] [blame] | 653 | // Only the active scorer or the system should be allowed to flush all scores. |
Jeremy Joslin | 35f34ea | 2017-02-03 09:13:51 -0800 | [diff] [blame] | 654 | if (isCallerActiveScorer(getCallingUid()) || callerCanRequestScores()) { |
Jeremy Joslin | 8f5521a | 2016-12-20 14:36:20 -0800 | [diff] [blame] | 655 | final long token = Binder.clearCallingIdentity(); |
| 656 | try { |
| 657 | clearInternal(); |
| 658 | return true; |
| 659 | } finally { |
| 660 | Binder.restoreCallingIdentity(token); |
| 661 | } |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 662 | } else { |
| 663 | throw new SecurityException( |
| 664 | "Caller is neither the active scorer nor the scorer manager."); |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | @Override |
| 669 | public boolean setActiveScorer(String packageName) { |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 670 | // Only the system can set the active scorer |
Jeremy Joslin | 3bddadd | 2017-03-21 16:16:46 -0700 | [diff] [blame] | 671 | if (!isCallerSystemProcess(getCallingUid()) && !callerCanRequestScores()) { |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 672 | throw new SecurityException( |
| 673 | "Caller is neither the system process nor a score requester."); |
| 674 | } |
Jeremy Joslin | e9052a3 | 2017-02-27 15:47:54 -0800 | [diff] [blame] | 675 | |
| 676 | return mNetworkScorerAppManager.setActiveScorer(packageName); |
Jeff Davidson | 26fd143 | 2014-07-29 09:39:52 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Jeremy Joslin | 134c9d3 | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 679 | /** |
| 680 | * Determine whether the application with the given UID is the enabled scorer. |
| 681 | * |
| 682 | * @param callingUid the UID to check |
| 683 | * @return true if the provided UID is the active scorer, false otherwise. |
| 684 | */ |
| 685 | @Override |
| 686 | public boolean isCallerActiveScorer(int callingUid) { |
| 687 | synchronized (mServiceConnectionLock) { |
Jeremy Joslin | 37e877b | 2017-02-02 11:06:14 -0800 | [diff] [blame] | 688 | return mServiceConnection != null |
| 689 | && mServiceConnection.mAppData.packageUid == callingUid; |
Jeremy Joslin | 134c9d3 | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 690 | } |
| 691 | } |
| 692 | |
Jeremy Joslin | a5172f6 | 2017-02-02 14:27:05 -0800 | [diff] [blame] | 693 | private boolean isCallerSystemProcess(int callingUid) { |
| 694 | return callingUid == Process.SYSTEM_UID; |
| 695 | } |
| 696 | |
Jeremy Joslin | 6c1ca28 | 2017-01-10 13:08:32 -0800 | [diff] [blame] | 697 | /** |
| 698 | * Obtain the package name of the current active network scorer. |
| 699 | * |
| 700 | * @return the full package name of the current active scorer, or null if there is no active |
| 701 | * scorer. |
| 702 | */ |
| 703 | @Override |
| 704 | public String getActiveScorerPackage() { |
| 705 | synchronized (mServiceConnectionLock) { |
| 706 | if (mServiceConnection != null) { |
Jeremy Joslin | 37e877b | 2017-02-02 11:06:14 -0800 | [diff] [blame] | 707 | return mServiceConnection.getPackageName(); |
Jeremy Joslin | 6c1ca28 | 2017-01-10 13:08:32 -0800 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | return null; |
| 711 | } |
| 712 | |
Jeremy Joslin | a5172f6 | 2017-02-02 14:27:05 -0800 | [diff] [blame] | 713 | /** |
| 714 | * Returns metadata about the active scorer or <code>null</code> if there is no active scorer. |
| 715 | */ |
| 716 | @Override |
| 717 | public NetworkScorerAppData getActiveScorer() { |
| 718 | // Only the system can access this data. |
| 719 | if (isCallerSystemProcess(getCallingUid()) || callerCanRequestScores()) { |
| 720 | synchronized (mServiceConnectionLock) { |
| 721 | if (mServiceConnection != null) { |
| 722 | return mServiceConnection.mAppData; |
| 723 | } |
| 724 | } |
| 725 | } else { |
| 726 | throw new SecurityException( |
| 727 | "Caller is neither the system process nor a score requester."); |
| 728 | } |
| 729 | |
| 730 | return null; |
| 731 | } |
| 732 | |
Jeremy Joslin | f95c865 | 2017-02-09 15:32:04 -0800 | [diff] [blame] | 733 | /** |
| 734 | * Returns the list of available scorer apps. The list will be empty if there are |
| 735 | * no valid scorers. |
| 736 | */ |
| 737 | @Override |
| 738 | public List<NetworkScorerAppData> getAllValidScorers() { |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 739 | // Only the system can access this data. |
Jeremy Joslin | 3bddadd | 2017-03-21 16:16:46 -0700 | [diff] [blame] | 740 | if (!isCallerSystemProcess(getCallingUid()) && !callerCanRequestScores()) { |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 741 | throw new SecurityException( |
| 742 | "Caller is neither the system process nor a score requester."); |
| 743 | } |
Jeremy Joslin | e9052a3 | 2017-02-27 15:47:54 -0800 | [diff] [blame] | 744 | |
| 745 | return mNetworkScorerAppManager.getAllValidScorers(); |
Jeremy Joslin | f95c865 | 2017-02-09 15:32:04 -0800 | [diff] [blame] | 746 | } |
| 747 | |
Jeff Davidson | 26fd143 | 2014-07-29 09:39:52 -0700 | [diff] [blame] | 748 | @Override |
| 749 | public void disableScoring() { |
Jeremy Joslin | 5519d7c | 2017-01-06 14:36:54 -0800 | [diff] [blame] | 750 | // Only the active scorer or the system should be allowed to disable scoring. |
Jeremy Joslin | 3bddadd | 2017-03-21 16:16:46 -0700 | [diff] [blame] | 751 | if (!isCallerActiveScorer(getCallingUid()) && !callerCanRequestScores()) { |
Jeff Davidson | 26fd143 | 2014-07-29 09:39:52 -0700 | [diff] [blame] | 752 | throw new SecurityException( |
| 753 | "Caller is neither the active scorer nor the scorer manager."); |
Jeff Davidson | b096bdc | 2014-07-01 12:29:11 -0700 | [diff] [blame] | 754 | } |
Jeremy Joslin | e9052a3 | 2017-02-27 15:47:54 -0800 | [diff] [blame] | 755 | |
| 756 | // no-op for now but we could write to the setting if needed. |
Jeff Davidson | 26fd143 | 2014-07-29 09:39:52 -0700 | [diff] [blame] | 757 | } |
| 758 | |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 759 | /** Clear scores. Callers are responsible for checking permissions as appropriate. */ |
| 760 | private void clearInternal() { |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 761 | sendCacheUpdateCallback(new BiConsumer<INetworkScoreCache, Object>() { |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 762 | @Override |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 763 | public void accept(INetworkScoreCache networkScoreCache, Object cookie) { |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 764 | try { |
| 765 | networkScoreCache.clearScores(); |
| 766 | } catch (RemoteException e) { |
| 767 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 768 | Log.v(TAG, "Unable to clear scores", e); |
| 769 | } |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 770 | } |
| 771 | } |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 772 | }, getScoreCacheLists()); |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | @Override |
Jeremy Joslin | c5ac587 | 2016-11-30 15:05:40 -0800 | [diff] [blame] | 776 | public void registerNetworkScoreCache(int networkType, |
| 777 | INetworkScoreCache scoreCache, |
| 778 | int filterType) { |
Jeremy Joslin | 5519d7c | 2017-01-06 14:36:54 -0800 | [diff] [blame] | 779 | mContext.enforceCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES, TAG); |
Jeremy Joslin | 8f5521a | 2016-12-20 14:36:20 -0800 | [diff] [blame] | 780 | final long token = Binder.clearCallingIdentity(); |
| 781 | try { |
| 782 | synchronized (mScoreCaches) { |
| 783 | RemoteCallbackList<INetworkScoreCache> callbackList = mScoreCaches.get(networkType); |
| 784 | if (callbackList == null) { |
| 785 | callbackList = new RemoteCallbackList<>(); |
| 786 | mScoreCaches.put(networkType, callbackList); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 787 | } |
Jeremy Joslin | 8f5521a | 2016-12-20 14:36:20 -0800 | [diff] [blame] | 788 | if (!callbackList.register(scoreCache, filterType)) { |
| 789 | if (callbackList.getRegisteredCallbackCount() == 0) { |
| 790 | mScoreCaches.remove(networkType); |
| 791 | } |
| 792 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 793 | Log.v(TAG, "Unable to register NetworkScoreCache for type " + networkType); |
| 794 | } |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 795 | } |
| 796 | } |
Jeremy Joslin | 8f5521a | 2016-12-20 14:36:20 -0800 | [diff] [blame] | 797 | } finally { |
| 798 | Binder.restoreCallingIdentity(token); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 799 | } |
| 800 | } |
| 801 | |
| 802 | @Override |
| 803 | public void unregisterNetworkScoreCache(int networkType, INetworkScoreCache scoreCache) { |
Jeremy Joslin | 5519d7c | 2017-01-06 14:36:54 -0800 | [diff] [blame] | 804 | mContext.enforceCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES, TAG); |
Jeremy Joslin | 8f5521a | 2016-12-20 14:36:20 -0800 | [diff] [blame] | 805 | final long token = Binder.clearCallingIdentity(); |
| 806 | try { |
| 807 | synchronized (mScoreCaches) { |
| 808 | RemoteCallbackList<INetworkScoreCache> callbackList = mScoreCaches.get(networkType); |
| 809 | if (callbackList == null || !callbackList.unregister(scoreCache)) { |
| 810 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 811 | Log.v(TAG, "Unable to unregister NetworkScoreCache for type " |
| 812 | + networkType); |
| 813 | } |
| 814 | } else if (callbackList.getRegisteredCallbackCount() == 0) { |
| 815 | mScoreCaches.remove(networkType); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 816 | } |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 817 | } |
Jeremy Joslin | 8f5521a | 2016-12-20 14:36:20 -0800 | [diff] [blame] | 818 | } finally { |
| 819 | Binder.restoreCallingIdentity(token); |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 820 | } |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | @Override |
Jeremy Joslin | d1daf6d | 2016-11-28 17:47:35 -0800 | [diff] [blame] | 824 | public RecommendationResult requestRecommendation(RecommendationRequest request) { |
Jeremy Joslin | 5519d7c | 2017-01-06 14:36:54 -0800 | [diff] [blame] | 825 | mContext.enforceCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES, TAG); |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 826 | throwIfCalledOnMainThread(); |
Jeremy Joslin | 8f5521a | 2016-12-20 14:36:20 -0800 | [diff] [blame] | 827 | final long token = Binder.clearCallingIdentity(); |
| 828 | try { |
| 829 | final INetworkRecommendationProvider provider = getRecommendationProvider(); |
| 830 | if (provider != null) { |
| 831 | try { |
Jeremy Joslin | 3452b69 | 2017-01-17 15:48:13 -0800 | [diff] [blame] | 832 | final RequestRecommendationCaller caller = mReqRecommendationCallerRef.get(); |
| 833 | return caller.getRecommendationResult(provider, request); |
Jeremy Joslin | 8f5521a | 2016-12-20 14:36:20 -0800 | [diff] [blame] | 834 | } catch (RemoteException | TimeoutException e) { |
| 835 | Log.w(TAG, "Failed to request a recommendation.", e); |
Jeremy Joslin | e71fe2b | 2017-01-25 11:40:08 -0800 | [diff] [blame] | 836 | // TODO: 12/15/16 - Keep track of failures. |
Jeremy Joslin | 8f5521a | 2016-12-20 14:36:20 -0800 | [diff] [blame] | 837 | } |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 838 | } |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 839 | |
Jeremy Joslin | 8f5521a | 2016-12-20 14:36:20 -0800 | [diff] [blame] | 840 | if (DBG) { |
| 841 | Log.d(TAG, "Returning the default network recommendation."); |
| 842 | } |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 843 | |
Jeremy Joslin | 26a45e5 | 2017-01-18 11:55:17 -0800 | [diff] [blame] | 844 | if (request != null && request.getDefaultWifiConfig() != null) { |
Jeremy Joslin | 8f5521a | 2016-12-20 14:36:20 -0800 | [diff] [blame] | 845 | return RecommendationResult.createConnectRecommendation( |
Jeremy Joslin | 26a45e5 | 2017-01-18 11:55:17 -0800 | [diff] [blame] | 846 | request.getDefaultWifiConfig()); |
Jeremy Joslin | 8f5521a | 2016-12-20 14:36:20 -0800 | [diff] [blame] | 847 | } |
| 848 | return RecommendationResult.createDoNotConnectRecommendation(); |
| 849 | } finally { |
| 850 | Binder.restoreCallingIdentity(token); |
Jeremy Joslin | d1daf6d | 2016-11-28 17:47:35 -0800 | [diff] [blame] | 851 | } |
Jeremy Joslin | d1daf6d | 2016-11-28 17:47:35 -0800 | [diff] [blame] | 852 | } |
| 853 | |
Jeremy Joslin | 998d7ca | 2016-12-28 15:56:46 -0800 | [diff] [blame] | 854 | /** |
| 855 | * Request a recommendation for the best network to connect to |
| 856 | * taking into account the inputs from the {@link RecommendationRequest}. |
| 857 | * |
| 858 | * @param request a {@link RecommendationRequest} instance containing the details of the request |
| 859 | * @param remoteCallback a {@link IRemoteCallback} instance to invoke when the recommendation |
| 860 | * is available. |
| 861 | * @throws SecurityException if the caller is not the system |
| 862 | */ |
| 863 | @Override |
| 864 | public void requestRecommendationAsync(RecommendationRequest request, |
| 865 | RemoteCallback remoteCallback) { |
Jeremy Joslin | 6397ab5 | 2017-01-18 15:12:01 -0800 | [diff] [blame] | 866 | mContext.enforceCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES, TAG); |
Jeremy Joslin | 998d7ca | 2016-12-28 15:56:46 -0800 | [diff] [blame] | 867 | |
Jeremy Joslin | ce73c6f | 2016-12-29 14:49:38 -0800 | [diff] [blame] | 868 | final OneTimeCallback oneTimeCallback = new OneTimeCallback(remoteCallback); |
| 869 | final Pair<RecommendationRequest, OneTimeCallback> pair = |
| 870 | Pair.create(request, oneTimeCallback); |
| 871 | final Message timeoutMsg = mHandler.obtainMessage( |
| 872 | ServiceHandler.MSG_RECOMMENDATION_REQUEST_TIMEOUT, pair); |
| 873 | final INetworkRecommendationProvider provider = getRecommendationProvider(); |
| 874 | final long token = Binder.clearCallingIdentity(); |
| 875 | try { |
| 876 | if (provider != null) { |
| 877 | try { |
| 878 | mHandler.sendMessageDelayed(timeoutMsg, mRecommendationRequestTimeoutMs); |
| 879 | provider.requestRecommendation(request, new IRemoteCallback.Stub() { |
| 880 | @Override |
| 881 | public void sendResult(Bundle data) throws RemoteException { |
| 882 | // Remove the timeout message |
| 883 | mHandler.removeMessages(timeoutMsg.what, pair); |
| 884 | oneTimeCallback.sendResult(data); |
| 885 | } |
| 886 | }, 0 /*sequence*/); |
| 887 | return; |
| 888 | } catch (RemoteException e) { |
| 889 | Log.w(TAG, "Failed to request a recommendation.", e); |
Jeremy Joslin | e71fe2b | 2017-01-25 11:40:08 -0800 | [diff] [blame] | 890 | // TODO: 12/15/16 - Keep track of failures. |
Jeremy Joslin | ce73c6f | 2016-12-29 14:49:38 -0800 | [diff] [blame] | 891 | // Remove the timeout message |
| 892 | mHandler.removeMessages(timeoutMsg.what, pair); |
| 893 | // Will fall through and send back the default recommendation. |
| 894 | } |
| 895 | } |
| 896 | } finally { |
| 897 | Binder.restoreCallingIdentity(token); |
Jeremy Joslin | 998d7ca | 2016-12-28 15:56:46 -0800 | [diff] [blame] | 898 | } |
| 899 | |
Jeremy Joslin | ce73c6f | 2016-12-29 14:49:38 -0800 | [diff] [blame] | 900 | // Else send back the default recommendation. |
| 901 | sendDefaultRecommendationResponse(request, oneTimeCallback); |
Jeremy Joslin | 998d7ca | 2016-12-28 15:56:46 -0800 | [diff] [blame] | 902 | } |
| 903 | |
Jeremy Joslin | d1daf6d | 2016-11-28 17:47:35 -0800 | [diff] [blame] | 904 | @Override |
Jeremy Joslin | b2087a1 | 2016-12-13 16:11:51 -0800 | [diff] [blame] | 905 | public boolean requestScores(NetworkKey[] networks) { |
Jeremy Joslin | 5519d7c | 2017-01-06 14:36:54 -0800 | [diff] [blame] | 906 | mContext.enforceCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES, TAG); |
Jeremy Joslin | 8f5521a | 2016-12-20 14:36:20 -0800 | [diff] [blame] | 907 | final long token = Binder.clearCallingIdentity(); |
| 908 | try { |
| 909 | final INetworkRecommendationProvider provider = getRecommendationProvider(); |
| 910 | if (provider != null) { |
| 911 | try { |
| 912 | provider.requestScores(networks); |
Jeremy Joslin | e71fe2b | 2017-01-25 11:40:08 -0800 | [diff] [blame] | 913 | // TODO: 12/15/16 - Consider pushing null scores into the cache to |
Jeremy Joslin | 8f5521a | 2016-12-20 14:36:20 -0800 | [diff] [blame] | 914 | // prevent repeated requests for the same scores. |
| 915 | return true; |
| 916 | } catch (RemoteException e) { |
| 917 | Log.w(TAG, "Failed to request scores.", e); |
Jeremy Joslin | e71fe2b | 2017-01-25 11:40:08 -0800 | [diff] [blame] | 918 | // TODO: 12/15/16 - Keep track of failures. |
Jeremy Joslin | 8f5521a | 2016-12-20 14:36:20 -0800 | [diff] [blame] | 919 | } |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 920 | } |
Jeremy Joslin | 8f5521a | 2016-12-20 14:36:20 -0800 | [diff] [blame] | 921 | return false; |
| 922 | } finally { |
| 923 | Binder.restoreCallingIdentity(token); |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 924 | } |
Jeremy Joslin | b2087a1 | 2016-12-13 16:11:51 -0800 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | @Override |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 928 | protected void dump(final FileDescriptor fd, final PrintWriter writer, final String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame^] | 929 | if (!DumpUtils.checkDumpPermission(mContext, TAG, writer)) return; |
Jeremy Joslin | 534b6cf | 2017-01-25 18:20:21 -0800 | [diff] [blame] | 930 | final long token = Binder.clearCallingIdentity(); |
| 931 | try { |
| 932 | NetworkScorerAppData currentScorer = mNetworkScorerAppManager.getActiveScorer(); |
| 933 | if (currentScorer == null) { |
| 934 | writer.println("Scoring is disabled."); |
| 935 | return; |
| 936 | } |
Jeremy Joslin | 37e877b | 2017-02-02 11:06:14 -0800 | [diff] [blame] | 937 | writer.println("Current scorer: " + currentScorer); |
Jeremy Joslin | 4ad7ca0 | 2017-02-13 11:35:03 -0800 | [diff] [blame] | 938 | writer.println("RecommendationRequestTimeoutMs: " + mRecommendationRequestTimeoutMs); |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 939 | |
Jeremy Joslin | 534b6cf | 2017-01-25 18:20:21 -0800 | [diff] [blame] | 940 | sendCacheUpdateCallback(new BiConsumer<INetworkScoreCache, Object>() { |
| 941 | @Override |
| 942 | public void accept(INetworkScoreCache networkScoreCache, Object cookie) { |
| 943 | try { |
| 944 | TransferPipe.dumpAsync(networkScoreCache.asBinder(), fd, args); |
| 945 | } catch (IOException | RemoteException e) { |
| 946 | writer.println("Failed to dump score cache: " + e); |
| 947 | } |
| 948 | } |
| 949 | }, getScoreCacheLists()); |
| 950 | |
| 951 | synchronized (mServiceConnectionLock) { |
| 952 | if (mServiceConnection != null) { |
| 953 | mServiceConnection.dump(fd, writer, args); |
| 954 | } else { |
| 955 | writer.println("ScoringServiceConnection: null"); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 956 | } |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 957 | } |
Jeremy Joslin | 534b6cf | 2017-01-25 18:20:21 -0800 | [diff] [blame] | 958 | writer.flush(); |
| 959 | } finally { |
| 960 | Binder.restoreCallingIdentity(token); |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 961 | } |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 962 | } |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 963 | |
| 964 | /** |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 965 | * 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] | 966 | * |
| 967 | * <p>May be used to perform an action on all score caches without potentially strange behavior |
| 968 | * if a new scorer is registered during that action's execution. |
| 969 | */ |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 970 | private Collection<RemoteCallbackList<INetworkScoreCache>> getScoreCacheLists() { |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 971 | synchronized (mScoreCaches) { |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 972 | return new ArrayList<>(mScoreCaches.values()); |
| 973 | } |
| 974 | } |
| 975 | |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 976 | private void sendCacheUpdateCallback(BiConsumer<INetworkScoreCache, Object> consumer, |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 977 | Collection<RemoteCallbackList<INetworkScoreCache>> remoteCallbackLists) { |
| 978 | for (RemoteCallbackList<INetworkScoreCache> callbackList : remoteCallbackLists) { |
| 979 | synchronized (callbackList) { // Ensure only one active broadcast per RemoteCallbackList |
| 980 | final int count = callbackList.beginBroadcast(); |
| 981 | try { |
| 982 | for (int i = 0; i < count; i++) { |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 983 | consumer.accept(callbackList.getBroadcastItem(i), |
Jeremy Joslin | 7890e19 | 2017-02-06 11:14:34 -0800 | [diff] [blame] | 984 | callbackList.getBroadcastCookie(i)); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 985 | } |
| 986 | } finally { |
| 987 | callbackList.finishBroadcast(); |
| 988 | } |
| 989 | } |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 990 | } |
| 991 | } |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 992 | |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 993 | private void throwIfCalledOnMainThread() { |
| 994 | if (Thread.currentThread() == mContext.getMainLooper().getThread()) { |
| 995 | throw new RuntimeException("Cannot invoke on the main thread"); |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | @Nullable |
| 1000 | private INetworkRecommendationProvider getRecommendationProvider() { |
| 1001 | synchronized (mServiceConnectionLock) { |
| 1002 | if (mServiceConnection != null) { |
| 1003 | return mServiceConnection.getRecommendationProvider(); |
| 1004 | } |
| 1005 | } |
| 1006 | return null; |
| 1007 | } |
| 1008 | |
Jeremy Joslin | ce73c6f | 2016-12-29 14:49:38 -0800 | [diff] [blame] | 1009 | @VisibleForTesting |
Jeremy Joslin | cb594f3 | 2017-01-03 17:31:23 -0800 | [diff] [blame] | 1010 | public void refreshRecommendationRequestTimeoutMs() { |
| 1011 | final ContentResolver cr = mContext.getContentResolver(); |
| 1012 | long timeoutMs = Settings.Global.getLong(cr, |
| 1013 | Global.NETWORK_RECOMMENDATION_REQUEST_TIMEOUT_MS, -1L /*default*/); |
| 1014 | if (timeoutMs < 0) { |
| 1015 | timeoutMs = TimedRemoteCaller.DEFAULT_CALL_TIMEOUT_MILLIS; |
| 1016 | } |
| 1017 | if (DBG) Log.d(TAG, "Updating the recommendation request timeout to " + timeoutMs + " ms"); |
| 1018 | mRecommendationRequestTimeoutMs = timeoutMs; |
Jeremy Joslin | 3452b69 | 2017-01-17 15:48:13 -0800 | [diff] [blame] | 1019 | mReqRecommendationCallerRef.set(new RequestRecommendationCaller(timeoutMs)); |
Jeremy Joslin | ce73c6f | 2016-12-29 14:49:38 -0800 | [diff] [blame] | 1020 | } |
| 1021 | |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 1022 | private static class ScoringServiceConnection implements ServiceConnection { |
Jeremy Joslin | 37e877b | 2017-02-02 11:06:14 -0800 | [diff] [blame] | 1023 | private final NetworkScorerAppData mAppData; |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 1024 | private volatile boolean mBound = false; |
| 1025 | private volatile boolean mConnected = false; |
| 1026 | private volatile INetworkRecommendationProvider mRecommendationProvider; |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 1027 | |
Jeremy Joslin | 37e877b | 2017-02-02 11:06:14 -0800 | [diff] [blame] | 1028 | ScoringServiceConnection(NetworkScorerAppData appData) { |
| 1029 | mAppData = appData; |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | void connect(Context context) { |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 1033 | if (!mBound) { |
Joe LaPenna | 25e7ec2 | 2016-12-27 14:50:14 -0800 | [diff] [blame] | 1034 | Intent service = new Intent(NetworkScoreManager.ACTION_RECOMMEND_NETWORKS); |
Jeremy Joslin | 37e877b | 2017-02-02 11:06:14 -0800 | [diff] [blame] | 1035 | service.setComponent(mAppData.getRecommendationServiceComponent()); |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame] | 1036 | mBound = context.bindServiceAsUser(service, this, |
| 1037 | Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE, |
| 1038 | UserHandle.SYSTEM); |
| 1039 | if (!mBound) { |
| 1040 | Log.w(TAG, "Bind call failed for " + service); |
| 1041 | } else { |
| 1042 | if (DBG) Log.d(TAG, "ScoringServiceConnection bound."); |
| 1043 | } |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | void disconnect(Context context) { |
| 1048 | try { |
| 1049 | if (mBound) { |
| 1050 | mBound = false; |
| 1051 | context.unbindService(this); |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame] | 1052 | if (DBG) Log.d(TAG, "ScoringServiceConnection unbound."); |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 1053 | } |
| 1054 | } catch (RuntimeException e) { |
| 1055 | Log.e(TAG, "Unbind failed.", e); |
| 1056 | } |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 1057 | |
| 1058 | mRecommendationProvider = null; |
| 1059 | } |
| 1060 | |
| 1061 | INetworkRecommendationProvider getRecommendationProvider() { |
| 1062 | return mRecommendationProvider; |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Jeremy Joslin | 37e877b | 2017-02-02 11:06:14 -0800 | [diff] [blame] | 1065 | String getPackageName() { |
| 1066 | return mAppData.getRecommendationServiceComponent().getPackageName(); |
| 1067 | } |
| 1068 | |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 1069 | @Override |
| 1070 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 1071 | if (DBG) Log.d(TAG, "ScoringServiceConnection: " + name.flattenToString()); |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame] | 1072 | mConnected = true; |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 1073 | mRecommendationProvider = INetworkRecommendationProvider.Stub.asInterface(service); |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | @Override |
| 1077 | public void onServiceDisconnected(ComponentName name) { |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame] | 1078 | if (DBG) { |
| 1079 | Log.d(TAG, "ScoringServiceConnection, disconnected: " + name.flattenToString()); |
| 1080 | } |
| 1081 | mConnected = false; |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 1082 | mRecommendationProvider = null; |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | public void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
Jeremy Joslin | 37e877b | 2017-02-02 11:06:14 -0800 | [diff] [blame] | 1086 | writer.println("ScoringServiceConnection: " |
| 1087 | + mAppData.getRecommendationServiceComponent() |
| 1088 | + ", bound: " + mBound |
Jeremy Joslin | 1ec8cd95 | 2016-05-26 15:28:48 -0700 | [diff] [blame] | 1089 | + ", connected: " + mConnected); |
Jeremy Joslin | dd251ef | 2016-03-14 11:17:41 -0700 | [diff] [blame] | 1090 | } |
| 1091 | } |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 1092 | |
| 1093 | /** |
| 1094 | * Executes the async requestRecommendation() call with a timeout. |
| 1095 | */ |
| 1096 | private static final class RequestRecommendationCaller |
| 1097 | extends TimedRemoteCaller<RecommendationResult> { |
| 1098 | private final IRemoteCallback mCallback; |
| 1099 | |
| 1100 | RequestRecommendationCaller(long callTimeoutMillis) { |
| 1101 | super(callTimeoutMillis); |
| 1102 | mCallback = new IRemoteCallback.Stub() { |
| 1103 | @Override |
| 1104 | public void sendResult(Bundle data) throws RemoteException { |
| 1105 | final RecommendationResult result = |
| 1106 | data.getParcelable(EXTRA_RECOMMENDATION_RESULT); |
| 1107 | final int sequence = data.getInt(EXTRA_SEQUENCE, -1); |
Jeremy Joslin | 4ad7ca0 | 2017-02-13 11:35:03 -0800 | [diff] [blame] | 1108 | if (VERBOSE) Log.v(TAG, "callback received for sequence " + sequence); |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 1109 | onRemoteMethodResult(result, sequence); |
| 1110 | } |
| 1111 | }; |
| 1112 | } |
| 1113 | |
| 1114 | /** |
| 1115 | * Runs the requestRecommendation() call on the given {@link INetworkRecommendationProvider} |
| 1116 | * instance. |
| 1117 | * |
| 1118 | * @param target the {@link INetworkRecommendationProvider} to request a recommendation |
| 1119 | * from |
| 1120 | * @param request the {@link RecommendationRequest} from the calling client |
| 1121 | * @return a {@link RecommendationResult} from the provider |
| 1122 | * @throws RemoteException if the call failed |
| 1123 | * @throws TimeoutException if the call took longer than the set timeout |
| 1124 | */ |
| 1125 | RecommendationResult getRecommendationResult(INetworkRecommendationProvider target, |
| 1126 | RecommendationRequest request) throws RemoteException, TimeoutException { |
| 1127 | final int sequence = onBeforeRemoteCall(); |
Jeremy Joslin | 4ad7ca0 | 2017-02-13 11:35:03 -0800 | [diff] [blame] | 1128 | if (VERBOSE) Log.v(TAG, "getRecommendationResult() seq=" + sequence); |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 1129 | target.requestRecommendation(request, mCallback, sequence); |
| 1130 | return getResultTimed(sequence); |
| 1131 | } |
| 1132 | } |
Jeremy Joslin | ce73c6f | 2016-12-29 14:49:38 -0800 | [diff] [blame] | 1133 | |
| 1134 | /** |
| 1135 | * A wrapper around {@link RemoteCallback} that guarantees |
| 1136 | * {@link RemoteCallback#sendResult(Bundle)} will be invoked at most once. |
| 1137 | */ |
| 1138 | @VisibleForTesting |
| 1139 | public static final class OneTimeCallback { |
| 1140 | private final RemoteCallback mRemoteCallback; |
| 1141 | private final AtomicBoolean mCallbackRun; |
| 1142 | |
| 1143 | public OneTimeCallback(RemoteCallback remoteCallback) { |
| 1144 | mRemoteCallback = remoteCallback; |
| 1145 | mCallbackRun = new AtomicBoolean(false); |
| 1146 | } |
| 1147 | |
| 1148 | public void sendResult(Bundle data) { |
| 1149 | if (mCallbackRun.compareAndSet(false, true)) { |
| 1150 | mRemoteCallback.sendResult(data); |
| 1151 | } |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | private static void sendDefaultRecommendationResponse(RecommendationRequest request, |
| 1156 | OneTimeCallback remoteCallback) { |
| 1157 | if (DBG) { |
| 1158 | Log.d(TAG, "Returning the default network recommendation."); |
| 1159 | } |
| 1160 | |
| 1161 | final RecommendationResult result; |
Jeremy Joslin | 26a45e5 | 2017-01-18 11:55:17 -0800 | [diff] [blame] | 1162 | if (request != null && request.getDefaultWifiConfig() != null) { |
Jeremy Joslin | ce73c6f | 2016-12-29 14:49:38 -0800 | [diff] [blame] | 1163 | result = RecommendationResult.createConnectRecommendation( |
Jeremy Joslin | 26a45e5 | 2017-01-18 11:55:17 -0800 | [diff] [blame] | 1164 | request.getDefaultWifiConfig()); |
Jeremy Joslin | ce73c6f | 2016-12-29 14:49:38 -0800 | [diff] [blame] | 1165 | } else { |
| 1166 | result = RecommendationResult.createDoNotConnectRecommendation(); |
| 1167 | } |
| 1168 | |
| 1169 | final Bundle data = new Bundle(); |
| 1170 | data.putParcelable(EXTRA_RECOMMENDATION_RESULT, result); |
| 1171 | remoteCallback.sendResult(data); |
| 1172 | } |
| 1173 | |
| 1174 | @VisibleForTesting |
Jeremy Joslin | cb594f3 | 2017-01-03 17:31:23 -0800 | [diff] [blame] | 1175 | public final class ServiceHandler extends Handler { |
Jeremy Joslin | ce73c6f | 2016-12-29 14:49:38 -0800 | [diff] [blame] | 1176 | public static final int MSG_RECOMMENDATION_REQUEST_TIMEOUT = 1; |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 1177 | public static final int MSG_RECOMMENDATIONS_PACKAGE_CHANGED = 2; |
Jeremy Joslin | cb594f3 | 2017-01-03 17:31:23 -0800 | [diff] [blame] | 1178 | public static final int MSG_RECOMMENDATION_REQUEST_TIMEOUT_CHANGED = 3; |
Jeremy Joslin | 9925c6a | 2017-03-06 10:39:35 -0800 | [diff] [blame] | 1179 | public static final int MSG_RECOMMENDATION_ENABLED_SETTING_CHANGED = 4; |
Jeremy Joslin | ce73c6f | 2016-12-29 14:49:38 -0800 | [diff] [blame] | 1180 | |
| 1181 | public ServiceHandler(Looper looper) { |
| 1182 | super(looper); |
| 1183 | } |
| 1184 | |
| 1185 | @Override |
| 1186 | public void handleMessage(Message msg) { |
| 1187 | final int what = msg.what; |
| 1188 | switch (what) { |
| 1189 | case MSG_RECOMMENDATION_REQUEST_TIMEOUT: |
| 1190 | if (DBG) { |
| 1191 | Log.d(TAG, "Network recommendation request timed out."); |
| 1192 | } |
| 1193 | final Pair<RecommendationRequest, OneTimeCallback> pair = |
| 1194 | (Pair<RecommendationRequest, OneTimeCallback>) msg.obj; |
| 1195 | final RecommendationRequest request = pair.first; |
| 1196 | final OneTimeCallback remoteCallback = pair.second; |
| 1197 | sendDefaultRecommendationResponse(request, remoteCallback); |
| 1198 | break; |
| 1199 | |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 1200 | case MSG_RECOMMENDATIONS_PACKAGE_CHANGED: |
Jeremy Joslin | 9925c6a | 2017-03-06 10:39:35 -0800 | [diff] [blame] | 1201 | case MSG_RECOMMENDATION_ENABLED_SETTING_CHANGED: |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 1202 | refreshBinding(); |
Jeremy Joslin | cb594f3 | 2017-01-03 17:31:23 -0800 | [diff] [blame] | 1203 | break; |
| 1204 | |
| 1205 | case MSG_RECOMMENDATION_REQUEST_TIMEOUT_CHANGED: |
| 1206 | refreshRecommendationRequestTimeoutMs(); |
| 1207 | break; |
| 1208 | |
Jeremy Joslin | ce73c6f | 2016-12-29 14:49:38 -0800 | [diff] [blame] | 1209 | default: |
| 1210 | Log.w(TAG,"Unknown message: " + what); |
| 1211 | } |
| 1212 | } |
| 1213 | } |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 1214 | } |