@Implements(value=android.app.usage.UsageStatsManager.class, minSdk=21) public class ShadowUsageStatsManager extends Object
UsageStatsManager
.Modifier and Type | Class | Description |
---|---|---|
static class |
ShadowUsageStatsManager.AppUsageLimitObserver |
App usage limit observer registered via
UsageStatsManager.registerAppUsageLimitObserver(int, String[], Duration, Duration,
PendingIntent) . |
static class |
ShadowUsageStatsManager.AppUsageObserver |
App usage observer registered via
UsageStatsManager.registerAppUsageObserver(int,
String[], long, TimeUnit, PendingIntent) . |
static class |
ShadowUsageStatsManager.EventBuilder |
Builder for constructing
UsageEvents.Event objects. |
static class |
ShadowUsageStatsManager.UsageSessionObserver |
Usage session observer registered via
UsageStatsManager#registerUsageSessionObserver(int, String[], long, TimeUnit, long, TimeUnit,
PendingIntent, PendingIntent) . |
static class |
ShadowUsageStatsManager.UsageStatsBuilder |
Builder for constructing
UsageStats objects. |
Modifier and Type | Field | Description |
---|---|---|
protected static Map<Integer,ShadowUsageStatsManager.UsageSessionObserver> |
usageSessionObserversById |
Constructor | Description |
---|---|
ShadowUsageStatsManager() |
Modifier and Type | Method | Description |
---|---|---|
void |
addEvent(UsageEvents.Event event) |
Adds an event to be returned by
UsageStatsManager.queryEvents(long, long) . |
void |
addEvent(String packageName,
long timeStamp,
int eventType) |
Deprecated.
Use
addEvent(Event) and ShadowUsageStatsManager.EventBuilder instead. |
void |
addUsageStats(int intervalType,
UsageStats stats) |
Adds an aggregated
UsageStats object, to be returned by queryUsageStats(int, long, long) . |
protected int |
getAppStandbyBucket() |
Returns the current app's standby bucket that is set by
setCurrentAppStandbyBucket . |
int |
getAppStandbyBucket(String packageName) |
Returns the current standby bucket of the specified app that is set by
setAppStandbyBucket . |
Map<String,Integer> |
getAppStandbyBuckets() |
|
com.google.common.collect.ImmutableList<ShadowUsageStatsManager.AppUsageLimitObserver> |
getRegisteredAppUsageLimitObservers() |
Returns the
ShadowUsageStatsManager.AppUsageLimitObserver s currently registered in UsageStatsManager . |
Collection<ShadowUsageStatsManager.AppUsageObserver> |
getRegisteredAppUsageObservers() |
Returns the
ShadowUsageStatsManager.AppUsageObserver s currently registered in UsageStatsManager . |
List<ShadowUsageStatsManager.UsageSessionObserver> |
getRegisteredUsageSessionObservers() |
Returns the
ShadowUsageStatsManager.UsageSessionObserver s currently registered in UsageStatsManager . |
protected int |
getUsageSource() |
|
protected UsageEvents |
queryEvents(long beginTime,
long endTime) |
|
protected List<UsageStats> |
queryUsageStats(int intervalType,
long beginTime,
long endTime) |
Returns aggregated UsageStats added by calling
addUsageStats(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 on
callbackIntent when
the sum of usages of apps and tokens in observedEntities exceeds timeLimit -
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 bucket
|
void |
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 registered
ShadowUsageStatsManager.AppUsageLimitObserver with observerId . |
void |
triggerRegisteredAppUsageObserver(int observerId,
long timeUsedInMillis) |
Triggers a currently registered
ShadowUsageStatsManager.AppUsageObserver with observerId . |
void |
triggerRegisteredSessionEndedObserver(int observerId) |
Triggers a currently registered
ShadowUsageStatsManager.UsageSessionObserver with observerId . |
void |
triggerRegisteredSessionStepObserver(int observerId,
long timeUsedInMillis) |
Triggers a currently registered
ShadowUsageStatsManager.UsageSessionObserver with observerId . |
protected void |
unregisterAppUsageLimitObserver(int observerId) |
Unregisters the app usage limit observer specified by
observerId . |
protected void |
unregisterAppUsageObserver(int observerId) |
|
protected void |
unregisterUsageSessionObserver(int observerId) |
protected static final Map<Integer,ShadowUsageStatsManager.UsageSessionObserver> usageSessionObserversById
@Implementation protected UsageEvents queryEvents(long beginTime, long endTime)
@Deprecated public void addEvent(String packageName, long timeStamp, int eventType)
addEvent(Event)
and ShadowUsageStatsManager.EventBuilder
instead.UsageStatsManager.queryEvents(long, long)
.
This method won't affect the results of queryUsageStats(int, long, long)
method.
public void addEvent(UsageEvents.Event event)
UsageStatsManager.queryEvents(long, long)
.
This method won't affect the results of queryUsageStats(int, long, long)
method.
The UsageEvents.Event
can be built by ShadowUsageStatsManager.EventBuilder
.
public void simulateTimeChange(long offsetToAddInMillis)
This method won't affect the results of queryUsageStats(int, long, long)
method.
offsetToAddInMillis
- the offset to be applied to all events. For example, if
offsetInMillis
is 60,000, then all UsageEvents.Event
s will be shifted forward by 1 minute
(into the future). Likewise, if offsetInMillis
is -60,000, then all UsageEvents.Event
s
will be shifted backward by 1 minute (into the past).@Implementation protected List<UsageStats> queryUsageStats(int intervalType, long beginTime, long endTime)
addUsageStats(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 using addUsageStats(int, android.app.usage.UsageStats)
.
public void addUsageStats(int intervalType, UsageStats stats)
UsageStats
object, to be returned by queryUsageStats(int, long, long)
.
Construct these objects with ShadowUsageStatsManager.UsageStatsBuilder
, and set the firstTimestamp and
lastTimestamp fields to make time filtering work in queryUsageStats(int, long, long)
.intervalType
- An interval type constant, e.g. UsageStatsManager.INTERVAL_WEEKLY
.@Implementation(minSdk=28) @HiddenApi public int getAppStandbyBucket(String packageName)
setAppStandbyBucket
. If the standby bucket value has never been set, return UsageStatsManager.STANDBY_BUCKET_ACTIVE
.@Implementation(minSdk=28) @HiddenApi public Map<String,Integer> getAppStandbyBuckets()
@Implementation(minSdk=28) @HiddenApi public void setAppStandbyBucket(String packageName, int bucket)
@Implementation(minSdk=28) @HiddenApi public void setAppStandbyBuckets(Map<String,Integer> appBuckets)
@Implementation(minSdk=28) @HiddenApi protected void registerAppUsageObserver(int observerId, String[] packages, long timeLimit, TimeUnit timeUnit, PendingIntent callbackIntent)
@Implementation(minSdk=28) @HiddenApi protected void unregisterAppUsageObserver(int observerId)
public Collection<ShadowUsageStatsManager.AppUsageObserver> getRegisteredAppUsageObservers()
ShadowUsageStatsManager.AppUsageObserver
s currently registered in UsageStatsManager
.public void triggerRegisteredAppUsageObserver(int observerId, long timeUsedInMillis)
ShadowUsageStatsManager.AppUsageObserver
with observerId
.
The observer will be no longer registered afterwards.
@Implementation(minSdk=29) protected void registerUsageSessionObserver(int observerId, String[] packages, Duration sessionStepDuration, Duration thresholdTimeDuration, PendingIntent sessionStepTriggeredIntent, PendingIntent sessionEndedIntent)
@Implementation(minSdk=29) protected void unregisterUsageSessionObserver(int observerId)
public List<ShadowUsageStatsManager.UsageSessionObserver> getRegisteredUsageSessionObservers()
ShadowUsageStatsManager.UsageSessionObserver
s currently registered in UsageStatsManager
.public void triggerRegisteredSessionStepObserver(int observerId, long timeUsedInMillis)
ShadowUsageStatsManager.UsageSessionObserver
with observerId
.
The observer SHOULD be registered afterwards.
public void triggerRegisteredSessionEndedObserver(int observerId)
ShadowUsageStatsManager.UsageSessionObserver
with observerId
.
The observer SHOULD be registered afterwards.
@Implementation(minSdk=29) @HiddenApi protected void registerAppUsageLimitObserver(int observerId, String[] observedEntities, Duration timeLimit, Duration timeUsed, PendingIntent callbackIntent)
callbackIntent
when
the sum of usages of apps and tokens in observedEntities
exceeds timeLimit -
timeUsed
.@Implementation(minSdk=29) @HiddenApi protected void unregisterAppUsageLimitObserver(int observerId)
observerId
.public com.google.common.collect.ImmutableList<ShadowUsageStatsManager.AppUsageLimitObserver> getRegisteredAppUsageLimitObservers()
ShadowUsageStatsManager.AppUsageLimitObserver
s currently registered in UsageStatsManager
.public void triggerRegisteredAppUsageLimitObserver(int observerId, Duration timeUsed)
ShadowUsageStatsManager.AppUsageLimitObserver
with observerId
.
The observer will still be registered afterwards.
@Implementation(minSdk=28) protected int getAppStandbyBucket()
setCurrentAppStandbyBucket
. If
the standby bucket value has never been set, return UsageStatsManager.STANDBY_BUCKET_ACTIVE
.public void setCurrentAppStandbyBucket(int bucket)
@Implementation(minSdk=29) @HiddenApi protected int getUsageSource()
public void setUsageSource(int usageSource)
@Resetter public static void reset()