Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 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 | |
Jeremy Joslin | a9f933e | 2017-04-28 13:08:17 -0700 | [diff] [blame] | 19 | import android.Manifest.permission; |
Jeremy Joslin | c5ac587 | 2016-11-30 15:05:40 -0800 | [diff] [blame] | 20 | import android.annotation.IntDef; |
Jeremy Joslin | a480795 | 2017-01-18 14:21:56 -0800 | [diff] [blame] | 21 | import android.annotation.Nullable; |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 22 | import android.annotation.RequiresPermission; |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 23 | import android.annotation.SdkConstant; |
| 24 | import android.annotation.SdkConstant.SdkConstantType; |
Jeff Davidson | 7be8e97 | 2014-07-16 17:24:46 -0700 | [diff] [blame] | 25 | import android.annotation.SystemApi; |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 26 | import android.annotation.SystemService; |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 27 | import android.content.Context; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 28 | import android.os.RemoteException; |
| 29 | import android.os.ServiceManager; |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 30 | import android.os.ServiceManager.ServiceNotFoundException; |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 31 | |
Jeremy Joslin | c5ac587 | 2016-11-30 15:05:40 -0800 | [diff] [blame] | 32 | import java.lang.annotation.Retention; |
| 33 | import java.lang.annotation.RetentionPolicy; |
Jeremy Joslin | f95c865 | 2017-02-09 15:32:04 -0800 | [diff] [blame] | 34 | import java.util.List; |
Jeremy Joslin | c5ac587 | 2016-11-30 15:05:40 -0800 | [diff] [blame] | 35 | |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Class that manages communication between network subsystems and a network scorer. |
| 38 | * |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 39 | * <p>A network scorer is any application which: |
| 40 | * <ul> |
Jeremy Joslin | 59502eb | 2017-07-14 15:00:53 -0700 | [diff] [blame] | 41 | * <li>Is granted the {@link permission#SCORE_NETWORKS} permission. |
| 42 | * <li>Is granted the {@link permission#ACCESS_COARSE_LOCATION} permission. |
Jeremy Joslin | 5519d7c | 2017-01-06 14:36:54 -0800 | [diff] [blame] | 43 | * <li>Include a Service for the {@link #ACTION_RECOMMEND_NETWORKS} action |
Jeremy Joslin | a9f933e | 2017-04-28 13:08:17 -0700 | [diff] [blame] | 44 | * protected by the {@link permission#BIND_NETWORK_RECOMMENDATION_SERVICE} |
Jeremy Joslin | 5519d7c | 2017-01-06 14:36:54 -0800 | [diff] [blame] | 45 | * permission. |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 46 | * </ul> |
| 47 | * |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 48 | * @hide |
| 49 | */ |
Jeff Davidson | 7be8e97 | 2014-07-16 17:24:46 -0700 | [diff] [blame] | 50 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 51 | @SystemService(Context.NETWORK_SCORE_SERVICE) |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 52 | public class NetworkScoreManager { |
| 53 | /** |
Jeff Davidson | ee2a121 | 2014-04-17 12:19:23 -0700 | [diff] [blame] | 54 | * Activity action: ask the user to change the active network scorer. This will show a dialog |
| 55 | * that asks the user whether they want to replace the current active scorer with the one |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 56 | * specified in {@link #EXTRA_PACKAGE_NAME}. The activity will finish with RESULT_OK if the |
Jeff Davidson | ee2a121 | 2014-04-17 12:19:23 -0700 | [diff] [blame] | 57 | * active scorer was changed or RESULT_CANCELED if it failed for any reason. |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 58 | */ |
| 59 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
Jeff Davidson | ee2a121 | 2014-04-17 12:19:23 -0700 | [diff] [blame] | 60 | public static final String ACTION_CHANGE_ACTIVE = "android.net.scoring.CHANGE_ACTIVE"; |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 61 | |
| 62 | /** |
Jeff Davidson | ee2a121 | 2014-04-17 12:19:23 -0700 | [diff] [blame] | 63 | * Extra used with {@link #ACTION_CHANGE_ACTIVE} to specify the new scorer package. Set with |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 64 | * {@link android.content.Intent#putExtra(String, String)}. |
| 65 | */ |
| 66 | public static final String EXTRA_PACKAGE_NAME = "packageName"; |
| 67 | |
| 68 | /** |
| 69 | * Broadcast action: new network scores are being requested. This intent will only be delivered |
Jeff Davidson | ee2a121 | 2014-04-17 12:19:23 -0700 | [diff] [blame] | 70 | * to the current active scorer app. That app is responsible for scoring the networks and |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 71 | * calling {@link #updateScores} when complete. The networks to score are specified in |
| 72 | * {@link #EXTRA_NETWORKS_TO_SCORE}, and will generally consist of all networks which have been |
| 73 | * configured by the user as well as any open networks. |
| 74 | * |
| 75 | * <p class="note">This is a protected intent that can only be sent by the system. |
| 76 | */ |
| 77 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 78 | public static final String ACTION_SCORE_NETWORKS = "android.net.scoring.SCORE_NETWORKS"; |
| 79 | |
| 80 | /** |
Jeff Davidson | b096bdc | 2014-07-01 12:29:11 -0700 | [diff] [blame] | 81 | * Extra used with {@link #ACTION_SCORE_NETWORKS} to specify the networks to be scored, as an |
| 82 | * array of {@link NetworkKey}s. Can be obtained with |
| 83 | * {@link android.content.Intent#getParcelableArrayExtra(String)}}. |
| 84 | */ |
| 85 | public static final String EXTRA_NETWORKS_TO_SCORE = "networksToScore"; |
| 86 | |
| 87 | /** |
Amin Shaikh | bc9a8e6 | 2017-02-02 15:39:12 -0800 | [diff] [blame] | 88 | * Activity action: launch an activity for configuring a provider for the feature that connects |
| 89 | * and secures open wifi networks available before enabling it. Applications that enable this |
| 90 | * feature must provide an activity for this action. The framework will launch this activity |
| 91 | * which must return RESULT_OK if the feature should be enabled. |
Jeff Davidson | b6646a8 | 2014-06-27 16:24:42 -0700 | [diff] [blame] | 92 | */ |
| 93 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
| 94 | public static final String ACTION_CUSTOM_ENABLE = "android.net.scoring.CUSTOM_ENABLE"; |
| 95 | |
| 96 | /** |
Stephen Chen | 8b1339a | 2017-02-28 18:11:34 -0800 | [diff] [blame] | 97 | * Meta-data specified on a {@link NetworkRecommendationProvider} that provides a user-visible |
| 98 | * label of the recommendation service. |
| 99 | * @hide |
| 100 | */ |
| 101 | public static final String RECOMMENDATION_SERVICE_LABEL_META_DATA = |
| 102 | "android.net.scoring.recommendation_service_label"; |
| 103 | |
| 104 | /** |
Amin Shaikh | bc9a8e6 | 2017-02-02 15:39:12 -0800 | [diff] [blame] | 105 | * Meta-data specified on a {@link NetworkRecommendationProvider} that specified the package |
| 106 | * name of the application that connects and secures open wifi networks automatically. The |
| 107 | * specified package must provide an Activity for {@link #ACTION_CUSTOM_ENABLE}. |
| 108 | * @hide |
| 109 | */ |
| 110 | public static final String USE_OPEN_WIFI_PACKAGE_META_DATA = |
| 111 | "android.net.wifi.use_open_wifi_package"; |
| 112 | |
| 113 | /** |
Amin Shaikh | d601360 | 2017-03-24 15:52:32 -0700 | [diff] [blame] | 114 | * Meta-data specified on a {@link NetworkRecommendationProvider} that specifies the |
| 115 | * {@link android.app.NotificationChannel} ID used to post open network notifications. |
| 116 | * @hide |
| 117 | */ |
| 118 | public static final String NETWORK_AVAILABLE_NOTIFICATION_CHANNEL_ID_META_DATA = |
| 119 | "android.net.wifi.notification_channel_id_network_available"; |
| 120 | |
| 121 | /** |
Jeff Davidson | b096bdc | 2014-07-01 12:29:11 -0700 | [diff] [blame] | 122 | * Broadcast action: the active scorer has been changed. Scorer apps may listen to this to |
| 123 | * perform initialization once selected as the active scorer, or clean up unneeded resources |
Jeremy Joslin | da11f5c | 2016-02-10 07:31:33 -0800 | [diff] [blame] | 124 | * if another scorer has been selected. This is an explicit broadcast only sent to the |
| 125 | * previous scorer and new scorer. Note that it is unnecessary to clear existing scores as |
Jeff Davidson | b096bdc | 2014-07-01 12:29:11 -0700 | [diff] [blame] | 126 | * this is handled by the system. |
| 127 | * |
| 128 | * <p>The new scorer will be specified in {@link #EXTRA_NEW_SCORER}. |
| 129 | * |
| 130 | * <p class="note">This is a protected intent that can only be sent by the system. |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 131 | */ |
Jeff Davidson | b096bdc | 2014-07-01 12:29:11 -0700 | [diff] [blame] | 132 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 133 | public static final String ACTION_SCORER_CHANGED = "android.net.scoring.SCORER_CHANGED"; |
| 134 | |
| 135 | /** |
Jeremy Joslin | 1397837 | 2016-11-28 17:47:35 -0800 | [diff] [blame] | 136 | * Service action: Used to discover and bind to a network recommendation provider. |
| 137 | * Implementations should return {@link NetworkRecommendationProvider#getBinder()} from |
| 138 | * their <code>onBind()</code> method. |
| 139 | */ |
| 140 | @SdkConstant(SdkConstantType.SERVICE_ACTION) |
| 141 | public static final String ACTION_RECOMMEND_NETWORKS = "android.net.action.RECOMMEND_NETWORKS"; |
| 142 | |
| 143 | /** |
Jeff Davidson | b096bdc | 2014-07-01 12:29:11 -0700 | [diff] [blame] | 144 | * Extra used with {@link #ACTION_SCORER_CHANGED} to specify the newly selected scorer's package |
| 145 | * name. Will be null if scoring was disabled. Can be obtained with |
| 146 | * {@link android.content.Intent#getStringExtra(String)}. |
| 147 | */ |
| 148 | public static final String EXTRA_NEW_SCORER = "newScorer"; |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 149 | |
Jeremy Joslin | c5ac587 | 2016-11-30 15:05:40 -0800 | [diff] [blame] | 150 | /** @hide */ |
| 151 | @IntDef({CACHE_FILTER_NONE, CACHE_FILTER_CURRENT_NETWORK, CACHE_FILTER_SCAN_RESULTS}) |
| 152 | @Retention(RetentionPolicy.SOURCE) |
| 153 | public @interface CacheUpdateFilter {} |
| 154 | |
| 155 | /** |
| 156 | * Do not filter updates sent to the cache. |
| 157 | * @hide |
| 158 | */ |
| 159 | public static final int CACHE_FILTER_NONE = 0; |
| 160 | |
| 161 | /** |
| 162 | * Only send cache updates when the network matches the connected network. |
| 163 | * @hide |
| 164 | */ |
| 165 | public static final int CACHE_FILTER_CURRENT_NETWORK = 1; |
| 166 | |
| 167 | /** |
| 168 | * Only send cache updates when the network is part of the current scan result set. |
| 169 | * @hide |
| 170 | */ |
| 171 | public static final int CACHE_FILTER_SCAN_RESULTS = 2; |
| 172 | |
Jeremy Joslin | 9925c6a | 2017-03-06 10:39:35 -0800 | [diff] [blame] | 173 | /** @hide */ |
| 174 | @IntDef({RECOMMENDATIONS_ENABLED_FORCED_OFF, RECOMMENDATIONS_ENABLED_OFF, |
| 175 | RECOMMENDATIONS_ENABLED_ON}) |
| 176 | @Retention(RetentionPolicy.SOURCE) |
| 177 | public @interface RecommendationsEnabledSetting {} |
| 178 | |
| 179 | /** |
| 180 | * Recommendations have been forced off. |
| 181 | * <p> |
| 182 | * This value is never set by any of the NetworkScore classes, it must be set via other means. |
| 183 | * This state is also "sticky" and we won't transition out of this state once entered. To move |
| 184 | * to a different state this value has to be explicitly set to a different value via |
| 185 | * other means. |
| 186 | * @hide |
| 187 | */ |
| 188 | public static final int RECOMMENDATIONS_ENABLED_FORCED_OFF = -1; |
| 189 | |
| 190 | /** |
| 191 | * Recommendations are not enabled. |
| 192 | * <p> |
| 193 | * This is a transient state that can be entered when the default recommendation app is enabled |
| 194 | * but no longer valid. This state will transition to RECOMMENDATIONS_ENABLED_ON when a valid |
| 195 | * recommendation app is enabled. |
| 196 | * @hide |
| 197 | */ |
| 198 | public static final int RECOMMENDATIONS_ENABLED_OFF = 0; |
| 199 | |
| 200 | /** |
| 201 | * Recommendations are enabled. |
| 202 | * <p> |
| 203 | * This is a transient state that means a valid recommendation app is active. This state will |
| 204 | * transition to RECOMMENDATIONS_ENABLED_OFF if the current and default recommendation apps |
| 205 | * become invalid. |
| 206 | * @hide |
| 207 | */ |
| 208 | public static final int RECOMMENDATIONS_ENABLED_ON = 1; |
| 209 | |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 210 | private final Context mContext; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 211 | private final INetworkScoreService mService; |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 212 | |
| 213 | /** @hide */ |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 214 | public NetworkScoreManager(Context context) throws ServiceNotFoundException { |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 215 | mContext = context; |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 216 | mService = INetworkScoreService.Stub |
| 217 | .asInterface(ServiceManager.getServiceOrThrow(Context.NETWORK_SCORE_SERVICE)); |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /** |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 221 | * Obtain the package name of the current active network scorer. |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 222 | * |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 223 | * <p>At any time, only one scorer application will receive {@link #ACTION_SCORE_NETWORKS} |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 224 | * broadcasts and be allowed to call {@link #updateScores}. Applications may use this method to |
| 225 | * determine the current scorer and offer the user the ability to select a different scorer via |
Jeff Davidson | ee2a121 | 2014-04-17 12:19:23 -0700 | [diff] [blame] | 226 | * the {@link #ACTION_CHANGE_ACTIVE} intent. |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 227 | * @return the full package name of the current active scorer, or null if there is no active |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 228 | * scorer. |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 229 | */ |
Jeremy Joslin | 2d55b18 | 2017-07-11 15:54:14 -0700 | [diff] [blame] | 230 | @RequiresPermission(anyOf = {android.Manifest.permission.SCORE_NETWORKS, |
| 231 | android.Manifest.permission.REQUEST_NETWORK_SCORES}) |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 232 | public String getActiveScorerPackage() { |
Jeremy Joslin | 6c1ca28 | 2017-01-10 13:08:32 -0800 | [diff] [blame] | 233 | try { |
| 234 | return mService.getActiveScorerPackage(); |
| 235 | } catch (RemoteException e) { |
| 236 | throw e.rethrowFromSystemServer(); |
Jeff Davidson | c741553 | 2014-06-23 18:15:34 -0700 | [diff] [blame] | 237 | } |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /** |
Jeremy Joslin | a5172f6 | 2017-02-02 14:27:05 -0800 | [diff] [blame] | 241 | * Returns metadata about the active scorer or <code>null</code> if there is no active scorer. |
| 242 | * |
| 243 | * @hide |
| 244 | */ |
| 245 | @Nullable |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 246 | @RequiresPermission(android.Manifest.permission.REQUEST_NETWORK_SCORES) |
Jeremy Joslin | a5172f6 | 2017-02-02 14:27:05 -0800 | [diff] [blame] | 247 | public NetworkScorerAppData getActiveScorer() { |
| 248 | try { |
| 249 | return mService.getActiveScorer(); |
| 250 | } catch (RemoteException e) { |
| 251 | throw e.rethrowFromSystemServer(); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /** |
Jeremy Joslin | f95c865 | 2017-02-09 15:32:04 -0800 | [diff] [blame] | 256 | * Returns the list of available scorer apps. The list will be empty if there are |
| 257 | * no valid scorers. |
| 258 | * |
| 259 | * @hide |
| 260 | */ |
| 261 | public List<NetworkScorerAppData> getAllValidScorers() { |
| 262 | try { |
| 263 | return mService.getAllValidScorers(); |
| 264 | } catch (RemoteException e) { |
| 265 | throw e.rethrowFromSystemServer(); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /** |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 270 | * Update network scores. |
| 271 | * |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 272 | * <p>This may be called at any time to re-score active networks. Scores will generally be |
| 273 | * updated quickly, but if this method is called too frequently, the scores may be held and |
| 274 | * applied at a later time. |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 275 | * |
| 276 | * @param networks the networks which have been scored by the scorer. |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 277 | * @return whether the update was successful. |
| 278 | * @throws SecurityException if the caller is not the active scorer. |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 279 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 280 | @RequiresPermission(android.Manifest.permission.SCORE_NETWORKS) |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 281 | public boolean updateScores(ScoredNetwork[] networks) throws SecurityException { |
| 282 | try { |
| 283 | return mService.updateScores(networks); |
| 284 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 285 | throw e.rethrowFromSystemServer(); |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Clear network scores. |
| 291 | * |
| 292 | * <p>Should be called when all scores need to be invalidated, i.e. because the scoring |
| 293 | * algorithm has changed and old scores can no longer be compared to future scores. |
| 294 | * |
| 295 | * <p>Note that scores will be cleared automatically when the active scorer changes, as scores |
| 296 | * from one scorer cannot be compared to those from another scorer. |
| 297 | * |
| 298 | * @return whether the clear was successful. |
| 299 | * @throws SecurityException if the caller is not the active scorer or privileged. |
| 300 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 301 | @RequiresPermission(android.Manifest.permission.REQUEST_NETWORK_SCORES) |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 302 | public boolean clearScores() throws SecurityException { |
| 303 | try { |
| 304 | return mService.clearScores(); |
| 305 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 306 | throw e.rethrowFromSystemServer(); |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Set the active scorer to a new package and clear existing scores. |
| 312 | * |
Jeff Davidson | e56f2bb | 2014-11-05 11:14:19 -0800 | [diff] [blame] | 313 | * <p>Should never be called directly without obtaining user consent. This can be done by using |
| 314 | * the {@link #ACTION_CHANGE_ACTIVE} broadcast, or using a custom configuration activity. |
| 315 | * |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 316 | * @return true if the operation succeeded, or false if the new package is not a valid scorer. |
Jeremy Joslin | e9052a3 | 2017-02-27 15:47:54 -0800 | [diff] [blame] | 317 | * @throws SecurityException if the caller is not a system process or does not hold the |
Jeremy Joslin | a9f933e | 2017-04-28 13:08:17 -0700 | [diff] [blame] | 318 | * {@link permission#SCORE_NETWORKS} permission |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 319 | * @hide |
| 320 | */ |
Jeff Davidson | e56f2bb | 2014-11-05 11:14:19 -0800 | [diff] [blame] | 321 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 322 | @RequiresPermission(android.Manifest.permission.SCORE_NETWORKS) |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 323 | public boolean setActiveScorer(String packageName) throws SecurityException { |
| 324 | try { |
| 325 | return mService.setActiveScorer(packageName); |
| 326 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 327 | throw e.rethrowFromSystemServer(); |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 328 | } |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 329 | } |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 330 | |
| 331 | /** |
Jeff Davidson | 26fd143 | 2014-07-29 09:39:52 -0700 | [diff] [blame] | 332 | * Turn off network scoring. |
| 333 | * |
| 334 | * <p>May only be called by the current scorer app, or the system. |
| 335 | * |
| 336 | * @throws SecurityException if the caller is neither the active scorer nor the system. |
| 337 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 338 | @RequiresPermission(android.Manifest.permission.REQUEST_NETWORK_SCORES) |
Jeff Davidson | 26fd143 | 2014-07-29 09:39:52 -0700 | [diff] [blame] | 339 | public void disableScoring() throws SecurityException { |
| 340 | try { |
| 341 | mService.disableScoring(); |
| 342 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 343 | throw e.rethrowFromSystemServer(); |
Jeff Davidson | 26fd143 | 2014-07-29 09:39:52 -0700 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
| 347 | /** |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 348 | * Request scoring for networks. |
| 349 | * |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 350 | * @return true if the broadcast was sent, or false if there is no active scorer. |
| 351 | * @throws SecurityException if the caller does not hold the |
Jeremy Joslin | a9f933e | 2017-04-28 13:08:17 -0700 | [diff] [blame] | 352 | * {@link permission#REQUEST_NETWORK_SCORES} permission. |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 353 | * @hide |
| 354 | */ |
| 355 | public boolean requestScores(NetworkKey[] networks) throws SecurityException { |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 356 | try { |
| 357 | return mService.requestScores(networks); |
| 358 | } catch (RemoteException e) { |
| 359 | throw e.rethrowFromSystemServer(); |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 360 | } |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Register a network score cache. |
| 365 | * |
| 366 | * @param networkType the type of network this cache can handle. See {@link NetworkKey#type}. |
| 367 | * @param scoreCache implementation of {@link INetworkScoreCache} to store the scores. |
| 368 | * @throws SecurityException if the caller does not hold the |
Jeremy Joslin | a9f933e | 2017-04-28 13:08:17 -0700 | [diff] [blame] | 369 | * {@link permission#REQUEST_NETWORK_SCORES} permission. |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 370 | * @throws IllegalArgumentException if a score cache is already registered for this type. |
Jeremy Joslin | c5ac587 | 2016-11-30 15:05:40 -0800 | [diff] [blame] | 371 | * @deprecated equivalent to registering for cache updates with CACHE_FILTER_NONE. |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 372 | * @hide |
| 373 | */ |
Jeremy Joslin | c5ac587 | 2016-11-30 15:05:40 -0800 | [diff] [blame] | 374 | @Deprecated // migrate to registerNetworkScoreCache(int, INetworkScoreCache, int) |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 375 | public void registerNetworkScoreCache(int networkType, INetworkScoreCache scoreCache) { |
Jeremy Joslin | c5ac587 | 2016-11-30 15:05:40 -0800 | [diff] [blame] | 376 | registerNetworkScoreCache(networkType, scoreCache, CACHE_FILTER_NONE); |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Register a network score cache. |
| 381 | * |
| 382 | * @param networkType the type of network this cache can handle. See {@link NetworkKey#type} |
| 383 | * @param scoreCache implementation of {@link INetworkScoreCache} to store the scores |
| 384 | * @param filterType the {@link CacheUpdateFilter} to apply |
| 385 | * @throws SecurityException if the caller does not hold the |
Jeremy Joslin | a9f933e | 2017-04-28 13:08:17 -0700 | [diff] [blame] | 386 | * {@link permission#REQUEST_NETWORK_SCORES} permission. |
Jeremy Joslin | c5ac587 | 2016-11-30 15:05:40 -0800 | [diff] [blame] | 387 | * @throws IllegalArgumentException if a score cache is already registered for this type. |
| 388 | * @hide |
| 389 | */ |
| 390 | public void registerNetworkScoreCache(int networkType, INetworkScoreCache scoreCache, |
| 391 | @CacheUpdateFilter int filterType) { |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 392 | try { |
Jeremy Joslin | c5ac587 | 2016-11-30 15:05:40 -0800 | [diff] [blame] | 393 | mService.registerNetworkScoreCache(networkType, scoreCache, filterType); |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 394 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 395 | throw e.rethrowFromSystemServer(); |
Jeff Davidson | 14f1ec0 | 2014-04-29 11:58:26 -0700 | [diff] [blame] | 396 | } |
| 397 | } |
Jeremy Joslin | 1397837 | 2016-11-28 17:47:35 -0800 | [diff] [blame] | 398 | |
| 399 | /** |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 400 | * Unregister a network score cache. |
| 401 | * |
| 402 | * @param networkType the type of network this cache can handle. See {@link NetworkKey#type}. |
| 403 | * @param scoreCache implementation of {@link INetworkScoreCache} to store the scores. |
| 404 | * @throws SecurityException if the caller does not hold the |
Jeremy Joslin | a9f933e | 2017-04-28 13:08:17 -0700 | [diff] [blame] | 405 | * {@link permission#REQUEST_NETWORK_SCORES} permission. |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 406 | * @throws IllegalArgumentException if a score cache is already registered for this type. |
| 407 | * @hide |
| 408 | */ |
| 409 | public void unregisterNetworkScoreCache(int networkType, INetworkScoreCache scoreCache) { |
| 410 | try { |
| 411 | mService.unregisterNetworkScoreCache(networkType, scoreCache); |
| 412 | } catch (RemoteException e) { |
| 413 | throw e.rethrowFromSystemServer(); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | /** |
Jeremy Joslin | 134c9d3 | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 418 | * Determine whether the application with the given UID is the enabled scorer. |
| 419 | * |
| 420 | * @param callingUid the UID to check |
| 421 | * @return true if the provided UID is the active scorer, false otherwise. |
| 422 | * @hide |
| 423 | */ |
| 424 | public boolean isCallerActiveScorer(int callingUid) { |
| 425 | try { |
| 426 | return mService.isCallerActiveScorer(callingUid); |
| 427 | } catch (RemoteException e) { |
| 428 | throw e.rethrowFromSystemServer(); |
| 429 | } |
| 430 | } |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 431 | } |