@Implements(value=android.app.usage.UsageStatsManager.class, minSdk=21) public class ShadowUsageStatsManager extends Object
Shadow of UsageStatsManager
.
Modifier and Type | Class and Description |
---|---|
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 and Description |
---|---|
protected static Map<Integer,ShadowUsageStatsManager.UsageSessionObserver> |
usageSessionObserversById |
Constructor and Description |
---|
ShadowUsageStatsManager() |
Modifier and Type | Method and Description |
---|---|
void |
addEvent(String packageName,
long timeStamp,
int eventType)
Deprecated.
Use
#addEvent(Event) and ShadowUsageStatsManager.EventBuilder instead. |
void |
addEvent(UsageEvents.Event event)
Adds an event to be returned by
UsageStatsManager.queryEvents(long, long) . |
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() |
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 |
registerAppUsageObserver(int observerId,
String[] packages,
long timeLimit,
TimeUnit timeUnit,
PendingIntent callbackIntent) |
protected void |
registerUsageSessionObserver(int observerId,
String[] packages,
long sessionStepTime,
TimeUnit sessionStepTimeUnit,
long thresholdTime,
TimeUnit thresholdTimeUnit,
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 |
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 |
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.Adds an event to be returned by UsageStatsManager.queryEvents(long, long)
.
This method won’t affect the results of queryUsageStats(int, long, long)
method.
public void addEvent(UsageEvents.Event event)
Adds an event to be returned by 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)
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.
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)
Returns aggregated UsageStats added by calling 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)
Adds an aggregated 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)
Returns the current standby bucket of the specified app that is set by 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)
Sets the standby bucket of the specified app.
@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()
Returns the ShadowUsageStatsManager.AppUsageObserver
s currently registered in UsageStatsManager
.
public void triggerRegisteredAppUsageObserver(int observerId, long timeUsedInMillis)
Triggers a currently registered ShadowUsageStatsManager.AppUsageObserver
with observerId
.
The observer will be no longer registered afterwards.
@Implementation(minSdk=10000) protected void registerUsageSessionObserver(int observerId, String[] packages, long sessionStepTime, TimeUnit sessionStepTimeUnit, long thresholdTime, TimeUnit thresholdTimeUnit, PendingIntent sessionStepTriggeredIntent, PendingIntent sessionEndedIntent)
@Implementation(minSdk=10000) protected void unregisterUsageSessionObserver(int observerId)
public List<ShadowUsageStatsManager.UsageSessionObserver> getRegisteredUsageSessionObservers()
Returns the ShadowUsageStatsManager.UsageSessionObserver
s currently registered in UsageStatsManager
.
public void triggerRegisteredSessionStepObserver(int observerId, long timeUsedInMillis)
Triggers a currently registered ShadowUsageStatsManager.UsageSessionObserver
with observerId
.
The observer SHOULD be registered afterwards.
public void triggerRegisteredSessionEndedObserver(int observerId)
Triggers a currently registered ShadowUsageStatsManager.UsageSessionObserver
with observerId
.
The observer SHOULD be registered afterwards.
@Implementation(minSdk=28) protected int getAppStandbyBucket()
Returns the current app’s standby bucket that is set by setCurrentAppStandbyBucket
. If the standby bucket value has never been set, return UsageStatsManager.STANDBY_BUCKET_ACTIVE
.
public void setCurrentAppStandbyBucket(int bucket)
Sets the current app’s standby bucket
@Implementation(minSdk=10000) @HiddenApi protected int getUsageSource()
public void setUsageSource(int usageSource)
Sets what app usage observers will consider the source of usage for an activity.
@Resetter public static void reset()