Make UsageStats API comply with API Council

- Fix documentation to mention units of time in APIs.
- Return a Map instead of an ArrayMap

Bug:17289531
Change-Id: I8b07a5328a01a0f490b62bc5d9b83c959a3e79c7
This commit is contained in:
Adam Lesinski
2014-08-27 11:52:52 -07:00
parent 032402cff1
commit c5bf108e70
5 changed files with 25 additions and 21 deletions

View File

@ -106,7 +106,9 @@ public final class UsageEvents implements Parcelable {
} }
/** /**
* The time at which this event occurred. * The time at which this event occurred, measured in milliseconds since the epoch.
* <p/>
* See {@link System#currentTimeMillis()}.
*/ */
public long getTimeStamp() { public long getTimeStamp() {
return mTimeStamp; return mTimeStamp;

View File

@ -81,28 +81,36 @@ public final class UsageStats implements Parcelable {
} }
/** /**
* Get the beginning of the time range this {@link android.app.usage.UsageStats} represents. * Get the beginning of the time range this {@link android.app.usage.UsageStats} represents,
* measured in milliseconds since the epoch.
* <p/>
* See {@link System#currentTimeMillis()}.
*/ */
public long getFirstTimeStamp() { public long getFirstTimeStamp() {
return mBeginTimeStamp; return mBeginTimeStamp;
} }
/** /**
* Get the end of the time range this {@link android.app.usage.UsageStats} represents. * Get the end of the time range this {@link android.app.usage.UsageStats} represents,
* measured in milliseconds since the epoch.
* <p/>
* See {@link System#currentTimeMillis()}.
*/ */
public long getLastTimeStamp() { public long getLastTimeStamp() {
return mEndTimeStamp; return mEndTimeStamp;
} }
/** /**
* Get the last time this package was used. * Get the last time this package was used, measured in milliseconds since the epoch.
* <p/>
* See {@link System#currentTimeMillis()}.
*/ */
public long getLastTimeUsed() { public long getLastTimeUsed() {
return mLastTimeUsed; return mLastTimeUsed;
} }
/** /**
* Get the total time this package spent in the foreground. * Get the total time this package spent in the foreground, measured in milliseconds.
*/ */
public long getTotalTimeInForeground() { public long getTotalTimeInForeground() {
return mTotalTimeInForeground; return mTotalTimeInForeground;

View File

@ -23,6 +23,7 @@ import android.util.ArrayMap;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* Provides access to device usage history and statistics. Usage data is aggregated into * Provides access to device usage history and statistics. Usage data is aggregated into
@ -149,7 +150,6 @@ public final class UsageStatsManager {
* @param endTime The exclusive end of the range of events to include in the results. * @param endTime The exclusive end of the range of events to include in the results.
* @return A {@link UsageEvents}. * @return A {@link UsageEvents}.
*/ */
@SuppressWarnings("unchecked")
public UsageEvents queryEvents(long beginTime, long endTime) { public UsageEvents queryEvents(long beginTime, long endTime) {
try { try {
UsageEvents iter = mService.queryEvents(beginTime, endTime, UsageEvents iter = mService.queryEvents(beginTime, endTime,
@ -170,15 +170,13 @@ public final class UsageStatsManager {
* *
* @param beginTime The inclusive beginning of the range of stats to include in the results. * @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. * @param endTime The exclusive end of the range of stats to include in the results.
* @return An {@link android.util.ArrayMap} keyed by package name or null if no stats are * @return A {@link java.util.Map} keyed by package name, or null if no stats are
* available. * available.
*/ */
public ArrayMap<String, UsageStats> queryAndAggregateUsageStats(long beginTime, long endTime) { public Map<String, UsageStats> queryAndAggregateUsageStats(long beginTime, long endTime) {
List<UsageStats> stats = queryUsageStats(INTERVAL_BEST, beginTime, endTime); List<UsageStats> stats = queryUsageStats(INTERVAL_BEST, beginTime, endTime);
if (stats.isEmpty()) { if (stats.isEmpty()) {
@SuppressWarnings("unchecked") return Collections.emptyMap();
ArrayMap<String, UsageStats> emptyStats = ArrayMap.EMPTY;
return emptyStats;
} }
ArrayMap<String, UsageStats> aggregatedStats = new ArrayMap<>(); ArrayMap<String, UsageStats> aggregatedStats = new ArrayMap<>();

View File

@ -23,7 +23,6 @@ import android.app.usage.UsageStatsManager;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.provider.Settings; import android.provider.Settings;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Slog; import android.util.Slog;
import android.widget.AbsListView; import android.widget.AbsListView;
import android.widget.GridView; import android.widget.GridView;
@ -73,6 +72,7 @@ import java.util.Comparator;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
/** /**
@ -100,7 +100,7 @@ public class ResolverActivity extends Activity implements AdapterView.OnItemClic
private boolean mResolvingHome = false; private boolean mResolvingHome = false;
private UsageStatsManager mUsm; private UsageStatsManager mUsm;
private ArrayMap<String, UsageStats> mStats; private Map<String, UsageStats> mStats;
private static final long USAGE_STATS_PERIOD = 1000 * 60 * 60 * 24 * 14; private static final long USAGE_STATS_PERIOD = 1000 * 60 * 60 * 24 * 14;
private boolean mRegistered; private boolean mRegistered;

View File

@ -23,7 +23,6 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.text.format.DateUtils; import android.text.format.DateUtils;
import android.util.ArrayMap;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
@ -36,6 +35,7 @@ import android.widget.TextView;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.Map;
public class UsageStatsActivity extends ListActivity { public class UsageStatsActivity extends ListActivity {
private static final long USAGE_STATS_PERIOD = 1000 * 60 * 60 * 24 * 14; private static final long USAGE_STATS_PERIOD = 1000 * 60 * 60 * 24 * 14;
@ -84,7 +84,7 @@ public class UsageStatsActivity extends ListActivity {
private void updateAdapter() { private void updateAdapter() {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
long beginTime = now - USAGE_STATS_PERIOD; long beginTime = now - USAGE_STATS_PERIOD;
ArrayMap<String, UsageStats> stats = mUsageStatsManager.queryAndAggregateUsageStats( Map<String, UsageStats> stats = mUsageStatsManager.queryAndAggregateUsageStats(
beginTime, now); beginTime, now);
mAdapter.update(stats); mAdapter.update(stats);
} }
@ -92,17 +92,13 @@ public class UsageStatsActivity extends ListActivity {
private class Adapter extends BaseAdapter { private class Adapter extends BaseAdapter {
private ArrayList<UsageStats> mStats = new ArrayList<>(); private ArrayList<UsageStats> mStats = new ArrayList<>();
public void update(ArrayMap<String, UsageStats> stats) { public void update(Map<String, UsageStats> stats) {
mStats.clear(); mStats.clear();
if (stats == null) { if (stats == null) {
return; return;
} }
final int packageCount = stats.size(); mStats.addAll(stats.values());
for (int i = 0; i < packageCount; i++) {
mStats.add(stats.valueAt(i));
}
Collections.sort(mStats, mComparator); Collections.sort(mStats, mComparator);
notifyDataSetChanged(); notifyDataSetChanged();
} }