Class ShadowUsageStatsManager
UsageStatsManager
.-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
App usage limit observer registered viaUsageStatsManager.registerAppUsageLimitObserver(int, String[], Duration, Duration, PendingIntent)
.static class
App usage observer registered viaUsageStatsManager.registerAppUsageObserver(int, String[], long, TimeUnit, PendingIntent)
.static class
Builder for constructingUsageEvents.Event
objects.static class
Usage session observer registered viaUsageStatsManager#registerUsageSessionObserver(int, String[], long, TimeUnit, long, TimeUnit, PendingIntent, PendingIntent)
.static class
Builder for constructingUsageStats
objects. -
Field Summary
Modifier and TypeFieldDescriptionprotected static final Map<Integer,
ShadowUsageStatsManager.UsageSessionObserver> -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addBroadcastResponseStats
(Object statsObject) void
addEvent
(UsageEvents.Event event) Adds an event to be returned byUsageStatsManager.queryEvents(long, long)
.void
Deprecated.void
addUsageStats
(int intervalType, UsageStats stats) Adds an aggregatedUsageStats
object, to be returned byqueryUsageStats(int, long, long)
.protected void
clearBroadcastResponseStats
(String packageName, long id) protected int
Returns the current app's standby bucket that is set bysetCurrentAppStandbyBucket
.int
getAppStandbyBucket
(String packageName) Returns the current standby bucket of the specified app that is set bysetAppStandbyBucket
.com.google.common.collect.ImmutableList<ShadowUsageStatsManager.AppUsageLimitObserver>
Returns theShadowUsageStatsManager.AppUsageLimitObserver
s currently registered inUsageStatsManager
.Returns theShadowUsageStatsManager.AppUsageObserver
s currently registered inUsageStatsManager
.Returns theShadowUsageStatsManager.UsageSessionObserver
s currently registered inUsageStatsManager
.protected int
protected Object
queryBroadcastResponseStats
(Object packageName, Object id) Requires loose signatures because return value is a list ofBroadcastResponseStats
, which is a hidden class introduced in Android T.protected UsageEvents
queryEvents
(long beginTime, long endTime) protected UsageEvents
queryEventsForSelf
(long beginTime, long endTime) protected List<UsageStats>
queryUsageStats
(int intervalType, long beginTime, long endTime) Returns aggregated UsageStats added by callingaddUsageStats(int, android.app.usage.UsageStats)
.protected void
registerAppUsageLimitObserver
(int observerId, String[] observedEntities, Duration timeLimit, Duration timeUsed, PendingIntent callbackIntent) Registers an app usage limit observer that receives a callback oncallbackIntent
when the sum of usages of apps and tokens inobservedEntities
exceedstimeLimit - timeUsed
.protected void
registerAppUsageObserver
(int observerId, String[] packages, long timeLimit, TimeUnit timeUnit, PendingIntent callbackIntent) protected void
registerUsageSessionObserver
(int observerId, String[] packages, Duration sessionStepDuration, Duration thresholdTimeDuration, PendingIntent sessionStepTriggeredIntent, PendingIntent sessionEndedIntent) static void
reset()
void
setAppStandbyBucket
(String packageName, int bucket) Sets the standby bucket of the specified app.void
setAppStandbyBuckets
(Map<String, Integer> appBuckets) void
setCurrentAppStandbyBucket
(int bucket) Sets the current app's standby bucketvoid
setUsageSource
(int usageSource) Sets what app usage observers will consider the source of usage for an activity.void
simulateTimeChange
(long offsetToAddInMillis) Simulates the operations done by the framework when there is a time change.void
triggerRegisteredAppUsageLimitObserver
(int observerId, Duration timeUsed) Triggers a currently registeredShadowUsageStatsManager.AppUsageLimitObserver
withobserverId
.void
triggerRegisteredAppUsageObserver
(int observerId, long timeUsedInMillis) Triggers a currently registeredShadowUsageStatsManager.AppUsageObserver
withobserverId
.void
triggerRegisteredSessionEndedObserver
(int observerId) Triggers a currently registeredShadowUsageStatsManager.UsageSessionObserver
withobserverId
.void
triggerRegisteredSessionStepObserver
(int observerId, long timeUsedInMillis) Triggers a currently registeredShadowUsageStatsManager.UsageSessionObserver
withobserverId
.protected void
unregisterAppUsageLimitObserver
(int observerId) Unregisters the app usage limit observer specified byobserverId
.protected void
unregisterAppUsageObserver
(int observerId) protected void
unregisterUsageSessionObserver
(int observerId)
-
Field Details
-
usageSessionObserversById
protected static final Map<Integer,ShadowUsageStatsManager.UsageSessionObserver> usageSessionObserversById
-
-
Constructor Details
-
ShadowUsageStatsManager
public ShadowUsageStatsManager()
-
-
Method Details
-
queryEvents
-
queryEventsForSelf
-
addEvent
Deprecated.UseaddEvent(Event)
andShadowUsageStatsManager.EventBuilder
instead.Adds an event to be returned byUsageStatsManager.queryEvents(long, long)
.This method won't affect the results of
queryUsageStats(int, long, long)
method. -
addEvent
Adds an event to be returned byUsageStatsManager.queryEvents(long, long)
.This method won't affect the results of
queryUsageStats(int, long, long)
method.The
UsageEvents.Event
can be built byShadowUsageStatsManager.EventBuilder
. -
simulateTimeChange
public void simulateTimeChange(long offsetToAddInMillis) Simulates the operations done by the framework when there is a time change. If the time is changed, the timestamps of all existing usage events will be shifted by the same offset as the time change, in order to make sure they remain stable relative to the new time.This method won't affect the results of
queryUsageStats(int, long, long)
method.- Parameters:
offsetToAddInMillis
- the offset to be applied to all events. For example, ifoffsetInMillis
is 60,000, then allUsageEvents.Event
s will be shifted forward by 1 minute (into the future). Likewise, ifoffsetInMillis
is -60,000, then allUsageEvents.Event
s will be shifted backward by 1 minute (into the past).
-
queryUsageStats
@Implementation protected List<UsageStats> queryUsageStats(int intervalType, long beginTime, long endTime) Returns aggregated UsageStats added by callingaddUsageStats(int, android.app.usage.UsageStats)
.The real implementation creates these aggregated objects from individual
UsageEvents.Event
. This aggregation logic is nontrivial, so the shadow implementation just returns the aggregate data added usingaddUsageStats(int, android.app.usage.UsageStats)
. -
addUsageStats
Adds an aggregatedUsageStats
object, to be returned byqueryUsageStats(int, long, long)
. Construct these objects withShadowUsageStatsManager.UsageStatsBuilder
, and set the firstTimestamp and lastTimestamp fields to make time filtering work inqueryUsageStats(int, long, long)
.- Parameters:
intervalType
- An interval type constant, e.g.UsageStatsManager.INTERVAL_WEEKLY
.
-
getAppStandbyBucket
Returns the current standby bucket of the specified app that is set bysetAppStandbyBucket
. If the standby bucket value has never been set, returnUsageStatsManager.STANDBY_BUCKET_ACTIVE
. -
getAppStandbyBuckets
-
setAppStandbyBucket
@Implementation(minSdk=28) @HiddenApi public void setAppStandbyBucket(String packageName, int bucket) Sets the standby bucket of the specified app. -
setAppStandbyBuckets
@Implementation(minSdk=28) @HiddenApi public void setAppStandbyBuckets(Map<String, Integer> appBuckets) -
registerAppUsageObserver
@Implementation(minSdk=28) @HiddenApi protected void registerAppUsageObserver(int observerId, String[] packages, long timeLimit, TimeUnit timeUnit, PendingIntent callbackIntent) -
unregisterAppUsageObserver
-
getRegisteredAppUsageObservers
Returns theShadowUsageStatsManager.AppUsageObserver
s currently registered inUsageStatsManager
. -
triggerRegisteredAppUsageObserver
public void triggerRegisteredAppUsageObserver(int observerId, long timeUsedInMillis) Triggers a currently registeredShadowUsageStatsManager.AppUsageObserver
withobserverId
.The observer will be no longer registered afterwards.
-
registerUsageSessionObserver
@Implementation(minSdk=29) protected void registerUsageSessionObserver(int observerId, String[] packages, Duration sessionStepDuration, Duration thresholdTimeDuration, PendingIntent sessionStepTriggeredIntent, PendingIntent sessionEndedIntent) -
unregisterUsageSessionObserver
-
getRegisteredUsageSessionObservers
Returns theShadowUsageStatsManager.UsageSessionObserver
s currently registered inUsageStatsManager
. -
triggerRegisteredSessionStepObserver
public void triggerRegisteredSessionStepObserver(int observerId, long timeUsedInMillis) Triggers a currently registeredShadowUsageStatsManager.UsageSessionObserver
withobserverId
.The observer SHOULD be registered afterwards.
-
triggerRegisteredSessionEndedObserver
public void triggerRegisteredSessionEndedObserver(int observerId) Triggers a currently registeredShadowUsageStatsManager.UsageSessionObserver
withobserverId
.The observer SHOULD be registered afterwards.
-
registerAppUsageLimitObserver
@Implementation(minSdk=29) @HiddenApi protected void registerAppUsageLimitObserver(int observerId, String[] observedEntities, Duration timeLimit, Duration timeUsed, PendingIntent callbackIntent) Registers an app usage limit observer that receives a callback oncallbackIntent
when the sum of usages of apps and tokens inobservedEntities
exceedstimeLimit - timeUsed
. -
unregisterAppUsageLimitObserver
@Implementation(minSdk=29) @HiddenApi protected void unregisterAppUsageLimitObserver(int observerId) Unregisters the app usage limit observer specified byobserverId
. -
getRegisteredAppUsageLimitObservers
public com.google.common.collect.ImmutableList<ShadowUsageStatsManager.AppUsageLimitObserver> getRegisteredAppUsageLimitObservers()Returns theShadowUsageStatsManager.AppUsageLimitObserver
s currently registered inUsageStatsManager
. -
triggerRegisteredAppUsageLimitObserver
Triggers a currently registeredShadowUsageStatsManager.AppUsageLimitObserver
withobserverId
.The observer will still be registered afterwards.
-
getAppStandbyBucket
Returns the current app's standby bucket that is set bysetCurrentAppStandbyBucket
. If the standby bucket value has never been set, returnUsageStatsManager.STANDBY_BUCKET_ACTIVE
. -
setCurrentAppStandbyBucket
public void setCurrentAppStandbyBucket(int bucket) Sets the current app's standby bucket -
getUsageSource
-
setUsageSource
public void setUsageSource(int usageSource) Sets what app usage observers will consider the source of usage for an activity. -
queryBroadcastResponseStats
@Implementation(minSdk=33) protected Object queryBroadcastResponseStats(Object packageName, Object id) Requires loose signatures because return value is a list ofBroadcastResponseStats
, which is a hidden class introduced in Android T. -
clearBroadcastResponseStats
-
addBroadcastResponseStats
-
reset
-
addEvent(Event)
andShadowUsageStatsManager.EventBuilder
instead.