Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 com.android.server.location; |
| 18 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 19 | import static android.location.LocationManager.FUSED_PROVIDER; |
| 20 | import static android.location.LocationManager.PASSIVE_PROVIDER; |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 21 | import static android.provider.Settings.Global.LOCATION_BACKGROUND_THROTTLE_INTERVAL_MS; |
| 22 | import static android.provider.Settings.Global.LOCATION_BACKGROUND_THROTTLE_PACKAGE_WHITELIST; |
| 23 | import static android.provider.Settings.Global.LOCATION_BACKGROUND_THROTTLE_PROXIMITY_ALERT_INTERVAL_MS; |
| 24 | import static android.provider.Settings.Global.LOCATION_IGNORE_SETTINGS_PACKAGE_WHITELIST; |
| 25 | import static android.provider.Settings.Global.LOCATION_LAST_LOCATION_MAX_AGE_MILLIS; |
| 26 | import static android.provider.Settings.Secure.LOCATION_MODE; |
| 27 | import static android.provider.Settings.Secure.LOCATION_MODE_OFF; |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 28 | |
| 29 | import android.app.ActivityManager; |
| 30 | import android.content.Context; |
| 31 | import android.database.ContentObserver; |
| 32 | import android.net.Uri; |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 33 | import android.os.Binder; |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 34 | import android.os.Handler; |
| 35 | import android.os.UserHandle; |
| 36 | import android.provider.Settings; |
| 37 | import android.text.TextUtils; |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 38 | import android.util.ArraySet; |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 39 | |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 40 | import com.android.internal.annotations.GuardedBy; |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 41 | import com.android.internal.util.IndentingPrintWriter; |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 42 | import com.android.internal.util.Preconditions; |
| 43 | import com.android.server.SystemConfig; |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 44 | |
| 45 | import java.io.FileDescriptor; |
| 46 | import java.io.PrintWriter; |
| 47 | import java.util.Arrays; |
| 48 | import java.util.Collections; |
| 49 | import java.util.List; |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 50 | import java.util.Set; |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 51 | import java.util.concurrent.CopyOnWriteArrayList; |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 52 | import java.util.function.Supplier; |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Provides accessors and listeners for all location related settings. |
| 56 | */ |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 57 | public class SettingsHelper { |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 58 | |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 59 | /** |
| 60 | * Listener for user-specific settings changes. |
| 61 | */ |
| 62 | public interface UserSettingChangedListener { |
| 63 | /** |
| 64 | * Called when setting changes. |
| 65 | */ |
| 66 | void onSettingChanged(int userId); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Listener for global settings changes. |
| 71 | */ |
| 72 | public interface GlobalSettingChangedListener extends UserSettingChangedListener { |
| 73 | /** |
| 74 | * Called when setting changes. |
| 75 | */ |
| 76 | void onSettingChanged(); |
| 77 | |
| 78 | @Override |
| 79 | default void onSettingChanged(int userId) { |
| 80 | onSettingChanged(); |
| 81 | } |
| 82 | } |
| 83 | |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 84 | private static final String LOCATION_PACKAGE_BLACKLIST = "locationPackagePrefixBlacklist"; |
| 85 | private static final String LOCATION_PACKAGE_WHITELIST = "locationPackagePrefixWhitelist"; |
| 86 | |
| 87 | private static final long DEFAULT_BACKGROUND_THROTTLE_INTERVAL_MS = 30 * 60 * 1000; |
| 88 | private static final long DEFAULT_BACKGROUND_THROTTLE_PROXIMITY_ALERT_INTERVAL_MS = |
| 89 | 30 * 60 * 1000; |
| 90 | private static final long DEFAULT_MAX_LAST_LOCATION_AGE_MS = 20 * 60 * 1000; |
| 91 | |
| 92 | private final Context mContext; |
| 93 | |
| 94 | private final IntegerSecureSetting mLocationMode; |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 95 | private final LongGlobalSetting mBackgroundThrottleIntervalMs; |
| 96 | private final StringListCachedSecureSetting mLocationPackageBlacklist; |
| 97 | private final StringListCachedSecureSetting mLocationPackageWhitelist; |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 98 | private final StringSetCachedGlobalSetting mBackgroundThrottlePackageWhitelist; |
| 99 | private final StringSetCachedGlobalSetting mIgnoreSettingsPackageWhitelist; |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 100 | |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 101 | // TODO: get rid of handler |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 102 | public SettingsHelper(Context context, Handler handler) { |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 103 | mContext = context; |
| 104 | |
| 105 | mLocationMode = new IntegerSecureSetting(context, LOCATION_MODE, handler); |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 106 | mBackgroundThrottleIntervalMs = new LongGlobalSetting(context, |
| 107 | LOCATION_BACKGROUND_THROTTLE_INTERVAL_MS, handler); |
| 108 | mLocationPackageBlacklist = new StringListCachedSecureSetting(context, |
| 109 | LOCATION_PACKAGE_BLACKLIST, handler); |
| 110 | mLocationPackageWhitelist = new StringListCachedSecureSetting(context, |
| 111 | LOCATION_PACKAGE_WHITELIST, handler); |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 112 | mBackgroundThrottlePackageWhitelist = new StringSetCachedGlobalSetting(context, |
| 113 | LOCATION_BACKGROUND_THROTTLE_PACKAGE_WHITELIST, |
| 114 | () -> SystemConfig.getInstance().getAllowUnthrottledLocation(), handler); |
| 115 | mIgnoreSettingsPackageWhitelist = new StringSetCachedGlobalSetting(context, |
| 116 | LOCATION_IGNORE_SETTINGS_PACKAGE_WHITELIST, |
| 117 | () -> SystemConfig.getInstance().getAllowIgnoreLocationSettings(), handler); |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 120 | /** Called when system is ready. */ |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 121 | public void onSystemReady() { |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 122 | mLocationMode.register(); |
| 123 | mBackgroundThrottleIntervalMs.register(); |
| 124 | mLocationPackageBlacklist.register(); |
| 125 | mLocationPackageWhitelist.register(); |
| 126 | mBackgroundThrottlePackageWhitelist.register(); |
| 127 | mIgnoreSettingsPackageWhitelist.register(); |
| 128 | } |
| 129 | |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 130 | /** |
| 131 | * Retrieve if location is enabled or not. |
| 132 | */ |
| 133 | public boolean isLocationEnabled(int userId) { |
| 134 | return mLocationMode.getValueForUser(LOCATION_MODE_OFF, userId) != LOCATION_MODE_OFF; |
| 135 | } |
| 136 | |
| 137 | /** |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 138 | * Add a listener for changes to the location enabled setting. Callbacks occur on an unspecified |
| 139 | * thread. |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 140 | */ |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 141 | public void addOnLocationEnabledChangedListener(UserSettingChangedListener listener) { |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 142 | mLocationMode.addListener(listener); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Remove a listener for changes to the location enabled setting. |
| 147 | */ |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 148 | public void removeOnLocationEnabledChangedListener(UserSettingChangedListener listener) { |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 149 | mLocationMode.addListener(listener); |
| 150 | } |
| 151 | |
| 152 | /** |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 153 | * Retrieve the background throttle interval. |
| 154 | */ |
| 155 | public long getBackgroundThrottleIntervalMs() { |
| 156 | return mBackgroundThrottleIntervalMs.getValue(DEFAULT_BACKGROUND_THROTTLE_INTERVAL_MS); |
| 157 | } |
| 158 | |
| 159 | /** |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 160 | * Add a listener for changes to the background throttle interval. Callbacks occur on an |
| 161 | * unspecified thread. |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 162 | */ |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 163 | public void addOnBackgroundThrottleIntervalChangedListener( |
| 164 | GlobalSettingChangedListener listener) { |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 165 | mBackgroundThrottleIntervalMs.addListener(listener); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Remove a listener for changes to the background throttle interval. |
| 170 | */ |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 171 | public void removeOnBackgroundThrottleIntervalChangedListener( |
| 172 | GlobalSettingChangedListener listener) { |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 173 | mBackgroundThrottleIntervalMs.removeListener(listener); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Check if the given package is blacklisted for location access. |
| 178 | */ |
| 179 | public boolean isLocationPackageBlacklisted(int userId, String packageName) { |
| 180 | List<String> locationPackageBlacklist = mLocationPackageBlacklist.getValueForUser(userId); |
| 181 | if (locationPackageBlacklist.isEmpty()) { |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | List<String> locationPackageWhitelist = mLocationPackageWhitelist.getValueForUser(userId); |
| 186 | for (String locationWhitelistPackage : locationPackageWhitelist) { |
| 187 | if (packageName.startsWith(locationWhitelistPackage)) { |
| 188 | return false; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | for (String locationBlacklistPackage : locationPackageBlacklist) { |
| 193 | if (packageName.startsWith(locationBlacklistPackage)) { |
| 194 | return true; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Retrieve the background throttle package whitelist. |
| 203 | */ |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 204 | public Set<String> getBackgroundThrottlePackageWhitelist() { |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 205 | return mBackgroundThrottlePackageWhitelist.getValue(); |
| 206 | } |
| 207 | |
| 208 | /** |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 209 | * Add a listener for changes to the background throttle package whitelist. Callbacks occur on |
| 210 | * an unspecified thread. |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 211 | */ |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 212 | public void addOnBackgroundThrottlePackageWhitelistChangedListener( |
| 213 | GlobalSettingChangedListener listener) { |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 214 | mBackgroundThrottlePackageWhitelist.addListener(listener); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Remove a listener for changes to the background throttle package whitelist. |
| 219 | */ |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 220 | public void removeOnBackgroundThrottlePackageWhitelistChangedListener( |
| 221 | GlobalSettingChangedListener listener) { |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 222 | mBackgroundThrottlePackageWhitelist.removeListener(listener); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Retrieve the ignore settings package whitelist. |
| 227 | */ |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 228 | public Set<String> getIgnoreSettingsPackageWhitelist() { |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 229 | return mIgnoreSettingsPackageWhitelist.getValue(); |
| 230 | } |
| 231 | |
| 232 | /** |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 233 | * Add a listener for changes to the ignore settings package whitelist. Callbacks occur on an |
| 234 | * unspecified thread. |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 235 | */ |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 236 | public void addOnIgnoreSettingsPackageWhitelistChangedListener( |
| 237 | GlobalSettingChangedListener listener) { |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 238 | mIgnoreSettingsPackageWhitelist.addListener(listener); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Remove a listener for changes to the ignore settings package whitelist. |
| 243 | */ |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 244 | public void removeOnIgnoreSettingsPackageWhitelistChangedListener( |
| 245 | GlobalSettingChangedListener listener) { |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 246 | mIgnoreSettingsPackageWhitelist.removeListener(listener); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Retrieve the background throttling proximity alert interval. |
| 251 | */ |
| 252 | public long getBackgroundThrottleProximityAlertIntervalMs() { |
| 253 | return Settings.Global.getLong(mContext.getContentResolver(), |
| 254 | LOCATION_BACKGROUND_THROTTLE_PROXIMITY_ALERT_INTERVAL_MS, |
| 255 | DEFAULT_BACKGROUND_THROTTLE_PROXIMITY_ALERT_INTERVAL_MS); |
| 256 | } |
| 257 | |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 258 | /** |
| 259 | * Retrieve maximum age of the last location. |
| 260 | */ |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 261 | public long getMaxLastLocationAgeMs() { |
| 262 | return Settings.Global.getLong( |
| 263 | mContext.getContentResolver(), |
| 264 | LOCATION_LAST_LOCATION_MAX_AGE_MILLIS, |
| 265 | DEFAULT_MAX_LAST_LOCATION_AGE_MS); |
| 266 | } |
| 267 | |
| 268 | /** |
Soonil Nagarkar | 8df02f4 | 2020-01-08 13:23:26 -0800 | [diff] [blame] | 269 | * Set a value for the deprecated LOCATION_PROVIDERS_ALLOWED setting. This is used purely for |
| 270 | * backwards compatibility for old clients, and may be removed in the future. |
| 271 | */ |
| 272 | public void setLocationProviderAllowed(String provider, boolean enabled, int userId) { |
| 273 | // fused and passive provider never get public updates for legacy reasons |
| 274 | if (FUSED_PROVIDER.equals(provider) || PASSIVE_PROVIDER.equals(provider)) { |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | long identity = Binder.clearCallingIdentity(); |
| 279 | try { |
| 280 | // update LOCATION_PROVIDERS_ALLOWED for best effort backwards compatibility |
| 281 | Settings.Secure.putStringForUser( |
| 282 | mContext.getContentResolver(), |
| 283 | Settings.Secure.LOCATION_PROVIDERS_ALLOWED, |
| 284 | (enabled ? "+" : "-") + provider, |
| 285 | userId); |
| 286 | } finally { |
| 287 | Binder.restoreCallingIdentity(identity); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /** |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 292 | * Dump info for debugging. |
| 293 | */ |
| 294 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 295 | IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " "); |
| 296 | int userId = ActivityManager.getCurrentUser(); |
| 297 | |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 298 | ipw.print("Location Enabled: "); |
| 299 | ipw.println(isLocationEnabled(userId)); |
| 300 | |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 301 | List<String> locationPackageBlacklist = mLocationPackageBlacklist.getValueForUser(userId); |
| 302 | if (!locationPackageBlacklist.isEmpty()) { |
| 303 | ipw.println("Location Blacklisted Packages:"); |
| 304 | ipw.increaseIndent(); |
| 305 | for (String packageName : locationPackageBlacklist) { |
| 306 | ipw.println(packageName); |
| 307 | } |
| 308 | ipw.decreaseIndent(); |
| 309 | |
| 310 | List<String> locationPackageWhitelist = mLocationPackageWhitelist.getValueForUser( |
| 311 | userId); |
| 312 | if (!locationPackageWhitelist.isEmpty()) { |
| 313 | ipw.println("Location Whitelisted Packages:"); |
| 314 | ipw.increaseIndent(); |
| 315 | for (String packageName : locationPackageWhitelist) { |
| 316 | ipw.println(packageName); |
| 317 | } |
| 318 | ipw.decreaseIndent(); |
| 319 | } |
| 320 | } |
| 321 | |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 322 | Set<String> backgroundThrottlePackageWhitelist = |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 323 | mBackgroundThrottlePackageWhitelist.getValue(); |
| 324 | if (!backgroundThrottlePackageWhitelist.isEmpty()) { |
| 325 | ipw.println("Throttling Whitelisted Packages:"); |
| 326 | ipw.increaseIndent(); |
| 327 | for (String packageName : backgroundThrottlePackageWhitelist) { |
| 328 | ipw.println(packageName); |
| 329 | } |
| 330 | ipw.decreaseIndent(); |
| 331 | } |
| 332 | |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 333 | Set<String> ignoreSettingsPackageWhitelist = mIgnoreSettingsPackageWhitelist.getValue(); |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 334 | if (!ignoreSettingsPackageWhitelist.isEmpty()) { |
| 335 | ipw.println("Bypass Whitelisted Packages:"); |
| 336 | ipw.increaseIndent(); |
| 337 | for (String packageName : ignoreSettingsPackageWhitelist) { |
| 338 | ipw.println(packageName); |
| 339 | } |
| 340 | ipw.decreaseIndent(); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | private abstract static class ObservingSetting extends ContentObserver { |
| 345 | |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 346 | private final CopyOnWriteArrayList<UserSettingChangedListener> mListeners; |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 347 | |
| 348 | @GuardedBy("this") |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 349 | private boolean mRegistered; |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 350 | |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 351 | private ObservingSetting(Handler handler) { |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 352 | super(handler); |
| 353 | mListeners = new CopyOnWriteArrayList<>(); |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 354 | } |
| 355 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 356 | protected synchronized boolean isRegistered() { |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 357 | return mRegistered; |
| 358 | } |
| 359 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 360 | protected synchronized void register(Context context, Uri uri) { |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 361 | if (mRegistered) { |
| 362 | return; |
| 363 | } |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 364 | |
| 365 | context.getContentResolver().registerContentObserver( |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 366 | uri, false, this, UserHandle.USER_ALL); |
| 367 | mRegistered = true; |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 370 | public void addListener(UserSettingChangedListener listener) { |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 371 | mListeners.add(listener); |
| 372 | } |
| 373 | |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 374 | public void removeListener(UserSettingChangedListener listener) { |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 375 | mListeners.remove(listener); |
| 376 | } |
| 377 | |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 378 | @Override |
| 379 | public void onChange(boolean selfChange, Uri uri, int userId) { |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 380 | for (UserSettingChangedListener listener : mListeners) { |
| 381 | listener.onSettingChanged(userId); |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | private static class IntegerSecureSetting extends ObservingSetting { |
| 387 | |
| 388 | private final Context mContext; |
| 389 | private final String mSettingName; |
| 390 | |
| 391 | private IntegerSecureSetting(Context context, String settingName, Handler handler) { |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 392 | super(handler); |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 393 | mContext = context; |
| 394 | mSettingName = settingName; |
| 395 | } |
| 396 | |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 397 | private void register() { |
| 398 | register(mContext, Settings.Secure.getUriFor(mSettingName)); |
| 399 | } |
| 400 | |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 401 | public int getValueForUser(int defaultValue, int userId) { |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 402 | long identity = Binder.clearCallingIdentity(); |
| 403 | try { |
| 404 | return Settings.Secure.getIntForUser(mContext.getContentResolver(), mSettingName, |
| 405 | defaultValue, userId); |
| 406 | } finally { |
| 407 | Binder.restoreCallingIdentity(identity); |
| 408 | } |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 409 | } |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | private static class StringListCachedSecureSetting extends ObservingSetting { |
| 413 | |
| 414 | private final Context mContext; |
| 415 | private final String mSettingName; |
| 416 | |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 417 | @GuardedBy("this") |
| 418 | private int mCachedUserId; |
| 419 | @GuardedBy("this") |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 420 | private List<String> mCachedValue; |
| 421 | |
| 422 | private StringListCachedSecureSetting(Context context, String settingName, |
| 423 | Handler handler) { |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 424 | super(handler); |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 425 | mContext = context; |
| 426 | mSettingName = settingName; |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 427 | |
| 428 | mCachedUserId = UserHandle.USER_NULL; |
| 429 | } |
| 430 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 431 | public void register() { |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 432 | register(mContext, Settings.Secure.getUriFor(mSettingName)); |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | public synchronized List<String> getValueForUser(int userId) { |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 436 | Preconditions.checkArgument(userId != UserHandle.USER_NULL); |
| 437 | |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 438 | List<String> value = mCachedValue; |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 439 | if (userId != mCachedUserId) { |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 440 | long identity = Binder.clearCallingIdentity(); |
| 441 | try { |
| 442 | String setting = Settings.Secure.getStringForUser(mContext.getContentResolver(), |
| 443 | mSettingName, userId); |
| 444 | if (TextUtils.isEmpty(setting)) { |
| 445 | value = Collections.emptyList(); |
| 446 | } else { |
| 447 | value = Arrays.asList(setting.split(",")); |
| 448 | } |
| 449 | } finally { |
| 450 | Binder.restoreCallingIdentity(identity); |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 451 | } |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 452 | |
| 453 | if (isRegistered()) { |
| 454 | mCachedUserId = userId; |
| 455 | mCachedValue = value; |
| 456 | } |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 459 | return value; |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | public synchronized void invalidateForUser(int userId) { |
| 463 | if (mCachedUserId == userId) { |
| 464 | mCachedUserId = UserHandle.USER_NULL; |
| 465 | mCachedValue = null; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | @Override |
| 470 | public void onChange(boolean selfChange, Uri uri, int userId) { |
| 471 | invalidateForUser(userId); |
| 472 | super.onChange(selfChange, uri, userId); |
| 473 | } |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | private static class LongGlobalSetting extends ObservingSetting { |
| 477 | |
| 478 | private final Context mContext; |
| 479 | private final String mSettingName; |
| 480 | |
| 481 | private LongGlobalSetting(Context context, String settingName, Handler handler) { |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 482 | super(handler); |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 483 | mContext = context; |
| 484 | mSettingName = settingName; |
| 485 | } |
| 486 | |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 487 | public void register() { |
| 488 | register(mContext, Settings.Global.getUriFor(mSettingName)); |
| 489 | } |
| 490 | |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 491 | public long getValue(long defaultValue) { |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 492 | long identity = Binder.clearCallingIdentity(); |
| 493 | try { |
| 494 | return Settings.Global.getLong(mContext.getContentResolver(), mSettingName, |
| 495 | defaultValue); |
| 496 | } finally { |
| 497 | Binder.restoreCallingIdentity(identity); |
| 498 | } |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 499 | } |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 502 | private static class StringSetCachedGlobalSetting extends ObservingSetting { |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 503 | |
| 504 | private final Context mContext; |
| 505 | private final String mSettingName; |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 506 | private final Supplier<ArraySet<String>> mBaseValuesSupplier; |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 507 | |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 508 | @GuardedBy("this") |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 509 | private boolean mValid; |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 510 | @GuardedBy("this") |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 511 | private ArraySet<String> mCachedValue; |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 512 | |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 513 | private StringSetCachedGlobalSetting(Context context, String settingName, |
| 514 | Supplier<ArraySet<String>> baseValuesSupplier, Handler handler) { |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 515 | super(handler); |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 516 | mContext = context; |
| 517 | mSettingName = settingName; |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 518 | mBaseValuesSupplier = baseValuesSupplier; |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 519 | |
| 520 | mValid = false; |
| 521 | } |
| 522 | |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 523 | public void register() { |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 524 | register(mContext, Settings.Global.getUriFor(mSettingName)); |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Soonil Nagarkar | 6bf751f | 2019-11-27 13:41:28 -0800 | [diff] [blame] | 527 | public synchronized Set<String> getValue() { |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 528 | ArraySet<String> value = mCachedValue; |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 529 | if (!mValid) { |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 530 | long identity = Binder.clearCallingIdentity(); |
| 531 | try { |
| 532 | value = new ArraySet<>(mBaseValuesSupplier.get()); |
| 533 | String setting = Settings.Global.getString(mContext.getContentResolver(), |
| 534 | mSettingName); |
| 535 | if (!TextUtils.isEmpty(setting)) { |
| 536 | value.addAll(Arrays.asList(setting.split(","))); |
| 537 | } |
| 538 | } finally { |
| 539 | Binder.restoreCallingIdentity(identity); |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 540 | } |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 541 | |
| 542 | if (isRegistered()) { |
| 543 | mValid = true; |
| 544 | mCachedValue = value; |
| 545 | } |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 548 | return value; |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | public synchronized void invalidate() { |
| 552 | mValid = false; |
| 553 | mCachedValue = null; |
| 554 | } |
| 555 | |
| 556 | @Override |
| 557 | public void onChange(boolean selfChange, Uri uri, int userId) { |
| 558 | invalidate(); |
| 559 | super.onChange(selfChange, uri, userId); |
| 560 | } |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 561 | } |
| 562 | } |