diff options
| author | Collin Fijalkovich <cfijalkovich@google.com> | 2019-11-13 18:47:27 +0000 |
|---|---|---|
| committer | Collin Fijalkovich <cfijalkovich@google.com> | 2020-02-14 10:03:44 -0800 |
| commit | 24d1336f1731dd76843e549136df34ebabe08817 (patch) | |
| tree | fe5c22e4ef0c0820b5b2b6112ae8b6c25cc722b1 /core/java/android/os/PowerManager.java | |
| parent | e9a33feb4e33cede20906c8ffd3cf53730f8b930 (diff) | |
Cache PowerManager binder calls
Use PropertyInvalidatedCache to save on redundant
binder calls for isPowerSaveMode and isInteractive.
Bug: 140788621
Test: atest BatterySaverTest
Test: atest PowerManagerTest
Change-Id: I849a48eb7c901d3178097d33dce01630dbb78267
Diffstat (limited to 'core/java/android/os/PowerManager.java')
| -rw-r--r-- | core/java/android/os/PowerManager.java | 60 |
1 files changed, 50 insertions, 10 deletions
diff --git a/core/java/android/os/PowerManager.java b/core/java/android/os/PowerManager.java index 267613f0af83..a8fa6db232a2 100644 --- a/core/java/android/os/PowerManager.java +++ b/core/java/android/os/PowerManager.java @@ -26,6 +26,7 @@ import android.annotation.SdkConstant; import android.annotation.SystemApi; import android.annotation.SystemService; import android.annotation.TestApi; +import android.app.PropertyInvalidatedCache; import android.compat.annotation.UnsupportedAppUsage; import android.content.Context; import android.service.dreams.Sandman; @@ -877,6 +878,39 @@ public final class PowerManager { } } + private static final String CACHE_KEY_IS_POWER_SAVE_MODE_PROPERTY = + "cache_key.is_power_save_mode"; + + private static final String CACHE_KEY_IS_INTERACTIVE_PROPERTY = "cache_key.is_interactive"; + + private static final int MAX_CACHE_ENTRIES = 1; + + private PropertyInvalidatedCache<Void, Boolean> mPowerSaveModeCache = + new PropertyInvalidatedCache<Void, Boolean>(MAX_CACHE_ENTRIES, + CACHE_KEY_IS_POWER_SAVE_MODE_PROPERTY) { + @Override + protected Boolean recompute(Void query) { + try { + return mService.isPowerSaveMode(); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + }; + + private PropertyInvalidatedCache<Void, Boolean> mInteractiveCache = + new PropertyInvalidatedCache<Void, Boolean>(MAX_CACHE_ENTRIES, + CACHE_KEY_IS_INTERACTIVE_PROPERTY) { + @Override + protected Boolean recompute(Void query) { + try { + return mService.isInteractive(); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + }; + final Context mContext; @UnsupportedAppUsage final IPowerManager mService; @@ -1440,11 +1474,7 @@ public final class PowerManager { * @see android.content.Intent#ACTION_SCREEN_OFF */ public boolean isInteractive() { - try { - return mService.isInteractive(); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } + return mInteractiveCache.query(null); } /** @@ -1504,11 +1534,7 @@ public final class PowerManager { * @return Returns true if currently in low power mode, else false. */ public boolean isPowerSaveMode() { - try { - return mService.isPowerSaveMode(); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } + return mPowerSaveModeCache.query(null); } /** @@ -2508,4 +2534,18 @@ public final class PowerManager { }; } } + + /** + * @hide + */ + public static void invalidatePowerSaveModeCaches() { + PropertyInvalidatedCache.invalidateCache(CACHE_KEY_IS_POWER_SAVE_MODE_PROPERTY); + } + + /** + * @hide + */ + public static void invalidateIsInteractiveCaches() { + PropertyInvalidatedCache.invalidateCache(CACHE_KEY_IS_INTERACTIVE_PROPERTY); + } } |
