blob: 343054712c7b2596b96ed778041cf57bc75bfc99 [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;
22
23/**
24 * Class that manages communication between network subsystems and a network scorer.
25 *
26 * <p>You can get an instance of this class by calling
27 * {@link android.content.Context#getSystemService(String)}:
28 *
29 * <pre>NetworkScoreManager manager =
30 * (NetworkScoreManager) getSystemService(Context.NETWORK_SCORE_SERVICE)</pre>
31 *
32 * <p>A network scorer is any application which:
33 * <ul>
34 * <li>Declares the {@link android.Manifest.permission#SCORE_NETWORKS} permission.
35 * <li>Includes a receiver for {@link #ACTION_SCORE_NETWORKS} guarded by the
36 * {@link android.Manifest.permission#BROADCAST_SCORE_NETWORKS} permission which scores networks
37 * and (eventually) calls {@link #updateScores} with the results.
38 * </ul>
39 *
40 * <p>The system keeps track of a default scorer application; at any time, only this application
41 * will receive {@link #ACTION_SCORE_NETWORKS} broadcasts and will be permitted to call
42 * {@link #updateScores}. Applications may determine the current default scorer with
43 * {@link #getDefaultScorerPackage()} and request to change the default scorer by sending an
44 * {@link #ACTION_CHANGE_DEFAULT} broadcast with another scorer.
45 *
46 * @hide
47 */
48public class NetworkScoreManager {
49 /**
50 * Activity action: ask the user to change the default network scorer. This will show a dialog
51 * that asks the user whether they want to replace the current default scorer with the one
52 * specified in {@link #EXTRA_PACKAGE_NAME}. The activity will finish with RESULT_OK if the
53 * default was changed or RESULT_CANCELED if it failed for any reason.
54 */
55 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
56 public static final String ACTION_CHANGE_DEFAULT = "android.net.scoring.CHANGE_DEFAULT";
57
58 /**
59 * Extra used with {@link #ACTION_CHANGE_DEFAULT} to specify the new scorer package. Set with
60 * {@link android.content.Intent#putExtra(String, String)}.
61 */
62 public static final String EXTRA_PACKAGE_NAME = "packageName";
63
64 /**
65 * Broadcast action: new network scores are being requested. This intent will only be delivered
66 * to the current default scorer app. That app is responsible for scoring the networks and
67 * calling {@link #updateScores} when complete. The networks to score are specified in
68 * {@link #EXTRA_NETWORKS_TO_SCORE}, and will generally consist of all networks which have been
69 * configured by the user as well as any open networks.
70 *
71 * <p class="note">This is a protected intent that can only be sent by the system.
72 */
73 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
74 public static final String ACTION_SCORE_NETWORKS = "android.net.scoring.SCORE_NETWORKS";
75
76 /**
77 * Extra used with {@link #ACTION_SCORE_NETWORKS} to specify the networks to be scored, as an
78 * array of {@link NetworkKey}s. Can be obtained with
79 * {@link android.content.Intent#getParcelableArrayExtra(String)}}.
80 */
81 public static final String EXTRA_NETWORKS_TO_SCORE = "networksToScore";
82
83 private final Context mContext;
84
85 /** @hide */
86 public NetworkScoreManager(Context context) {
87 mContext = context;
88 }
89
90 /**
91 * Obtain the package name of the current default network scorer.
92 *
93 * At any time, only one scorer application will receive {@link #ACTION_SCORE_NETWORKS}
94 * broadcasts and be allowed to call {@link #updateScores}. Applications may use this method to
95 * determine the current scorer and offer the user the ability to select a different scorer via
96 * the {@link #ACTION_CHANGE_DEFAULT} intent.
97 * @return the full package name of the current default scorer, or null if there is no active
98 * scorer.
99 */
100 public String getDefaultScorerPackage() {
101 // TODO: Implement.
102 return null;
103 }
104
105 /**
106 * Update network scores.
107 *
108 * This may be called at any time to re-score active networks. Scores will generally be updated
109 * quickly, but if this method is called too frequently, the scores may be held and applied at
110 * a later time.
111 *
112 * @param networks the networks which have been scored by the scorer.
113 * @throws SecurityException if the caller is not the default scorer.
114 */
115 public void updateScores(ScoredNetwork[] networks) throws SecurityException {
116 // TODO: Implement.
117 }
118}