Add test UsageStats App

Also fixed UsageStatsService to do the right thing
when AppOps returns MODE_DEFAULT.

Change-Id: I0bdb28350fb2528daf2859cdcbbf9ca48d96dde9
diff --git a/services/usage/java/com/android/server/usage/UsageStatsService.java b/services/usage/java/com/android/server/usage/UsageStatsService.java
index 475824a..4018def 100644
--- a/services/usage/java/com/android/server/usage/UsageStatsService.java
+++ b/services/usage/java/com/android/server/usage/UsageStatsService.java
@@ -386,8 +386,8 @@
         private boolean hasPermission(String callingPackage) {
             final int mode = mAppOps.checkOp(AppOpsManager.OP_GET_USAGE_STATS,
                     Binder.getCallingUid(), callingPackage);
-            if (mode == AppOpsManager.MODE_IGNORED) {
-                // If AppOpsManager ignores this, still allow if we have the system level
+            if (mode == AppOpsManager.MODE_DEFAULT) {
+                // The default behavior here is to check if PackageManager has given the app
                 // permission.
                 return getContext().checkCallingPermission(Manifest.permission.PACKAGE_USAGE_STATS)
                         == PackageManager.PERMISSION_GRANTED;
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