blob: bfd4247abf40f1510622c06f7f6d6927fa998c27 [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 Joslind816abe2017-07-14 15:00:53 -070021import android.app.AppOpsManager;
Jeremy Joslin37e877b2017-02-02 11:06:14 -080022import android.content.ComponentName;
Jeff Davidsondd6fd1e2014-04-14 15:14:30 -070023import android.content.Context;
24import android.content.Intent;
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) {
213 final PackageManager pm = mContext.getPackageManager();
214 final AppOpsManager appOpsManager =
215 (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
216 return isLocationModeEnabled()
217 && pm.checkPermission(permission.ACCESS_COARSE_LOCATION, packageName)
218 == PackageManager.PERMISSION_GRANTED
219 && appOpsManager.noteOp(AppOpsManager.OP_COARSE_LOCATION, uid, packageName)
220 == AppOpsManager.MODE_ALLOWED;
221 }
222
223 private boolean isLocationModeEnabled() {
224 return mSettingsFacade.getSecureInt(mContext, Settings.Secure.LOCATION_MODE,
225 Settings.Secure.LOCATION_MODE_OFF) != Settings.Secure.LOCATION_MODE_OFF;
226 }
227
Jeff Davidsondd6fd1e2014-04-14 15:14:30 -0700228 /**
229 * Set the specified package as the default scorer application.
230 *
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800231 * <p>The caller must have permission to write to {@link Settings.Global}.
Jeff Davidsondd6fd1e2014-04-14 15:14:30 -0700232 *
Jeremy Joslin7aa0b0d2017-04-27 13:32:49 -0700233 * @param packageName the packageName of the new scorer to use. If null, scoring will be forced
234 * off, otherwise the scorer will only be set if it is a valid scorer
235 * application.
236 * @return true if the package was a valid scorer (including <code>null</code>) and now
237 * represents the active scorer, false otherwise.
Jeff Davidsondd6fd1e2014-04-14 15:14:30 -0700238 */
Jeremy Joslinf1acc1e2017-04-04 17:28:23 -0700239 @VisibleForTesting
240 public boolean setActiveScorer(String packageName) {
Jeremy Joslin9925c6a2017-03-06 10:39:35 -0800241 final String oldPackageName = getNetworkRecommendationsPackage();
242
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800243 if (TextUtils.equals(oldPackageName, packageName)) {
244 // No change.
245 return true;
246 }
247
Jeremy Joslin7aa0b0d2017-04-27 13:32:49 -0700248 if (TextUtils.isEmpty(packageName)) {
249 Log.i(TAG, "Network scorer forced off, was: " + oldPackageName);
250 setNetworkRecommendationsPackage(null);
251 setNetworkRecommendationsEnabledSetting(
252 NetworkScoreManager.RECOMMENDATIONS_ENABLED_FORCED_OFF);
253 return true;
Jeremy Joslin9925c6a2017-03-06 10:39:35 -0800254 }
255
Jeremy Joslin9925c6a2017-03-06 10:39:35 -0800256 // We only make the change if the new package is valid.
257 if (getScorer(packageName) != null) {
Jeremy Joslin7aa0b0d2017-04-27 13:32:49 -0700258 Log.i(TAG, "Changing network scorer from " + oldPackageName + " to " + packageName);
Jeremy Joslin9925c6a2017-03-06 10:39:35 -0800259 setNetworkRecommendationsPackage(packageName);
Jeremy Joslin7aa0b0d2017-04-27 13:32:49 -0700260 setNetworkRecommendationsEnabledSetting(NetworkScoreManager.RECOMMENDATIONS_ENABLED_ON);
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800261 return true;
262 } else {
Jeremy Joslin9925c6a2017-03-06 10:39:35 -0800263 Log.w(TAG, "Requested network scorer is not valid: " + packageName);
264 return false;
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800265 }
Jeff Davidsondd6fd1e2014-04-14 15:14:30 -0700266 }
267
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800268 /**
Jeremy Joslin9925c6a2017-03-06 10:39:35 -0800269 * Ensures the {@link Settings.Global#NETWORK_RECOMMENDATIONS_PACKAGE} setting points to a valid
270 * package and {@link Settings.Global#NETWORK_RECOMMENDATIONS_ENABLED} is consistent.
271 *
272 * If {@link Settings.Global#NETWORK_RECOMMENDATIONS_PACKAGE} doesn't point to a valid package
273 * then it will be reverted to the default package specified by
274 * {@link R.string#config_defaultNetworkRecommendationProviderPackage}. If the default package
275 * is no longer valid then {@link Settings.Global#NETWORK_RECOMMENDATIONS_ENABLED} will be set
276 * to <code>0</code> (disabled).
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800277 */
Jeremy Joslinf1acc1e2017-04-04 17:28:23 -0700278 @VisibleForTesting
279 public void updateState() {
Jeremy Joslin9925c6a2017-03-06 10:39:35 -0800280 final int enabledSetting = getNetworkRecommendationsEnabledSetting();
281 if (enabledSetting == NetworkScoreManager.RECOMMENDATIONS_ENABLED_FORCED_OFF) {
282 // Don't change anything if it's forced off.
283 if (DEBUG) Log.d(TAG, "Recommendations forced off.");
284 return;
285 }
286
287 // First, see if the current package is still valid. If so, then we can exit early.
288 final String currentPackageName = getNetworkRecommendationsPackage();
289 if (getScorer(currentPackageName) != null) {
290 if (VERBOSE) Log.v(TAG, currentPackageName + " is the active scorer.");
291 setNetworkRecommendationsEnabledSetting(NetworkScoreManager.RECOMMENDATIONS_ENABLED_ON);
292 return;
293 }
294
Jeremy Joslind816abe2017-07-14 15:00:53 -0700295 int newEnabledSetting = NetworkScoreManager.RECOMMENDATIONS_ENABLED_OFF;
296 // the active scorer isn't valid, revert to the default if it's different and valid
Jeremy Joslin9925c6a2017-03-06 10:39:35 -0800297 final String defaultPackageName = getDefaultPackageSetting();
Jeremy Joslind816abe2017-07-14 15:00:53 -0700298 if (!TextUtils.equals(currentPackageName, defaultPackageName)
299 && getScorer(defaultPackageName) != null) {
Jeremy Joslin9925c6a2017-03-06 10:39:35 -0800300 if (DEBUG) {
Jeremy Joslind816abe2017-07-14 15:00:53 -0700301 Log.d(TAG, "Defaulting the network recommendations app to: "
302 + defaultPackageName);
Jeremy Joslin9925c6a2017-03-06 10:39:35 -0800303 }
Jeremy Joslind816abe2017-07-14 15:00:53 -0700304 setNetworkRecommendationsPackage(defaultPackageName);
305 newEnabledSetting = NetworkScoreManager.RECOMMENDATIONS_ENABLED_ON;
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800306 }
Jeremy Joslind816abe2017-07-14 15:00:53 -0700307
308 setNetworkRecommendationsEnabledSetting(newEnabledSetting);
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800309 }
310
Jeremy Joslinb0fe2172017-03-31 10:38:31 -0700311 /**
312 * Migrates the NETWORK_SCORER_APP Setting to the USE_OPEN_WIFI_PACKAGE Setting.
313 */
Jeremy Joslinf1acc1e2017-04-04 17:28:23 -0700314 @VisibleForTesting
315 public void migrateNetworkScorerAppSettingIfNeeded() {
Jeremy Joslinb0fe2172017-03-31 10:38:31 -0700316 final String scorerAppPkgNameSetting =
317 mSettingsFacade.getString(mContext, Settings.Global.NETWORK_SCORER_APP);
318 if (TextUtils.isEmpty(scorerAppPkgNameSetting)) {
319 // Early exit, nothing to do.
320 return;
321 }
322
323 final NetworkScorerAppData currentAppData = getActiveScorer();
324 if (currentAppData == null) {
325 // Don't touch anything until we have an active scorer to work with.
326 return;
327 }
328
329 if (DEBUG) {
330 Log.d(TAG, "Migrating Settings.Global.NETWORK_SCORER_APP "
331 + "(" + scorerAppPkgNameSetting + ")...");
332 }
333
334 // If the new (useOpenWifi) Setting isn't set and the old Setting's value matches the
335 // new metadata value then update the new Setting with the old value. Otherwise it's a
336 // mismatch so we shouldn't enable the Setting automatically.
337 final ComponentName enableUseOpenWifiActivity =
338 currentAppData.getEnableUseOpenWifiActivity();
339 final String useOpenWifiSetting =
340 mSettingsFacade.getString(mContext, Settings.Global.USE_OPEN_WIFI_PACKAGE);
341 if (TextUtils.isEmpty(useOpenWifiSetting)
342 && enableUseOpenWifiActivity != null
343 && scorerAppPkgNameSetting.equals(enableUseOpenWifiActivity.getPackageName())) {
344 mSettingsFacade.putString(mContext, Settings.Global.USE_OPEN_WIFI_PACKAGE,
345 scorerAppPkgNameSetting);
346 if (DEBUG) {
347 Log.d(TAG, "Settings.Global.USE_OPEN_WIFI_PACKAGE set to "
348 + "'" + scorerAppPkgNameSetting + "'.");
349 }
350 }
351
352 // Clear out the old setting so we don't run through the migration code again.
353 mSettingsFacade.putString(mContext, Settings.Global.NETWORK_SCORER_APP, null);
354 if (DEBUG) {
355 Log.d(TAG, "Settings.Global.NETWORK_SCORER_APP migration complete.");
356 final String setting =
357 mSettingsFacade.getString(mContext, Settings.Global.USE_OPEN_WIFI_PACKAGE);
358 Log.d(TAG, "Settings.Global.USE_OPEN_WIFI_PACKAGE is: '" + setting + "'.");
359 }
360 }
361
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800362 private String getDefaultPackageSetting() {
363 return mContext.getResources().getString(
364 R.string.config_defaultNetworkRecommendationProviderPackage);
365 }
366
367 private String getNetworkRecommendationsPackage() {
368 return mSettingsFacade.getString(mContext, Settings.Global.NETWORK_RECOMMENDATIONS_PACKAGE);
369 }
370
371 private void setNetworkRecommendationsPackage(String packageName) {
372 mSettingsFacade.putString(mContext,
373 Settings.Global.NETWORK_RECOMMENDATIONS_PACKAGE, packageName);
Jeremy Joslind816abe2017-07-14 15:00:53 -0700374 if (VERBOSE) {
375 Log.d(TAG, Settings.Global.NETWORK_RECOMMENDATIONS_PACKAGE + " set to " + packageName);
376 }
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800377 }
378
Jeremy Joslin9925c6a2017-03-06 10:39:35 -0800379 private int getNetworkRecommendationsEnabledSetting() {
380 return mSettingsFacade.getInt(mContext, Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED, 0);
381 }
382
383 private void setNetworkRecommendationsEnabledSetting(int value) {
384 mSettingsFacade.putInt(mContext,
385 Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED, value);
Jeremy Joslind816abe2017-07-14 15:00:53 -0700386 if (VERBOSE) {
387 Log.d(TAG, Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED + " set to " + value);
388 }
Jeremy Joslin9925c6a2017-03-06 10:39:35 -0800389 }
390
Jeremy Joslinee3fb5c2017-02-13 13:44:11 -0800391 /**
392 * Wrapper around Settings to make testing easier.
393 */
394 public static class SettingsFacade {
395 public boolean putString(Context context, String name, String value) {
396 return Settings.Global.putString(context.getContentResolver(), name, value);
397 }
398
399 public String getString(Context context, String name) {
400 return Settings.Global.getString(context.getContentResolver(), name);
401 }
Jeremy Joslin9925c6a2017-03-06 10:39:35 -0800402
403 public boolean putInt(Context context, String name, int value) {
404 return Settings.Global.putInt(context.getContentResolver(), name, value);
405 }
406
407 public int getInt(Context context, String name, int defaultValue) {
408 return Settings.Global.getInt(context.getContentResolver(), name, defaultValue);
409 }
Jeremy Joslind816abe2017-07-14 15:00:53 -0700410
411 public int getSecureInt(Context context, String name, int defaultValue) {
412 return Settings.Secure.getInt(context.getContentResolver(), name, defaultValue);
413 }
Jeff Davidsondd6fd1e2014-04-14 15:14:30 -0700414 }
415}