OSDN Git Service

Initial implementation of NetworkScoreManager's backing service.
[android-x86/frameworks-base.git] / core / java / android / net / NetworkScoreManager.java
1 /*
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
17 package android.net;
18
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;
25
26 /**
27  * Class that manages communication between network subsystems and a network scorer.
28  *
29  * <p>You can get an instance of this class by calling
30  * {@link android.content.Context#getSystemService(String)}:
31  *
32  * <pre>NetworkScoreManager manager =
33  *     (NetworkScoreManager) getSystemService(Context.NETWORK_SCORE_SERVICE)</pre>
34  *
35  * <p>A network scorer is any application which:
36  * <ul>
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.
41  * </ul>
42  *
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.
48  *
49  * @hide
50  */
51 public class NetworkScoreManager {
52     /**
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.
57      */
58     @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
59     public static final String ACTION_CHANGE_DEFAULT = "android.net.scoring.CHANGE_DEFAULT";
60
61     /**
62      * Extra used with {@link #ACTION_CHANGE_DEFAULT} to specify the new scorer package. Set with
63      * {@link android.content.Intent#putExtra(String, String)}.
64      */
65     public static final String EXTRA_PACKAGE_NAME = "packageName";
66
67     /**
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.
73      *
74      * <p class="note">This is a protected intent that can only be sent by the system.
75      */
76     @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
77     public static final String ACTION_SCORE_NETWORKS = "android.net.scoring.SCORE_NETWORKS";
78
79     /**
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)}}.
83      */
84     public static final String EXTRA_NETWORKS_TO_SCORE = "networksToScore";
85
86     private final Context mContext;
87     private final INetworkScoreService mService;
88
89     /** @hide */
90     public NetworkScoreManager(Context context) {
91         mContext = context;
92         IBinder iBinder = ServiceManager.getService(Context.NETWORK_SCORE_SERVICE);
93         mService = INetworkScoreService.Stub.asInterface(iBinder);
94     }
95
96     /**
97      * Obtain the package name of the current active network scorer.
98      *
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
104      *     scorer.
105      */
106     public String getActiveScorerPackage() {
107         return NetworkScorerAppManager.getActiveScorer(mContext);
108     }
109
110     /**
111      * Update network scores.
112      *
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.
116      *
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.
120      */
121     public boolean updateScores(ScoredNetwork[] networks) throws SecurityException {
122         try {
123             return mService.updateScores(networks);
124         } catch (RemoteException e) {
125             return false;
126         }
127     }
128
129     /**
130      * Clear network scores.
131      *
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.
134      *
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.
137      *
138      * @return whether the clear was successful.
139      * @throws SecurityException if the caller is not the active scorer or privileged.
140      */
141     public boolean clearScores() throws SecurityException {
142         try {
143             return mService.clearScores();
144         } catch (RemoteException e) {
145             return false;
146         }
147     }
148
149     /**
150      * Set the active scorer to a new package and clear existing scores.
151      *
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.
156      * @hide
157      */
158     public boolean setActiveScorer(String packageName) throws SecurityException {
159         try {
160             return mService.setActiveScorer(packageName);
161         } catch (RemoteException e) {
162             return false;
163         }
164     }
165 }