1 /**
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */
16 
17 package android.app.usage;
18 
19 import android.annotation.UserIdInt;
20 import android.app.usage.UsageStatsManager.StandbyBuckets;
21 import android.content.ComponentName;
22 import android.content.res.Configuration;
23 
24 import java.util.List;
25 import java.util.Set;
26 
27 /**
28  * UsageStatsManager local system service interface.
29  *
30  * {@hide} Only for use within the system server.
31  */
32 public abstract class UsageStatsManagerInternal {
33 
34     /**
35      * Reports an event to the UsageStatsManager.
36      *
37      * @param component The component for which this event occurred.
38      * @param userId The user id to which the component belongs to.
39      * @param eventType The event that occurred. Valid values can be found at
40      * {@link UsageEvents}
41      */
reportEvent(ComponentName component, @UserIdInt int userId, int eventType)42     public abstract void reportEvent(ComponentName component, @UserIdInt int userId, int eventType);
43 
44     /**
45      * Reports an event to the UsageStatsManager.
46      *
47      * @param packageName The package for which this event occurred.
48      * @param userId The user id to which the component belongs to.
49      * @param eventType The event that occurred. Valid values can be found at
50      * {@link UsageEvents}
51      */
reportEvent(String packageName, @UserIdInt int userId, int eventType)52     public abstract void reportEvent(String packageName, @UserIdInt int userId, int eventType);
53 
54     /**
55      * Reports a configuration change to the UsageStatsManager.
56      *
57      * @param config The new device configuration.
58      */
reportConfigurationChange(Configuration config, @UserIdInt int userId)59     public abstract void reportConfigurationChange(Configuration config, @UserIdInt int userId);
60 
61     /**
62      * Reports that an application has posted an interruptive notification.
63      *
64      * @param packageName The package name of the app that posted the notification
65      * @param channelId The ID of the NotificationChannel to which the notification was posted
66      * @param userId The user in which the notification was posted
67      */
reportInterruptiveNotification(String packageName, String channelId, @UserIdInt int userId)68     public abstract void reportInterruptiveNotification(String packageName, String channelId,
69             @UserIdInt int userId);
70 
71     /**
72      * Reports that an action equivalent to a ShortcutInfo is taken by the user.
73      *
74      * @param packageName The package name of the shortcut publisher
75      * @param shortcutId The ID of the shortcut in question
76      * @param userId The user in which the content provider was accessed.
77      *
78      * @see android.content.pm.ShortcutManager#reportShortcutUsed(String)
79      */
reportShortcutUsage(String packageName, String shortcutId, @UserIdInt int userId)80     public abstract void reportShortcutUsage(String packageName, String shortcutId,
81             @UserIdInt int userId);
82 
83     /**
84      * Reports that a content provider has been accessed by a foreground app.
85      * @param name The authority of the content provider
86      * @param pkgName The package name of the content provider
87      * @param userId The user in which the content provider was accessed.
88      */
reportContentProviderUsage(String name, String pkgName, @UserIdInt int userId)89     public abstract void reportContentProviderUsage(String name, String pkgName,
90             @UserIdInt int userId);
91 
92     /**
93      * Prepares the UsageStatsService for shutdown.
94      */
prepareShutdown()95     public abstract void prepareShutdown();
96 
97     /**
98      * Returns true if the app has not been used for a certain amount of time. How much time?
99      * Could be hours, could be days, who knows?
100      *
101      * @param packageName
102      * @param uidForAppId The uid of the app, which will be used for its app id
103      * @param userId
104      * @return
105      */
isAppIdle(String packageName, int uidForAppId, @UserIdInt int userId)106     public abstract boolean isAppIdle(String packageName, int uidForAppId, @UserIdInt int userId);
107 
108     /**
109      * Returns the app standby bucket that the app is currently in.  This accessor does
110      * <em>not</em> obfuscate instant apps.
111      *
112      * @param packageName
113      * @param userId
114      * @param nowElapsed The current time, in the elapsedRealtime time base
115      * @return the AppStandby bucket code the app currently resides in.  If the app is
116      *     unknown in the given user, STANDBY_BUCKET_NEVER is returned.
117      */
getAppStandbyBucket(String packageName, @UserIdInt int userId, long nowElapsed)118     @StandbyBuckets public abstract int getAppStandbyBucket(String packageName,
119             @UserIdInt int userId, long nowElapsed);
120 
121     /**
122      * Returns all of the uids for a given user where all packages associating with that uid
123      * are in the app idle state -- there are no associated apps that are not idle.  This means
124      * all of the returned uids can be safely considered app idle.
125      */
getIdleUidsForUser(@serIdInt int userId)126     public abstract int[] getIdleUidsForUser(@UserIdInt int userId);
127 
128     /**
129      * @return True if currently app idle parole mode is on.  This means all idle apps are allow to
130      * run for a short period of time.
131      */
isAppIdleParoleOn()132     public abstract boolean isAppIdleParoleOn();
133 
134     /**
135      * Sets up a listener for changes to packages being accessed.
136      * @param listener A listener within the system process.
137      */
addAppIdleStateChangeListener( AppIdleStateChangeListener listener)138     public abstract void addAppIdleStateChangeListener(
139             AppIdleStateChangeListener listener);
140 
141     /**
142      * Removes a listener that was previously added for package usage state changes.
143      * @param listener The listener within the system process to remove.
144      */
removeAppIdleStateChangeListener( AppIdleStateChangeListener listener)145     public abstract void removeAppIdleStateChangeListener(
146             AppIdleStateChangeListener listener);
147 
148     public static abstract class AppIdleStateChangeListener {
149 
150         /** Callback to inform listeners that the idle state has changed to a new bucket. */
onAppIdleStateChanged(String packageName, @UserIdInt int userId, boolean idle, int bucket, int reason)151         public abstract void onAppIdleStateChanged(String packageName, @UserIdInt int userId,
152                 boolean idle, int bucket, int reason);
153 
154         /**
155          * Callback to inform listeners that the parole state has changed. This means apps are
156          * allowed to do work even if they're idle or in a low bucket.
157          */
onParoleStateChanged(boolean isParoleOn)158         public abstract void onParoleStateChanged(boolean isParoleOn);
159 
160         /**
161          * Optional callback to inform the listener that the app has transitioned into
162          * an active state due to user interaction.
163          */
onUserInteractionStarted(String packageName, @UserIdInt int userId)164         public void onUserInteractionStarted(String packageName, @UserIdInt int userId) {
165             // No-op by default
166         }
167     }
168 
169     /**  Backup/Restore API */
getBackupPayload(@serIdInt int userId, String key)170     public abstract byte[] getBackupPayload(@UserIdInt int userId, String key);
171 
172     /**
173      * ?
174      * @param userId
175      * @param key
176      * @param payload
177      */
applyRestoredPayload(@serIdInt int userId, String key, byte[] payload)178     public abstract void applyRestoredPayload(@UserIdInt int userId, String key, byte[] payload);
179 
180     /**
181      * Called by DevicePolicyManagerService to inform that a new admin has been added.
182      *
183      * @param packageName the package in which the admin component is part of.
184      * @param userId the userId in which the admin has been added.
185      */
onActiveAdminAdded(String packageName, int userId)186     public abstract void onActiveAdminAdded(String packageName, int userId);
187 
188     /**
189      * Called by DevicePolicyManagerService to inform about the active admins in an user.
190      *
191      * @param adminApps the set of active admins in {@param userId} or null if there are none.
192      * @param userId the userId to which the admin apps belong.
193      */
setActiveAdminApps(Set<String> adminApps, int userId)194     public abstract void setActiveAdminApps(Set<String> adminApps, int userId);
195 
196     /**
197      * Called by DevicePolicyManagerService during boot to inform that admin data is loaded and
198      * pushed to UsageStatsService.
199      */
onAdminDataAvailable()200     public abstract void onAdminDataAvailable();
201 
202     /**
203      * Return usage stats.
204      *
205      * @param obfuscateInstantApps whether instant app package names need to be obfuscated in the
206      *     result.
207      */
queryUsageStatsForUser(@serIdInt int userId, int interval, long beginTime, long endTime, boolean obfuscateInstantApps)208     public abstract List<UsageStats> queryUsageStatsForUser(@UserIdInt int userId, int interval,
209             long beginTime, long endTime, boolean obfuscateInstantApps);
210 
211     /**
212      * Used to persist the last time a job was run for this app, in order to make decisions later
213      * whether a job should be deferred until later. The time passed in should be in elapsed
214      * realtime since boot.
215      * @param packageName the app that executed a job.
216      * @param userId the user associated with the job.
217      * @param elapsedRealtime the time when the job was executed, in elapsed realtime millis since
218      *                        boot.
219      */
setLastJobRunTime(String packageName, @UserIdInt int userId, long elapsedRealtime)220     public abstract void setLastJobRunTime(String packageName, @UserIdInt int userId,
221             long elapsedRealtime);
222 
223     /**
224      * Returns the time in millis since a job was executed for this app, in elapsed realtime
225      * timebase. This value can be larger than the current elapsed realtime if the job was executed
226      * before the device was rebooted. The default value is {@link Long#MAX_VALUE}.
227      * @param packageName the app you're asking about.
228      * @param userId the user associated with the job.
229      * @return the time in millis since a job was last executed for the app, provided it was
230      * indicated here before by a call to {@link #setLastJobRunTime(String, int, long)}.
231      */
getTimeSinceLastJobRun(String packageName, @UserIdInt int userId)232     public abstract long getTimeSinceLastJobRun(String packageName, @UserIdInt int userId);
233 
234     /**
235      * Report a few data points about an app's job state at the current time.
236      *
237      * @param packageName the app whose job state is being described
238      * @param userId which user the app is associated with
239      * @param numDeferredJobs the number of pending jobs that were deferred
240      *   due to bucketing policy
241      * @param timeSinceLastJobRun number of milliseconds since the last time one of
242      *   this app's jobs was executed
243      */
reportAppJobState(String packageName, @UserIdInt int userId, int numDeferredJobs, long timeSinceLastJobRun)244     public abstract void reportAppJobState(String packageName, @UserIdInt int userId,
245             int numDeferredJobs, long timeSinceLastJobRun);
246 
247     /**
248      * Report a sync is scheduled by a foreground app.
249      *
250      * @param packageName name of the package that owns the sync adapter.
251      * @param userId which user the app is associated with
252      */
reportExemptedSyncScheduled(String packageName, @UserIdInt int userId)253     public abstract void reportExemptedSyncScheduled(String packageName, @UserIdInt int userId);
254 
255     /**
256      * Report a sync that was scheduled by a foreground app is about to be executed.
257      *
258      * @param packageName name of the package that owns the sync adapter.
259      * @param userId which user the app is associated with
260      */
reportExemptedSyncStart(String packageName, @UserIdInt int userId)261     public abstract void reportExemptedSyncStart(String packageName, @UserIdInt int userId);
262 }
263