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
17package android.net;
18
19import android.Manifest;
20import android.Manifest.permission;
21import android.annotation.Nullable;
22import android.content.Context;
23import android.content.Intent;
24import android.content.pm.ActivityInfo;
25import android.content.pm.PackageManager;
26import android.content.pm.ResolveInfo;
27import android.os.UserHandle;
28import android.provider.Settings;
29import android.text.TextUtils;
30import android.util.Log;
31
32import java.util.ArrayList;
33import java.util.Collection;
34import java.util.Collections;
35import java.util.List;
36
37/**
38 * Internal class for managing the primary network scorer application.
39 *
40 * TODO: Rename this to something more generic.
41 *
42 * @hide
43 */
44public final class NetworkScorerAppManager {
45    private static final String TAG = "NetworkScorerAppManager";
46
47    private static final Intent SCORE_INTENT =
48            new Intent(NetworkScoreManager.ACTION_SCORE_NETWORKS);
49
50    /** This class cannot be instantiated. */
51    private NetworkScorerAppManager() {}
52
53    public static class NetworkScorerAppData {
54        /** Package name of this scorer app. */
55        public final String mPackageName;
56
57        /** UID of the scorer app. */
58        public final int mPackageUid;
59
60        /** Name of this scorer app for display. */
61        public final CharSequence mScorerName;
62
63        /**
64         * Optional class name of a configuration activity. Null if none is set.
65         *
66         * @see NetworkScoreManager#ACTION_CUSTOM_ENABLE
67         */
68        public final String mConfigurationActivityClassName;
69
70        /**
71         * Optional class name of the scoring service we can bind to. Null if none is set.
72         */
73        public final String mScoringServiceClassName;
74
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;
83        }
84
85        @Override
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)
92                    .append('\'');
93            sb.append(", mScoringServiceClassName='").append(mScoringServiceClassName).append('\'');
94            sb.append('}');
95            return sb.toString();
96        }
97    }
98
99    /**
100     * Returns the list of available scorer apps.
101     *
102     * <p>A network scorer is any application which:
103     * <ul>
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.
107     * </ul>
108     *
109     * @return the list of scorers, or the empty list if there are no valid scorers.
110     */
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
113        // primary user.
114        if (UserHandle.getCallingUserId() != UserHandle.USER_SYSTEM) {
115            return Collections.emptyList();
116        }
117
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.
129                continue;
130            }
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
134                // requests.
135                continue;
136            }
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.
141                continue;
142            }
143
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;
153                }
154            }
155
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;
163            }
164
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));
170        }
171
172        return scorers;
173    }
174
175    /**
176     * Get the application to use for scoring networks.
177     *
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).
181     */
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);
186    }
187
188    /**
189     * Set the specified package as the default scorer application.
190     *
191     * <p>The caller must have permission to write to {@link android.provider.Settings.Global}.
192     *
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.
197     */
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)) {
202            // No change.
203            return true;
204        }
205
206        Log.i(TAG, "Changing network scorer from " + oldPackageName + " to " + packageName);
207
208        if (packageName == null) {
209            Settings.Global.putString(context.getContentResolver(),
210                    Settings.Global.NETWORK_SCORER_APP, null);
211            return true;
212        } else {
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);
217                return true;
218            } else {
219                Log.w(TAG, "Requested network scorer is not valid: " + packageName);
220                return false;
221            }
222        }
223    }
224
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) {
229            return false;
230        }
231        if (callingUid != defaultApp.mPackageUid) {
232            return false;
233        }
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;
238    }
239
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)) {
243            return null;
244        }
245        Collection<NetworkScorerAppData> applications = getAllValidScorers(context);
246        for (NetworkScorerAppData app : applications) {
247            if (packageName.equals(app.mPackageName)) {
248                return app;
249            }
250        }
251        return null;
252    }
253}
254