blob: c09492cfd14b6bee4dd6e4d455c0e3308391e048 [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 Davidsonb6646a82014-06-27 16:24:42 -070084 * 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 Davidsonb51e0a62014-04-09 12:38:15 -070095 * 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 Davidson6a4b2202014-04-16 17:29:40 -0700102 private final INetworkScoreService mService;
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700103
104 /** @hide */
105 public NetworkScoreManager(Context context) {
106 mContext = context;
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700107 IBinder iBinder = ServiceManager.getService(Context.NETWORK_SCORE_SERVICE);
108 mService = INetworkScoreService.Stub.asInterface(iBinder);
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700109 }
110
111 /**
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700112 * Obtain the package name of the current active network scorer.
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700113 *
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700114 * <p>At any time, only one scorer application will receive {@link #ACTION_SCORE_NETWORKS}
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700115 * 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 Davidsonee2a1212014-04-17 12:19:23 -0700117 * the {@link #ACTION_CHANGE_ACTIVE} intent.
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700118 * @return the full package name of the current active scorer, or null if there is no active
Jeff Davidson14f1ec02014-04-29 11:58:26 -0700119 * scorer.
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700120 */
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700121 public String getActiveScorerPackage() {
Jeff Davidsonc7415532014-06-23 18:15:34 -0700122 NetworkScorerAppData app = NetworkScorerAppManager.getActiveScorer(mContext);
123 if (app == null) {
124 return null;
125 }
126 return app.mPackageName;
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700127 }
128
129 /**
130 * Update network scores.
131 *
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700132 * <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 Davidsonb51e0a62014-04-09 12:38:15 -0700135 *
136 * @param networks the networks which have been scored by the scorer.
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700137 * @return whether the update was successful.
138 * @throws SecurityException if the caller is not the active scorer.
Jeff Davidsonb51e0a62014-04-09 12:38:15 -0700139 */
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700140 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 Davidson14f1ec02014-04-29 11:58:26 -0700173 * {@link android.Manifest.permission#BROADCAST_SCORE_NETWORKS} permission indicating
174 * that it can manage scorer applications.
Jeff Davidson6a4b2202014-04-16 17:29:40 -0700175 * @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 Davidsonb51e0a62014-04-09 12:38:15 -0700183 }
Jeff Davidson14f1ec02014-04-29 11:58:26 -0700184
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 Davidsonb51e0a62014-04-09 12:38:15 -0700224}