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 | |
| 19 | import static android.provider.Settings.Global.LOCATION_BACKGROUND_THROTTLE_INTERVAL_MS; |
| 20 | import static android.provider.Settings.Global.LOCATION_BACKGROUND_THROTTLE_PACKAGE_WHITELIST; |
| 21 | import static android.provider.Settings.Global.LOCATION_BACKGROUND_THROTTLE_PROXIMITY_ALERT_INTERVAL_MS; |
| 22 | import static android.provider.Settings.Global.LOCATION_IGNORE_SETTINGS_PACKAGE_WHITELIST; |
| 23 | import static android.provider.Settings.Global.LOCATION_LAST_LOCATION_MAX_AGE_MILLIS; |
| 24 | import static android.provider.Settings.Secure.LOCATION_MODE; |
| 25 | import static android.provider.Settings.Secure.LOCATION_MODE_OFF; |
| 26 | import static android.provider.Settings.Secure.LOCATION_PROVIDERS_ALLOWED; |
| 27 | |
| 28 | import android.app.ActivityManager; |
| 29 | import android.content.Context; |
| 30 | import android.database.ContentObserver; |
| 31 | import android.net.Uri; |
| 32 | import android.os.Handler; |
| 33 | import android.os.UserHandle; |
| 34 | import android.provider.Settings; |
| 35 | import android.text.TextUtils; |
| 36 | |
| 37 | import com.android.internal.util.IndentingPrintWriter; |
| 38 | |
| 39 | import java.io.FileDescriptor; |
| 40 | import java.io.PrintWriter; |
| 41 | import java.util.Arrays; |
| 42 | import java.util.Collections; |
| 43 | import java.util.List; |
| 44 | import java.util.concurrent.CopyOnWriteArrayList; |
| 45 | |
| 46 | /** |
| 47 | * Provides accessors and listeners for all location related settings. |
| 48 | */ |
| 49 | public class LocationSettingsStore { |
| 50 | |
| 51 | private static final String LOCATION_PACKAGE_BLACKLIST = "locationPackagePrefixBlacklist"; |
| 52 | private static final String LOCATION_PACKAGE_WHITELIST = "locationPackagePrefixWhitelist"; |
| 53 | |
| 54 | private static final long DEFAULT_BACKGROUND_THROTTLE_INTERVAL_MS = 30 * 60 * 1000; |
| 55 | private static final long DEFAULT_BACKGROUND_THROTTLE_PROXIMITY_ALERT_INTERVAL_MS = |
| 56 | 30 * 60 * 1000; |
| 57 | private static final long DEFAULT_MAX_LAST_LOCATION_AGE_MS = 20 * 60 * 1000; |
| 58 | |
| 59 | private final Context mContext; |
| 60 | |
| 61 | private final IntegerSecureSetting mLocationMode; |
| 62 | private final StringListCachedSecureSetting mLocationProvidersAllowed; |
| 63 | private final LongGlobalSetting mBackgroundThrottleIntervalMs; |
| 64 | private final StringListCachedSecureSetting mLocationPackageBlacklist; |
| 65 | private final StringListCachedSecureSetting mLocationPackageWhitelist; |
| 66 | private final StringListCachedGlobalSetting mBackgroundThrottlePackageWhitelist; |
| 67 | private final StringListCachedGlobalSetting mIgnoreSettingsPackageWhitelist; |
| 68 | |
| 69 | public LocationSettingsStore(Context context, Handler handler) { |
| 70 | mContext = context; |
| 71 | |
| 72 | mLocationMode = new IntegerSecureSetting(context, LOCATION_MODE, handler); |
| 73 | mLocationProvidersAllowed = new StringListCachedSecureSetting(context, |
| 74 | LOCATION_PROVIDERS_ALLOWED, handler); |
| 75 | mBackgroundThrottleIntervalMs = new LongGlobalSetting(context, |
| 76 | LOCATION_BACKGROUND_THROTTLE_INTERVAL_MS, handler); |
| 77 | mLocationPackageBlacklist = new StringListCachedSecureSetting(context, |
| 78 | LOCATION_PACKAGE_BLACKLIST, handler); |
| 79 | mLocationPackageWhitelist = new StringListCachedSecureSetting(context, |
| 80 | LOCATION_PACKAGE_WHITELIST, handler); |
| 81 | mBackgroundThrottlePackageWhitelist = new StringListCachedGlobalSetting(context, |
| 82 | LOCATION_BACKGROUND_THROTTLE_PACKAGE_WHITELIST, handler); |
| 83 | mIgnoreSettingsPackageWhitelist = new StringListCachedGlobalSetting(context, |
| 84 | LOCATION_IGNORE_SETTINGS_PACKAGE_WHITELIST, handler); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Retrieve if location is enabled or not. |
| 89 | */ |
| 90 | public boolean isLocationEnabled(int userId) { |
| 91 | return mLocationMode.getValueForUser(LOCATION_MODE_OFF, userId) != LOCATION_MODE_OFF; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Add a listener for changes to the location enabled setting. |
| 96 | */ |
| 97 | public void addOnLocationEnabledChangedListener(Runnable listener) { |
| 98 | mLocationMode.addListener(listener); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Remove a listener for changes to the location enabled setting. |
| 103 | */ |
| 104 | public void removeOnLocationEnabledChangedListener(Runnable listener) { |
| 105 | mLocationMode.addListener(listener); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Retrieve the currently allowed location providers. |
| 110 | */ |
| 111 | public List<String> getLocationProvidersAllowed(int userId) { |
| 112 | return mLocationProvidersAllowed.getValueForUser(userId); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Add a listener for changes to the currently allowed location providers. |
| 117 | */ |
| 118 | public void addOnLocationProvidersAllowedChangedListener(Runnable runnable) { |
| 119 | mLocationProvidersAllowed.addListener(runnable); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Remove a listener for changes to the currently allowed location providers. |
| 124 | */ |
| 125 | public void removeOnLocationProvidersAllowedChangedListener(Runnable runnable) { |
| 126 | mLocationProvidersAllowed.removeListener(runnable); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Retrieve the background throttle interval. |
| 131 | */ |
| 132 | public long getBackgroundThrottleIntervalMs() { |
| 133 | return mBackgroundThrottleIntervalMs.getValue(DEFAULT_BACKGROUND_THROTTLE_INTERVAL_MS); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Add a listener for changes to the background throttle interval. |
| 138 | */ |
| 139 | public void addOnBackgroundThrottleIntervalChangedListener(Runnable listener) { |
| 140 | mBackgroundThrottleIntervalMs.addListener(listener); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Remove a listener for changes to the background throttle interval. |
| 145 | */ |
| 146 | public void removeOnBackgroundThrottleIntervalChangedListener(Runnable listener) { |
| 147 | mBackgroundThrottleIntervalMs.removeListener(listener); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Check if the given package is blacklisted for location access. |
| 152 | */ |
| 153 | public boolean isLocationPackageBlacklisted(int userId, String packageName) { |
| 154 | List<String> locationPackageBlacklist = mLocationPackageBlacklist.getValueForUser(userId); |
| 155 | if (locationPackageBlacklist.isEmpty()) { |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | List<String> locationPackageWhitelist = mLocationPackageWhitelist.getValueForUser(userId); |
| 160 | for (String locationWhitelistPackage : locationPackageWhitelist) { |
| 161 | if (packageName.startsWith(locationWhitelistPackage)) { |
| 162 | return false; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | for (String locationBlacklistPackage : locationPackageBlacklist) { |
| 167 | if (packageName.startsWith(locationBlacklistPackage)) { |
| 168 | return true; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Retrieve the background throttle package whitelist. |
| 177 | */ |
| 178 | public List<String> getBackgroundThrottlePackageWhitelist() { |
| 179 | return mBackgroundThrottlePackageWhitelist.getValue(); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Add a listener for changes to the background throttle package whitelist. |
| 184 | */ |
| 185 | public void addOnBackgroundThrottlePackageWhitelistChangedListener(Runnable listener) { |
| 186 | mBackgroundThrottlePackageWhitelist.addListener(listener); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Remove a listener for changes to the background throttle package whitelist. |
| 191 | */ |
| 192 | public void removeOnBackgroundThrottlePackageWhitelistChangedListener(Runnable listener) { |
| 193 | mBackgroundThrottlePackageWhitelist.removeListener(listener); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Retrieve the ignore settings package whitelist. |
| 198 | */ |
| 199 | public List<String> getIgnoreSettingsPackageWhitelist() { |
| 200 | return mIgnoreSettingsPackageWhitelist.getValue(); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Add a listener for changes to the ignore settings package whitelist. |
| 205 | */ |
| 206 | public void addOnIgnoreSettingsPackageWhitelistChangedListener(Runnable listener) { |
| 207 | mIgnoreSettingsPackageWhitelist.addListener(listener); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Remove a listener for changes to the ignore settings package whitelist. |
| 212 | */ |
| 213 | public void removeOnIgnoreSettingsPackageWhitelistChangedListener(Runnable listener) { |
| 214 | mIgnoreSettingsPackageWhitelist.removeListener(listener); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Retrieve the background throttling proximity alert interval. |
| 219 | */ |
| 220 | public long getBackgroundThrottleProximityAlertIntervalMs() { |
| 221 | return Settings.Global.getLong(mContext.getContentResolver(), |
| 222 | LOCATION_BACKGROUND_THROTTLE_PROXIMITY_ALERT_INTERVAL_MS, |
| 223 | DEFAULT_BACKGROUND_THROTTLE_PROXIMITY_ALERT_INTERVAL_MS); |
| 224 | } |
| 225 | |
| 226 | public long getMaxLastLocationAgeMs() { |
| 227 | return Settings.Global.getLong( |
| 228 | mContext.getContentResolver(), |
| 229 | LOCATION_LAST_LOCATION_MAX_AGE_MILLIS, |
| 230 | DEFAULT_MAX_LAST_LOCATION_AGE_MS); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Dump info for debugging. |
| 235 | */ |
| 236 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 237 | IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " "); |
| 238 | int userId = ActivityManager.getCurrentUser(); |
| 239 | |
Soonil Nagarkar | b8466b7 | 2019-10-25 14:10:30 -0700 | [diff] [blame] | 240 | ipw.print("Location Enabled: "); |
| 241 | ipw.println(isLocationEnabled(userId)); |
| 242 | |
| 243 | ipw.print("Location Providers Allowed: "); |
| 244 | ipw.println(getLocationProvidersAllowed(userId)); |
| 245 | |
| 246 | List<String> locationPackageBlacklist = mLocationPackageBlacklist.getValueForUser(userId); |
| 247 | if (!locationPackageBlacklist.isEmpty()) { |
| 248 | ipw.println("Location Blacklisted Packages:"); |
| 249 | ipw.increaseIndent(); |
| 250 | for (String packageName : locationPackageBlacklist) { |
| 251 | ipw.println(packageName); |
| 252 | } |
| 253 | ipw.decreaseIndent(); |
| 254 | |
| 255 | List<String> locationPackageWhitelist = mLocationPackageWhitelist.getValueForUser( |
| 256 | userId); |
| 257 | if (!locationPackageWhitelist.isEmpty()) { |
| 258 | ipw.println("Location Whitelisted Packages:"); |
| 259 | ipw.increaseIndent(); |
| 260 | for (String packageName : locationPackageWhitelist) { |
| 261 | ipw.println(packageName); |
| 262 | } |
| 263 | ipw.decreaseIndent(); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | List<String> backgroundThrottlePackageWhitelist = |
| 268 | mBackgroundThrottlePackageWhitelist.getValue(); |
| 269 | if (!backgroundThrottlePackageWhitelist.isEmpty()) { |
| 270 | ipw.println("Throttling Whitelisted Packages:"); |
| 271 | ipw.increaseIndent(); |
| 272 | for (String packageName : backgroundThrottlePackageWhitelist) { |
| 273 | ipw.println(packageName); |
| 274 | } |
| 275 | ipw.decreaseIndent(); |
| 276 | } |
| 277 | |
| 278 | List<String> ignoreSettingsPackageWhitelist = mIgnoreSettingsPackageWhitelist.getValue(); |
| 279 | if (!ignoreSettingsPackageWhitelist.isEmpty()) { |
| 280 | ipw.println("Bypass Whitelisted Packages:"); |
| 281 | ipw.increaseIndent(); |
| 282 | for (String packageName : ignoreSettingsPackageWhitelist) { |
| 283 | ipw.println(packageName); |
| 284 | } |
| 285 | ipw.decreaseIndent(); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | private abstract static class ObservingSetting extends ContentObserver { |
| 290 | |
| 291 | private final CopyOnWriteArrayList<Runnable> mListeners; |
| 292 | |
| 293 | private ObservingSetting(Context context, String settingName, Handler handler) { |
| 294 | super(handler); |
| 295 | mListeners = new CopyOnWriteArrayList<>(); |
| 296 | |
| 297 | context.getContentResolver().registerContentObserver( |
| 298 | getUriFor(settingName), false, this, UserHandle.USER_ALL); |
| 299 | } |
| 300 | |
| 301 | public void addListener(Runnable listener) { |
| 302 | mListeners.add(listener); |
| 303 | } |
| 304 | |
| 305 | public void removeListener(Runnable listener) { |
| 306 | mListeners.remove(listener); |
| 307 | } |
| 308 | |
| 309 | protected abstract Uri getUriFor(String settingName); |
| 310 | |
| 311 | @Override |
| 312 | public void onChange(boolean selfChange, Uri uri, int userId) { |
| 313 | for (Runnable listener : mListeners) { |
| 314 | listener.run(); |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | private static class IntegerSecureSetting extends ObservingSetting { |
| 320 | |
| 321 | private final Context mContext; |
| 322 | private final String mSettingName; |
| 323 | |
| 324 | private IntegerSecureSetting(Context context, String settingName, Handler handler) { |
| 325 | super(context, settingName, handler); |
| 326 | mContext = context; |
| 327 | mSettingName = settingName; |
| 328 | } |
| 329 | |
| 330 | public int getValueForUser(int defaultValue, int userId) { |
| 331 | return Settings.Secure.getIntForUser(mContext.getContentResolver(), mSettingName, |
| 332 | defaultValue, userId); |
| 333 | } |
| 334 | |
| 335 | @Override |
| 336 | protected Uri getUriFor(String settingName) { |
| 337 | return Settings.Secure.getUriFor(settingName); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | private static class StringListCachedSecureSetting extends ObservingSetting { |
| 342 | |
| 343 | private final Context mContext; |
| 344 | private final String mSettingName; |
| 345 | |
| 346 | private int mCachedUserId = UserHandle.USER_NULL; |
| 347 | private List<String> mCachedValue; |
| 348 | |
| 349 | private StringListCachedSecureSetting(Context context, String settingName, |
| 350 | Handler handler) { |
| 351 | super(context, settingName, handler); |
| 352 | mContext = context; |
| 353 | mSettingName = settingName; |
| 354 | } |
| 355 | |
| 356 | public synchronized List<String> getValueForUser(int userId) { |
| 357 | if (userId != mCachedUserId) { |
| 358 | String setting = Settings.Secure.getStringForUser(mContext.getContentResolver(), |
| 359 | mSettingName, userId); |
| 360 | if (TextUtils.isEmpty(setting)) { |
| 361 | mCachedValue = Collections.emptyList(); |
| 362 | } else { |
| 363 | mCachedValue = Arrays.asList(setting.split(",")); |
| 364 | } |
| 365 | mCachedUserId = userId; |
| 366 | } |
| 367 | |
| 368 | return mCachedValue; |
| 369 | } |
| 370 | |
| 371 | public synchronized void invalidateForUser(int userId) { |
| 372 | if (mCachedUserId == userId) { |
| 373 | mCachedUserId = UserHandle.USER_NULL; |
| 374 | mCachedValue = null; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | @Override |
| 379 | public void onChange(boolean selfChange, Uri uri, int userId) { |
| 380 | invalidateForUser(userId); |
| 381 | super.onChange(selfChange, uri, userId); |
| 382 | } |
| 383 | |
| 384 | @Override |
| 385 | protected Uri getUriFor(String settingName) { |
| 386 | return Settings.Secure.getUriFor(settingName); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | private static class LongGlobalSetting extends ObservingSetting { |
| 391 | |
| 392 | private final Context mContext; |
| 393 | private final String mSettingName; |
| 394 | |
| 395 | private LongGlobalSetting(Context context, String settingName, Handler handler) { |
| 396 | super(context, settingName, handler); |
| 397 | mContext = context; |
| 398 | mSettingName = settingName; |
| 399 | } |
| 400 | |
| 401 | public long getValue(long defaultValue) { |
| 402 | return Settings.Global.getLong(mContext.getContentResolver(), mSettingName, |
| 403 | defaultValue); |
| 404 | } |
| 405 | |
| 406 | @Override |
| 407 | protected Uri getUriFor(String settingName) { |
| 408 | return Settings.Global.getUriFor(settingName); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | private static class StringListCachedGlobalSetting extends ObservingSetting { |
| 413 | |
| 414 | private final Context mContext; |
| 415 | private final String mSettingName; |
| 416 | |
| 417 | private boolean mValid; |
| 418 | private List<String> mCachedValue; |
| 419 | |
| 420 | private StringListCachedGlobalSetting(Context context, String settingName, |
| 421 | Handler handler) { |
| 422 | super(context, settingName, handler); |
| 423 | mContext = context; |
| 424 | mSettingName = settingName; |
| 425 | } |
| 426 | |
| 427 | public synchronized List<String> getValue() { |
| 428 | if (!mValid) { |
| 429 | String setting = Settings.Global.getString(mContext.getContentResolver(), |
| 430 | mSettingName); |
| 431 | if (TextUtils.isEmpty(setting)) { |
| 432 | mCachedValue = Collections.emptyList(); |
| 433 | } else { |
| 434 | mCachedValue = Arrays.asList(setting.split(",")); |
| 435 | } |
| 436 | mValid = true; |
| 437 | } |
| 438 | |
| 439 | return mCachedValue; |
| 440 | } |
| 441 | |
| 442 | public synchronized void invalidate() { |
| 443 | mValid = false; |
| 444 | mCachedValue = null; |
| 445 | } |
| 446 | |
| 447 | @Override |
| 448 | public void onChange(boolean selfChange, Uri uri, int userId) { |
| 449 | invalidate(); |
| 450 | super.onChange(selfChange, uri, userId); |
| 451 | } |
| 452 | |
| 453 | @Override |
| 454 | protected Uri getUriFor(String settingName) { |
| 455 | return Settings.Global.getUriFor(settingName); |
| 456 | } |
| 457 | } |
| 458 | } |