summaryrefslogtreecommitdiffstats
path: root/tests/UsageStatsTest
diff options
context:
space:
mode:
authorAdam Lesinski <adamlesinski@google.com>2014-07-22 15:55:50 -0700
committerAdam Lesinski <adamlesinski@google.com>2014-07-22 18:21:58 -0700
commite159e2e0b83f9ea9d48f4c8666f9536e9338a70c (patch)
treeb3da726ce05637a30ec5aa3d8038b92af4e93050 /tests/UsageStatsTest
parent921ec8056a3010d0a21400f97312cf3f7d97ffaa (diff)
downloadframeworks_base-e159e2e0b83f9ea9d48f4c8666f9536e9338a70c.zip
frameworks_base-e159e2e0b83f9ea9d48f4c8666f9536e9338a70c.tar.gz
frameworks_base-e159e2e0b83f9ea9d48f4c8666f9536e9338a70c.tar.bz2
Add test UsageStats App
Also fixed UsageStatsService to do the right thing when AppOps returns MODE_DEFAULT. Change-Id: I0bdb28350fb2528daf2859cdcbbf9ca48d96dde9
Diffstat (limited to 'tests/UsageStatsTest')
-rw-r--r--tests/UsageStatsTest/Android.mk11
-rw-r--r--tests/UsageStatsTest/AndroidManifest.xml17
-rw-r--r--tests/UsageStatsTest/res/layout/row_item.xml20
-rw-r--r--tests/UsageStatsTest/src/com/android/tests/usagestats/UsageStatsActivity.java128
4 files changed, 176 insertions, 0 deletions
diff --git a/tests/UsageStatsTest/Android.mk b/tests/UsageStatsTest/Android.mk
new file mode 100644
index 0000000..69fefeb
--- /dev/null
+++ b/tests/UsageStatsTest/Android.mk
@@ -0,0 +1,11 @@
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE_TAGS := tests
+
+# Only compile source java files in this apk.
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+LOCAL_PACKAGE_NAME := UsageStatsTest
+
+include $(BUILD_PACKAGE)
diff --git a/tests/UsageStatsTest/AndroidManifest.xml b/tests/UsageStatsTest/AndroidManifest.xml
new file mode 100644
index 0000000..fac5810
--- /dev/null
+++ b/tests/UsageStatsTest/AndroidManifest.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.android.tests.usagestats">
+
+ <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" />
+
+ <application android:label="Usage Access Test">
+ <activity android:name=".UsageStatsActivity"
+ android:label="Device Usage History">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="android.intent.category.LAUNCHER" />
+ </intent-filter>
+ </activity>
+ </application>
+</manifest>
diff --git a/tests/UsageStatsTest/res/layout/row_item.xml b/tests/UsageStatsTest/res/layout/row_item.xml
new file mode 100644
index 0000000..da50163
--- /dev/null
+++ b/tests/UsageStatsTest/res/layout/row_item.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:paddingLeft="16dp"
+ android:paddingRight="16dp">
+
+ <TextView android:id="@android:id/text1"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_alignParentLeft="true"
+ android:layout_centerVertical="true"
+ android:textStyle="bold"/>
+
+ <TextView android:id="@android:id/text2"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_alignParentRight="true"
+ android:layout_alignBaseline="@android:id/text1"/>
+</RelativeLayout>
diff --git a/tests/UsageStatsTest/src/com/android/tests/usagestats/UsageStatsActivity.java b/tests/UsageStatsTest/src/com/android/tests/usagestats/UsageStatsActivity.java
new file mode 100644
index 0000000..73143c5
--- /dev/null
+++ b/tests/UsageStatsTest/src/com/android/tests/usagestats/UsageStatsActivity.java
@@ -0,0 +1,128 @@
+/*
+ * 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 com.android.tests.usagestats;
+
+import android.app.ListActivity;
+import android.app.usage.PackageUsageStats;
+import android.app.usage.UsageStats;
+import android.app.usage.UsageStatsManager;
+import android.content.Context;
+import android.os.Bundle;
+import android.text.format.DateUtils;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.BaseAdapter;
+import android.widget.TextView;
+
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Collections;
+import java.util.Comparator;
+
+public class UsageStatsActivity extends ListActivity {
+
+ private UsageStatsManager mUsageStatsManager;
+ private Adapter mAdapter;
+ private Comparator<PackageUsageStats> mComparator = new Comparator<PackageUsageStats>() {
+ @Override
+ public int compare(PackageUsageStats o1, PackageUsageStats o2) {
+ return Long.compare(o2.getTotalTimeSpent(), o1.getTotalTimeSpent());
+ }
+ };
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ mUsageStatsManager = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);
+ mAdapter = new Adapter();
+ updateAdapter();
+ setListAdapter(mAdapter);
+ }
+
+ @Override
+ protected void onResume() {
+ super.onResume();
+ updateAdapter();
+ }
+
+ private void updateAdapter() {
+ Calendar cal = Calendar.getInstance();
+ cal.add(Calendar.DAY_OF_YEAR, -14);
+ UsageStats stats = mUsageStatsManager.getRecentStatsSince(cal.getTimeInMillis());
+ mAdapter.update(stats);
+ }
+
+ private class Adapter extends BaseAdapter {
+ private ArrayList<PackageUsageStats> mStats = new ArrayList<>();
+
+ public void update(UsageStats stats) {
+ mStats.clear();
+ if (stats == null) {
+ return;
+ }
+
+ final int packageCount = stats.getPackageCount();
+ for (int i = 0; i < packageCount; i++) {
+ mStats.add(stats.getPackage(i));
+ }
+
+ Collections.sort(mStats, mComparator);
+ notifyDataSetChanged();
+ }
+
+ @Override
+ public int getCount() {
+ return mStats.size();
+ }
+
+ @Override
+ public Object getItem(int position) {
+ return mStats.get(position);
+ }
+
+ @Override
+ public long getItemId(int position) {
+ return position;
+ }
+
+ @Override
+ public View getView(int position, View convertView, ViewGroup parent) {
+ final ViewHolder holder;
+ if (convertView == null) {
+ convertView = LayoutInflater.from(UsageStatsActivity.this)
+ .inflate(R.layout.row_item, parent, false);
+ holder = new ViewHolder();
+ holder.packageName = (TextView) convertView.findViewById(android.R.id.text1);
+ holder.usageTime = (TextView) convertView.findViewById(android.R.id.text2);
+ convertView.setTag(holder);
+ } else {
+ holder = (ViewHolder) convertView.getTag();
+ }
+
+ holder.packageName.setText(mStats.get(position).getPackageName());
+ holder.usageTime.setText(DateUtils.formatDuration(
+ mStats.get(position).getTotalTimeSpent()));
+ return convertView;
+ }
+ }
+
+ private static class ViewHolder {
+ TextView packageName;
+ TextView usageTime;
+ }
+} \ No newline at end of file