10debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski/** 20debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * Copyright (C) 2014 The Android Open Source Project 30debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * 40debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * Licensed under the Apache License, Version 2.0 (the "License"); you may not 50debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * use this file except in compliance with the License. You may obtain a copy 60debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * of the License at 70debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * 80debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * http://www.apache.org/licenses/LICENSE-2.0 90debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * 100debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * Unless required by applicable law or agreed to in writing, software 110debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 120debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 130debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * License for the specific language governing permissions and limitations 140debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * under the License. 150debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski */ 160debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 170debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinskipackage android.app.usage; 180debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 19af575b9f8e1b59be9c8862b6a65c0dcb88145a23Amith Yamasaniimport android.annotation.SystemApi; 200debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinskiimport android.content.Context; 213516800b611a79339a3c188332d13a26e9086b09Adam Lesinskiimport android.content.pm.ParceledListSlice; 220debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinskiimport android.os.RemoteException; 23cf76872a62075abf9008e99ca08413fb70761dc2Amith Yamasaniimport android.os.UserHandle; 243516800b611a79339a3c188332d13a26e9086b09Adam Lesinskiimport android.util.ArrayMap; 250debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 263516800b611a79339a3c188332d13a26e9086b09Adam Lesinskiimport java.util.Collections; 273516800b611a79339a3c188332d13a26e9086b09Adam Lesinskiimport java.util.List; 28cc562a811da508b275254f275d6e0c1758a47d07Adam Lesinskiimport java.util.Map; 293516800b611a79339a3c188332d13a26e9086b09Adam Lesinski 303516800b611a79339a3c188332d13a26e9086b09Adam Lesinski/** 313516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * Provides access to device usage history and statistics. Usage data is aggregated into 323516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * time intervals: days, weeks, months, and years. 333516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * <p /> 343516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * When requesting usage data since a particular time, the request might look something like this: 353516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * <pre> 363516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * PAST REQUEST_TIME TODAY FUTURE 373516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * ————————————————————————————||———————————————————————————¦-----------------------| 383516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * YEAR || ¦ | 393516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * ————————————————————————————||———————————————————————————¦-----------------------| 403516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * MONTH | || MONTH ¦ | 413516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * ——————————————————|—————————||———————————————————————————¦-----------------------| 423516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * | WEEK | WEEK|| | WEEK | WE¦EK | WEEK | 433516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * ————————————————————————————||———————————————————|———————¦-----------------------| 443516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * || |DAY|DAY|DAY|DAY¦DAY|DAY|DAY|DAY|DAY|DAY| 453516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * ————————————————————————————||———————————————————————————¦-----------------------| 463516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * </pre> 473516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * A request for data in the middle of a time interval will include that interval. 483516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * <p/> 493516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * <b>NOTE:</b> This API requires the permission android.permission.PACKAGE_USAGE_STATS, which 503516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * is a system-level permission and will not be granted to third-party apps. However, declaring 513516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * the permission implies intention to use the API and the user of the device can grant permission 523516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * through the Settings application. 533516800b611a79339a3c188332d13a26e9086b09Adam Lesinski */ 540debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinskipublic final class UsageStatsManager { 553516800b611a79339a3c188332d13a26e9086b09Adam Lesinski 560debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski /** 573516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * An interval type that spans a day. See {@link #queryUsageStats(int, long, long)}. 580debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski */ 593516800b611a79339a3c188332d13a26e9086b09Adam Lesinski public static final int INTERVAL_DAILY = 0; 600debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 610debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski /** 623516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * An interval type that spans a week. See {@link #queryUsageStats(int, long, long)}. 630debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski */ 643516800b611a79339a3c188332d13a26e9086b09Adam Lesinski public static final int INTERVAL_WEEKLY = 1; 650debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 660debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski /** 673516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * An interval type that spans a month. See {@link #queryUsageStats(int, long, long)}. 680debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski */ 693516800b611a79339a3c188332d13a26e9086b09Adam Lesinski public static final int INTERVAL_MONTHLY = 2; 700debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 710debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski /** 723516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * An interval type that spans a year. See {@link #queryUsageStats(int, long, long)}. 730debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski */ 743516800b611a79339a3c188332d13a26e9086b09Adam Lesinski public static final int INTERVAL_YEARLY = 3; 750debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 760debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski /** 773516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * An interval type that will use the best fit interval for the given time range. 783516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * See {@link #queryUsageStats(int, long, long)}. 793516800b611a79339a3c188332d13a26e9086b09Adam Lesinski */ 803516800b611a79339a3c188332d13a26e9086b09Adam Lesinski public static final int INTERVAL_BEST = 4; 813516800b611a79339a3c188332d13a26e9086b09Adam Lesinski 823516800b611a79339a3c188332d13a26e9086b09Adam Lesinski /** 833516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * The number of available intervals. Does not include {@link #INTERVAL_BEST}, since it 843516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * is a pseudo interval (it actually selects a real interval). 850debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * {@hide} 860debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski */ 873516800b611a79339a3c188332d13a26e9086b09Adam Lesinski public static final int INTERVAL_COUNT = 4; 883516800b611a79339a3c188332d13a26e9086b09Adam Lesinski 893516800b611a79339a3c188332d13a26e9086b09Adam Lesinski private static final UsageEvents sEmptyResults = new UsageEvents(); 900debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 910debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski private final Context mContext; 920debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski private final IUsageStatsManager mService; 930debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 940debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski /** 950debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * {@hide} 960debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski */ 970debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski public UsageStatsManager(Context context, IUsageStatsManager service) { 980debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski mContext = context; 990debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski mService = service; 1000debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski } 1010debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 1023516800b611a79339a3c188332d13a26e9086b09Adam Lesinski /** 1033516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * Gets application usage stats for the given time range, aggregated by the specified interval. 1043516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * <p>The returned list will contain a {@link UsageStats} object for each package that 1053516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * has data for an interval that is a subset of the time range given. To illustrate:</p> 1063516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * <pre> 1073516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * intervalType = INTERVAL_YEARLY 1083516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * beginTime = 2013 1093516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * endTime = 2015 (exclusive) 1103516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * 1113516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * Results: 1123516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * 2013 - com.example.alpha 1133516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * 2013 - com.example.beta 1143516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * 2014 - com.example.alpha 1153516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * 2014 - com.example.beta 1163516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * 2014 - com.example.charlie 1173516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * </pre> 1183516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * 1193516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * @param intervalType The time interval by which the stats are aggregated. 1203516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * @param beginTime The inclusive beginning of the range of stats to include in the results. 1213516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * @param endTime The exclusive end of the range of stats to include in the results. 1223516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * @return A list of {@link UsageStats} or null if none are available. 1233516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * 1243516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * @see #INTERVAL_DAILY 1253516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * @see #INTERVAL_WEEKLY 1263516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * @see #INTERVAL_MONTHLY 1273516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * @see #INTERVAL_YEARLY 1283516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * @see #INTERVAL_BEST 1293516800b611a79339a3c188332d13a26e9086b09Adam Lesinski */ 1303516800b611a79339a3c188332d13a26e9086b09Adam Lesinski public List<UsageStats> queryUsageStats(int intervalType, long beginTime, long endTime) { 1310debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski try { 1327f61e96db7c90c1f4418359672aa4656aebee500Adam Lesinski @SuppressWarnings("unchecked") 1333516800b611a79339a3c188332d13a26e9086b09Adam Lesinski ParceledListSlice<UsageStats> slice = mService.queryUsageStats(intervalType, beginTime, 1343516800b611a79339a3c188332d13a26e9086b09Adam Lesinski endTime, mContext.getOpPackageName()); 1353516800b611a79339a3c188332d13a26e9086b09Adam Lesinski if (slice != null) { 1363516800b611a79339a3c188332d13a26e9086b09Adam Lesinski return slice.getList(); 1373516800b611a79339a3c188332d13a26e9086b09Adam Lesinski } 1380debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski } catch (RemoteException e) { 1393516800b611a79339a3c188332d13a26e9086b09Adam Lesinski // fallthrough and return null. 1400debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski } 1417f61e96db7c90c1f4418359672aa4656aebee500Adam Lesinski return Collections.emptyList(); 1427f61e96db7c90c1f4418359672aa4656aebee500Adam Lesinski } 1437f61e96db7c90c1f4418359672aa4656aebee500Adam Lesinski 1447f61e96db7c90c1f4418359672aa4656aebee500Adam Lesinski /** 1457f61e96db7c90c1f4418359672aa4656aebee500Adam Lesinski * Gets the hardware configurations the device was in for the given time range, aggregated by 1467f61e96db7c90c1f4418359672aa4656aebee500Adam Lesinski * the specified interval. The results are ordered as in 1477f61e96db7c90c1f4418359672aa4656aebee500Adam Lesinski * {@link #queryUsageStats(int, long, long)}. 1487f61e96db7c90c1f4418359672aa4656aebee500Adam Lesinski * 1497f61e96db7c90c1f4418359672aa4656aebee500Adam Lesinski * @param intervalType The time interval by which the stats are aggregated. 1507f61e96db7c90c1f4418359672aa4656aebee500Adam Lesinski * @param beginTime The inclusive beginning of the range of stats to include in the results. 1517f61e96db7c90c1f4418359672aa4656aebee500Adam Lesinski * @param endTime The exclusive end of the range of stats to include in the results. 1527f61e96db7c90c1f4418359672aa4656aebee500Adam Lesinski * @return A list of {@link ConfigurationStats} or null if none are available. 1537f61e96db7c90c1f4418359672aa4656aebee500Adam Lesinski */ 1547f61e96db7c90c1f4418359672aa4656aebee500Adam Lesinski public List<ConfigurationStats> queryConfigurations(int intervalType, long beginTime, 1557f61e96db7c90c1f4418359672aa4656aebee500Adam Lesinski long endTime) { 1567f61e96db7c90c1f4418359672aa4656aebee500Adam Lesinski try { 1577f61e96db7c90c1f4418359672aa4656aebee500Adam Lesinski @SuppressWarnings("unchecked") 1587f61e96db7c90c1f4418359672aa4656aebee500Adam Lesinski ParceledListSlice<ConfigurationStats> slice = mService.queryConfigurationStats( 1597f61e96db7c90c1f4418359672aa4656aebee500Adam Lesinski intervalType, beginTime, endTime, mContext.getOpPackageName()); 1607f61e96db7c90c1f4418359672aa4656aebee500Adam Lesinski if (slice != null) { 1617f61e96db7c90c1f4418359672aa4656aebee500Adam Lesinski return slice.getList(); 1627f61e96db7c90c1f4418359672aa4656aebee500Adam Lesinski } 1637f61e96db7c90c1f4418359672aa4656aebee500Adam Lesinski } catch (RemoteException e) { 1647f61e96db7c90c1f4418359672aa4656aebee500Adam Lesinski // fallthrough and return the empty list. 1657f61e96db7c90c1f4418359672aa4656aebee500Adam Lesinski } 1667f61e96db7c90c1f4418359672aa4656aebee500Adam Lesinski return Collections.emptyList(); 1670debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski } 1680debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 1693516800b611a79339a3c188332d13a26e9086b09Adam Lesinski /** 1703516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * Query for events in the given time range. Events are only kept by the system for a few 1713516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * days. 1723516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * <p /> 1733516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * <b>NOTE:</b> The last few minutes of the event log will be truncated to prevent abuse 1743516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * by applications. 1753516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * 1763516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * @param beginTime The inclusive beginning of the range of events to include in the results. 1773516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * @param endTime The exclusive end of the range of events to include in the results. 1783516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * @return A {@link UsageEvents}. 1793516800b611a79339a3c188332d13a26e9086b09Adam Lesinski */ 1803516800b611a79339a3c188332d13a26e9086b09Adam Lesinski public UsageEvents queryEvents(long beginTime, long endTime) { 1810debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski try { 1823516800b611a79339a3c188332d13a26e9086b09Adam Lesinski UsageEvents iter = mService.queryEvents(beginTime, endTime, 1833516800b611a79339a3c188332d13a26e9086b09Adam Lesinski mContext.getOpPackageName()); 1843516800b611a79339a3c188332d13a26e9086b09Adam Lesinski if (iter != null) { 1853516800b611a79339a3c188332d13a26e9086b09Adam Lesinski return iter; 1863516800b611a79339a3c188332d13a26e9086b09Adam Lesinski } 1870debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski } catch (RemoteException e) { 1883516800b611a79339a3c188332d13a26e9086b09Adam Lesinski // fallthrough and return null 1890debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski } 1903516800b611a79339a3c188332d13a26e9086b09Adam Lesinski return sEmptyResults; 1910debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski } 1920debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 1933516800b611a79339a3c188332d13a26e9086b09Adam Lesinski /** 1943516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * A convenience method that queries for all stats in the given range (using the best interval 1953516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * for that range), merges the resulting data, and keys it by package name. 1963516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * See {@link #queryUsageStats(int, long, long)}. 1973516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * 1983516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * @param beginTime The inclusive beginning of the range of stats to include in the results. 1993516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * @param endTime The exclusive end of the range of stats to include in the results. 200cc562a811da508b275254f275d6e0c1758a47d07Adam Lesinski * @return A {@link java.util.Map} keyed by package name, or null if no stats are 2013516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * available. 2023516800b611a79339a3c188332d13a26e9086b09Adam Lesinski */ 203cc562a811da508b275254f275d6e0c1758a47d07Adam Lesinski public Map<String, UsageStats> queryAndAggregateUsageStats(long beginTime, long endTime) { 2043516800b611a79339a3c188332d13a26e9086b09Adam Lesinski List<UsageStats> stats = queryUsageStats(INTERVAL_BEST, beginTime, endTime); 2053516800b611a79339a3c188332d13a26e9086b09Adam Lesinski if (stats.isEmpty()) { 206cc562a811da508b275254f275d6e0c1758a47d07Adam Lesinski return Collections.emptyMap(); 2070debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski } 2080debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 2093516800b611a79339a3c188332d13a26e9086b09Adam Lesinski ArrayMap<String, UsageStats> aggregatedStats = new ArrayMap<>(); 2103516800b611a79339a3c188332d13a26e9086b09Adam Lesinski final int statCount = stats.size(); 2113516800b611a79339a3c188332d13a26e9086b09Adam Lesinski for (int i = 0; i < statCount; i++) { 2123516800b611a79339a3c188332d13a26e9086b09Adam Lesinski UsageStats newStat = stats.get(i); 2133516800b611a79339a3c188332d13a26e9086b09Adam Lesinski UsageStats existingStat = aggregatedStats.get(newStat.getPackageName()); 2143516800b611a79339a3c188332d13a26e9086b09Adam Lesinski if (existingStat == null) { 2153516800b611a79339a3c188332d13a26e9086b09Adam Lesinski aggregatedStats.put(newStat.mPackageName, newStat); 2163516800b611a79339a3c188332d13a26e9086b09Adam Lesinski } else { 2173516800b611a79339a3c188332d13a26e9086b09Adam Lesinski existingStat.add(newStat); 2180debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski } 2190debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski } 2200debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski return aggregatedStats; 2210debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski } 222cf76872a62075abf9008e99ca08413fb70761dc2Amith Yamasani 223cf76872a62075abf9008e99ca08413fb70761dc2Amith Yamasani /** 224e5f330456bdf5e138485ee117929fc4337866132Amith Yamasani * Returns whether the specified app is currently considered inactive. This will be true if the 225cf76872a62075abf9008e99ca08413fb70761dc2Amith Yamasani * app hasn't been used directly or indirectly for a period of time defined by the system. This 226cf76872a62075abf9008e99ca08413fb70761dc2Amith Yamasani * could be of the order of several hours or days. 227cf76872a62075abf9008e99ca08413fb70761dc2Amith Yamasani * @param packageName The package name of the app to query 228e5f330456bdf5e138485ee117929fc4337866132Amith Yamasani * @return whether the app is currently considered inactive 229cf76872a62075abf9008e99ca08413fb70761dc2Amith Yamasani */ 230e5f330456bdf5e138485ee117929fc4337866132Amith Yamasani public boolean isAppInactive(String packageName) { 231cf76872a62075abf9008e99ca08413fb70761dc2Amith Yamasani try { 232e5f330456bdf5e138485ee117929fc4337866132Amith Yamasani return mService.isAppInactive(packageName, UserHandle.myUserId()); 233cf76872a62075abf9008e99ca08413fb70761dc2Amith Yamasani } catch (RemoteException e) { 234cf76872a62075abf9008e99ca08413fb70761dc2Amith Yamasani // fall through and return default 235cf76872a62075abf9008e99ca08413fb70761dc2Amith Yamasani } 236cf76872a62075abf9008e99ca08413fb70761dc2Amith Yamasani return false; 237cf76872a62075abf9008e99ca08413fb70761dc2Amith Yamasani } 238901e924392dd79c6d1b61c3bb92461585f7b3204Amith Yamasani 239901e924392dd79c6d1b61c3bb92461585f7b3204Amith Yamasani /** 240901e924392dd79c6d1b61c3bb92461585f7b3204Amith Yamasani * @hide 241901e924392dd79c6d1b61c3bb92461585f7b3204Amith Yamasani */ 242901e924392dd79c6d1b61c3bb92461585f7b3204Amith Yamasani public void setAppInactive(String packageName, boolean inactive) { 243901e924392dd79c6d1b61c3bb92461585f7b3204Amith Yamasani try { 244901e924392dd79c6d1b61c3bb92461585f7b3204Amith Yamasani mService.setAppInactive(packageName, inactive, UserHandle.myUserId()); 245901e924392dd79c6d1b61c3bb92461585f7b3204Amith Yamasani } catch (RemoteException e) { 246901e924392dd79c6d1b61c3bb92461585f7b3204Amith Yamasani // fall through 247901e924392dd79c6d1b61c3bb92461585f7b3204Amith Yamasani } 248901e924392dd79c6d1b61c3bb92461585f7b3204Amith Yamasani } 249af575b9f8e1b59be9c8862b6a65c0dcb88145a23Amith Yamasani 250af575b9f8e1b59be9c8862b6a65c0dcb88145a23Amith Yamasani /** 251af575b9f8e1b59be9c8862b6a65c0dcb88145a23Amith Yamasani * {@hide} 252af575b9f8e1b59be9c8862b6a65c0dcb88145a23Amith Yamasani * Temporarily whitelist the specified app for a short duration. This is to allow an app 253af575b9f8e1b59be9c8862b6a65c0dcb88145a23Amith Yamasani * receiving a high priority message to be able to access the network and acquire wakelocks 254af575b9f8e1b59be9c8862b6a65c0dcb88145a23Amith Yamasani * even if the device is in power-save mode or the app is currently considered inactive. 255af575b9f8e1b59be9c8862b6a65c0dcb88145a23Amith Yamasani * The caller must hold the CHANGE_DEVICE_IDLE_TEMP_WHITELIST permission. 256af575b9f8e1b59be9c8862b6a65c0dcb88145a23Amith Yamasani * @param packageName The package name of the app to whitelist. 257af575b9f8e1b59be9c8862b6a65c0dcb88145a23Amith Yamasani * @param duration Duration to whitelist the app for, in milliseconds. It is recommended that 258af575b9f8e1b59be9c8862b6a65c0dcb88145a23Amith Yamasani * this be limited to 10s of seconds. Requested duration will be clamped to a few minutes. 259af575b9f8e1b59be9c8862b6a65c0dcb88145a23Amith Yamasani * @param user The user for whom the package should be whitelisted. Passing in a user that is 260af575b9f8e1b59be9c8862b6a65c0dcb88145a23Amith Yamasani * not the same as the caller's process will require the INTERACT_ACROSS_USERS permission. 261af575b9f8e1b59be9c8862b6a65c0dcb88145a23Amith Yamasani * @see #isAppInactive(String) 262af575b9f8e1b59be9c8862b6a65c0dcb88145a23Amith Yamasani */ 263af575b9f8e1b59be9c8862b6a65c0dcb88145a23Amith Yamasani @SystemApi 264af575b9f8e1b59be9c8862b6a65c0dcb88145a23Amith Yamasani public void whitelistAppTemporarily(String packageName, long duration, UserHandle user) { 265af575b9f8e1b59be9c8862b6a65c0dcb88145a23Amith Yamasani try { 266af575b9f8e1b59be9c8862b6a65c0dcb88145a23Amith Yamasani mService.whitelistAppTemporarily(packageName, duration, user.getIdentifier()); 267af575b9f8e1b59be9c8862b6a65c0dcb88145a23Amith Yamasani } catch (RemoteException re) { 268af575b9f8e1b59be9c8862b6a65c0dcb88145a23Amith Yamasani } 269af575b9f8e1b59be9c8862b6a65c0dcb88145a23Amith Yamasani } 2704ec6368f13a53906757283d50a17ed3ed43eccc4Amith Yamasani 2714ec6368f13a53906757283d50a17ed3ed43eccc4Amith Yamasani /** 2724ec6368f13a53906757283d50a17ed3ed43eccc4Amith Yamasani * Inform usage stats that the carrier privileged apps access rules have changed. 2734ec6368f13a53906757283d50a17ed3ed43eccc4Amith Yamasani * @hide 2744ec6368f13a53906757283d50a17ed3ed43eccc4Amith Yamasani */ 2754ec6368f13a53906757283d50a17ed3ed43eccc4Amith Yamasani public void onCarrierPrivilegedAppsChanged() { 2764ec6368f13a53906757283d50a17ed3ed43eccc4Amith Yamasani try { 2774ec6368f13a53906757283d50a17ed3ed43eccc4Amith Yamasani mService.onCarrierPrivilegedAppsChanged(); 2784ec6368f13a53906757283d50a17ed3ed43eccc4Amith Yamasani } catch (RemoteException re) { 2794ec6368f13a53906757283d50a17ed3ed43eccc4Amith Yamasani } 2804ec6368f13a53906757283d50a17ed3ed43eccc4Amith Yamasani } 2810debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski} 282