2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
19 import android.annotation.SdkConstant;
20 import android.annotation.SdkConstant.SdkConstantType;
21 import android.content.Context;
22 import android.os.IBinder;
23 import android.os.RemoteException;
24 import android.os.ServiceManager;
27 * Class that manages communication between network subsystems and a network scorer.
29 * <p>You can get an instance of this class by calling
30 * {@link android.content.Context#getSystemService(String)}:
32 * <pre>NetworkScoreManager manager =
33 * (NetworkScoreManager) getSystemService(Context.NETWORK_SCORE_SERVICE)</pre>
35 * <p>A network scorer is any application which:
37 * <li>Declares the {@link android.Manifest.permission#SCORE_NETWORKS} permission.
38 * <li>Includes a receiver for {@link #ACTION_SCORE_NETWORKS} guarded by the
39 * {@link android.Manifest.permission#BROADCAST_SCORE_NETWORKS} permission which scores networks
40 * and (eventually) calls {@link #updateScores} with the results.
43 * <p>The system keeps track of a default scorer application; at any time, only this application
44 * will receive {@link #ACTION_SCORE_NETWORKS} broadcasts and will be permitted to call
45 * {@link #updateScores}. Applications may determine the current default scorer with
46 * {@link #getActiveScorerPackage()} and request to change the default scorer by sending an
47 * {@link #ACTION_CHANGE_DEFAULT} broadcast with another scorer.
51 public class NetworkScoreManager {
53 * Activity action: ask the user to change the default network scorer. This will show a dialog
54 * that asks the user whether they want to replace the current default scorer with the one
55 * specified in {@link #EXTRA_PACKAGE_NAME}. The activity will finish with RESULT_OK if the
56 * default was changed or RESULT_CANCELED if it failed for any reason.
58 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
59 public static final String ACTION_CHANGE_DEFAULT = "android.net.scoring.CHANGE_DEFAULT";
62 * Extra used with {@link #ACTION_CHANGE_DEFAULT} to specify the new scorer package. Set with
63 * {@link android.content.Intent#putExtra(String, String)}.
65 public static final String EXTRA_PACKAGE_NAME = "packageName";
68 * Broadcast action: new network scores are being requested. This intent will only be delivered
69 * to the current default scorer app. That app is responsible for scoring the networks and
70 * calling {@link #updateScores} when complete. The networks to score are specified in
71 * {@link #EXTRA_NETWORKS_TO_SCORE}, and will generally consist of all networks which have been
72 * configured by the user as well as any open networks.
74 * <p class="note">This is a protected intent that can only be sent by the system.
76 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
77 public static final String ACTION_SCORE_NETWORKS = "android.net.scoring.SCORE_NETWORKS";
80 * Extra used with {@link #ACTION_SCORE_NETWORKS} to specify the networks to be scored, as an
81 * array of {@link NetworkKey}s. Can be obtained with
82 * {@link android.content.Intent#getParcelableArrayExtra(String)}}.
84 public static final String EXTRA_NETWORKS_TO_SCORE = "networksToScore";
86 private final Context mContext;
87 private final INetworkScoreService mService;
90 public NetworkScoreManager(Context context) {
92 IBinder iBinder = ServiceManager.getService(Context.NETWORK_SCORE_SERVICE);
93 mService = INetworkScoreService.Stub.asInterface(iBinder);
97 * Obtain the package name of the current active network scorer.
99 * <p>At any time, only one scorer application will receive {@link #ACTION_SCORE_NETWORKS}
100 * broadcasts and be allowed to call {@link #updateScores}. Applications may use this method to
101 * determine the current scorer and offer the user the ability to select a different scorer via
102 * the {@link #ACTION_CHANGE_DEFAULT} intent.
103 * @return the full package name of the current active scorer, or null if there is no active
106 public String getActiveScorerPackage() {
107 return NetworkScorerAppManager.getActiveScorer(mContext);
111 * Update network scores.
113 * <p>This may be called at any time to re-score active networks. Scores will generally be
114 * updated quickly, but if this method is called too frequently, the scores may be held and
115 * applied at a later time.
117 * @param networks the networks which have been scored by the scorer.
118 * @return whether the update was successful.
119 * @throws SecurityException if the caller is not the active scorer.
121 public boolean updateScores(ScoredNetwork[] networks) throws SecurityException {
123 return mService.updateScores(networks);
124 } catch (RemoteException e) {
130 * Clear network scores.
132 * <p>Should be called when all scores need to be invalidated, i.e. because the scoring
133 * algorithm has changed and old scores can no longer be compared to future scores.
135 * <p>Note that scores will be cleared automatically when the active scorer changes, as scores
136 * from one scorer cannot be compared to those from another scorer.
138 * @return whether the clear was successful.
139 * @throws SecurityException if the caller is not the active scorer or privileged.
141 public boolean clearScores() throws SecurityException {
143 return mService.clearScores();
144 } catch (RemoteException e) {
150 * Set the active scorer to a new package and clear existing scores.
152 * @return true if the operation succeeded, or false if the new package is not a valid scorer.
153 * @throws SecurityException if the caller does not hold the
154 * {@link android.Manifest.permission#BROADCAST_SCORE_NETWORKS} permission indicating that
155 * it can manage scorer applications.
158 public boolean setActiveScorer(String packageName) throws SecurityException {
160 return mService.setActiveScorer(packageName);
161 } catch (RemoteException e) {