blob: c599dfccc63249f687d3b9b3e57506b7d8f11f30 [file] [log] [blame]
Jeff Davidsonb51e0a62014-04-09 12:38:15 -07001/*
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
17package android.net;
18
19import android.annotation.SdkConstant;
20import android.annotation.SdkConstant.SdkConstantType;
21import android.content.Context;
Jeff Davidson14f1ec02014-04-29 11:58:26 -070022import android.content.Intent;
Jeff Davidsonc7415532014-06-23 18:15:34 -070023import android.net.NetworkScorerAppManager.NetworkScorerAppData;
Jeff Davidson6a4b2202014-04-16 17:29:40 -070024import android.os.IBinder;
25import android.os.RemoteException;
26import android.os.ServiceManager;
Jeff Davidsonb51e0a62014-04-09 12:38:15 -070027
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 Davidsonc7415532014-06-23 18:15:34 -070042 * 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 Davidsonb51e0a62014-04-09 12:38:15 -070045 * </ul>
46 *
Jeff Davidsonee2a1212014-04-17 12:19:23 -070047 * <p>The system keeps track of an active scorer application; at any time, only this application
Jeff Davidsonb51e0a62014-04-09 12:38:15 -070048 * will receive {@link #ACTION_SCORE_NETWORKS} broadcasts and will be permitted to call
Jeff Davidsonee2a1212014-04-17 12:19:23 -070049 * {@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 Davidsonb51e0a62014-04-09 12:38:15 -070052 *
53 * @hide
54 */
55public class NetworkScoreManager {
56 /**
Jeff Davidsonee2a1212014-04-17 12:19:23 -070057 * 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 Davidsonb51e0a62014-04-09 12:38:15 -070059 * specified in {@link #EXTRA_PACKAGE_NAME}. The activity will finish with RESULT_OK if the
Jeff Davidsonee2a1212014-04-17 12:19:23 -070060 * active scorer was changed or RESULT_CANCELED if it failed for any reason.
Jeff Davidsonb51e0a62014-04-09 12:38:15 -070061 */
62 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
Jeff Davidsonee2a1212014-04-17 12:19:23 -070063 public static final String ACTION_CHANGE_ACTIVE = "android.net.scoring.CHANGE_ACTIVE";
Jeff Davidsonb51e0a62014-04-09 12:38:15 -070064
65 /**
Jeff Davidsonee2a1212014-04-17 12:19:23 -070066 * Extra used with {@link #ACTION_CHANGE_ACTIVE} to specify the new scorer package. Set with
Jeff Davidsonb51e0a62014-04-09 12:38:15 -070067 * {@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 Davidsonee2a1212014-04-17 12:19:23 -070073 * to the current active scorer app. That app is responsible for scoring the networks and
Jeff Davidsonb51e0a62014-04-09 12:38:15 -070074 * 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 Davidsonb096bdc2014-07-01 12:29:11 -070084 * Extra used with {@link #ACTION_SCORE_NETWORKS} to specify the networks to be scored, as an
85 * array of {@link NetworkKey}s. Can be obtained with
86 * {@link android.content.Intent#getParcelableArrayExtra(String)}}.
87 */
88 public static final String EXTRA_NETWORKS_TO_SCORE = "networksToScore";
89
90 /**
Jeff Davidsonb6646a82014-06-27 16:24:42 -070091 * Activity action: launch a custom activity for configuring a scorer before enabling it.
92 * Scorer applications may choose to specify an activity for this action, in which case the
93 * framework will launch that activity which should return RESULT_OK if scoring was enabled.
94 *
95 * <p>If no activity is included in a scorer which implements this action, the system dialog for
96 * selecting a scorer will be shown instead.
97 */
98 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
99 public static final String ACTION_CUSTOM_ENABLE = "android.net.scoring.CUSTOM_ENABLE";
100
101 /**
Jeff Davidsonb096bdc2014-07-01 12:29:11 -0700102 * Broadcast action: the active scorer has been changed. Scorer apps may listen to this to
103 * perform initialization once selected as the active scorer, or clean up unneeded resources
104 * if another scorer has been selected. Note that it is unnecessary to clear existing scores as
105 * this is handled by the system.
106 *
107 * <p>The new scorer will be specified in {@link #EXTRA_NEW_SCORER}.
108 *
109 * <p class="note">This is a protected intent that can only be sent by the system.
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700110 */
Jeff Davidsonb096bdc2014-07-01 12:29:11 -0700111 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
112 public static final String ACTION_SCORER_CHANGED = "android.net.scoring.SCORER_CHANGED";
113
114 /**
115 * Extra used with {@link #ACTION_SCORER_CHANGED} to specify the newly selected scorer's package
116 * name. Will be null if scoring was disabled. Can be obtained with
117 * {@link android.content.Intent#getStringExtra(String)}.
118 */
119 public static final String EXTRA_NEW_SCORER = "newScorer";
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700120
121 private final Context mContext;
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700122 private final INetworkScoreService mService;
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700123
124 /** @hide */
125 public NetworkScoreManager(Context context) {
126 mContext = context;
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700127 IBinder iBinder = ServiceManager.getService(Context.NETWORK_SCORE_SERVICE);
128 mService = INetworkScoreService.Stub.asInterface(iBinder);
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700129 }
130
131 /**
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700132 * Obtain the package name of the current active network scorer.
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700133 *
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700134 * <p>At any time, only one scorer application will receive {@link #ACTION_SCORE_NETWORKS}
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700135 * broadcasts and be allowed to call {@link #updateScores}. Applications may use this method to
136 * determine the current scorer and offer the user the ability to select a different scorer via
Jeff Davidsonee2a1212014-04-17 12:19:23 -0700137 * the {@link #ACTION_CHANGE_ACTIVE} intent.
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700138 * @return the full package name of the current active scorer, or null if there is no active
Jeff Davidson14f1ec02014-04-29 11:58:26 -0700139 * scorer.
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700140 */
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700141 public String getActiveScorerPackage() {
Jeff Davidsonc7415532014-06-23 18:15:34 -0700142 NetworkScorerAppData app = NetworkScorerAppManager.getActiveScorer(mContext);
143 if (app == null) {
144 return null;
145 }
146 return app.mPackageName;
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700147 }
148
149 /**
150 * Update network scores.
151 *
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700152 * <p>This may be called at any time to re-score active networks. Scores will generally be
153 * updated quickly, but if this method is called too frequently, the scores may be held and
154 * applied at a later time.
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700155 *
156 * @param networks the networks which have been scored by the scorer.
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700157 * @return whether the update was successful.
158 * @throws SecurityException if the caller is not the active scorer.
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700159 */
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700160 public boolean updateScores(ScoredNetwork[] networks) throws SecurityException {
161 try {
162 return mService.updateScores(networks);
163 } catch (RemoteException e) {
164 return false;
165 }
166 }
167
168 /**
169 * Clear network scores.
170 *
171 * <p>Should be called when all scores need to be invalidated, i.e. because the scoring
172 * algorithm has changed and old scores can no longer be compared to future scores.
173 *
174 * <p>Note that scores will be cleared automatically when the active scorer changes, as scores
175 * from one scorer cannot be compared to those from another scorer.
176 *
177 * @return whether the clear was successful.
178 * @throws SecurityException if the caller is not the active scorer or privileged.
179 */
180 public boolean clearScores() throws SecurityException {
181 try {
182 return mService.clearScores();
183 } catch (RemoteException e) {
184 return false;
185 }
186 }
187
188 /**
189 * Set the active scorer to a new package and clear existing scores.
190 *
191 * @return true if the operation succeeded, or false if the new package is not a valid scorer.
192 * @throws SecurityException if the caller does not hold the
Jeff Davidson14f1ec02014-04-29 11:58:26 -0700193 * {@link android.Manifest.permission#BROADCAST_SCORE_NETWORKS} permission indicating
194 * that it can manage scorer applications.
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700195 * @hide
196 */
197 public boolean setActiveScorer(String packageName) throws SecurityException {
198 try {
199 return mService.setActiveScorer(packageName);
200 } catch (RemoteException e) {
201 return false;
202 }
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700203 }
Jeff Davidson14f1ec02014-04-29 11:58:26 -0700204
205 /**
206 * Request scoring for networks.
207 *
208 * <p>Note that this is just a helper method to assemble the broadcast, and will run in the
209 * calling process.
210 *
211 * @return true if the broadcast was sent, or false if there is no active scorer.
212 * @throws SecurityException if the caller does not hold the
213 * {@link android.Manifest.permission#BROADCAST_SCORE_NETWORKS} permission.
214 * @hide
215 */
216 public boolean requestScores(NetworkKey[] networks) throws SecurityException {
217 String activeScorer = getActiveScorerPackage();
218 if (activeScorer == null) {
219 return false;
220 }
221 Intent intent = new Intent(ACTION_SCORE_NETWORKS);
222 intent.setPackage(activeScorer);
223 intent.putExtra(EXTRA_NETWORKS_TO_SCORE, networks);
224 mContext.sendBroadcast(intent);
225 return true;
226 }
227
228 /**
229 * Register a network score cache.
230 *
231 * @param networkType the type of network this cache can handle. See {@link NetworkKey#type}.
232 * @param scoreCache implementation of {@link INetworkScoreCache} to store the scores.
233 * @throws SecurityException if the caller does not hold the
234 * {@link android.Manifest.permission#BROADCAST_SCORE_NETWORKS} permission.
235 * @throws IllegalArgumentException if a score cache is already registered for this type.
236 * @hide
237 */
238 public void registerNetworkScoreCache(int networkType, INetworkScoreCache scoreCache) {
239 try {
240 mService.registerNetworkScoreCache(networkType, scoreCache);
241 } catch (RemoteException e) {
242 }
243 }
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700244}