1b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson/*
2b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson * Copyright (C) 2014 The Android Open Source Project
3b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson *
4b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson * Licensed under the Apache License, Version 2.0 (the "License");
5b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson * you may not use this file except in compliance with the License.
6b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson * You may obtain a copy of the License at
7b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson *
8b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson *      http://www.apache.org/licenses/LICENSE-2.0
9b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson *
10b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson * Unless required by applicable law or agreed to in writing, software
11b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson * distributed under the License is distributed on an "AS IS" BASIS,
12b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson * See the License for the specific language governing permissions and
14b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson * limitations under the License
15b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson */
16b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson
17b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidsonpackage android.net;
18b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson
19ac7285dc1e13f30d59dad30fe2ad1116e5f676cbJeff Davidsonimport android.Manifest;
20b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidsonimport android.annotation.SdkConstant;
21b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidsonimport android.annotation.SdkConstant.SdkConstantType;
227be8e9725f4a60f0d1a6cb175a05a320968d7439Jeff Davidsonimport android.annotation.SystemApi;
23b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidsonimport android.content.Context;
2414f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidsonimport android.content.Intent;
25c741553644f8b19c63938ab9e36af1721c2cfa34Jeff Davidsonimport android.net.NetworkScorerAppManager.NetworkScorerAppData;
266a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidsonimport android.os.IBinder;
276a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidsonimport android.os.RemoteException;
286a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidsonimport android.os.ServiceManager;
29ac7285dc1e13f30d59dad30fe2ad1116e5f676cbJeff Davidsonimport android.os.UserHandle;
30b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson
31b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson/**
32b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson * Class that manages communication between network subsystems and a network scorer.
33b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson *
34b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson * <p>You can get an instance of this class by calling
35b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson * {@link android.content.Context#getSystemService(String)}:
36b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson *
37b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson * <pre>NetworkScoreManager manager =
38b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson *     (NetworkScoreManager) getSystemService(Context.NETWORK_SCORE_SERVICE)</pre>
39b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson *
40b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson * <p>A network scorer is any application which:
41b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson * <ul>
42b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson * <li>Declares the {@link android.Manifest.permission#SCORE_NETWORKS} permission.
43b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson * <li>Includes a receiver for {@link #ACTION_SCORE_NETWORKS} guarded by the
44161977998feebf0a855ea56558464470877040cfJeff Davidson *     {@link android.Manifest.permission#BROADCAST_NETWORK_PRIVILEGED} permission which scores
45161977998feebf0a855ea56558464470877040cfJeff Davidson *     networks and (eventually) calls {@link #updateScores} with the results. If this receiver
46161977998feebf0a855ea56558464470877040cfJeff Davidson *     specifies an android:label attribute, this label will be used when referring to the
47161977998feebf0a855ea56558464470877040cfJeff Davidson *     application throughout system settings; otherwise, the application label will be used.
48b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson * </ul>
49b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson *
50ee2a1219d3c59d88c83fca65289166fa9d7e53e9Jeff Davidson * <p>The system keeps track of an active scorer application; at any time, only this application
51b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson * will receive {@link #ACTION_SCORE_NETWORKS} broadcasts and will be permitted to call
52ee2a1219d3c59d88c83fca65289166fa9d7e53e9Jeff Davidson * {@link #updateScores}. Applications may determine the current active scorer with
53ee2a1219d3c59d88c83fca65289166fa9d7e53e9Jeff Davidson * {@link #getActiveScorerPackage()} and request to change the active scorer by sending an
54ee2a1219d3c59d88c83fca65289166fa9d7e53e9Jeff Davidson * {@link #ACTION_CHANGE_ACTIVE} broadcast with another scorer.
55b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson *
56b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson * @hide
57b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson */
587be8e9725f4a60f0d1a6cb175a05a320968d7439Jeff Davidson@SystemApi
59b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidsonpublic class NetworkScoreManager {
60b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson    /**
61ee2a1219d3c59d88c83fca65289166fa9d7e53e9Jeff Davidson     * Activity action: ask the user to change the active network scorer. This will show a dialog
62ee2a1219d3c59d88c83fca65289166fa9d7e53e9Jeff Davidson     * that asks the user whether they want to replace the current active scorer with the one
63b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson     * specified in {@link #EXTRA_PACKAGE_NAME}. The activity will finish with RESULT_OK if the
64ee2a1219d3c59d88c83fca65289166fa9d7e53e9Jeff Davidson     * active scorer was changed or RESULT_CANCELED if it failed for any reason.
65b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson     */
66b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson    @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
67ee2a1219d3c59d88c83fca65289166fa9d7e53e9Jeff Davidson    public static final String ACTION_CHANGE_ACTIVE = "android.net.scoring.CHANGE_ACTIVE";
68b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson
69b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson    /**
70ee2a1219d3c59d88c83fca65289166fa9d7e53e9Jeff Davidson     * Extra used with {@link #ACTION_CHANGE_ACTIVE} to specify the new scorer package. Set with
71b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson     * {@link android.content.Intent#putExtra(String, String)}.
72b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson     */
73b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson    public static final String EXTRA_PACKAGE_NAME = "packageName";
74b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson
75b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson    /**
76b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson     * Broadcast action: new network scores are being requested. This intent will only be delivered
77ee2a1219d3c59d88c83fca65289166fa9d7e53e9Jeff Davidson     * to the current active scorer app. That app is responsible for scoring the networks and
78b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson     * calling {@link #updateScores} when complete. The networks to score are specified in
79b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson     * {@link #EXTRA_NETWORKS_TO_SCORE}, and will generally consist of all networks which have been
80b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson     * configured by the user as well as any open networks.
81b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson     *
82b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson     * <p class="note">This is a protected intent that can only be sent by the system.
83b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson     */
84b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
85b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson    public static final String ACTION_SCORE_NETWORKS = "android.net.scoring.SCORE_NETWORKS";
86b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson
87b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson    /**
88b096bdceaf2a4becffd2d930a870ccb57bfbe99cJeff Davidson     * Extra used with {@link #ACTION_SCORE_NETWORKS} to specify the networks to be scored, as an
89b096bdceaf2a4becffd2d930a870ccb57bfbe99cJeff Davidson     * array of {@link NetworkKey}s. Can be obtained with
90b096bdceaf2a4becffd2d930a870ccb57bfbe99cJeff Davidson     * {@link android.content.Intent#getParcelableArrayExtra(String)}}.
91b096bdceaf2a4becffd2d930a870ccb57bfbe99cJeff Davidson     */
92b096bdceaf2a4becffd2d930a870ccb57bfbe99cJeff Davidson    public static final String EXTRA_NETWORKS_TO_SCORE = "networksToScore";
93b096bdceaf2a4becffd2d930a870ccb57bfbe99cJeff Davidson
94b096bdceaf2a4becffd2d930a870ccb57bfbe99cJeff Davidson    /**
95b6646a8987a8ce6abd34cc05d9ca0193a6d7bea9Jeff Davidson     * Activity action: launch a custom activity for configuring a scorer before enabling it.
96b6646a8987a8ce6abd34cc05d9ca0193a6d7bea9Jeff Davidson     * Scorer applications may choose to specify an activity for this action, in which case the
97b6646a8987a8ce6abd34cc05d9ca0193a6d7bea9Jeff Davidson     * framework will launch that activity which should return RESULT_OK if scoring was enabled.
98b6646a8987a8ce6abd34cc05d9ca0193a6d7bea9Jeff Davidson     *
99b6646a8987a8ce6abd34cc05d9ca0193a6d7bea9Jeff Davidson     * <p>If no activity is included in a scorer which implements this action, the system dialog for
100b6646a8987a8ce6abd34cc05d9ca0193a6d7bea9Jeff Davidson     * selecting a scorer will be shown instead.
101b6646a8987a8ce6abd34cc05d9ca0193a6d7bea9Jeff Davidson     */
102b6646a8987a8ce6abd34cc05d9ca0193a6d7bea9Jeff Davidson    @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
103b6646a8987a8ce6abd34cc05d9ca0193a6d7bea9Jeff Davidson    public static final String ACTION_CUSTOM_ENABLE = "android.net.scoring.CUSTOM_ENABLE";
104b6646a8987a8ce6abd34cc05d9ca0193a6d7bea9Jeff Davidson
105b6646a8987a8ce6abd34cc05d9ca0193a6d7bea9Jeff Davidson    /**
106b096bdceaf2a4becffd2d930a870ccb57bfbe99cJeff Davidson     * Broadcast action: the active scorer has been changed. Scorer apps may listen to this to
107b096bdceaf2a4becffd2d930a870ccb57bfbe99cJeff Davidson     * perform initialization once selected as the active scorer, or clean up unneeded resources
108da11f5cdf55ace0330008613fa7153993ddd943fJeremy Joslin     * if another scorer has been selected. This is an explicit broadcast only sent to the
109da11f5cdf55ace0330008613fa7153993ddd943fJeremy Joslin     * previous scorer and new scorer. Note that it is unnecessary to clear existing scores as
110b096bdceaf2a4becffd2d930a870ccb57bfbe99cJeff Davidson     * this is handled by the system.
111b096bdceaf2a4becffd2d930a870ccb57bfbe99cJeff Davidson     *
112b096bdceaf2a4becffd2d930a870ccb57bfbe99cJeff Davidson     * <p>The new scorer will be specified in {@link #EXTRA_NEW_SCORER}.
113b096bdceaf2a4becffd2d930a870ccb57bfbe99cJeff Davidson     *
114b096bdceaf2a4becffd2d930a870ccb57bfbe99cJeff Davidson     * <p class="note">This is a protected intent that can only be sent by the system.
115b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson     */
116b096bdceaf2a4becffd2d930a870ccb57bfbe99cJeff Davidson    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
117b096bdceaf2a4becffd2d930a870ccb57bfbe99cJeff Davidson    public static final String ACTION_SCORER_CHANGED = "android.net.scoring.SCORER_CHANGED";
118b096bdceaf2a4becffd2d930a870ccb57bfbe99cJeff Davidson
119b096bdceaf2a4becffd2d930a870ccb57bfbe99cJeff Davidson    /**
120b096bdceaf2a4becffd2d930a870ccb57bfbe99cJeff Davidson     * Extra used with {@link #ACTION_SCORER_CHANGED} to specify the newly selected scorer's package
121b096bdceaf2a4becffd2d930a870ccb57bfbe99cJeff Davidson     * name. Will be null if scoring was disabled. Can be obtained with
122b096bdceaf2a4becffd2d930a870ccb57bfbe99cJeff Davidson     * {@link android.content.Intent#getStringExtra(String)}.
123b096bdceaf2a4becffd2d930a870ccb57bfbe99cJeff Davidson     */
124b096bdceaf2a4becffd2d930a870ccb57bfbe99cJeff Davidson    public static final String EXTRA_NEW_SCORER = "newScorer";
125b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson
126b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson    private final Context mContext;
1276a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson    private final INetworkScoreService mService;
128b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson
129b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson    /** @hide */
130b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson    public NetworkScoreManager(Context context) {
131b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson        mContext = context;
1326a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson        IBinder iBinder = ServiceManager.getService(Context.NETWORK_SCORE_SERVICE);
1336a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson        mService = INetworkScoreService.Stub.asInterface(iBinder);
134b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson    }
135b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson
136b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson    /**
1376a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson     * Obtain the package name of the current active network scorer.
138b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson     *
1396a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson     * <p>At any time, only one scorer application will receive {@link #ACTION_SCORE_NETWORKS}
140b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson     * broadcasts and be allowed to call {@link #updateScores}. Applications may use this method to
141b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson     * determine the current scorer and offer the user the ability to select a different scorer via
142ee2a1219d3c59d88c83fca65289166fa9d7e53e9Jeff Davidson     * the {@link #ACTION_CHANGE_ACTIVE} intent.
1436a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson     * @return the full package name of the current active scorer, or null if there is no active
14414f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson     *         scorer.
145b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson     */
1466a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson    public String getActiveScorerPackage() {
147c741553644f8b19c63938ab9e36af1721c2cfa34Jeff Davidson        NetworkScorerAppData app = NetworkScorerAppManager.getActiveScorer(mContext);
148c741553644f8b19c63938ab9e36af1721c2cfa34Jeff Davidson        if (app == null) {
149c741553644f8b19c63938ab9e36af1721c2cfa34Jeff Davidson            return null;
150c741553644f8b19c63938ab9e36af1721c2cfa34Jeff Davidson        }
151c741553644f8b19c63938ab9e36af1721c2cfa34Jeff Davidson        return app.mPackageName;
152b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson    }
153b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson
154b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson    /**
155b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson     * Update network scores.
156b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson     *
1576a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson     * <p>This may be called at any time to re-score active networks. Scores will generally be
1586a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson     * updated quickly, but if this method is called too frequently, the scores may be held and
1596a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson     * applied at a later time.
160b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson     *
161b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson     * @param networks the networks which have been scored by the scorer.
1626a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson     * @return whether the update was successful.
1636a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson     * @throws SecurityException if the caller is not the active scorer.
164b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson     */
1656a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson    public boolean updateScores(ScoredNetwork[] networks) throws SecurityException {
1666a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson        try {
1676a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson            return mService.updateScores(networks);
1686a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson        } catch (RemoteException e) {
169c53962d4ede82a03b62f0c8bb86bd0da090a15ebJeff Sharkey            throw e.rethrowFromSystemServer();
1706a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson        }
1716a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson    }
1726a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson
1736a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson    /**
1746a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson     * Clear network scores.
1756a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson     *
1766a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson     * <p>Should be called when all scores need to be invalidated, i.e. because the scoring
1776a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson     * algorithm has changed and old scores can no longer be compared to future scores.
1786a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson     *
1796a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson     * <p>Note that scores will be cleared automatically when the active scorer changes, as scores
1806a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson     * from one scorer cannot be compared to those from another scorer.
1816a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson     *
1826a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson     * @return whether the clear was successful.
1836a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson     * @throws SecurityException if the caller is not the active scorer or privileged.
1846a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson     */
1856a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson    public boolean clearScores() throws SecurityException {
1866a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson        try {
1876a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson            return mService.clearScores();
1886a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson        } catch (RemoteException e) {
189c53962d4ede82a03b62f0c8bb86bd0da090a15ebJeff Sharkey            throw e.rethrowFromSystemServer();
1906a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson        }
1916a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson    }
1926a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson
1936a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson    /**
1946a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson     * Set the active scorer to a new package and clear existing scores.
1956a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson     *
196e56f2bb5ecae624e1b6573515f855a26f756aed5Jeff Davidson     * <p>Should never be called directly without obtaining user consent. This can be done by using
197e56f2bb5ecae624e1b6573515f855a26f756aed5Jeff Davidson     * the {@link #ACTION_CHANGE_ACTIVE} broadcast, or using a custom configuration activity.
198e56f2bb5ecae624e1b6573515f855a26f756aed5Jeff Davidson     *
1996a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson     * @return true if the operation succeeded, or false if the new package is not a valid scorer.
2006a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson     * @throws SecurityException if the caller does not hold the
201e56f2bb5ecae624e1b6573515f855a26f756aed5Jeff Davidson     *         {@link android.Manifest.permission#SCORE_NETWORKS} permission.
2026a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson     * @hide
2036a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson     */
204e56f2bb5ecae624e1b6573515f855a26f756aed5Jeff Davidson    @SystemApi
2056a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson    public boolean setActiveScorer(String packageName) throws SecurityException {
2066a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson        try {
2076a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson            return mService.setActiveScorer(packageName);
2086a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson        } catch (RemoteException e) {
209c53962d4ede82a03b62f0c8bb86bd0da090a15ebJeff Sharkey            throw e.rethrowFromSystemServer();
2106a4b220f1263d95fdefe6361c2bc87bbb04bbed0Jeff Davidson        }
211b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson    }
21214f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson
21314f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson    /**
21426fd143326a11c9dd7942e31acca6df56288d194Jeff Davidson     * Turn off network scoring.
21526fd143326a11c9dd7942e31acca6df56288d194Jeff Davidson     *
21626fd143326a11c9dd7942e31acca6df56288d194Jeff Davidson     * <p>May only be called by the current scorer app, or the system.
21726fd143326a11c9dd7942e31acca6df56288d194Jeff Davidson     *
21826fd143326a11c9dd7942e31acca6df56288d194Jeff Davidson     * @throws SecurityException if the caller is neither the active scorer nor the system.
21926fd143326a11c9dd7942e31acca6df56288d194Jeff Davidson     */
22026fd143326a11c9dd7942e31acca6df56288d194Jeff Davidson    public void disableScoring() throws SecurityException {
22126fd143326a11c9dd7942e31acca6df56288d194Jeff Davidson        try {
22226fd143326a11c9dd7942e31acca6df56288d194Jeff Davidson            mService.disableScoring();
22326fd143326a11c9dd7942e31acca6df56288d194Jeff Davidson        } catch (RemoteException e) {
224c53962d4ede82a03b62f0c8bb86bd0da090a15ebJeff Sharkey            throw e.rethrowFromSystemServer();
22526fd143326a11c9dd7942e31acca6df56288d194Jeff Davidson        }
22626fd143326a11c9dd7942e31acca6df56288d194Jeff Davidson    }
22726fd143326a11c9dd7942e31acca6df56288d194Jeff Davidson
22826fd143326a11c9dd7942e31acca6df56288d194Jeff Davidson    /**
22914f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson     * Request scoring for networks.
23014f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson     *
23114f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson     * <p>Note that this is just a helper method to assemble the broadcast, and will run in the
23214f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson     * calling process.
23314f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson     *
23414f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson     * @return true if the broadcast was sent, or false if there is no active scorer.
23514f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson     * @throws SecurityException if the caller does not hold the
236161977998feebf0a855ea56558464470877040cfJeff Davidson     *         {@link android.Manifest.permission#BROADCAST_NETWORK_PRIVILEGED} permission.
23714f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson     * @hide
23814f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson     */
23914f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson    public boolean requestScores(NetworkKey[] networks) throws SecurityException {
24014f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson        String activeScorer = getActiveScorerPackage();
24114f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson        if (activeScorer == null) {
24214f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson            return false;
24314f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson        }
24414f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson        Intent intent = new Intent(ACTION_SCORE_NETWORKS);
24514f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson        intent.setPackage(activeScorer);
246505c4a3093e57b91a0333ff09072395f534a06d9Jeff Davidson        intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
24714f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson        intent.putExtra(EXTRA_NETWORKS_TO_SCORE, networks);
248ac7285dc1e13f30d59dad30fe2ad1116e5f676cbJeff Davidson        // A scorer should never become active if its package doesn't hold SCORE_NETWORKS, but
249ac7285dc1e13f30d59dad30fe2ad1116e5f676cbJeff Davidson        // ensure the package still holds it to be extra safe.
250e4de5a0d3b6e0c897c1cea0912b58e11db962365Xiaohui Chen        // TODO: http://b/23422763
251e4de5a0d3b6e0c897c1cea0912b58e11db962365Xiaohui Chen        mContext.sendBroadcastAsUser(intent, UserHandle.SYSTEM, Manifest.permission.SCORE_NETWORKS);
25214f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson        return true;
25314f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson    }
25414f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson
25514f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson    /**
25614f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson     * Register a network score cache.
25714f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson     *
25814f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson     * @param networkType the type of network this cache can handle. See {@link NetworkKey#type}.
25914f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson     * @param scoreCache implementation of {@link INetworkScoreCache} to store the scores.
26014f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson     * @throws SecurityException if the caller does not hold the
261161977998feebf0a855ea56558464470877040cfJeff Davidson     *         {@link android.Manifest.permission#BROADCAST_NETWORK_PRIVILEGED} permission.
26214f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson     * @throws IllegalArgumentException if a score cache is already registered for this type.
26314f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson     * @hide
26414f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson     */
26514f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson    public void registerNetworkScoreCache(int networkType, INetworkScoreCache scoreCache) {
26614f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson        try {
26714f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson            mService.registerNetworkScoreCache(networkType, scoreCache);
26814f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson        } catch (RemoteException e) {
269c53962d4ede82a03b62f0c8bb86bd0da090a15ebJeff Sharkey            throw e.rethrowFromSystemServer();
27014f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson        }
27114f1ec05b2add5ee051c0d2e7c7c3b36a6e77b92Jeff Davidson    }
272b51e0a6c4b249e0d6f404fafef007645221be0d3Jeff Davidson}
273