Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 47509fe1 authored by Adam Lesinski's avatar Adam Lesinski Committed by Android Git Automerger
Browse files

am f0ba6878: Merge "Add test UsageStats App" into lmp-dev

* commit 'f0ba6878405bb774c8bfc8120ca8cd1f8b5b7aac':
  Add test UsageStats App
parents fc04b1d8 59abf530
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -386,8 +386,8 @@ public class UsageStatsService extends SystemService {
        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;
+11 −0
Original line number Diff line number Diff line
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)
+17 −0
Original line number Diff line number Diff line
<?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>
+20 −0
Original line number Diff line number Diff line
<?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>
+128 −0
Original line number Diff line number Diff line
/*
 * 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