/** * Copyright (C) 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy * of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */ package android.app.usage; import android.content.Context; import android.content.pm.ParceledListSlice; import android.os.RemoteException; import android.util.ArrayMap; import java.util.Collections; import java.util.List; import java.util.Map; /** * Provides access to device usage history and statistics. Usage data is aggregated into * time intervals: days, weeks, months, and years. *
* When requesting usage data since a particular time, the request might look something like this: ** PAST REQUEST_TIME TODAY FUTURE * ————————————————————————————||———————————————————————————¦-----------------------| * YEAR || ¦ | * ————————————————————————————||———————————————————————————¦-----------------------| * MONTH | || MONTH ¦ | * ——————————————————|—————————||———————————————————————————¦-----------------------| * | WEEK | WEEK|| | WEEK | WE¦EK | WEEK | * ————————————————————————————||———————————————————|———————¦-----------------------| * || |DAY|DAY|DAY|DAY¦DAY|DAY|DAY|DAY|DAY|DAY| * ————————————————————————————||———————————————————————————¦-----------------------| ** A request for data in the middle of a time interval will include that interval. * * NOTE: This API requires the permission android.permission.PACKAGE_USAGE_STATS, which * is a system-level permission and will not be granted to third-party apps. However, declaring * the permission implies intention to use the API and the user of the device can grant permission * through the Settings application. */ public final class UsageStatsManager { /** * An interval type that spans a day. See {@link #queryUsageStats(int, long, long)}. */ public static final int INTERVAL_DAILY = 0; /** * An interval type that spans a week. See {@link #queryUsageStats(int, long, long)}. */ public static final int INTERVAL_WEEKLY = 1; /** * An interval type that spans a month. See {@link #queryUsageStats(int, long, long)}. */ public static final int INTERVAL_MONTHLY = 2; /** * An interval type that spans a year. See {@link #queryUsageStats(int, long, long)}. */ public static final int INTERVAL_YEARLY = 3; /** * An interval type that will use the best fit interval for the given time range. * See {@link #queryUsageStats(int, long, long)}. */ public static final int INTERVAL_BEST = 4; /** * The number of available intervals. Does not include {@link #INTERVAL_BEST}, since it * is a pseudo interval (it actually selects a real interval). * {@hide} */ public static final int INTERVAL_COUNT = 4; private static final UsageEvents sEmptyResults = new UsageEvents(); private final Context mContext; private final IUsageStatsManager mService; /** * {@hide} */ public UsageStatsManager(Context context, IUsageStatsManager service) { mContext = context; mService = service; } /** * Gets application usage stats for the given time range, aggregated by the specified interval. *
The returned list will contain a {@link UsageStats} object for each package that * has data for an interval that is a subset of the time range given. To illustrate:
** intervalType = INTERVAL_YEARLY * beginTime = 2013 * endTime = 2015 (exclusive) * * Results: * 2013 - com.example.alpha * 2013 - com.example.beta * 2014 - com.example.alpha * 2014 - com.example.beta * 2014 - com.example.charlie ** * @param intervalType The time interval by which the stats are aggregated. * @param beginTime The inclusive beginning of the range of stats to include in the results. * @param endTime The exclusive end of the range of stats to include in the results. * @return A list of {@link UsageStats} or null if none are available. * * @see #INTERVAL_DAILY * @see #INTERVAL_WEEKLY * @see #INTERVAL_MONTHLY * @see #INTERVAL_YEARLY * @see #INTERVAL_BEST */ public List