Adam Lesinski | 0debc9a | 2014-07-16 19:09:13 -0700 | [diff] [blame] | 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 | |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 20 | import android.annotation.RequiresPermission; |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 21 | import android.annotation.SystemApi; |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 22 | import android.annotation.SystemService; |
Adam Lesinski | 0debc9a | 2014-07-16 19:09:13 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 24 | import android.content.pm.ParceledListSlice; |
Adam Lesinski | 0debc9a | 2014-07-16 19:09:13 -0700 | [diff] [blame] | 25 | import android.os.RemoteException; |
Amith Yamasani | cf76872 | 2015-04-23 20:36:41 -0700 | [diff] [blame] | 26 | import android.os.UserHandle; |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 27 | import android.util.ArrayMap; |
Adam Lesinski | 0debc9a | 2014-07-16 19:09:13 -0700 | [diff] [blame] | 28 | |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 29 | import java.lang.annotation.Retention; |
| 30 | import java.lang.annotation.RetentionPolicy; |
Suprabh Shukla | 868bde2 | 2018-02-20 20:59:52 -0800 | [diff] [blame] | 31 | import java.util.ArrayList; |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 32 | import java.util.Collections; |
| 33 | import java.util.List; |
Adam Lesinski | cc562a8 | 2014-08-27 11:52:52 -0700 | [diff] [blame] | 34 | import java.util.Map; |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Provides access to device usage history and statistics. Usage data is aggregated into |
| 38 | * time intervals: days, weeks, months, and years. |
| 39 | * <p /> |
| 40 | * When requesting usage data since a particular time, the request might look something like this: |
| 41 | * <pre> |
| 42 | * PAST REQUEST_TIME TODAY FUTURE |
| 43 | * ————————————————————————————||———————————————————————————¦-----------------------| |
| 44 | * YEAR || ¦ | |
| 45 | * ————————————————————————————||———————————————————————————¦-----------------------| |
| 46 | * MONTH | || MONTH ¦ | |
| 47 | * ——————————————————|—————————||———————————————————————————¦-----------------------| |
| 48 | * | WEEK | WEEK|| | WEEK | WE¦EK | WEEK | |
| 49 | * ————————————————————————————||———————————————————|———————¦-----------------------| |
| 50 | * || |DAY|DAY|DAY|DAY¦DAY|DAY|DAY|DAY|DAY|DAY| |
| 51 | * ————————————————————————————||———————————————————————————¦-----------------------| |
| 52 | * </pre> |
| 53 | * A request for data in the middle of a time interval will include that interval. |
| 54 | * <p/> |
Esteban Talavera | fa96231 | 2017-10-09 14:58:28 +0100 | [diff] [blame] | 55 | * <b>NOTE:</b> This API requires the permission android.permission.PACKAGE_USAGE_STATS. |
| 56 | * However, declaring the permission implies intention to use the API and the user of the device |
| 57 | * still needs to grant permission through the Settings application. |
| 58 | * See {@link android.provider.Settings#ACTION_USAGE_ACCESS_SETTINGS} |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 59 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 60 | @SystemService(Context.USAGE_STATS_SERVICE) |
Adam Lesinski | 0debc9a | 2014-07-16 19:09:13 -0700 | [diff] [blame] | 61 | public final class UsageStatsManager { |
Adam Lesinski | 0debc9a | 2014-07-16 19:09:13 -0700 | [diff] [blame] | 62 | |
| 63 | /** |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 64 | * An interval type that spans a day. See {@link #queryUsageStats(int, long, long)}. |
Adam Lesinski | 0debc9a | 2014-07-16 19:09:13 -0700 | [diff] [blame] | 65 | */ |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 66 | public static final int INTERVAL_DAILY = 0; |
Adam Lesinski | 0debc9a | 2014-07-16 19:09:13 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 69 | * An interval type that spans a week. See {@link #queryUsageStats(int, long, long)}. |
Adam Lesinski | 0debc9a | 2014-07-16 19:09:13 -0700 | [diff] [blame] | 70 | */ |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 71 | public static final int INTERVAL_WEEKLY = 1; |
Adam Lesinski | 0debc9a | 2014-07-16 19:09:13 -0700 | [diff] [blame] | 72 | |
| 73 | /** |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 74 | * An interval type that spans a month. See {@link #queryUsageStats(int, long, long)}. |
Adam Lesinski | 0debc9a | 2014-07-16 19:09:13 -0700 | [diff] [blame] | 75 | */ |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 76 | public static final int INTERVAL_MONTHLY = 2; |
Adam Lesinski | 0debc9a | 2014-07-16 19:09:13 -0700 | [diff] [blame] | 77 | |
| 78 | /** |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 79 | * An interval type that spans a year. See {@link #queryUsageStats(int, long, long)}. |
| 80 | */ |
| 81 | public static final int INTERVAL_YEARLY = 3; |
| 82 | |
| 83 | /** |
| 84 | * An interval type that will use the best fit interval for the given time range. |
| 85 | * See {@link #queryUsageStats(int, long, long)}. |
| 86 | */ |
| 87 | public static final int INTERVAL_BEST = 4; |
| 88 | |
| 89 | /** |
| 90 | * The number of available intervals. Does not include {@link #INTERVAL_BEST}, since it |
| 91 | * is a pseudo interval (it actually selects a real interval). |
Adam Lesinski | 0debc9a | 2014-07-16 19:09:13 -0700 | [diff] [blame] | 92 | * {@hide} |
| 93 | */ |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 94 | public static final int INTERVAL_COUNT = 4; |
| 95 | |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 96 | |
| 97 | /** |
| 98 | * The app is whitelisted for some reason and the bucket cannot be changed. |
| 99 | * {@hide} |
| 100 | */ |
| 101 | @SystemApi |
| 102 | public static final int STANDBY_BUCKET_EXEMPTED = 5; |
| 103 | |
| 104 | /** |
| 105 | * The app was used very recently, currently in use or likely to be used very soon. |
| 106 | * @see #getAppStandbyBucket() |
| 107 | */ |
| 108 | public static final int STANDBY_BUCKET_ACTIVE = 10; |
| 109 | |
| 110 | /** |
| 111 | * The app was used recently and/or likely to be used in the next few hours. |
| 112 | * @see #getAppStandbyBucket() |
| 113 | */ |
| 114 | public static final int STANDBY_BUCKET_WORKING_SET = 20; |
| 115 | |
| 116 | /** |
| 117 | * The app was used in the last few days and/or likely to be used in the next few days. |
| 118 | * @see #getAppStandbyBucket() |
| 119 | */ |
| 120 | public static final int STANDBY_BUCKET_FREQUENT = 30; |
| 121 | |
| 122 | /** |
| 123 | * The app has not be used for several days and/or is unlikely to be used for several days. |
| 124 | * @see #getAppStandbyBucket() |
| 125 | */ |
| 126 | public static final int STANDBY_BUCKET_RARE = 40; |
| 127 | |
| 128 | /** |
| 129 | * The app has never been used. |
| 130 | * {@hide} |
| 131 | */ |
| 132 | @SystemApi |
| 133 | public static final int STANDBY_BUCKET_NEVER = 50; |
| 134 | |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 135 | /** @hide */ |
| 136 | public static final int REASON_MAIN_MASK = 0xFF00; |
| 137 | /** @hide */ |
| 138 | public static final int REASON_MAIN_DEFAULT = 0x0100; |
| 139 | /** @hide */ |
| 140 | public static final int REASON_MAIN_TIMEOUT = 0x0200; |
| 141 | /** @hide */ |
| 142 | public static final int REASON_MAIN_USAGE = 0x0300; |
| 143 | /** @hide */ |
| 144 | public static final int REASON_MAIN_FORCED = 0x0400; |
| 145 | /** @hide */ |
| 146 | public static final int REASON_MAIN_PREDICTED = 0x0500; |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 147 | |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 148 | /** @hide */ |
| 149 | public static final int REASON_SUB_MASK = 0x00FF; |
| 150 | /** @hide */ |
| 151 | public static final int REASON_SUB_USAGE_SYSTEM_INTERACTION = 0x0001; |
| 152 | /** @hide */ |
| 153 | public static final int REASON_SUB_USAGE_NOTIFICATION_SEEN = 0x0002; |
| 154 | /** @hide */ |
| 155 | public static final int REASON_SUB_USAGE_USER_INTERACTION = 0x0003; |
| 156 | /** @hide */ |
| 157 | public static final int REASON_SUB_USAGE_MOVE_TO_FOREGROUND = 0x0004; |
| 158 | /** @hide */ |
| 159 | public static final int REASON_SUB_USAGE_MOVE_TO_BACKGROUND = 0x0005; |
| 160 | /** @hide */ |
| 161 | public static final int REASON_SUB_USAGE_SYSTEM_UPDATE = 0x0006; |
| 162 | /** @hide */ |
| 163 | public static final int REASON_SUB_USAGE_ACTIVE_TIMEOUT = 0x0007; |
| 164 | /** @hide */ |
| 165 | public static final int REASON_SUB_USAGE_SYNC_ADAPTER = 0x0008; |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 166 | |
| 167 | /** @hide */ |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 168 | @IntDef(flag = false, prefix = { "STANDBY_BUCKET_" }, value = { |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 169 | STANDBY_BUCKET_EXEMPTED, |
| 170 | STANDBY_BUCKET_ACTIVE, |
| 171 | STANDBY_BUCKET_WORKING_SET, |
| 172 | STANDBY_BUCKET_FREQUENT, |
| 173 | STANDBY_BUCKET_RARE, |
| 174 | STANDBY_BUCKET_NEVER, |
| 175 | }) |
| 176 | @Retention(RetentionPolicy.SOURCE) |
| 177 | public @interface StandbyBuckets {} |
| 178 | |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 179 | private static final UsageEvents sEmptyResults = new UsageEvents(); |
Adam Lesinski | 0debc9a | 2014-07-16 19:09:13 -0700 | [diff] [blame] | 180 | |
| 181 | private final Context mContext; |
| 182 | private final IUsageStatsManager mService; |
| 183 | |
| 184 | /** |
| 185 | * {@hide} |
| 186 | */ |
| 187 | public UsageStatsManager(Context context, IUsageStatsManager service) { |
| 188 | mContext = context; |
| 189 | mService = service; |
| 190 | } |
| 191 | |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 192 | /** |
| 193 | * Gets application usage stats for the given time range, aggregated by the specified interval. |
| 194 | * <p>The returned list will contain a {@link UsageStats} object for each package that |
| 195 | * has data for an interval that is a subset of the time range given. To illustrate:</p> |
| 196 | * <pre> |
| 197 | * intervalType = INTERVAL_YEARLY |
| 198 | * beginTime = 2013 |
| 199 | * endTime = 2015 (exclusive) |
| 200 | * |
| 201 | * Results: |
| 202 | * 2013 - com.example.alpha |
| 203 | * 2013 - com.example.beta |
| 204 | * 2014 - com.example.alpha |
| 205 | * 2014 - com.example.beta |
| 206 | * 2014 - com.example.charlie |
| 207 | * </pre> |
| 208 | * |
| 209 | * @param intervalType The time interval by which the stats are aggregated. |
| 210 | * @param beginTime The inclusive beginning of the range of stats to include in the results. |
| 211 | * @param endTime The exclusive end of the range of stats to include in the results. |
Esteban Talavera | fa96231 | 2017-10-09 14:58:28 +0100 | [diff] [blame] | 212 | * @return A list of {@link UsageStats} |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 213 | * |
| 214 | * @see #INTERVAL_DAILY |
| 215 | * @see #INTERVAL_WEEKLY |
| 216 | * @see #INTERVAL_MONTHLY |
| 217 | * @see #INTERVAL_YEARLY |
| 218 | * @see #INTERVAL_BEST |
| 219 | */ |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 220 | public List<UsageStats> queryUsageStats(int intervalType, long beginTime, long endTime) { |
Adam Lesinski | 0debc9a | 2014-07-16 19:09:13 -0700 | [diff] [blame] | 221 | try { |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 222 | @SuppressWarnings("unchecked") |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 223 | ParceledListSlice<UsageStats> slice = mService.queryUsageStats(intervalType, beginTime, |
| 224 | endTime, mContext.getOpPackageName()); |
| 225 | if (slice != null) { |
| 226 | return slice.getList(); |
Adam Lesinski | 0debc9a | 2014-07-16 19:09:13 -0700 | [diff] [blame] | 227 | } |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 228 | } catch (RemoteException e) { |
Esteban Talavera | fa96231 | 2017-10-09 14:58:28 +0100 | [diff] [blame] | 229 | // fallthrough and return the empty list. |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 230 | } |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 231 | return Collections.emptyList(); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Gets the hardware configurations the device was in for the given time range, aggregated by |
| 236 | * the specified interval. The results are ordered as in |
| 237 | * {@link #queryUsageStats(int, long, long)}. |
| 238 | * |
| 239 | * @param intervalType The time interval by which the stats are aggregated. |
| 240 | * @param beginTime The inclusive beginning of the range of stats to include in the results. |
| 241 | * @param endTime The exclusive end of the range of stats to include in the results. |
Esteban Talavera | fa96231 | 2017-10-09 14:58:28 +0100 | [diff] [blame] | 242 | * @return A list of {@link ConfigurationStats} |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 243 | */ |
| 244 | public List<ConfigurationStats> queryConfigurations(int intervalType, long beginTime, |
| 245 | long endTime) { |
| 246 | try { |
| 247 | @SuppressWarnings("unchecked") |
| 248 | ParceledListSlice<ConfigurationStats> slice = mService.queryConfigurationStats( |
| 249 | intervalType, beginTime, endTime, mContext.getOpPackageName()); |
| 250 | if (slice != null) { |
| 251 | return slice.getList(); |
| 252 | } |
| 253 | } catch (RemoteException e) { |
| 254 | // fallthrough and return the empty list. |
| 255 | } |
| 256 | return Collections.emptyList(); |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 257 | } |
Adam Lesinski | 0debc9a | 2014-07-16 19:09:13 -0700 | [diff] [blame] | 258 | |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 259 | /** |
| 260 | * Query for events in the given time range. Events are only kept by the system for a few |
| 261 | * days. |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 262 | * |
| 263 | * @param beginTime The inclusive beginning of the range of events to include in the results. |
| 264 | * @param endTime The exclusive end of the range of events to include in the results. |
| 265 | * @return A {@link UsageEvents}. |
| 266 | */ |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 267 | public UsageEvents queryEvents(long beginTime, long endTime) { |
| 268 | try { |
| 269 | UsageEvents iter = mService.queryEvents(beginTime, endTime, |
| 270 | mContext.getOpPackageName()); |
| 271 | if (iter != null) { |
| 272 | return iter; |
| 273 | } |
| 274 | } catch (RemoteException e) { |
Esteban Talavera | fa96231 | 2017-10-09 14:58:28 +0100 | [diff] [blame] | 275 | // fallthrough and return empty result. |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 276 | } |
| 277 | return sEmptyResults; |
| 278 | } |
Adam Lesinski | 0debc9a | 2014-07-16 19:09:13 -0700 | [diff] [blame] | 279 | |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 280 | /** |
| 281 | * A convenience method that queries for all stats in the given range (using the best interval |
| 282 | * for that range), merges the resulting data, and keys it by package name. |
| 283 | * See {@link #queryUsageStats(int, long, long)}. |
| 284 | * |
| 285 | * @param beginTime The inclusive beginning of the range of stats to include in the results. |
| 286 | * @param endTime The exclusive end of the range of stats to include in the results. |
Esteban Talavera | fa96231 | 2017-10-09 14:58:28 +0100 | [diff] [blame] | 287 | * @return A {@link java.util.Map} keyed by package name |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 288 | */ |
Adam Lesinski | cc562a8 | 2014-08-27 11:52:52 -0700 | [diff] [blame] | 289 | public Map<String, UsageStats> queryAndAggregateUsageStats(long beginTime, long endTime) { |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 290 | List<UsageStats> stats = queryUsageStats(INTERVAL_BEST, beginTime, endTime); |
| 291 | if (stats.isEmpty()) { |
Adam Lesinski | cc562a8 | 2014-08-27 11:52:52 -0700 | [diff] [blame] | 292 | return Collections.emptyMap(); |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 293 | } |
Adam Lesinski | 0debc9a | 2014-07-16 19:09:13 -0700 | [diff] [blame] | 294 | |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 295 | ArrayMap<String, UsageStats> aggregatedStats = new ArrayMap<>(); |
| 296 | final int statCount = stats.size(); |
| 297 | for (int i = 0; i < statCount; i++) { |
| 298 | UsageStats newStat = stats.get(i); |
| 299 | UsageStats existingStat = aggregatedStats.get(newStat.getPackageName()); |
| 300 | if (existingStat == null) { |
| 301 | aggregatedStats.put(newStat.mPackageName, newStat); |
| 302 | } else { |
| 303 | existingStat.add(newStat); |
Adam Lesinski | 0debc9a | 2014-07-16 19:09:13 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | return aggregatedStats; |
| 307 | } |
Amith Yamasani | cf76872 | 2015-04-23 20:36:41 -0700 | [diff] [blame] | 308 | |
| 309 | /** |
Amith Yamasani | e5f3304 | 2015-05-08 13:20:22 -0700 | [diff] [blame] | 310 | * Returns whether the specified app is currently considered inactive. This will be true if the |
Amith Yamasani | cf76872 | 2015-04-23 20:36:41 -0700 | [diff] [blame] | 311 | * app hasn't been used directly or indirectly for a period of time defined by the system. This |
| 312 | * could be of the order of several hours or days. |
| 313 | * @param packageName The package name of the app to query |
Amith Yamasani | e5f3304 | 2015-05-08 13:20:22 -0700 | [diff] [blame] | 314 | * @return whether the app is currently considered inactive |
Amith Yamasani | cf76872 | 2015-04-23 20:36:41 -0700 | [diff] [blame] | 315 | */ |
Amith Yamasani | e5f3304 | 2015-05-08 13:20:22 -0700 | [diff] [blame] | 316 | public boolean isAppInactive(String packageName) { |
Amith Yamasani | cf76872 | 2015-04-23 20:36:41 -0700 | [diff] [blame] | 317 | try { |
Jeff Sharkey | ad357d1 | 2018-02-02 13:25:31 -0700 | [diff] [blame] | 318 | return mService.isAppInactive(packageName, mContext.getUserId()); |
Amith Yamasani | cf76872 | 2015-04-23 20:36:41 -0700 | [diff] [blame] | 319 | } catch (RemoteException e) { |
| 320 | // fall through and return default |
| 321 | } |
| 322 | return false; |
| 323 | } |
Amith Yamasani | 901e924 | 2015-05-13 18:21:09 -0700 | [diff] [blame] | 324 | |
| 325 | /** |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 326 | * {@hide} |
Amith Yamasani | 901e924 | 2015-05-13 18:21:09 -0700 | [diff] [blame] | 327 | */ |
| 328 | public void setAppInactive(String packageName, boolean inactive) { |
| 329 | try { |
Jeff Sharkey | ad357d1 | 2018-02-02 13:25:31 -0700 | [diff] [blame] | 330 | mService.setAppInactive(packageName, inactive, mContext.getUserId()); |
Amith Yamasani | 901e924 | 2015-05-13 18:21:09 -0700 | [diff] [blame] | 331 | } catch (RemoteException e) { |
| 332 | // fall through |
| 333 | } |
| 334 | } |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 335 | |
| 336 | /** |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 337 | * Returns the current standby bucket of the calling app. The system determines the standby |
| 338 | * state of the app based on app usage patterns. Standby buckets determine how much an app will |
| 339 | * be restricted from running background tasks such as jobs, alarms and certain PendingIntent |
| 340 | * callbacks. |
Amith Yamasani | e878931 | 2017-12-10 14:34:26 -0800 | [diff] [blame] | 341 | * <p>Restrictions increase progressively from {@link #STANDBY_BUCKET_ACTIVE} to |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 342 | * {@link #STANDBY_BUCKET_RARE}, with {@link #STANDBY_BUCKET_ACTIVE} being the least |
| 343 | * restrictive. The battery level of the device might also affect the restrictions. |
| 344 | * |
Amith Yamasani | e878931 | 2017-12-10 14:34:26 -0800 | [diff] [blame] | 345 | * @return the current standby bucket of the calling app. One of STANDBY_BUCKET_* constants. |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 346 | */ |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 347 | public @StandbyBuckets int getAppStandbyBucket() { |
| 348 | try { |
| 349 | return mService.getAppStandbyBucket(mContext.getOpPackageName(), |
| 350 | mContext.getOpPackageName(), |
| 351 | mContext.getUserId()); |
| 352 | } catch (RemoteException e) { |
| 353 | } |
| 354 | return STANDBY_BUCKET_ACTIVE; |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * {@hide} |
| 359 | * Returns the current standby bucket of the specified app. The caller must hold the permission |
| 360 | * android.permission.PACKAGE_USAGE_STATS. |
| 361 | * @param packageName the package for which to fetch the current standby bucket. |
| 362 | */ |
| 363 | @SystemApi |
| 364 | @RequiresPermission(android.Manifest.permission.PACKAGE_USAGE_STATS) |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 365 | public @StandbyBuckets int getAppStandbyBucket(String packageName) { |
| 366 | try { |
| 367 | return mService.getAppStandbyBucket(packageName, mContext.getOpPackageName(), |
| 368 | mContext.getUserId()); |
| 369 | } catch (RemoteException e) { |
| 370 | } |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 371 | return STANDBY_BUCKET_ACTIVE; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /** |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 375 | * {@hide} |
Amith Yamasani | e878931 | 2017-12-10 14:34:26 -0800 | [diff] [blame] | 376 | * Changes an app's standby bucket to the provided value. The caller can only set the standby |
| 377 | * bucket for a different app than itself. |
| 378 | * @param packageName the package name of the app to set the bucket for. A SecurityException |
| 379 | * will be thrown if the package name is that of the caller. |
| 380 | * @param bucket the standby bucket to set it to, which should be one of STANDBY_BUCKET_*. |
| 381 | * Setting a standby bucket outside of the range of STANDBY_BUCKET_ACTIVE to |
| 382 | * STANDBY_BUCKET_NEVER will result in a SecurityException. |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 383 | */ |
Amith Yamasani | 4470ab9 | 2017-10-31 13:29:00 -0700 | [diff] [blame] | 384 | @SystemApi |
| 385 | @RequiresPermission(android.Manifest.permission.CHANGE_APP_IDLE_STATE) |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 386 | public void setAppStandbyBucket(String packageName, @StandbyBuckets int bucket) { |
| 387 | try { |
| 388 | mService.setAppStandbyBucket(packageName, bucket, mContext.getUserId()); |
| 389 | } catch (RemoteException e) { |
| 390 | // Nothing to do |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | /** |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 395 | * {@hide} |
Amith Yamasani | e878931 | 2017-12-10 14:34:26 -0800 | [diff] [blame] | 396 | * Returns the current standby bucket of every app that has a bucket assigned to it. |
| 397 | * The caller must hold the permission android.permission.PACKAGE_USAGE_STATS. The key of the |
| 398 | * returned Map is the package name and the value is the bucket assigned to the package. |
| 399 | * @see #getAppStandbyBucket() |
| 400 | */ |
| 401 | @SystemApi |
| 402 | @RequiresPermission(android.Manifest.permission.PACKAGE_USAGE_STATS) |
| 403 | public Map<String, Integer> getAppStandbyBuckets() { |
| 404 | try { |
Suprabh Shukla | 868bde2 | 2018-02-20 20:59:52 -0800 | [diff] [blame] | 405 | final ParceledListSlice<AppStandbyInfo> slice = mService.getAppStandbyBuckets( |
Amith Yamasani | e878931 | 2017-12-10 14:34:26 -0800 | [diff] [blame] | 406 | mContext.getOpPackageName(), mContext.getUserId()); |
Suprabh Shukla | 868bde2 | 2018-02-20 20:59:52 -0800 | [diff] [blame] | 407 | final List<AppStandbyInfo> bucketList = slice.getList(); |
| 408 | final ArrayMap<String, Integer> bucketMap = new ArrayMap<>(); |
| 409 | final int n = bucketList.size(); |
| 410 | for (int i = 0; i < n; i++) { |
| 411 | final AppStandbyInfo bucketInfo = bucketList.get(i); |
| 412 | bucketMap.put(bucketInfo.mPackageName, bucketInfo.mStandbyBucket); |
| 413 | } |
| 414 | return bucketMap; |
Amith Yamasani | e878931 | 2017-12-10 14:34:26 -0800 | [diff] [blame] | 415 | } catch (RemoteException e) { |
| 416 | } |
| 417 | return Collections.EMPTY_MAP; |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * {@hide} |
| 422 | * Changes the app standby bucket for multiple apps at once. The Map is keyed by the package |
| 423 | * name and the value is one of STANDBY_BUCKET_*. |
| 424 | * @param appBuckets a map of package name to bucket value. |
| 425 | */ |
| 426 | @SystemApi |
| 427 | @RequiresPermission(android.Manifest.permission.CHANGE_APP_IDLE_STATE) |
| 428 | public void setAppStandbyBuckets(Map<String, Integer> appBuckets) { |
Suprabh Shukla | 868bde2 | 2018-02-20 20:59:52 -0800 | [diff] [blame] | 429 | if (appBuckets == null) { |
| 430 | return; |
| 431 | } |
| 432 | final List<AppStandbyInfo> bucketInfoList = new ArrayList<>(appBuckets.size()); |
| 433 | for (Map.Entry<String, Integer> bucketEntry : appBuckets.entrySet()) { |
| 434 | bucketInfoList.add(new AppStandbyInfo(bucketEntry.getKey(), bucketEntry.getValue())); |
| 435 | } |
| 436 | final ParceledListSlice<AppStandbyInfo> slice = new ParceledListSlice<>(bucketInfoList); |
Amith Yamasani | e878931 | 2017-12-10 14:34:26 -0800 | [diff] [blame] | 437 | try { |
Suprabh Shukla | 868bde2 | 2018-02-20 20:59:52 -0800 | [diff] [blame] | 438 | mService.setAppStandbyBuckets(slice, mContext.getUserId()); |
Amith Yamasani | e878931 | 2017-12-10 14:34:26 -0800 | [diff] [blame] | 439 | } catch (RemoteException e) { |
| 440 | } |
| 441 | } |
| 442 | |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 443 | /** @hide */ |
| 444 | public static String reasonToString(int standbyReason) { |
| 445 | StringBuilder sb = new StringBuilder(); |
| 446 | switch (standbyReason & REASON_MAIN_MASK) { |
| 447 | case REASON_MAIN_DEFAULT: |
| 448 | sb.append("d"); |
| 449 | break; |
| 450 | case REASON_MAIN_FORCED: |
| 451 | sb.append("f"); |
| 452 | break; |
| 453 | case REASON_MAIN_PREDICTED: |
| 454 | sb.append("p"); |
| 455 | break; |
| 456 | case REASON_MAIN_TIMEOUT: |
| 457 | sb.append("t"); |
| 458 | break; |
| 459 | case REASON_MAIN_USAGE: |
| 460 | sb.append("u-"); |
| 461 | switch (standbyReason & REASON_SUB_MASK) { |
| 462 | case REASON_SUB_USAGE_SYSTEM_INTERACTION: |
| 463 | sb.append("si"); |
| 464 | break; |
| 465 | case REASON_SUB_USAGE_NOTIFICATION_SEEN: |
| 466 | sb.append("ns"); |
| 467 | break; |
| 468 | case REASON_SUB_USAGE_USER_INTERACTION: |
| 469 | sb.append("ui"); |
| 470 | break; |
| 471 | case REASON_SUB_USAGE_MOVE_TO_FOREGROUND: |
| 472 | sb.append("mf"); |
| 473 | break; |
| 474 | case REASON_SUB_USAGE_MOVE_TO_BACKGROUND: |
| 475 | sb.append("mb"); |
| 476 | break; |
| 477 | case REASON_SUB_USAGE_SYSTEM_UPDATE: |
| 478 | sb.append("su"); |
| 479 | break; |
| 480 | case REASON_SUB_USAGE_ACTIVE_TIMEOUT: |
| 481 | sb.append("at"); |
| 482 | break; |
| 483 | case REASON_SUB_USAGE_SYNC_ADAPTER: |
| 484 | sb.append("sa"); |
| 485 | break; |
| 486 | } |
| 487 | break; |
| 488 | } |
| 489 | return sb.toString(); |
| 490 | } |
| 491 | |
Amith Yamasani | e878931 | 2017-12-10 14:34:26 -0800 | [diff] [blame] | 492 | /** |
| 493 | * {@hide} |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 494 | * Temporarily whitelist the specified app for a short duration. This is to allow an app |
| 495 | * receiving a high priority message to be able to access the network and acquire wakelocks |
| 496 | * even if the device is in power-save mode or the app is currently considered inactive. |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 497 | * @param packageName The package name of the app to whitelist. |
| 498 | * @param duration Duration to whitelist the app for, in milliseconds. It is recommended that |
| 499 | * this be limited to 10s of seconds. Requested duration will be clamped to a few minutes. |
| 500 | * @param user The user for whom the package should be whitelisted. Passing in a user that is |
| 501 | * not the same as the caller's process will require the INTERACT_ACROSS_USERS permission. |
| 502 | * @see #isAppInactive(String) |
| 503 | */ |
| 504 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 505 | @RequiresPermission(android.Manifest.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST) |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 506 | public void whitelistAppTemporarily(String packageName, long duration, UserHandle user) { |
| 507 | try { |
| 508 | mService.whitelistAppTemporarily(packageName, duration, user.getIdentifier()); |
| 509 | } catch (RemoteException re) { |
| 510 | } |
| 511 | } |
Amith Yamasani | 4ec6368 | 2016-02-19 12:55:27 -0800 | [diff] [blame] | 512 | |
| 513 | /** |
| 514 | * Inform usage stats that the carrier privileged apps access rules have changed. |
| 515 | * @hide |
| 516 | */ |
| 517 | public void onCarrierPrivilegedAppsChanged() { |
| 518 | try { |
| 519 | mService.onCarrierPrivilegedAppsChanged(); |
| 520 | } catch (RemoteException re) { |
| 521 | } |
| 522 | } |
Kang Li | 53b4314 | 2016-11-14 14:38:25 -0800 | [diff] [blame] | 523 | |
| 524 | /** |
| 525 | * Reports a Chooser action to the UsageStatsManager. |
| 526 | * |
| 527 | * @param packageName The package name of the app that is selected. |
| 528 | * @param userId The user id of who makes the selection. |
| 529 | * @param contentType The type of the content, e.g., Image, Video, App. |
| 530 | * @param annotations The annotations of the content, e.g., Game, Selfie. |
| 531 | * @param action The action type of Intent that invokes ChooserActivity. |
| 532 | * {@link UsageEvents} |
| 533 | * @hide |
| 534 | */ |
| 535 | public void reportChooserSelection(String packageName, int userId, String contentType, |
| 536 | String[] annotations, String action) { |
| 537 | try { |
| 538 | mService.reportChooserSelection(packageName, userId, contentType, annotations, action); |
| 539 | } catch (RemoteException re) { |
| 540 | } |
| 541 | } |
Adam Lesinski | 0debc9a | 2014-07-16 19:09:13 -0700 | [diff] [blame] | 542 | } |