blob: a6854dcb8eb3e77e1df99a6c5b98ac09e0fdbb2c [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
Jeff Davidsonac7285d2014-08-08 15:12:47 -070019import android.Manifest;
Jeremy Joslin823db052016-11-30 15:05:40 -080020import android.annotation.IntDef;
Jeff Davidsonb51e0a62014-04-09 12:38:15 -070021import android.annotation.SdkConstant;
22import android.annotation.SdkConstant.SdkConstantType;
Jeff Davidson7be8e972014-07-16 17:24:46 -070023import android.annotation.SystemApi;
Jeff Davidsonb51e0a62014-04-09 12:38:15 -070024import android.content.Context;
Jeff Davidson14f1ec02014-04-29 11:58:26 -070025import android.content.Intent;
Jeff Davidsonc7415532014-06-23 18:15:34 -070026import android.net.NetworkScorerAppManager.NetworkScorerAppData;
Jeff Davidson6a4b2202014-04-16 17:29:40 -070027import android.os.IBinder;
28import android.os.RemoteException;
29import android.os.ServiceManager;
Jeff Davidsonb51e0a62014-04-09 12:38:15 -070030
Jeremy Joslin823db052016-11-30 15:05:40 -080031import java.lang.annotation.Retention;
32import java.lang.annotation.RetentionPolicy;
33
Jeff Davidsonb51e0a62014-04-09 12:38:15 -070034/**
35 * Class that manages communication between network subsystems and a network scorer.
36 *
37 * <p>You can get an instance of this class by calling
38 * {@link android.content.Context#getSystemService(String)}:
39 *
40 * <pre>NetworkScoreManager manager =
41 * (NetworkScoreManager) getSystemService(Context.NETWORK_SCORE_SERVICE)</pre>
42 *
43 * <p>A network scorer is any application which:
44 * <ul>
45 * <li>Declares the {@link android.Manifest.permission#SCORE_NETWORKS} permission.
Jeremy Joslin186f3332017-01-06 14:36:54 -080046 * <li>Include a Service for the {@link #ACTION_RECOMMEND_NETWORKS} action
47 * protected by the {@link android.Manifest.permission#BIND_NETWORK_RECOMMENDATION_SERVICE}
48 * permission.
Jeff Davidsonb51e0a62014-04-09 12:38:15 -070049 * </ul>
50 *
Jeff Davidsonb51e0a62014-04-09 12:38:15 -070051 * @hide
52 */
Jeff Davidson7be8e972014-07-16 17:24:46 -070053@SystemApi
Jeff Davidsonb51e0a62014-04-09 12:38:15 -070054public class NetworkScoreManager {
55 /**
Jeff Davidsonee2a1212014-04-17 12:19:23 -070056 * Activity action: ask the user to change the active network scorer. This will show a dialog
57 * that asks the user whether they want to replace the current active scorer with the one
Jeff Davidsonb51e0a62014-04-09 12:38:15 -070058 * specified in {@link #EXTRA_PACKAGE_NAME}. The activity will finish with RESULT_OK if the
Jeff Davidsonee2a1212014-04-17 12:19:23 -070059 * active scorer was changed or RESULT_CANCELED if it failed for any reason.
Jeff Davidsonb51e0a62014-04-09 12:38:15 -070060 */
61 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
Jeff Davidsonee2a1212014-04-17 12:19:23 -070062 public static final String ACTION_CHANGE_ACTIVE = "android.net.scoring.CHANGE_ACTIVE";
Jeff Davidsonb51e0a62014-04-09 12:38:15 -070063
64 /**
Jeff Davidsonee2a1212014-04-17 12:19:23 -070065 * Extra used with {@link #ACTION_CHANGE_ACTIVE} to specify the new scorer package. Set with
Jeff Davidsonb51e0a62014-04-09 12:38:15 -070066 * {@link android.content.Intent#putExtra(String, String)}.
67 */
68 public static final String EXTRA_PACKAGE_NAME = "packageName";
69
70 /**
71 * Broadcast action: new network scores are being requested. This intent will only be delivered
Jeff Davidsonee2a1212014-04-17 12:19:23 -070072 * to the current active scorer app. That app is responsible for scoring the networks and
Jeff Davidsonb51e0a62014-04-09 12:38:15 -070073 * calling {@link #updateScores} when complete. The networks to score are specified in
74 * {@link #EXTRA_NETWORKS_TO_SCORE}, and will generally consist of all networks which have been
75 * configured by the user as well as any open networks.
76 *
77 * <p class="note">This is a protected intent that can only be sent by the system.
78 */
79 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
80 public static final String ACTION_SCORE_NETWORKS = "android.net.scoring.SCORE_NETWORKS";
81
82 /**
Jeff Davidsonb096bdc2014-07-01 12:29:11 -070083 * Extra used with {@link #ACTION_SCORE_NETWORKS} to specify the networks to be scored, as an
84 * array of {@link NetworkKey}s. Can be obtained with
85 * {@link android.content.Intent#getParcelableArrayExtra(String)}}.
86 */
87 public static final String EXTRA_NETWORKS_TO_SCORE = "networksToScore";
88
89 /**
Jeff Davidsonb6646a82014-06-27 16:24:42 -070090 * Activity action: launch a custom activity for configuring a scorer before enabling it.
91 * Scorer applications may choose to specify an activity for this action, in which case the
92 * framework will launch that activity which should return RESULT_OK if scoring was enabled.
93 *
94 * <p>If no activity is included in a scorer which implements this action, the system dialog for
95 * selecting a scorer will be shown instead.
96 */
97 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
98 public static final String ACTION_CUSTOM_ENABLE = "android.net.scoring.CUSTOM_ENABLE";
99
100 /**
Jeff Davidsonb096bdc2014-07-01 12:29:11 -0700101 * Broadcast action: the active scorer has been changed. Scorer apps may listen to this to
102 * perform initialization once selected as the active scorer, or clean up unneeded resources
Jeremy Joslinda11f5c2016-02-10 07:31:33 -0800103 * if another scorer has been selected. This is an explicit broadcast only sent to the
104 * previous scorer and new scorer. Note that it is unnecessary to clear existing scores as
Jeff Davidsonb096bdc2014-07-01 12:29:11 -0700105 * 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 /**
Jeremy Joslind1daf6d2016-11-28 17:47:35 -0800115 * Service action: Used to discover and bind to a network recommendation provider.
116 * Implementations should return {@link NetworkRecommendationProvider#getBinder()} from
117 * their <code>onBind()</code> method.
118 */
119 @SdkConstant(SdkConstantType.SERVICE_ACTION)
120 public static final String ACTION_RECOMMEND_NETWORKS = "android.net.action.RECOMMEND_NETWORKS";
121
122 /**
Jeff Davidsonb096bdc2014-07-01 12:29:11 -0700123 * Extra used with {@link #ACTION_SCORER_CHANGED} to specify the newly selected scorer's package
124 * name. Will be null if scoring was disabled. Can be obtained with
125 * {@link android.content.Intent#getStringExtra(String)}.
126 */
127 public static final String EXTRA_NEW_SCORER = "newScorer";
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700128
Jeremy Joslin823db052016-11-30 15:05:40 -0800129 /** @hide */
130 @IntDef({CACHE_FILTER_NONE, CACHE_FILTER_CURRENT_NETWORK, CACHE_FILTER_SCAN_RESULTS})
131 @Retention(RetentionPolicy.SOURCE)
132 public @interface CacheUpdateFilter {}
133
134 /**
135 * Do not filter updates sent to the cache.
136 * @hide
137 */
138 public static final int CACHE_FILTER_NONE = 0;
139
140 /**
141 * Only send cache updates when the network matches the connected network.
142 * @hide
143 */
144 public static final int CACHE_FILTER_CURRENT_NETWORK = 1;
145
146 /**
147 * Only send cache updates when the network is part of the current scan result set.
148 * @hide
149 */
150 public static final int CACHE_FILTER_SCAN_RESULTS = 2;
151
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700152 private final Context mContext;
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700153 private final INetworkScoreService mService;
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700154
155 /** @hide */
156 public NetworkScoreManager(Context context) {
157 mContext = context;
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700158 IBinder iBinder = ServiceManager.getService(Context.NETWORK_SCORE_SERVICE);
159 mService = INetworkScoreService.Stub.asInterface(iBinder);
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700160 }
161
162 /**
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700163 * Obtain the package name of the current active network scorer.
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700164 *
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700165 * <p>At any time, only one scorer application will receive {@link #ACTION_SCORE_NETWORKS}
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700166 * broadcasts and be allowed to call {@link #updateScores}. Applications may use this method to
167 * determine the current scorer and offer the user the ability to select a different scorer via
Jeff Davidsonee2a1212014-04-17 12:19:23 -0700168 * the {@link #ACTION_CHANGE_ACTIVE} intent.
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700169 * @return the full package name of the current active scorer, or null if there is no active
Jeff Davidson14f1ec02014-04-29 11:58:26 -0700170 * scorer.
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700171 */
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700172 public String getActiveScorerPackage() {
Jeremy Joslind7670f62017-01-10 13:08:32 -0800173 try {
174 return mService.getActiveScorerPackage();
175 } catch (RemoteException e) {
176 throw e.rethrowFromSystemServer();
Jeff Davidsonc7415532014-06-23 18:15:34 -0700177 }
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700178 }
179
180 /**
181 * Update network scores.
182 *
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700183 * <p>This may be called at any time to re-score active networks. Scores will generally be
184 * updated quickly, but if this method is called too frequently, the scores may be held and
185 * applied at a later time.
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700186 *
187 * @param networks the networks which have been scored by the scorer.
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700188 * @return whether the update was successful.
189 * @throws SecurityException if the caller is not the active scorer.
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700190 */
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700191 public boolean updateScores(ScoredNetwork[] networks) throws SecurityException {
192 try {
193 return mService.updateScores(networks);
194 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700195 throw e.rethrowFromSystemServer();
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700196 }
197 }
198
199 /**
200 * Clear network scores.
201 *
202 * <p>Should be called when all scores need to be invalidated, i.e. because the scoring
203 * algorithm has changed and old scores can no longer be compared to future scores.
204 *
205 * <p>Note that scores will be cleared automatically when the active scorer changes, as scores
206 * from one scorer cannot be compared to those from another scorer.
207 *
208 * @return whether the clear was successful.
209 * @throws SecurityException if the caller is not the active scorer or privileged.
210 */
211 public boolean clearScores() throws SecurityException {
212 try {
213 return mService.clearScores();
214 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700215 throw e.rethrowFromSystemServer();
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700216 }
217 }
218
219 /**
220 * Set the active scorer to a new package and clear existing scores.
221 *
Jeff Davidsone56f2bb2014-11-05 11:14:19 -0800222 * <p>Should never be called directly without obtaining user consent. This can be done by using
223 * the {@link #ACTION_CHANGE_ACTIVE} broadcast, or using a custom configuration activity.
224 *
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700225 * @return true if the operation succeeded, or false if the new package is not a valid scorer.
226 * @throws SecurityException if the caller does not hold the
Jeff Davidsone56f2bb2014-11-05 11:14:19 -0800227 * {@link android.Manifest.permission#SCORE_NETWORKS} permission.
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700228 * @hide
229 */
Jeff Davidsone56f2bb2014-11-05 11:14:19 -0800230 @SystemApi
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700231 public boolean setActiveScorer(String packageName) throws SecurityException {
232 try {
233 return mService.setActiveScorer(packageName);
234 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700235 throw e.rethrowFromSystemServer();
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700236 }
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700237 }
Jeff Davidson14f1ec02014-04-29 11:58:26 -0700238
239 /**
Jeff Davidson26fd1432014-07-29 09:39:52 -0700240 * Turn off network scoring.
241 *
242 * <p>May only be called by the current scorer app, or the system.
243 *
244 * @throws SecurityException if the caller is neither the active scorer nor the system.
245 */
246 public void disableScoring() throws SecurityException {
247 try {
248 mService.disableScoring();
249 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700250 throw e.rethrowFromSystemServer();
Jeff Davidson26fd1432014-07-29 09:39:52 -0700251 }
252 }
253
254 /**
Jeff Davidson14f1ec02014-04-29 11:58:26 -0700255 * Request scoring for networks.
256 *
Jeff Davidson14f1ec02014-04-29 11:58:26 -0700257 * @return true if the broadcast was sent, or false if there is no active scorer.
258 * @throws SecurityException if the caller does not hold the
Jeremy Joslin186f3332017-01-06 14:36:54 -0800259 * {@link android.Manifest.permission#REQUEST_NETWORK_SCORES} permission.
Jeff Davidson14f1ec02014-04-29 11:58:26 -0700260 * @hide
261 */
262 public boolean requestScores(NetworkKey[] networks) throws SecurityException {
Jeremy Joslin36d4c482016-12-09 13:11:51 -0800263 try {
264 return mService.requestScores(networks);
265 } catch (RemoteException e) {
266 throw e.rethrowFromSystemServer();
Jeff Davidson14f1ec02014-04-29 11:58:26 -0700267 }
Jeff Davidson14f1ec02014-04-29 11:58:26 -0700268 }
269
270 /**
271 * Register a network score cache.
272 *
273 * @param networkType the type of network this cache can handle. See {@link NetworkKey#type}.
274 * @param scoreCache implementation of {@link INetworkScoreCache} to store the scores.
275 * @throws SecurityException if the caller does not hold the
Jeremy Joslin186f3332017-01-06 14:36:54 -0800276 * {@link android.Manifest.permission#REQUEST_NETWORK_SCORES} permission.
Jeff Davidson14f1ec02014-04-29 11:58:26 -0700277 * @throws IllegalArgumentException if a score cache is already registered for this type.
Jeremy Joslin823db052016-11-30 15:05:40 -0800278 * @deprecated equivalent to registering for cache updates with CACHE_FILTER_NONE.
Jeff Davidson14f1ec02014-04-29 11:58:26 -0700279 * @hide
280 */
Jeremy Joslin823db052016-11-30 15:05:40 -0800281 @Deprecated // migrate to registerNetworkScoreCache(int, INetworkScoreCache, int)
Jeff Davidson14f1ec02014-04-29 11:58:26 -0700282 public void registerNetworkScoreCache(int networkType, INetworkScoreCache scoreCache) {
Jeremy Joslin823db052016-11-30 15:05:40 -0800283 registerNetworkScoreCache(networkType, scoreCache, CACHE_FILTER_NONE);
284 }
285
286 /**
287 * Register a network score cache.
288 *
289 * @param networkType the type of network this cache can handle. See {@link NetworkKey#type}
290 * @param scoreCache implementation of {@link INetworkScoreCache} to store the scores
291 * @param filterType the {@link CacheUpdateFilter} to apply
292 * @throws SecurityException if the caller does not hold the
Jeremy Joslin186f3332017-01-06 14:36:54 -0800293 * {@link android.Manifest.permission#REQUEST_NETWORK_SCORES} permission.
Jeremy Joslin823db052016-11-30 15:05:40 -0800294 * @throws IllegalArgumentException if a score cache is already registered for this type.
295 * @hide
296 */
297 public void registerNetworkScoreCache(int networkType, INetworkScoreCache scoreCache,
298 @CacheUpdateFilter int filterType) {
Jeff Davidson14f1ec02014-04-29 11:58:26 -0700299 try {
Jeremy Joslin823db052016-11-30 15:05:40 -0800300 mService.registerNetworkScoreCache(networkType, scoreCache, filterType);
Jeff Davidson14f1ec02014-04-29 11:58:26 -0700301 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700302 throw e.rethrowFromSystemServer();
Jeff Davidson14f1ec02014-04-29 11:58:26 -0700303 }
304 }
Jeremy Joslind1daf6d2016-11-28 17:47:35 -0800305
306 /**
Amin Shaikh972e2362016-12-07 14:08:09 -0800307 * Unregister a network score cache.
308 *
309 * @param networkType the type of network this cache can handle. See {@link NetworkKey#type}.
310 * @param scoreCache implementation of {@link INetworkScoreCache} to store the scores.
311 * @throws SecurityException if the caller does not hold the
Jeremy Joslin186f3332017-01-06 14:36:54 -0800312 * {@link android.Manifest.permission#REQUEST_NETWORK_SCORES} permission.
Amin Shaikh972e2362016-12-07 14:08:09 -0800313 * @throws IllegalArgumentException if a score cache is already registered for this type.
314 * @hide
315 */
316 public void unregisterNetworkScoreCache(int networkType, INetworkScoreCache scoreCache) {
317 try {
318 mService.unregisterNetworkScoreCache(networkType, scoreCache);
319 } catch (RemoteException e) {
320 throw e.rethrowFromSystemServer();
321 }
322 }
323
324 /**
Jeremy Joslind1daf6d2016-11-28 17:47:35 -0800325 * Request a recommendation for which network to connect to.
326 *
Jeremy Joslin36d4c482016-12-09 13:11:51 -0800327 * <p>It is not safe to call this method from the main thread.
328 *
Jeremy Joslind1daf6d2016-11-28 17:47:35 -0800329 * @param request a {@link RecommendationRequest} instance containing additional
330 * request details
331 * @return a {@link RecommendationResult} instance containing the recommended network
332 * to connect to
Jeremy Joslin186f3332017-01-06 14:36:54 -0800333 * @throws SecurityException if the caller does not hold the
334 * {@link android.Manifest.permission#REQUEST_NETWORK_SCORES} permission.
Jeremy Joslind1daf6d2016-11-28 17:47:35 -0800335 */
336 public RecommendationResult requestRecommendation(RecommendationRequest request)
337 throws SecurityException {
338 try {
339 return mService.requestRecommendation(request);
340 } catch (RemoteException e) {
341 throw e.rethrowFromSystemServer();
342 }
343 }
Jeremy Joslin9b442fa2017-01-09 16:22:20 -0800344
345 /**
346 * Determine whether the application with the given UID is the enabled scorer.
347 *
348 * @param callingUid the UID to check
349 * @return true if the provided UID is the active scorer, false otherwise.
350 * @hide
351 */
352 public boolean isCallerActiveScorer(int callingUid) {
353 try {
354 return mService.isCallerActiveScorer(callingUid);
355 } catch (RemoteException e) {
356 throw e.rethrowFromSystemServer();
357 }
358 }
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700359}