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