diff options
author | Tim Murray <timmurray@google.com> | 2019-11-12 18:23:50 -0800 |
---|---|---|
committer | Tim Murray <timmurray@google.com> | 2020-02-20 17:11:04 -0800 |
commit | b5e1153d03dc5e830519a21224f308248450fae4 (patch) | |
tree | 6d1c81924ddad1dd740e7ab99749b8f6168e2521 /location/java | |
parent | 58b9a11b072de9bac8a0a2c7b9673c6c587ba047 (diff) |
LocationManager: cache isLocationEnabledForUser
This method is in the hot path for all location tasks but whether
location is enabled for a particular user doesn't change that
often. Cache the result and invalidate from system_server in order to
reduce CPU consumption.
Test: boots, works, gets location
Bug: 140788621
Change-Id: I84ad2c570e7024187a728071dcbb6f72b177e17a
Diffstat (limited to 'location/java')
-rw-r--r-- | location/java/android/location/LocationManager.java | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/location/java/android/location/LocationManager.java b/location/java/android/location/LocationManager.java index 19085bff0033..6028a8a90141 100644 --- a/location/java/android/location/LocationManager.java +++ b/location/java/android/location/LocationManager.java @@ -35,6 +35,7 @@ import android.annotation.SystemService; import android.annotation.TestApi; import android.app.AlarmManager; import android.app.PendingIntent; +import android.app.PropertyInvalidatedCache; import android.compat.Compatibility; import android.compat.annotation.ChangeId; import android.compat.annotation.EnabledAfter; @@ -84,6 +85,23 @@ import java.util.function.Consumer; @RequiresFeature(PackageManager.FEATURE_LOCATION) public class LocationManager { + @GuardedBy("mLock") + private PropertyInvalidatedCache<Integer, Boolean> mLocationEnabledCache = + new PropertyInvalidatedCache<Integer, Boolean>( + 4, + CACHE_KEY_LOCATION_ENABLED_PROPERTY) { + @Override + protected Boolean recompute(Integer userHandle) { + try { + return mService.isLocationEnabledForUser(userHandle); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + }; + + private final Object mLock = new Object(); + /** * For apps targeting Android K and above, supplied {@link PendingIntent}s must be targeted to a * specific package. @@ -462,6 +480,13 @@ public class LocationManager { */ @SystemApi public boolean isLocationEnabledForUser(@NonNull UserHandle userHandle) { + synchronized (mLock) { + if (mLocationEnabledCache != null) { + return mLocationEnabledCache.query(userHandle.getIdentifier()); + } + } + + // fallback if cache is disabled try { return mService.isLocationEnabledForUser(userHandle.getIdentifier()); } catch (RemoteException e) { @@ -3107,6 +3132,29 @@ public class LocationManager { public void onLocationBatch(List<Location> locations) { execute((listener) -> listener.onLocationBatch(locations)); } + + } + } + + /** + * @hide + */ + public static final String CACHE_KEY_LOCATION_ENABLED_PROPERTY = + "cache_key.location_enabled"; + + /** + * @hide + */ + public static void invalidateLocalLocationEnabledCaches() { + PropertyInvalidatedCache.invalidateCache(CACHE_KEY_LOCATION_ENABLED_PROPERTY); + } + + /** + * @hide + */ + public void disableLocalLocationEnabledCaches() { + synchronized (mLock) { + mLocationEnabledCache = null; } } } |