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