Jeff Davidson | dd6fd1e | 2014-04-14 15:14:30 -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 | |
Jeremy Joslin | f621bc9 | 2017-02-16 11:11:57 -0800 | [diff] [blame] | 17 | package com.android.server; |
Jeff Davidson | dd6fd1e | 2014-04-14 15:14:30 -0700 | [diff] [blame] | 18 | |
| 19 | import android.Manifest.permission; |
Jeff Davidson | b6646a8 | 2014-06-27 16:24:42 -0700 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Jeremy Joslin | 37e877b | 2017-02-02 11:06:14 -0800 | [diff] [blame] | 21 | import android.content.ComponentName; |
Jeff Davidson | dd6fd1e | 2014-04-14 15:14:30 -0700 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
Philip P. Moltmann | 04acad9 | 2019-10-03 12:22:52 -0700 | [diff] [blame] | 24 | import android.content.PermissionChecker; |
Jeff Davidson | dd6fd1e | 2014-04-14 15:14:30 -0700 | [diff] [blame] | 25 | import android.content.pm.PackageManager; |
| 26 | import android.content.pm.ResolveInfo; |
Amin Shaikh | bc9a8e6 | 2017-02-02 15:39:12 -0800 | [diff] [blame] | 27 | import android.content.pm.ServiceInfo; |
Jeremy Joslin | f621bc9 | 2017-02-16 11:11:57 -0800 | [diff] [blame] | 28 | import android.net.NetworkScoreManager; |
| 29 | import android.net.NetworkScorerAppData; |
Jeff Davidson | dd6fd1e | 2014-04-14 15:14:30 -0700 | [diff] [blame] | 30 | import android.provider.Settings; |
Jeff Davidson | dd6fd1e | 2014-04-14 15:14:30 -0700 | [diff] [blame] | 31 | import android.text.TextUtils; |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 32 | import android.util.Log; |
Jeremy Joslin | 134c9d3 | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 33 | |
Jeremy Joslin | b8418ac8 | 2016-12-06 07:42:38 -0800 | [diff] [blame] | 34 | import com.android.internal.R; |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 35 | import com.android.internal.annotations.VisibleForTesting; |
Jeremy Joslin | 134c9d3 | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 36 | |
Jeff Davidson | dd6fd1e | 2014-04-14 15:14:30 -0700 | [diff] [blame] | 37 | import java.util.ArrayList; |
Jeremy Joslin | 5b294b4 | 2015-12-17 17:38:04 -0800 | [diff] [blame] | 38 | import java.util.Collections; |
Jeff Davidson | dd6fd1e | 2014-04-14 15:14:30 -0700 | [diff] [blame] | 39 | import java.util.List; |
| 40 | |
| 41 | /** |
Jeremy Joslin | b8418ac8 | 2016-12-06 07:42:38 -0800 | [diff] [blame] | 42 | * Internal class for discovering and managing the network scorer/recommendation application. |
Jeff Davidson | c741553 | 2014-06-23 18:15:34 -0700 | [diff] [blame] | 43 | * |
Jeff Davidson | dd6fd1e | 2014-04-14 15:14:30 -0700 | [diff] [blame] | 44 | * @hide |
| 45 | */ |
Jeremy Joslin | f1acc1e | 2017-04-04 17:28:23 -0700 | [diff] [blame] | 46 | @VisibleForTesting |
Amin Shaikh | aa09aa0 | 2016-11-21 17:27:53 -0800 | [diff] [blame] | 47 | public class NetworkScorerAppManager { |
Jeff Davidson | 6a4b220 | 2014-04-16 17:29:40 -0700 | [diff] [blame] | 48 | private static final String TAG = "NetworkScorerAppManager"; |
Jeremy Joslin | b8418ac8 | 2016-12-06 07:42:38 -0800 | [diff] [blame] | 49 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
| 50 | private static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE); |
Amin Shaikh | aa09aa0 | 2016-11-21 17:27:53 -0800 | [diff] [blame] | 51 | private final Context mContext; |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 52 | private final SettingsFacade mSettingsFacade; |
Amin Shaikh | aa09aa0 | 2016-11-21 17:27:53 -0800 | [diff] [blame] | 53 | |
| 54 | public NetworkScorerAppManager(Context context) { |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 55 | this(context, new SettingsFacade()); |
| 56 | } |
| 57 | |
| 58 | @VisibleForTesting |
| 59 | public NetworkScorerAppManager(Context context, SettingsFacade settingsFacade) { |
| 60 | mContext = context; |
| 61 | mSettingsFacade = settingsFacade; |
Amin Shaikh | aa09aa0 | 2016-11-21 17:27:53 -0800 | [diff] [blame] | 62 | } |
Jeff Davidson | dd6fd1e | 2014-04-14 15:14:30 -0700 | [diff] [blame] | 63 | |
Jeremy Joslin | b8418ac8 | 2016-12-06 07:42:38 -0800 | [diff] [blame] | 64 | /** |
Jeremy Joslin | f95c865 | 2017-02-09 15:32:04 -0800 | [diff] [blame] | 65 | * Returns the list of available scorer apps. The list will be empty if there are |
| 66 | * no valid scorers. |
| 67 | */ |
Jeremy Joslin | f1acc1e | 2017-04-04 17:28:23 -0700 | [diff] [blame] | 68 | @VisibleForTesting |
| 69 | public List<NetworkScorerAppData> getAllValidScorers() { |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 70 | 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 Joslin | b8418ac8 | 2016-12-06 07:42:38 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 80 | List<NetworkScorerAppData> appDataList = new ArrayList<>(); |
| 81 | for (int i = 0; i < resolveInfos.size(); i++) { |
| 82 | final ServiceInfo serviceInfo = resolveInfos.get(i).serviceInfo; |
Jeremy Joslin | d816abe | 2017-07-14 15:00:53 -0700 | [diff] [blame] | 83 | if (hasPermissions(serviceInfo.applicationInfo.uid, serviceInfo.packageName)) { |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 84 | if (VERBOSE) { |
| 85 | Log.v(TAG, serviceInfo.packageName + " is a valid scorer/recommender."); |
| 86 | } |
Amin Shaikh | bc9a8e6 | 2017-02-02 15:39:12 -0800 | [diff] [blame] | 87 | final ComponentName serviceComponentName = |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 88 | new ComponentName(serviceInfo.packageName, serviceInfo.name); |
Stephen Chen | 8b1339a | 2017-02-28 18:11:34 -0800 | [diff] [blame] | 89 | final String serviceLabel = getRecommendationServiceLabel(serviceInfo, pm); |
Amin Shaikh | bc9a8e6 | 2017-02-02 15:39:12 -0800 | [diff] [blame] | 90 | final ComponentName useOpenWifiNetworksActivity = |
| 91 | findUseOpenWifiNetworksActivity(serviceInfo); |
Amin Shaikh | d601360 | 2017-03-24 15:52:32 -0700 | [diff] [blame] | 92 | final String networkAvailableNotificationChannelId = |
| 93 | getNetworkAvailableNotificationChannelId(serviceInfo); |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 94 | appDataList.add( |
| 95 | new NetworkScorerAppData(serviceInfo.applicationInfo.uid, |
Amin Shaikh | d601360 | 2017-03-24 15:52:32 -0700 | [diff] [blame] | 96 | serviceComponentName, serviceLabel, useOpenWifiNetworksActivity, |
| 97 | networkAvailableNotificationChannelId)); |
Jeremy Joslin | b8418ac8 | 2016-12-06 07:42:38 -0800 | [diff] [blame] | 98 | } else { |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 99 | if (VERBOSE) Log.v(TAG, serviceInfo.packageName |
| 100 | + " is NOT a valid scorer/recommender."); |
Jeremy Joslin | b8418ac8 | 2016-12-06 07:42:38 -0800 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 104 | return appDataList; |
Jeremy Joslin | b8418ac8 | 2016-12-06 07:42:38 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 107 | @Nullable |
Stephen Chen | 8b1339a | 2017-02-28 18:11:34 -0800 | [diff] [blame] | 108 | 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 Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 121 | private ComponentName findUseOpenWifiNetworksActivity(ServiceInfo serviceInfo) { |
Amin Shaikh | bc9a8e6 | 2017-02-02 15:39:12 -0800 | [diff] [blame] | 122 | if (serviceInfo.metaData == null) { |
| 123 | if (DEBUG) { |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 124 | Log.d(TAG, "No metadata found on " + serviceInfo.getComponentName()); |
Amin Shaikh | bc9a8e6 | 2017-02-02 15:39:12 -0800 | [diff] [blame] | 125 | } |
| 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 Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 132 | Log.d(TAG, "No use_open_wifi_package metadata found on " |
| 133 | + serviceInfo.getComponentName()); |
Amin Shaikh | bc9a8e6 | 2017-02-02 15:39:12 -0800 | [diff] [blame] | 134 | } |
| 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 Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 142 | Log.d(TAG, "Resolved " + enableUseOpenWifiIntent + " to " + resolveActivityInfo); |
Amin Shaikh | bc9a8e6 | 2017-02-02 15:39:12 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | if (resolveActivityInfo != null && resolveActivityInfo.activityInfo != null) { |
| 146 | return resolveActivityInfo.activityInfo.getComponentName(); |
| 147 | } |
| 148 | |
| 149 | return null; |
| 150 | } |
| 151 | |
Amin Shaikh | d601360 | 2017-03-24 15:52:32 -0700 | [diff] [blame] | 152 | @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 Joslin | b8418ac8 | 2016-12-06 07:42:38 -0800 | [diff] [blame] | 166 | /** |
Jeff Davidson | c741553 | 2014-06-23 18:15:34 -0700 | [diff] [blame] | 167 | * Get the application to use for scoring networks. |
Jeff Davidson | dd6fd1e | 2014-04-14 15:14:30 -0700 | [diff] [blame] | 168 | * |
Jeff Davidson | c741553 | 2014-06-23 18:15:34 -0700 | [diff] [blame] | 169 | * @return the scorer app info or null if scoring is disabled (including if no scorer was ever |
Jeff Davidson | dd6fd1e | 2014-04-14 15:14:30 -0700 | [diff] [blame] | 170 | * 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 Joslin | b8418ac8 | 2016-12-06 07:42:38 -0800 | [diff] [blame] | 173 | @Nullable |
Jeremy Joslin | f1acc1e | 2017-04-04 17:28:23 -0700 | [diff] [blame] | 174 | @VisibleForTesting |
| 175 | public NetworkScorerAppData getActiveScorer() { |
Jeremy Joslin | 9925c6a | 2017-03-06 10:39:35 -0800 | [diff] [blame] | 176 | final int enabledSetting = getNetworkRecommendationsEnabledSetting(); |
| 177 | if (enabledSetting == NetworkScoreManager.RECOMMENDATIONS_ENABLED_FORCED_OFF) { |
| 178 | return null; |
| 179 | } |
| 180 | |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 181 | return getScorer(getNetworkRecommendationsPackage()); |
| 182 | } |
| 183 | |
| 184 | private NetworkScorerAppData getScorer(String packageName) { |
| 185 | if (TextUtils.isEmpty(packageName)) { |
Jeremy Joslin | b8418ac8 | 2016-12-06 07:42:38 -0800 | [diff] [blame] | 186 | return null; |
| 187 | } |
| 188 | |
| 189 | // Otherwise return the recommendation provider (which may be null). |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 190 | 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 Joslin | d816abe | 2017-07-14 15:00:53 -0700 | [diff] [blame] | 201 | 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 Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 207 | final PackageManager pm = mContext.getPackageManager(); |
| 208 | return pm.checkPermission(permission.SCORE_NETWORKS, packageName) |
| 209 | == PackageManager.PERMISSION_GRANTED; |
Jeff Davidson | dd6fd1e | 2014-04-14 15:14:30 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Jeremy Joslin | d816abe | 2017-07-14 15:00:53 -0700 | [diff] [blame] | 212 | private boolean canAccessLocation(int uid, String packageName) { |
Philip P. Moltmann | 04acad9 | 2019-10-03 12:22:52 -0700 | [diff] [blame] | 213 | return isLocationModeEnabled() && PermissionChecker.checkPermissionForPreflight(mContext, |
| 214 | permission.ACCESS_COARSE_LOCATION, PermissionChecker.PID_UNKNOWN, uid, packageName) |
| 215 | == PermissionChecker.PERMISSION_GRANTED; |
Jeremy Joslin | d816abe | 2017-07-14 15:00:53 -0700 | [diff] [blame] | 216 | } |
| 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 Davidson | dd6fd1e | 2014-04-14 15:14:30 -0700 | [diff] [blame] | 223 | /** |
| 224 | * Set the specified package as the default scorer application. |
| 225 | * |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 226 | * <p>The caller must have permission to write to {@link Settings.Global}. |
Jeff Davidson | dd6fd1e | 2014-04-14 15:14:30 -0700 | [diff] [blame] | 227 | * |
Jeremy Joslin | 7aa0b0d | 2017-04-27 13:32:49 -0700 | [diff] [blame] | 228 | * @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 Davidson | dd6fd1e | 2014-04-14 15:14:30 -0700 | [diff] [blame] | 233 | */ |
Jeremy Joslin | f1acc1e | 2017-04-04 17:28:23 -0700 | [diff] [blame] | 234 | @VisibleForTesting |
| 235 | public boolean setActiveScorer(String packageName) { |
Jeremy Joslin | 9925c6a | 2017-03-06 10:39:35 -0800 | [diff] [blame] | 236 | final String oldPackageName = getNetworkRecommendationsPackage(); |
| 237 | |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 238 | if (TextUtils.equals(oldPackageName, packageName)) { |
| 239 | // No change. |
| 240 | return true; |
| 241 | } |
| 242 | |
Jeremy Joslin | 7aa0b0d | 2017-04-27 13:32:49 -0700 | [diff] [blame] | 243 | 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 Joslin | 9925c6a | 2017-03-06 10:39:35 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Jeremy Joslin | 9925c6a | 2017-03-06 10:39:35 -0800 | [diff] [blame] | 251 | // We only make the change if the new package is valid. |
| 252 | if (getScorer(packageName) != null) { |
Jeremy Joslin | 7aa0b0d | 2017-04-27 13:32:49 -0700 | [diff] [blame] | 253 | Log.i(TAG, "Changing network scorer from " + oldPackageName + " to " + packageName); |
Jeremy Joslin | 9925c6a | 2017-03-06 10:39:35 -0800 | [diff] [blame] | 254 | setNetworkRecommendationsPackage(packageName); |
Jeremy Joslin | 7aa0b0d | 2017-04-27 13:32:49 -0700 | [diff] [blame] | 255 | setNetworkRecommendationsEnabledSetting(NetworkScoreManager.RECOMMENDATIONS_ENABLED_ON); |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 256 | return true; |
| 257 | } else { |
Jeremy Joslin | 9925c6a | 2017-03-06 10:39:35 -0800 | [diff] [blame] | 258 | Log.w(TAG, "Requested network scorer is not valid: " + packageName); |
| 259 | return false; |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 260 | } |
Jeff Davidson | dd6fd1e | 2014-04-14 15:14:30 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 263 | /** |
Jeremy Joslin | 9925c6a | 2017-03-06 10:39:35 -0800 | [diff] [blame] | 264 | * 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 Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 272 | */ |
Jeremy Joslin | f1acc1e | 2017-04-04 17:28:23 -0700 | [diff] [blame] | 273 | @VisibleForTesting |
| 274 | public void updateState() { |
Jeremy Joslin | 9925c6a | 2017-03-06 10:39:35 -0800 | [diff] [blame] | 275 | 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 Joslin | d816abe | 2017-07-14 15:00:53 -0700 | [diff] [blame] | 290 | int newEnabledSetting = NetworkScoreManager.RECOMMENDATIONS_ENABLED_OFF; |
| 291 | // the active scorer isn't valid, revert to the default if it's different and valid |
Jeremy Joslin | 9925c6a | 2017-03-06 10:39:35 -0800 | [diff] [blame] | 292 | final String defaultPackageName = getDefaultPackageSetting(); |
Jeremy Joslin | d816abe | 2017-07-14 15:00:53 -0700 | [diff] [blame] | 293 | if (!TextUtils.equals(currentPackageName, defaultPackageName) |
| 294 | && getScorer(defaultPackageName) != null) { |
Jeremy Joslin | 9925c6a | 2017-03-06 10:39:35 -0800 | [diff] [blame] | 295 | if (DEBUG) { |
Jeremy Joslin | d816abe | 2017-07-14 15:00:53 -0700 | [diff] [blame] | 296 | Log.d(TAG, "Defaulting the network recommendations app to: " |
| 297 | + defaultPackageName); |
Jeremy Joslin | 9925c6a | 2017-03-06 10:39:35 -0800 | [diff] [blame] | 298 | } |
Jeremy Joslin | d816abe | 2017-07-14 15:00:53 -0700 | [diff] [blame] | 299 | setNetworkRecommendationsPackage(defaultPackageName); |
| 300 | newEnabledSetting = NetworkScoreManager.RECOMMENDATIONS_ENABLED_ON; |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 301 | } |
Jeremy Joslin | d816abe | 2017-07-14 15:00:53 -0700 | [diff] [blame] | 302 | |
| 303 | setNetworkRecommendationsEnabledSetting(newEnabledSetting); |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Jeremy Joslin | b0fe217 | 2017-03-31 10:38:31 -0700 | [diff] [blame] | 306 | /** |
| 307 | * Migrates the NETWORK_SCORER_APP Setting to the USE_OPEN_WIFI_PACKAGE Setting. |
| 308 | */ |
Jeremy Joslin | f1acc1e | 2017-04-04 17:28:23 -0700 | [diff] [blame] | 309 | @VisibleForTesting |
| 310 | public void migrateNetworkScorerAppSettingIfNeeded() { |
Jeremy Joslin | b0fe217 | 2017-03-31 10:38:31 -0700 | [diff] [blame] | 311 | 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 Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 357 | 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 Joslin | d816abe | 2017-07-14 15:00:53 -0700 | [diff] [blame] | 369 | if (VERBOSE) { |
| 370 | Log.d(TAG, Settings.Global.NETWORK_RECOMMENDATIONS_PACKAGE + " set to " + packageName); |
| 371 | } |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 372 | } |
| 373 | |
Jeremy Joslin | 9925c6a | 2017-03-06 10:39:35 -0800 | [diff] [blame] | 374 | 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 Joslin | d816abe | 2017-07-14 15:00:53 -0700 | [diff] [blame] | 381 | if (VERBOSE) { |
| 382 | Log.d(TAG, Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED + " set to " + value); |
| 383 | } |
Jeremy Joslin | 9925c6a | 2017-03-06 10:39:35 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 386 | /** |
| 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 Joslin | 9925c6a | 2017-03-06 10:39:35 -0800 | [diff] [blame] | 397 | |
| 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 Joslin | d816abe | 2017-07-14 15:00:53 -0700 | [diff] [blame] | 405 | |
| 406 | public int getSecureInt(Context context, String name, int defaultValue) { |
| 407 | return Settings.Secure.getInt(context.getContentResolver(), name, defaultValue); |
| 408 | } |
Jeff Davidson | dd6fd1e | 2014-04-14 15:14:30 -0700 | [diff] [blame] | 409 | } |
| 410 | } |