1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.tests.usagestats; 18 19import android.app.AlertDialog; 20import android.app.ListActivity; 21import android.app.usage.UsageStats; 22import android.app.usage.UsageStatsManager; 23import android.content.Context; 24import android.content.DialogInterface; 25import android.content.Intent; 26import android.os.Bundle; 27import android.text.InputType; 28import android.text.TextUtils; 29import android.text.format.DateUtils; 30import android.view.LayoutInflater; 31import android.view.Menu; 32import android.view.MenuInflater; 33import android.view.MenuItem; 34import android.view.View; 35import android.view.ViewGroup; 36import android.widget.BaseAdapter; 37import android.widget.EditText; 38import android.widget.TextView; 39 40import java.util.ArrayList; 41import java.util.Collections; 42import java.util.Comparator; 43import java.util.Map; 44 45public class UsageStatsActivity extends ListActivity { 46 private static final long USAGE_STATS_PERIOD = 1000 * 60 * 60 * 24 * 14; 47 private UsageStatsManager mUsageStatsManager; 48 private Adapter mAdapter; 49 private Comparator<UsageStats> mComparator = new Comparator<UsageStats>() { 50 @Override 51 public int compare(UsageStats o1, UsageStats o2) { 52 return Long.compare(o2.getTotalTimeInForeground(), o1.getTotalTimeInForeground()); 53 } 54 }; 55 56 @Override 57 protected void onCreate(Bundle savedInstanceState) { 58 super.onCreate(savedInstanceState); 59 mUsageStatsManager = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE); 60 mAdapter = new Adapter(); 61 setListAdapter(mAdapter); 62 } 63 64 @Override 65 public boolean onCreateOptionsMenu(Menu menu) { 66 MenuInflater inflater = getMenuInflater(); 67 inflater.inflate(R.menu.main, menu); 68 return super.onCreateOptionsMenu(menu); 69 } 70 71 @Override 72 public boolean onOptionsItemSelected(MenuItem item) { 73 switch (item.getItemId()) { 74 case R.id.log: 75 startActivity(new Intent(this, UsageLogActivity.class)); 76 return true; 77 case R.id.call_is_app_inactive: 78 callIsAppInactive(); 79 return true; 80 81 default: 82 return super.onOptionsItemSelected(item); 83 } 84 } 85 86 @Override 87 protected void onResume() { 88 super.onResume(); 89 updateAdapter(); 90 } 91 92 private void callIsAppInactive() { 93 final AlertDialog.Builder builder = new AlertDialog.Builder(this); 94 builder.setTitle("Enter package name"); 95 final EditText input = new EditText(this); 96 input.setInputType(InputType.TYPE_CLASS_TEXT); 97 input.setHint("com.android.tests.usagestats"); 98 builder.setView(input); 99 100 builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 101 @Override 102 public void onClick(DialogInterface dialog, int which) { 103 final String packageName = input.getText().toString().trim(); 104 if (!TextUtils.isEmpty(packageName)) { 105 showInactive(packageName); 106 } 107 } 108 }); 109 builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 110 @Override 111 public void onClick(DialogInterface dialog, int which) { 112 dialog.cancel(); 113 } 114 }); 115 116 builder.show(); 117 } 118 119 private void showInactive(String packageName) { 120 final AlertDialog.Builder builder = new AlertDialog.Builder(this); 121 builder.setMessage( 122 "isAppInactive(\"" + packageName + "\") = " 123 + (mUsageStatsManager.isAppInactive(packageName) ? "true" : "false")); 124 builder.show(); 125 } 126 127 private void updateAdapter() { 128 long now = System.currentTimeMillis(); 129 long beginTime = now - USAGE_STATS_PERIOD; 130 Map<String, UsageStats> stats = mUsageStatsManager.queryAndAggregateUsageStats( 131 beginTime, now); 132 mAdapter.update(stats); 133 } 134 135 private class Adapter extends BaseAdapter { 136 private ArrayList<UsageStats> mStats = new ArrayList<>(); 137 138 public void update(Map<String, UsageStats> stats) { 139 mStats.clear(); 140 if (stats == null) { 141 return; 142 } 143 144 mStats.addAll(stats.values()); 145 Collections.sort(mStats, mComparator); 146 notifyDataSetChanged(); 147 } 148 149 @Override 150 public int getCount() { 151 return mStats.size(); 152 } 153 154 @Override 155 public Object getItem(int position) { 156 return mStats.get(position); 157 } 158 159 @Override 160 public long getItemId(int position) { 161 return position; 162 } 163 164 @Override 165 public View getView(int position, View convertView, ViewGroup parent) { 166 final ViewHolder holder; 167 if (convertView == null) { 168 convertView = LayoutInflater.from(UsageStatsActivity.this) 169 .inflate(R.layout.row_item, parent, false); 170 holder = new ViewHolder(); 171 holder.packageName = (TextView) convertView.findViewById(android.R.id.text1); 172 holder.usageTime = (TextView) convertView.findViewById(android.R.id.text2); 173 convertView.setTag(holder); 174 } else { 175 holder = (ViewHolder) convertView.getTag(); 176 } 177 178 holder.packageName.setText(mStats.get(position).getPackageName()); 179 holder.usageTime.setText(DateUtils.formatDuration( 180 mStats.get(position).getTotalTimeInForeground())); 181 return convertView; 182 } 183 } 184 185 private static class ViewHolder { 186 TextView packageName; 187 TextView usageTime; 188 } 189}