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