Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -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 android.net; |
| 18 | |
Jeff Davidson | ac7285d | 2014-08-08 15:12:47 -0700 | [diff] [blame] | 19 | import android.Manifest; |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 20 | import android.annotation.SdkConstant; |
| 21 | import android.annotation.SdkConstant.SdkConstantType; |
Jeff Davidson | 7be8e97 | 2014-07-16 17:24:46 -0700 | [diff] [blame] | 22 | import android.annotation.SystemApi; |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 24 | import android.content.Intent; |
Jeff Davidson | c741553 | 2014-06-23 18:15:34 -0700 | [diff] [blame] | 25 | import android.net.NetworkScorerAppManager.NetworkScorerAppData; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 26 | import android.os.IBinder; |
| 27 | import android.os.RemoteException; |
| 28 | import android.os.ServiceManager; |
Jeff Davidson | ac7285d | 2014-08-08 15:12:47 -0700 | [diff] [blame] | 29 | import android.os.UserHandle; |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Class that manages communication between network subsystems and a network scorer. |
| 33 | * |
| 34 | * <p>You can get an instance of this class by calling |
| 35 | * {@link android.content.Context#getSystemService(String)}: |
| 36 | * |
| 37 | * <pre>NetworkScoreManager manager = |
| 38 | * (NetworkScoreManager) getSystemService(Context.NETWORK_SCORE_SERVICE)</pre> |
| 39 | * |
| 40 | * <p>A network scorer is any application which: |
| 41 | * <ul> |
| 42 | * <li>Declares the {@link android.Manifest.permission#SCORE_NETWORKS} permission. |
| 43 | * <li>Includes a receiver for {@link #ACTION_SCORE_NETWORKS} guarded by the |
Jeff Davidson | 1619779 | 2014-11-03 17:39:54 -0800 | [diff] [blame] | 44 | * {@link android.Manifest.permission#BROADCAST_NETWORK_PRIVILEGED} permission which scores |
| 45 | * networks and (eventually) calls {@link #updateScores} with the results. If this receiver |
| 46 | * specifies an android:label attribute, this label will be used when referring to the |
| 47 | * application throughout system settings; otherwise, the application label will be used. |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 48 | * </ul> |
| 49 | * |
Jeff Davidson | ee2a121 | 2014-04-17 12:19:23 -0700 | [diff] [blame] | 50 | * <p>The system keeps track of an active scorer application; at any time, only this application |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 51 | * will receive {@link #ACTION_SCORE_NETWORKS} broadcasts and will be permitted to call |
Jeff Davidson | ee2a121 | 2014-04-17 12:19:23 -0700 | [diff] [blame] | 52 | * {@link #updateScores}. Applications may determine the current active scorer with |
| 53 | * {@link #getActiveScorerPackage()} and request to change the active scorer by sending an |
| 54 | * {@link #ACTION_CHANGE_ACTIVE} broadcast with another scorer. |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 55 | * |
| 56 | * @hide |
| 57 | */ |
Jeff Davidson | 7be8e97 | 2014-07-16 17:24:46 -0700 | [diff] [blame] | 58 | @SystemApi |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 59 | public class NetworkScoreManager { |
| 60 | /** |
Jeff Davidson | ee2a121 | 2014-04-17 12:19:23 -0700 | [diff] [blame] | 61 | * Activity action: ask the user to change the active network scorer. This will show a dialog |
| 62 | * that asks the user whether they want to replace the current active scorer with the one |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 63 | * specified in {@link #EXTRA_PACKAGE_NAME}. The activity will finish with RESULT_OK if the |
Jeff Davidson | ee2a121 | 2014-04-17 12:19:23 -0700 | [diff] [blame] | 64 | * active scorer was changed or RESULT_CANCELED if it failed for any reason. |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 65 | */ |
| 66 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
Jeff Davidson | ee2a121 | 2014-04-17 12:19:23 -0700 | [diff] [blame] | 67 | public static final String ACTION_CHANGE_ACTIVE = "android.net.scoring.CHANGE_ACTIVE"; |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 68 | |
| 69 | /** |
Jeff Davidson | ee2a121 | 2014-04-17 12:19:23 -0700 | [diff] [blame] | 70 | * Extra used with {@link #ACTION_CHANGE_ACTIVE} to specify the new scorer package. Set with |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 71 | * {@link android.content.Intent#putExtra(String, String)}. |
| 72 | */ |
| 73 | public static final String EXTRA_PACKAGE_NAME = "packageName"; |
| 74 | |
| 75 | /** |
| 76 | * Broadcast action: new network scores are being requested. This intent will only be delivered |
Jeff Davidson | ee2a121 | 2014-04-17 12:19:23 -0700 | [diff] [blame] | 77 | * to the current active scorer app. That app is responsible for scoring the networks and |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 78 | * calling {@link #updateScores} when complete. The networks to score are specified in |
| 79 | * {@link #EXTRA_NETWORKS_TO_SCORE}, and will generally consist of all networks which have been |
| 80 | * configured by the user as well as any open networks. |
| 81 | * |
| 82 | * <p class="note">This is a protected intent that can only be sent by the system. |
| 83 | */ |
| 84 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 85 | public static final String ACTION_SCORE_NETWORKS = "android.net.scoring.SCORE_NETWORKS"; |
| 86 | |
| 87 | /** |
Jeff Davidson | b096bdc | 2014-07-01 12:29:11 -0700 | [diff] [blame] | 88 | * Extra used with {@link #ACTION_SCORE_NETWORKS} to specify the networks to be scored, as an |
| 89 | * array of {@link NetworkKey}s. Can be obtained with |
| 90 | * {@link android.content.Intent#getParcelableArrayExtra(String)}}. |
| 91 | */ |
| 92 | public static final String EXTRA_NETWORKS_TO_SCORE = "networksToScore"; |
| 93 | |
| 94 | /** |
Jeff Davidson | b6646a8 | 2014-06-27 16:24:42 -0700 | [diff] [blame] | 95 | * Activity action: launch a custom activity for configuring a scorer before enabling it. |
| 96 | * Scorer applications may choose to specify an activity for this action, in which case the |
| 97 | * framework will launch that activity which should return RESULT_OK if scoring was enabled. |
| 98 | * |
| 99 | * <p>If no activity is included in a scorer which implements this action, the system dialog for |
| 100 | * selecting a scorer will be shown instead. |
| 101 | */ |
| 102 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
| 103 | public static final String ACTION_CUSTOM_ENABLE = "android.net.scoring.CUSTOM_ENABLE"; |
| 104 | |
| 105 | /** |
Jeff Davidson | b096bdc | 2014-07-01 12:29:11 -0700 | [diff] [blame] | 106 | * Broadcast action: the active scorer has been changed. Scorer apps may listen to this to |
| 107 | * perform initialization once selected as the active scorer, or clean up unneeded resources |
Jeremy Joslin | da11f5c | 2016-02-10 07:31:33 -0800 | [diff] [blame] | 108 | * if another scorer has been selected. This is an explicit broadcast only sent to the |
| 109 | * previous scorer and new scorer. Note that it is unnecessary to clear existing scores as |
Jeff Davidson | b096bdc | 2014-07-01 12:29:11 -0700 | [diff] [blame] | 110 | * this is handled by the system. |
| 111 | * |
| 112 | * <p>The new scorer will be specified in {@link #EXTRA_NEW_SCORER}. |
| 113 | * |
| 114 | * <p class="note">This is a protected intent that can only be sent by the system. |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 115 | */ |
Jeff Davidson | b096bdc | 2014-07-01 12:29:11 -0700 | [diff] [blame] | 116 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 117 | public static final String ACTION_SCORER_CHANGED = "android.net.scoring.SCORER_CHANGED"; |
| 118 | |
| 119 | /** |
| 120 | * Extra used with {@link #ACTION_SCORER_CHANGED} to specify the newly selected scorer's package |
| 121 | * name. Will be null if scoring was disabled. Can be obtained with |
| 122 | * {@link android.content.Intent#getStringExtra(String)}. |
| 123 | */ |
| 124 | public static final String EXTRA_NEW_SCORER = "newScorer"; |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 125 | |
| 126 | private final Context mContext; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 127 | private final INetworkScoreService mService; |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 128 | |
| 129 | /** @hide */ |
| 130 | public NetworkScoreManager(Context context) { |
| 131 | mContext = context; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 132 | IBinder iBinder = ServiceManager.getService(Context.NETWORK_SCORE_SERVICE); |
| 133 | mService = INetworkScoreService.Stub.asInterface(iBinder); |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /** |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 137 | * Obtain the package name of the current active network scorer. |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 138 | * |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 139 | * <p>At any time, only one scorer application will receive {@link #ACTION_SCORE_NETWORKS} |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 140 | * broadcasts and be allowed to call {@link #updateScores}. Applications may use this method to |
| 141 | * determine the current scorer and offer the user the ability to select a different scorer via |
Jeff Davidson | ee2a121 | 2014-04-17 12:19:23 -0700 | [diff] [blame] | 142 | * the {@link #ACTION_CHANGE_ACTIVE} intent. |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 143 | * @return the full package name of the current active scorer, or null if there is no active |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 144 | * scorer. |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 145 | */ |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 146 | public String getActiveScorerPackage() { |
Jeff Davidson | c741553 | 2014-06-23 18:15:34 -0700 | [diff] [blame] | 147 | NetworkScorerAppData app = NetworkScorerAppManager.getActiveScorer(mContext); |
| 148 | if (app == null) { |
| 149 | return null; |
| 150 | } |
| 151 | return app.mPackageName; |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Update network scores. |
| 156 | * |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 157 | * <p>This may be called at any time to re-score active networks. Scores will generally be |
| 158 | * updated quickly, but if this method is called too frequently, the scores may be held and |
| 159 | * applied at a later time. |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 160 | * |
| 161 | * @param networks the networks which have been scored by the scorer. |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 162 | * @return whether the update was successful. |
| 163 | * @throws SecurityException if the caller is not the active scorer. |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 164 | */ |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 165 | public boolean updateScores(ScoredNetwork[] networks) throws SecurityException { |
| 166 | try { |
| 167 | return mService.updateScores(networks); |
| 168 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 169 | throw e.rethrowFromSystemServer(); |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Clear network scores. |
| 175 | * |
| 176 | * <p>Should be called when all scores need to be invalidated, i.e. because the scoring |
| 177 | * algorithm has changed and old scores can no longer be compared to future scores. |
| 178 | * |
| 179 | * <p>Note that scores will be cleared automatically when the active scorer changes, as scores |
| 180 | * from one scorer cannot be compared to those from another scorer. |
| 181 | * |
| 182 | * @return whether the clear was successful. |
| 183 | * @throws SecurityException if the caller is not the active scorer or privileged. |
| 184 | */ |
| 185 | public boolean clearScores() throws SecurityException { |
| 186 | try { |
| 187 | return mService.clearScores(); |
| 188 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 189 | throw e.rethrowFromSystemServer(); |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Set the active scorer to a new package and clear existing scores. |
| 195 | * |
Jeff Davidson | e56f2bb | 2014-11-05 11:14:19 -0800 | [diff] [blame] | 196 | * <p>Should never be called directly without obtaining user consent. This can be done by using |
| 197 | * the {@link #ACTION_CHANGE_ACTIVE} broadcast, or using a custom configuration activity. |
| 198 | * |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 199 | * @return true if the operation succeeded, or false if the new package is not a valid scorer. |
| 200 | * @throws SecurityException if the caller does not hold the |
Jeff Davidson | e56f2bb | 2014-11-05 11:14:19 -0800 | [diff] [blame] | 201 | * {@link android.Manifest.permission#SCORE_NETWORKS} permission. |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 202 | * @hide |
| 203 | */ |
Jeff Davidson | e56f2bb | 2014-11-05 11:14:19 -0800 | [diff] [blame] | 204 | @SystemApi |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 205 | public boolean setActiveScorer(String packageName) throws SecurityException { |
| 206 | try { |
| 207 | return mService.setActiveScorer(packageName); |
| 208 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 209 | throw e.rethrowFromSystemServer(); |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 210 | } |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 211 | } |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 212 | |
| 213 | /** |
Jeff Davidson | 26fd143 | 2014-07-29 09:39:52 -0700 | [diff] [blame] | 214 | * Turn off network scoring. |
| 215 | * |
| 216 | * <p>May only be called by the current scorer app, or the system. |
| 217 | * |
| 218 | * @throws SecurityException if the caller is neither the active scorer nor the system. |
| 219 | */ |
| 220 | public void disableScoring() throws SecurityException { |
| 221 | try { |
| 222 | mService.disableScoring(); |
| 223 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 224 | throw e.rethrowFromSystemServer(); |
Jeff Davidson | 26fd143 | 2014-07-29 09:39:52 -0700 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
| 228 | /** |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 229 | * Request scoring for networks. |
| 230 | * |
| 231 | * <p>Note that this is just a helper method to assemble the broadcast, and will run in the |
| 232 | * calling process. |
| 233 | * |
| 234 | * @return true if the broadcast was sent, or false if there is no active scorer. |
| 235 | * @throws SecurityException if the caller does not hold the |
Jeff Davidson | 1619779 | 2014-11-03 17:39:54 -0800 | [diff] [blame] | 236 | * {@link android.Manifest.permission#BROADCAST_NETWORK_PRIVILEGED} permission. |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 237 | * @hide |
| 238 | */ |
| 239 | public boolean requestScores(NetworkKey[] networks) throws SecurityException { |
| 240 | String activeScorer = getActiveScorerPackage(); |
| 241 | if (activeScorer == null) { |
| 242 | return false; |
| 243 | } |
| 244 | Intent intent = new Intent(ACTION_SCORE_NETWORKS); |
| 245 | intent.setPackage(activeScorer); |
Jeff Davidson | 505c4a3 | 2014-07-29 16:18:04 -0700 | [diff] [blame] | 246 | intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 247 | intent.putExtra(EXTRA_NETWORKS_TO_SCORE, networks); |
Jeff Davidson | ac7285d | 2014-08-08 15:12:47 -0700 | [diff] [blame] | 248 | // A scorer should never become active if its package doesn't hold SCORE_NETWORKS, but |
| 249 | // ensure the package still holds it to be extra safe. |
Xiaohui Chen | e4de5a0 | 2015-09-22 15:33:31 -0700 | [diff] [blame] | 250 | // TODO: http://b/23422763 |
| 251 | mContext.sendBroadcastAsUser(intent, UserHandle.SYSTEM, Manifest.permission.SCORE_NETWORKS); |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 252 | return true; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Register a network score cache. |
| 257 | * |
| 258 | * @param networkType the type of network this cache can handle. See {@link NetworkKey#type}. |
| 259 | * @param scoreCache implementation of {@link INetworkScoreCache} to store the scores. |
| 260 | * @throws SecurityException if the caller does not hold the |
Jeff Davidson | 1619779 | 2014-11-03 17:39:54 -0800 | [diff] [blame] | 261 | * {@link android.Manifest.permission#BROADCAST_NETWORK_PRIVILEGED} permission. |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 262 | * @throws IllegalArgumentException if a score cache is already registered for this type. |
| 263 | * @hide |
| 264 | */ |
| 265 | public void registerNetworkScoreCache(int networkType, INetworkScoreCache scoreCache) { |
| 266 | try { |
| 267 | mService.registerNetworkScoreCache(networkType, scoreCache); |
| 268 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 269 | throw e.rethrowFromSystemServer(); |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 270 | } |
| 271 | } |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 272 | } |