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.Manifest;
20 import android.Manifest.permission;
21 import android.annotation.Nullable;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.pm.ActivityInfo;
25 import android.content.pm.PackageManager;
26 import android.content.pm.ResolveInfo;
27 import android.os.UserHandle;
28 import android.provider.Settings;
29 import android.text.TextUtils;
30 import android.util.Log;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.Collections;
35 import java.util.List;
38 * Internal class for managing the primary network scorer application.
40 * TODO: Rename this to something more generic.
44 public final class NetworkScorerAppManager {
45 private static final String TAG = "NetworkScorerAppManager";
47 private static final Intent SCORE_INTENT =
48 new Intent(NetworkScoreManager.ACTION_SCORE_NETWORKS);
50 /** This class cannot be instantiated. */
51 private NetworkScorerAppManager() {}
53 public static class NetworkScorerAppData {
54 /** Package name of this scorer app. */
55 public final String mPackageName;
57 /** UID of the scorer app. */
58 public final int mPackageUid;
60 /** Name of this scorer app for display. */
61 public final CharSequence mScorerName;
64 * Optional class name of a configuration activity. Null if none is set.
66 * @see NetworkScoreManager#ACTION_CUSTOM_ENABLE
68 public final String mConfigurationActivityClassName;
71 * Optional class name of the scoring service we can bind to. Null if none is set.
73 public final String mScoringServiceClassName;
75 public NetworkScorerAppData(String packageName, int packageUid, CharSequence scorerName,
76 @Nullable String configurationActivityClassName,
77 @Nullable String scoringServiceClassName) {
78 mScorerName = scorerName;
79 mPackageName = packageName;
80 mPackageUid = packageUid;
81 mConfigurationActivityClassName = configurationActivityClassName;
82 mScoringServiceClassName = scoringServiceClassName;
86 public String toString() {
87 final StringBuilder sb = new StringBuilder("NetworkScorerAppData{");
88 sb.append("mPackageName='").append(mPackageName).append('\'');
89 sb.append(", mPackageUid=").append(mPackageUid);
90 sb.append(", mScorerName=").append(mScorerName);
91 sb.append(", mConfigurationActivityClassName='").append(mConfigurationActivityClassName)
93 sb.append(", mScoringServiceClassName='").append(mScoringServiceClassName).append('\'');
100 * Returns the list of available scorer apps.
102 * <p>A network scorer is any application which:
104 * <li>Declares the {@link android.Manifest.permission#SCORE_NETWORKS} permission.
105 * <li>Includes a receiver for {@link NetworkScoreManager#ACTION_SCORE_NETWORKS} guarded by the
106 * {@link android.Manifest.permission#BROADCAST_NETWORK_PRIVILEGED} permission.
109 * @return the list of scorers, or the empty list if there are no valid scorers.
111 public static Collection<NetworkScorerAppData> getAllValidScorers(Context context) {
112 // Network scorer apps can only run as the primary user so exit early if we're not the
114 if (UserHandle.getCallingUserId() != UserHandle.USER_SYSTEM) {
115 return Collections.emptyList();
118 List<NetworkScorerAppData> scorers = new ArrayList<>();
119 PackageManager pm = context.getPackageManager();
120 // Only apps installed under the primary user of the device can be scorers.
121 // TODO: http://b/23422763
122 List<ResolveInfo> receivers =
123 pm.queryBroadcastReceiversAsUser(SCORE_INTENT, 0 /* flags */, UserHandle.USER_SYSTEM);
124 for (ResolveInfo receiver : receivers) {
125 // This field is a misnomer, see android.content.pm.ResolveInfo#activityInfo
126 final ActivityInfo receiverInfo = receiver.activityInfo;
127 if (receiverInfo == null) {
128 // Should never happen with queryBroadcastReceivers, but invalid nonetheless.
131 if (!permission.BROADCAST_NETWORK_PRIVILEGED.equals(receiverInfo.permission)) {
132 // Receiver doesn't require the BROADCAST_NETWORK_PRIVILEGED permission, which
133 // means anyone could trigger network scoring and flood the framework with score
137 if (pm.checkPermission(permission.SCORE_NETWORKS, receiverInfo.packageName) !=
138 PackageManager.PERMISSION_GRANTED) {
139 // Application doesn't hold the SCORE_NETWORKS permission, so the user never
140 // approved it as a network scorer.
144 // Optionally, this package may specify a configuration activity.
145 String configurationActivityClassName = null;
146 Intent intent = new Intent(NetworkScoreManager.ACTION_CUSTOM_ENABLE);
147 intent.setPackage(receiverInfo.packageName);
148 List<ResolveInfo> configActivities = pm.queryIntentActivities(intent, 0 /* flags */);
149 if (configActivities != null && !configActivities.isEmpty()) {
150 ActivityInfo activityInfo = configActivities.get(0).activityInfo;
151 if (activityInfo != null) {
152 configurationActivityClassName = activityInfo.name;
156 // Find the scoring service class we can bind to, if any.
157 String scoringServiceClassName = null;
158 Intent serviceIntent = new Intent(NetworkScoreManager.ACTION_SCORE_NETWORKS);
159 serviceIntent.setPackage(receiverInfo.packageName);
160 ResolveInfo resolveServiceInfo = pm.resolveService(serviceIntent, 0 /* flags */);
161 if (resolveServiceInfo != null && resolveServiceInfo.serviceInfo != null) {
162 scoringServiceClassName = resolveServiceInfo.serviceInfo.name;
165 // NOTE: loadLabel will attempt to load the receiver's label and fall back to the
166 // app label if none is present.
167 scorers.add(new NetworkScorerAppData(receiverInfo.packageName,
168 receiverInfo.applicationInfo.uid, receiverInfo.loadLabel(pm),
169 configurationActivityClassName, scoringServiceClassName));
176 * Get the application to use for scoring networks.
178 * @return the scorer app info or null if scoring is disabled (including if no scorer was ever
179 * selected) or if the previously-set scorer is no longer a valid scorer app (e.g. because
180 * it was disabled or uninstalled).
182 public static NetworkScorerAppData getActiveScorer(Context context) {
183 String scorerPackage = Settings.Global.getString(context.getContentResolver(),
184 Settings.Global.NETWORK_SCORER_APP);
185 return getScorer(context, scorerPackage);
189 * Set the specified package as the default scorer application.
191 * <p>The caller must have permission to write to {@link android.provider.Settings.Global}.
193 * @param context the context of the calling application
194 * @param packageName the packageName of the new scorer to use. If null, scoring will be
195 * disabled. Otherwise, the scorer will only be set if it is a valid scorer application.
196 * @return true if the scorer was changed, or false if the package is not a valid scorer.
198 public static boolean setActiveScorer(Context context, String packageName) {
199 String oldPackageName = Settings.Global.getString(context.getContentResolver(),
200 Settings.Global.NETWORK_SCORER_APP);
201 if (TextUtils.equals(oldPackageName, packageName)) {
206 Log.i(TAG, "Changing network scorer from " + oldPackageName + " to " + packageName);
208 if (packageName == null) {
209 Settings.Global.putString(context.getContentResolver(),
210 Settings.Global.NETWORK_SCORER_APP, null);
213 // We only make the change if the new package is valid.
214 if (getScorer(context, packageName) != null) {
215 Settings.Global.putString(context.getContentResolver(),
216 Settings.Global.NETWORK_SCORER_APP, packageName);
219 Log.w(TAG, "Requested network scorer is not valid: " + packageName);
225 /** Determine whether the application with the given UID is the enabled scorer. */
226 public static boolean isCallerActiveScorer(Context context, int callingUid) {
227 NetworkScorerAppData defaultApp = getActiveScorer(context);
228 if (defaultApp == null) {
231 if (callingUid != defaultApp.mPackageUid) {
234 // To be extra safe, ensure the caller holds the SCORE_NETWORKS permission. It always
235 // should, since it couldn't become the active scorer otherwise, but this can't hurt.
236 return context.checkCallingPermission(Manifest.permission.SCORE_NETWORKS) ==
237 PackageManager.PERMISSION_GRANTED;
240 /** Returns the {@link NetworkScorerAppData} for the given app, or null if it's not a scorer. */
241 public static NetworkScorerAppData getScorer(Context context, String packageName) {
242 if (TextUtils.isEmpty(packageName)) {
245 Collection<NetworkScorerAppData> applications = getAllValidScorers(context);
246 for (NetworkScorerAppData app : applications) {
247 if (packageName.equals(app.mPackageName)) {