blob: 3bcb36fd0e00d90dde7e04b8a39d1359d1370cd1 [file] [log] [blame]
Jeff Davidsondd6fd1e2014-04-14 15:14:30 -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
Jeremy Joslinf621bc92017-02-16 11:11:57 -080017package com.android.server;
Jeff Davidsondd6fd1e2014-04-14 15:14:30 -070018
19import android.Manifest.permission;
Jeff Davidsonb6646a82014-06-27 16:24:42 -070020import android.annotation.Nullable;
Jeremy Joslin37e877b2017-02-02 11:06:14 -080021import android.content.ComponentName;
Jeff Davidsondd6fd1e2014-04-14 15:14:30 -070022import android.content.Context;
23import android.content.Intent;
Philip P. Moltmann04acad92019-10-03 12:22:52 -070024import android.content.PermissionChecker;
Jeff Davidsondd6fd1e2014-04-14 15:14:30 -070025import android.content.pm.PackageManager;
26import android.content.pm.ResolveInfo;
Amin Shaikhbc9a8e62017-02-02 15:39:12 -080027import android.content.pm.ServiceInfo;
Jeremy Joslinf621bc92017-02-16 11:11:57 -080028import android.net.NetworkScoreManager;
29import android.net.NetworkScorerAppData;
Jeff Davidsondd6fd1e2014-04-14 15:14:30 -070030import android.provider.Settings;
Jeff Davidsondd6fd1e2014-04-14 15:14:30 -070031import android.text.TextUtils;
Jeff Davidson6a4b2202014-04-16 17:29:40 -070032import android.util.Log;
Jeremy Joslin134c9d32017-01-09 16:22:20 -080033
Jeremy Joslinb8418ac82016-12-06 07:42:38 -080034import com.android.internal.R;
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -080035import com.android.internal.annotations.VisibleForTesting;
Jeremy Joslin134c9d32017-01-09 16:22:20 -080036
Jeff Davidsondd6fd1e2014-04-14 15:14:30 -070037import java.util.ArrayList;
Jeremy Joslin5b294b42015-12-17 17:38:04 -080038import java.util.Collections;
Jeff Davidsondd6fd1e2014-04-14 15:14:30 -070039import java.util.List;
40
41/**
Jeremy Joslinb8418ac82016-12-06 07:42:38 -080042 * Internal class for discovering and managing the network scorer/recommendation application.
Jeff Davidsonc7415532014-06-23 18:15:34 -070043 *
Jeff Davidsondd6fd1e2014-04-14 15:14:30 -070044 * @hide
45 */
Jeremy Joslinf1acc1e2017-04-04 17:28:23 -070046@VisibleForTesting
Amin Shaikhaa09aa02016-11-21 17:27:53 -080047public class NetworkScorerAppManager {
Jeff Davidson6a4b2202014-04-16 17:29:40 -070048 private static final String TAG = "NetworkScorerAppManager";
Jeremy Joslinb8418ac82016-12-06 07:42:38 -080049 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
50 private static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE);
Amin Shaikhaa09aa02016-11-21 17:27:53 -080051 private final Context mContext;
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -080052 private final SettingsFacade mSettingsFacade;
Amin Shaikhaa09aa02016-11-21 17:27:53 -080053
54 public NetworkScorerAppManager(Context context) {
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -080055 this(context, new SettingsFacade());
56 }
57
58 @VisibleForTesting
59 public NetworkScorerAppManager(Context context, SettingsFacade settingsFacade) {
60 mContext = context;
61 mSettingsFacade = settingsFacade;
Amin Shaikhaa09aa02016-11-21 17:27:53 -080062 }
Jeff Davidsondd6fd1e2014-04-14 15:14:30 -070063
Jeremy Joslinb8418ac82016-12-06 07:42:38 -080064 /**
Jeremy Joslinf95c8652017-02-09 15:32:04 -080065 * Returns the list of available scorer apps. The list will be empty if there are
66 * no valid scorers.
67 */
Jeremy Joslinf1acc1e2017-04-04 17:28:23 -070068 @VisibleForTesting
69 public List<NetworkScorerAppData> getAllValidScorers() {
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -080070 if (VERBOSE) Log.v(TAG, "getAllValidScorers()");
71 final PackageManager pm = mContext.getPackageManager();
72 final Intent serviceIntent = new Intent(NetworkScoreManager.ACTION_RECOMMEND_NETWORKS);
73 final List<ResolveInfo> resolveInfos =
74 pm.queryIntentServices(serviceIntent, PackageManager.GET_META_DATA);
75 if (resolveInfos == null || resolveInfos.isEmpty()) {
76 if (DEBUG) Log.d(TAG, "Found 0 Services able to handle " + serviceIntent);
77 return Collections.emptyList();
Jeremy Joslinb8418ac82016-12-06 07:42:38 -080078 }
79
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -080080 List<NetworkScorerAppData> appDataList = new ArrayList<>();
81 for (int i = 0; i < resolveInfos.size(); i++) {
82 final ServiceInfo serviceInfo = resolveInfos.get(i).serviceInfo;
Jeremy Joslind816abe2017-07-14 15:00:53 -070083 if (hasPermissions(serviceInfo.applicationInfo.uid, serviceInfo.packageName)) {
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -080084 if (VERBOSE) {
85 Log.v(TAG, serviceInfo.packageName + " is a valid scorer/recommender.");
86 }
Amin Shaikhbc9a8e62017-02-02 15:39:12 -080087 final ComponentName serviceComponentName =
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -080088 new ComponentName(serviceInfo.packageName, serviceInfo.name);
Stephen Chen8b1339a2017-02-28 18:11:34 -080089 final String serviceLabel = getRecommendationServiceLabel(serviceInfo, pm);
Amin Shaikhbc9a8e62017-02-02 15:39:12 -080090 final ComponentName useOpenWifiNetworksActivity =
91 findUseOpenWifiNetworksActivity(serviceInfo);
Amin Shaikhd6013602017-03-24 15:52:32 -070092 final String networkAvailableNotificationChannelId =
93 getNetworkAvailableNotificationChannelId(serviceInfo);
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -080094 appDataList.add(
95 new NetworkScorerAppData(serviceInfo.applicationInfo.uid,
Amin Shaikhd6013602017-03-24 15:52:32 -070096 serviceComponentName, serviceLabel, useOpenWifiNetworksActivity,
97 networkAvailableNotificationChannelId));
Jeremy Joslinb8418ac82016-12-06 07:42:38 -080098 } else {
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -080099 if (VERBOSE) Log.v(TAG, serviceInfo.packageName
100 + " is NOT a valid scorer/recommender.");
Jeremy Joslinb8418ac82016-12-06 07:42:38 -0800101 }
102 }
103
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800104 return appDataList;
Jeremy Joslinb8418ac82016-12-06 07:42:38 -0800105 }
106
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800107 @Nullable
Stephen Chen8b1339a2017-02-28 18:11:34 -0800108 private String getRecommendationServiceLabel(ServiceInfo serviceInfo, PackageManager pm) {
109 if (serviceInfo.metaData != null) {
110 final String label = serviceInfo.metaData
111 .getString(NetworkScoreManager.RECOMMENDATION_SERVICE_LABEL_META_DATA);
112 if (!TextUtils.isEmpty(label)) {
113 return label;
114 }
115 }
116 CharSequence label = serviceInfo.loadLabel(pm);
117 return label == null ? null : label.toString();
118 }
119
120 @Nullable
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800121 private ComponentName findUseOpenWifiNetworksActivity(ServiceInfo serviceInfo) {
Amin Shaikhbc9a8e62017-02-02 15:39:12 -0800122 if (serviceInfo.metaData == null) {
123 if (DEBUG) {
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800124 Log.d(TAG, "No metadata found on " + serviceInfo.getComponentName());
Amin Shaikhbc9a8e62017-02-02 15:39:12 -0800125 }
126 return null;
127 }
128 final String useOpenWifiPackage = serviceInfo.metaData
129 .getString(NetworkScoreManager.USE_OPEN_WIFI_PACKAGE_META_DATA);
130 if (TextUtils.isEmpty(useOpenWifiPackage)) {
131 if (DEBUG) {
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800132 Log.d(TAG, "No use_open_wifi_package metadata found on "
133 + serviceInfo.getComponentName());
Amin Shaikhbc9a8e62017-02-02 15:39:12 -0800134 }
135 return null;
136 }
137 final Intent enableUseOpenWifiIntent = new Intent(NetworkScoreManager.ACTION_CUSTOM_ENABLE)
138 .setPackage(useOpenWifiPackage);
139 final ResolveInfo resolveActivityInfo = mContext.getPackageManager()
140 .resolveActivity(enableUseOpenWifiIntent, 0 /* flags */);
141 if (VERBOSE) {
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800142 Log.d(TAG, "Resolved " + enableUseOpenWifiIntent + " to " + resolveActivityInfo);
Amin Shaikhbc9a8e62017-02-02 15:39:12 -0800143 }
144
145 if (resolveActivityInfo != null && resolveActivityInfo.activityInfo != null) {
146 return resolveActivityInfo.activityInfo.getComponentName();
147 }
148
149 return null;
150 }
151
Amin Shaikhd6013602017-03-24 15:52:32 -0700152 @Nullable
153 private static String getNetworkAvailableNotificationChannelId(ServiceInfo serviceInfo) {
154 if (serviceInfo.metaData == null) {
155 if (DEBUG) {
156 Log.d(TAG, "No metadata found on " + serviceInfo.getComponentName());
157 }
158 return null;
159 }
160
161 return serviceInfo.metaData.getString(
162 NetworkScoreManager.NETWORK_AVAILABLE_NOTIFICATION_CHANNEL_ID_META_DATA);
163 }
164
165
Jeremy Joslinb8418ac82016-12-06 07:42:38 -0800166 /**
Jeff Davidsonc7415532014-06-23 18:15:34 -0700167 * Get the application to use for scoring networks.
Jeff Davidsondd6fd1e2014-04-14 15:14:30 -0700168 *
Jeff Davidsonc7415532014-06-23 18:15:34 -0700169 * @return the scorer app info or null if scoring is disabled (including if no scorer was ever
Jeff Davidsondd6fd1e2014-04-14 15:14:30 -0700170 * selected) or if the previously-set scorer is no longer a valid scorer app (e.g. because
171 * it was disabled or uninstalled).
172 */
Jeremy Joslinb8418ac82016-12-06 07:42:38 -0800173 @Nullable
Jeremy Joslinf1acc1e2017-04-04 17:28:23 -0700174 @VisibleForTesting
175 public NetworkScorerAppData getActiveScorer() {
Jeremy Joslin9925c6a2017-03-06 10:39:35 -0800176 final int enabledSetting = getNetworkRecommendationsEnabledSetting();
177 if (enabledSetting == NetworkScoreManager.RECOMMENDATIONS_ENABLED_FORCED_OFF) {
178 return null;
179 }
180
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800181 return getScorer(getNetworkRecommendationsPackage());
182 }
183
184 private NetworkScorerAppData getScorer(String packageName) {
185 if (TextUtils.isEmpty(packageName)) {
Jeremy Joslinb8418ac82016-12-06 07:42:38 -0800186 return null;
187 }
188
189 // Otherwise return the recommendation provider (which may be null).
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800190 List<NetworkScorerAppData> apps = getAllValidScorers();
191 for (int i = 0; i < apps.size(); i++) {
192 NetworkScorerAppData app = apps.get(i);
193 if (app.getRecommendationServicePackageName().equals(packageName)) {
194 return app;
195 }
196 }
197
198 return null;
199 }
200
Jeremy Joslind816abe2017-07-14 15:00:53 -0700201 private boolean hasPermissions(final int uid, final String packageName) {
202 return hasScoreNetworksPermission(packageName)
203 && canAccessLocation(uid, packageName);
204 }
205
206 private boolean hasScoreNetworksPermission(String packageName) {
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800207 final PackageManager pm = mContext.getPackageManager();
208 return pm.checkPermission(permission.SCORE_NETWORKS, packageName)
209 == PackageManager.PERMISSION_GRANTED;
Jeff Davidsondd6fd1e2014-04-14 15:14:30 -0700210 }
211
Jeremy Joslind816abe2017-07-14 15:00:53 -0700212 private boolean canAccessLocation(int uid, String packageName) {
Philip P. Moltmann04acad92019-10-03 12:22:52 -0700213 return isLocationModeEnabled() && PermissionChecker.checkPermissionForPreflight(mContext,
214 permission.ACCESS_COARSE_LOCATION, PermissionChecker.PID_UNKNOWN, uid, packageName)
215 == PermissionChecker.PERMISSION_GRANTED;
Jeremy Joslind816abe2017-07-14 15:00:53 -0700216 }
217
218 private boolean isLocationModeEnabled() {
219 return mSettingsFacade.getSecureInt(mContext, Settings.Secure.LOCATION_MODE,
220 Settings.Secure.LOCATION_MODE_OFF) != Settings.Secure.LOCATION_MODE_OFF;
221 }
222
Jeff Davidsondd6fd1e2014-04-14 15:14:30 -0700223 /**
224 * Set the specified package as the default scorer application.
225 *
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800226 * <p>The caller must have permission to write to {@link Settings.Global}.
Jeff Davidsondd6fd1e2014-04-14 15:14:30 -0700227 *
Jeremy Joslin7aa0b0d2017-04-27 13:32:49 -0700228 * @param packageName the packageName of the new scorer to use. If null, scoring will be forced
229 * off, otherwise the scorer will only be set if it is a valid scorer
230 * application.
231 * @return true if the package was a valid scorer (including <code>null</code>) and now
232 * represents the active scorer, false otherwise.
Jeff Davidsondd6fd1e2014-04-14 15:14:30 -0700233 */
Jeremy Joslinf1acc1e2017-04-04 17:28:23 -0700234 @VisibleForTesting
235 public boolean setActiveScorer(String packageName) {
Jeremy Joslin9925c6a2017-03-06 10:39:35 -0800236 final String oldPackageName = getNetworkRecommendationsPackage();
237
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800238 if (TextUtils.equals(oldPackageName, packageName)) {
239 // No change.
240 return true;
241 }
242
Jeremy Joslin7aa0b0d2017-04-27 13:32:49 -0700243 if (TextUtils.isEmpty(packageName)) {
244 Log.i(TAG, "Network scorer forced off, was: " + oldPackageName);
245 setNetworkRecommendationsPackage(null);
246 setNetworkRecommendationsEnabledSetting(
247 NetworkScoreManager.RECOMMENDATIONS_ENABLED_FORCED_OFF);
248 return true;
Jeremy Joslin9925c6a2017-03-06 10:39:35 -0800249 }
250
Jeremy Joslin9925c6a2017-03-06 10:39:35 -0800251 // We only make the change if the new package is valid.
252 if (getScorer(packageName) != null) {
Jeremy Joslin7aa0b0d2017-04-27 13:32:49 -0700253 Log.i(TAG, "Changing network scorer from " + oldPackageName + " to " + packageName);
Jeremy Joslin9925c6a2017-03-06 10:39:35 -0800254 setNetworkRecommendationsPackage(packageName);
Jeremy Joslin7aa0b0d2017-04-27 13:32:49 -0700255 setNetworkRecommendationsEnabledSetting(NetworkScoreManager.RECOMMENDATIONS_ENABLED_ON);
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800256 return true;
257 } else {
Jeremy Joslin9925c6a2017-03-06 10:39:35 -0800258 Log.w(TAG, "Requested network scorer is not valid: " + packageName);
259 return false;
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800260 }
Jeff Davidsondd6fd1e2014-04-14 15:14:30 -0700261 }
262
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800263 /**
Jeremy Joslin9925c6a2017-03-06 10:39:35 -0800264 * Ensures the {@link Settings.Global#NETWORK_RECOMMENDATIONS_PACKAGE} setting points to a valid
265 * package and {@link Settings.Global#NETWORK_RECOMMENDATIONS_ENABLED} is consistent.
266 *
267 * If {@link Settings.Global#NETWORK_RECOMMENDATIONS_PACKAGE} doesn't point to a valid package
268 * then it will be reverted to the default package specified by
269 * {@link R.string#config_defaultNetworkRecommendationProviderPackage}. If the default package
270 * is no longer valid then {@link Settings.Global#NETWORK_RECOMMENDATIONS_ENABLED} will be set
271 * to <code>0</code> (disabled).
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800272 */
Jeremy Joslinf1acc1e2017-04-04 17:28:23 -0700273 @VisibleForTesting
274 public void updateState() {
Jeremy Joslin9925c6a2017-03-06 10:39:35 -0800275 final int enabledSetting = getNetworkRecommendationsEnabledSetting();
276 if (enabledSetting == NetworkScoreManager.RECOMMENDATIONS_ENABLED_FORCED_OFF) {
277 // Don't change anything if it's forced off.
278 if (DEBUG) Log.d(TAG, "Recommendations forced off.");
279 return;
280 }
281
282 // First, see if the current package is still valid. If so, then we can exit early.
283 final String currentPackageName = getNetworkRecommendationsPackage();
284 if (getScorer(currentPackageName) != null) {
285 if (VERBOSE) Log.v(TAG, currentPackageName + " is the active scorer.");
286 setNetworkRecommendationsEnabledSetting(NetworkScoreManager.RECOMMENDATIONS_ENABLED_ON);
287 return;
288 }
289
Jeremy Joslind816abe2017-07-14 15:00:53 -0700290 int newEnabledSetting = NetworkScoreManager.RECOMMENDATIONS_ENABLED_OFF;
291 // the active scorer isn't valid, revert to the default if it's different and valid
Jeremy Joslin9925c6a2017-03-06 10:39:35 -0800292 final String defaultPackageName = getDefaultPackageSetting();
Jeremy Joslind816abe2017-07-14 15:00:53 -0700293 if (!TextUtils.equals(currentPackageName, defaultPackageName)
294 && getScorer(defaultPackageName) != null) {
Jeremy Joslin9925c6a2017-03-06 10:39:35 -0800295 if (DEBUG) {
Jeremy Joslind816abe2017-07-14 15:00:53 -0700296 Log.d(TAG, "Defaulting the network recommendations app to: "
297 + defaultPackageName);
Jeremy Joslin9925c6a2017-03-06 10:39:35 -0800298 }
Jeremy Joslind816abe2017-07-14 15:00:53 -0700299 setNetworkRecommendationsPackage(defaultPackageName);
300 newEnabledSetting = NetworkScoreManager.RECOMMENDATIONS_ENABLED_ON;
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800301 }
Jeremy Joslind816abe2017-07-14 15:00:53 -0700302
303 setNetworkRecommendationsEnabledSetting(newEnabledSetting);
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800304 }
305
Jeremy Joslinb0fe2172017-03-31 10:38:31 -0700306 /**
307 * Migrates the NETWORK_SCORER_APP Setting to the USE_OPEN_WIFI_PACKAGE Setting.
308 */
Jeremy Joslinf1acc1e2017-04-04 17:28:23 -0700309 @VisibleForTesting
310 public void migrateNetworkScorerAppSettingIfNeeded() {
Jeremy Joslinb0fe2172017-03-31 10:38:31 -0700311 final String scorerAppPkgNameSetting =
312 mSettingsFacade.getString(mContext, Settings.Global.NETWORK_SCORER_APP);
313 if (TextUtils.isEmpty(scorerAppPkgNameSetting)) {
314 // Early exit, nothing to do.
315 return;
316 }
317
318 final NetworkScorerAppData currentAppData = getActiveScorer();
319 if (currentAppData == null) {
320 // Don't touch anything until we have an active scorer to work with.
321 return;
322 }
323
324 if (DEBUG) {
325 Log.d(TAG, "Migrating Settings.Global.NETWORK_SCORER_APP "
326 + "(" + scorerAppPkgNameSetting + ")...");
327 }
328
329 // If the new (useOpenWifi) Setting isn't set and the old Setting's value matches the
330 // new metadata value then update the new Setting with the old value. Otherwise it's a
331 // mismatch so we shouldn't enable the Setting automatically.
332 final ComponentName enableUseOpenWifiActivity =
333 currentAppData.getEnableUseOpenWifiActivity();
334 final String useOpenWifiSetting =
335 mSettingsFacade.getString(mContext, Settings.Global.USE_OPEN_WIFI_PACKAGE);
336 if (TextUtils.isEmpty(useOpenWifiSetting)
337 && enableUseOpenWifiActivity != null
338 && scorerAppPkgNameSetting.equals(enableUseOpenWifiActivity.getPackageName())) {
339 mSettingsFacade.putString(mContext, Settings.Global.USE_OPEN_WIFI_PACKAGE,
340 scorerAppPkgNameSetting);
341 if (DEBUG) {
342 Log.d(TAG, "Settings.Global.USE_OPEN_WIFI_PACKAGE set to "
343 + "'" + scorerAppPkgNameSetting + "'.");
344 }
345 }
346
347 // Clear out the old setting so we don't run through the migration code again.
348 mSettingsFacade.putString(mContext, Settings.Global.NETWORK_SCORER_APP, null);
349 if (DEBUG) {
350 Log.d(TAG, "Settings.Global.NETWORK_SCORER_APP migration complete.");
351 final String setting =
352 mSettingsFacade.getString(mContext, Settings.Global.USE_OPEN_WIFI_PACKAGE);
353 Log.d(TAG, "Settings.Global.USE_OPEN_WIFI_PACKAGE is: '" + setting + "'.");
354 }
355 }
356
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800357 private String getDefaultPackageSetting() {
358 return mContext.getResources().getString(
359 R.string.config_defaultNetworkRecommendationProviderPackage);
360 }
361
362 private String getNetworkRecommendationsPackage() {
363 return mSettingsFacade.getString(mContext, Settings.Global.NETWORK_RECOMMENDATIONS_PACKAGE);
364 }
365
366 private void setNetworkRecommendationsPackage(String packageName) {
367 mSettingsFacade.putString(mContext,
368 Settings.Global.NETWORK_RECOMMENDATIONS_PACKAGE, packageName);
Jeremy Joslind816abe2017-07-14 15:00:53 -0700369 if (VERBOSE) {
370 Log.d(TAG, Settings.Global.NETWORK_RECOMMENDATIONS_PACKAGE + " set to " + packageName);
371 }
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800372 }
373
Jeremy Joslin9925c6a2017-03-06 10:39:35 -0800374 private int getNetworkRecommendationsEnabledSetting() {
375 return mSettingsFacade.getInt(mContext, Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED, 0);
376 }
377
378 private void setNetworkRecommendationsEnabledSetting(int value) {
379 mSettingsFacade.putInt(mContext,
380 Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED, value);
Jeremy Joslind816abe2017-07-14 15:00:53 -0700381 if (VERBOSE) {
382 Log.d(TAG, Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED + " set to " + value);
383 }
Jeremy Joslin9925c6a2017-03-06 10:39:35 -0800384 }
385
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800386 /**
387 * Wrapper around Settings to make testing easier.
388 */
389 public static class SettingsFacade {
390 public boolean putString(Context context, String name, String value) {
391 return Settings.Global.putString(context.getContentResolver(), name, value);
392 }
393
394 public String getString(Context context, String name) {
395 return Settings.Global.getString(context.getContentResolver(), name);
396 }
Jeremy Joslin9925c6a2017-03-06 10:39:35 -0800397
398 public boolean putInt(Context context, String name, int value) {
399 return Settings.Global.putInt(context.getContentResolver(), name, value);
400 }
401
402 public int getInt(Context context, String name, int defaultValue) {
403 return Settings.Global.getInt(context.getContentResolver(), name, defaultValue);
404 }
Jeremy Joslind816abe2017-07-14 15:00:53 -0700405
406 public int getSecureInt(Context context, String name, int defaultValue) {
407 return Settings.Secure.getInt(context.getContentResolver(), name, defaultValue);
408 }
Jeff Davidsondd6fd1e2014-04-14 15:14:30 -0700409 }
410}