blob: 352512e5f2a0cc04c724bd584ba1c151d83d870b [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 Davidson6a4b2202014-04-16 17:29:40 -070023import android.os.IBinder;
24import android.os.RemoteException;
25import android.os.ServiceManager;
Jeff Davidsonb51e0a62014-04-09 12:38:15 -070026
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 Davidsonee2a1212014-04-17 12:19:23 -070044 * <p>The system keeps track of an active scorer application; at any time, only this application
Jeff Davidsonb51e0a62014-04-09 12:38:15 -070045 * will receive {@link #ACTION_SCORE_NETWORKS} broadcasts and will be permitted to call
Jeff Davidsonee2a1212014-04-17 12:19:23 -070046 * {@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 Davidsonb51e0a62014-04-09 12:38:15 -070049 *
50 * @hide
51 */
52public class NetworkScoreManager {
53 /**
Jeff Davidsonee2a1212014-04-17 12:19:23 -070054 * 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 Davidsonb51e0a62014-04-09 12:38:15 -070056 * specified in {@link #EXTRA_PACKAGE_NAME}. The activity will finish with RESULT_OK if the
Jeff Davidsonee2a1212014-04-17 12:19:23 -070057 * active scorer was changed or RESULT_CANCELED if it failed for any reason.
Jeff Davidsonb51e0a62014-04-09 12:38:15 -070058 */
59 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
Jeff Davidsonee2a1212014-04-17 12:19:23 -070060 public static final String ACTION_CHANGE_ACTIVE = "android.net.scoring.CHANGE_ACTIVE";
Jeff Davidsonb51e0a62014-04-09 12:38:15 -070061
62 /**
Jeff Davidsonee2a1212014-04-17 12:19:23 -070063 * Extra used with {@link #ACTION_CHANGE_ACTIVE} to specify the new scorer package. Set with
Jeff Davidsonb51e0a62014-04-09 12:38:15 -070064 * {@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 Davidsonee2a1212014-04-17 12:19:23 -070070 * to the current active scorer app. That app is responsible for scoring the networks and
Jeff Davidsonb51e0a62014-04-09 12:38:15 -070071 * 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 Davidson6a4b2202014-04-16 17:29:40 -070088 private final INetworkScoreService mService;
Jeff Davidsonb51e0a62014-04-09 12:38:15 -070089
90 /** @hide */
91 public NetworkScoreManager(Context context) {
92 mContext = context;
Jeff Davidson6a4b2202014-04-16 17:29:40 -070093 IBinder iBinder = ServiceManager.getService(Context.NETWORK_SCORE_SERVICE);
94 mService = INetworkScoreService.Stub.asInterface(iBinder);
Jeff Davidsonb51e0a62014-04-09 12:38:15 -070095 }
96
97 /**
Jeff Davidson6a4b2202014-04-16 17:29:40 -070098 * Obtain the package name of the current active network scorer.
Jeff Davidsonb51e0a62014-04-09 12:38:15 -070099 *
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700100 * <p>At any time, only one scorer application will receive {@link #ACTION_SCORE_NETWORKS}
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700101 * 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 Davidsonee2a1212014-04-17 12:19:23 -0700103 * the {@link #ACTION_CHANGE_ACTIVE} intent.
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700104 * @return the full package name of the current active scorer, or null if there is no active
Jeff Davidson14f1ec02014-04-29 11:58:26 -0700105 * scorer.
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700106 */
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700107 public String getActiveScorerPackage() {
108 return NetworkScorerAppManager.getActiveScorer(mContext);
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700109 }
110
111 /**
112 * Update network scores.
113 *
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700114 * <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 Davidsonb51e0a62014-04-09 12:38:15 -0700117 *
118 * @param networks the networks which have been scored by the scorer.
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700119 * @return whether the update was successful.
120 * @throws SecurityException if the caller is not the active scorer.
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700121 */
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700122 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 Davidson14f1ec02014-04-29 11:58:26 -0700155 * {@link android.Manifest.permission#BROADCAST_SCORE_NETWORKS} permission indicating
156 * that it can manage scorer applications.
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700157 * @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 Davidsonb51e0a62014-04-09 12:38:15 -0700165 }
Jeff Davidson14f1ec02014-04-29 11:58:26 -0700166
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 Davidsonb51e0a62014-04-09 12:38:15 -0700206}