Merge pull request '1' (#13) from develop into master
commit
fe10873007
@ -0,0 +1,278 @@
|
|||||||
|
package com.tanshui.timeuse;
|
||||||
|
|
||||||
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
|
||||||
|
import android.app.AppOpsManager;
|
||||||
|
import android.app.ProgressDialog;
|
||||||
|
import android.app.usage.UsageStats;
|
||||||
|
import android.app.usage.UsageStatsManager;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.pm.ApplicationInfo;
|
||||||
|
import android.content.pm.PackageInfo;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
|
import android.graphics.drawable.Drawable;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.provider.Settings;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.Button;
|
||||||
|
import android.widget.ImageView;
|
||||||
|
import android.widget.ListView;
|
||||||
|
import android.widget.SimpleAdapter;
|
||||||
|
import android.widget.Toast;
|
||||||
|
import com.tanshui.timeuse.database.timeUseDBHelper;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
||||||
|
public class appTimeUse extends AppCompatActivity {
|
||||||
|
|
||||||
|
private ArrayList<AppInfo> mApplicationInfos;
|
||||||
|
private timeUseDBHelper dbHelper;
|
||||||
|
private List<AppInfo> appInfoList;
|
||||||
|
private SimpleAdapter simpleAdapter;
|
||||||
|
private ProgressDialog progressDialog;
|
||||||
|
private ArrayList<HashMap<String, Object>> listItem;
|
||||||
|
private String date;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
setContentView(R.layout.activity_app_time_use);
|
||||||
|
|
||||||
|
dbHelper = timeUseDBHelper.getInstance(this);
|
||||||
|
dbHelper.openWriteLink();
|
||||||
|
dbHelper.openReadLink();
|
||||||
|
showProgressDialog("1","11");
|
||||||
|
ListView lv_app = findViewById(R.id.lv_app);
|
||||||
|
Button list = findViewById(R.id.jump_app_list);
|
||||||
|
Button report = findViewById(R.id.jump_app_report);
|
||||||
|
Button appClassify =findViewById(R.id.jump_app_appClassify);
|
||||||
|
// 接受task_show传的date
|
||||||
|
Intent intent1=getIntent();
|
||||||
|
date = intent1.getStringExtra("date");
|
||||||
|
|
||||||
|
|
||||||
|
list.setOnClickListener(v -> {
|
||||||
|
Intent intent=new Intent(appTimeUse.this,task_show.class);
|
||||||
|
startActivity(intent);
|
||||||
|
});
|
||||||
|
report.setOnClickListener(v -> {
|
||||||
|
Intent intent=new Intent(appTimeUse.this,report.class);
|
||||||
|
startActivity(intent);
|
||||||
|
});
|
||||||
|
appClassify.setOnClickListener(v -> {
|
||||||
|
Intent intent=new Intent(appTimeUse.this,software_classification.class);
|
||||||
|
startActivity(intent);
|
||||||
|
});
|
||||||
|
|
||||||
|
mApplicationInfos=getAllAppInfo(this);
|
||||||
|
listItem = new ArrayList<>();
|
||||||
|
|
||||||
|
listItem =getApp(dbHelper);
|
||||||
|
// ---------------------------------------
|
||||||
|
simpleAdapter = new SimpleAdapter(this,
|
||||||
|
//绑定的数据
|
||||||
|
listItem,
|
||||||
|
//每一行的布局
|
||||||
|
R.layout.appitem,
|
||||||
|
//动态数组中的数据源的键映射到布局文件对应的控件中
|
||||||
|
new String[] {"ItemImage", "ItemLabel","ItemTime"},
|
||||||
|
new int[] {R.id.iv_appIcon, R.id.label,R.id.times});
|
||||||
|
lv_app.setAdapter(simpleAdapter);
|
||||||
|
|
||||||
|
|
||||||
|
// 显示app图标
|
||||||
|
simpleAdapter.setViewBinder(new SimpleAdapter.ViewBinder() {
|
||||||
|
@Override
|
||||||
|
public boolean setViewValue(View view, Object o, String s) {
|
||||||
|
if(view instanceof ImageView && o instanceof Drawable){
|
||||||
|
|
||||||
|
ImageView iv=(ImageView)view;
|
||||||
|
iv.setImageDrawable((Drawable)o);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
hideProgressDialog();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onStart() {
|
||||||
|
super.onStart();
|
||||||
|
|
||||||
|
// 从数据库获取applist
|
||||||
|
// appInfoList=dbHelper.queryAllAppInfo();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getTimeFromInt(long time) {
|
||||||
|
|
||||||
|
if (time <= 0) { return "已结束"; }
|
||||||
|
// long day = time / (1 * 60 * 60 * 24);
|
||||||
|
String hour = time / (1000*60*60) % 60 +"";
|
||||||
|
String minute = time / (1000*60) % 60<10?"0"+time / (1000*60) % 60:""+time / (1000*60) % 60;
|
||||||
|
String second = time /1000 % 60<10?"0"+time / 1000 % 60:""+time / 1000 % 60;
|
||||||
|
|
||||||
|
return hour+"小时"+minute+"分钟"+second+"秒";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取手机已安装应用列表
|
||||||
|
* @param ctx
|
||||||
|
* isFilterSystem 是否过滤系统应用
|
||||||
|
*/
|
||||||
|
public ArrayList<AppInfo> getAllAppInfo(Context ctx) {
|
||||||
|
ArrayList<AppInfo> appBeanList = new ArrayList<>();
|
||||||
|
AppInfo bean = null;
|
||||||
|
PackageManager packageManager = ctx.getPackageManager();
|
||||||
|
List<PackageInfo> list = packageManager.getInstalledPackages(0);
|
||||||
|
for (PackageInfo p : list) {
|
||||||
|
bean = new AppInfo();
|
||||||
|
|
||||||
|
bean.setIcon(p.applicationInfo.loadIcon(packageManager));
|
||||||
|
bean.setLabel(packageManager.getApplicationLabel(p.applicationInfo).toString());
|
||||||
|
bean.setPackage_name(p.applicationInfo.packageName);
|
||||||
|
|
||||||
|
int flags = p.applicationInfo.flags;
|
||||||
|
// 判断是否是属于系统的apk
|
||||||
|
if ((flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
|
||||||
|
// bean.setSystem(true);
|
||||||
|
} else {
|
||||||
|
appBeanList.add(bean);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return appBeanList;
|
||||||
|
}
|
||||||
|
// 弹出正在加载弹窗
|
||||||
|
public void showProgressDialog(String title, String message) {
|
||||||
|
if (progressDialog == null) {
|
||||||
|
|
||||||
|
progressDialog = ProgressDialog.show(appTimeUse.this,
|
||||||
|
title, message, true, false);
|
||||||
|
} else if (progressDialog.isShowing()) {
|
||||||
|
progressDialog.setTitle(title);
|
||||||
|
progressDialog.setMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
progressDialog.show();
|
||||||
|
|
||||||
|
}
|
||||||
|
// 隐藏提示加载弹窗
|
||||||
|
public void hideProgressDialog() {
|
||||||
|
|
||||||
|
if (progressDialog != null && progressDialog.isShowing()) {
|
||||||
|
progressDialog.dismiss();
|
||||||
|
}
|
||||||
|
progressDialog=null;
|
||||||
|
}
|
||||||
|
// 获取应用信息
|
||||||
|
public ArrayList<HashMap<String, Object>> getApp(timeUseDBHelper dbHelper){
|
||||||
|
|
||||||
|
// 从早上8点开始监听
|
||||||
|
Calendar setBeginCal=Calendar.getInstance();
|
||||||
|
|
||||||
|
|
||||||
|
Calendar setEndCal=Calendar.getInstance();
|
||||||
|
|
||||||
|
|
||||||
|
Calendar beginCal = Calendar.getInstance();
|
||||||
|
int hour = beginCal.get(Calendar.HOUR_OF_DAY);
|
||||||
|
int minute = beginCal.get(Calendar.MINUTE);
|
||||||
|
int second = beginCal.get(Calendar.SECOND);
|
||||||
|
|
||||||
|
setBeginCal.add(Calendar.SECOND, -1 * second);
|
||||||
|
setBeginCal.add(Calendar.MINUTE, -1 * minute);
|
||||||
|
setBeginCal.add(Calendar.HOUR, -1 * hour+8);
|
||||||
|
|
||||||
|
setEndCal.add(Calendar.SECOND, -1 * second);
|
||||||
|
setEndCal.add(Calendar.MINUTE, -1 * minute);
|
||||||
|
setEndCal.add(Calendar.HOUR, -1 * hour+10);
|
||||||
|
|
||||||
|
beginCal.add(Calendar.SECOND, -1 * second);
|
||||||
|
beginCal.add(Calendar.MINUTE, -1 * minute);
|
||||||
|
beginCal.add(Calendar.HOUR, -1 * hour+8);
|
||||||
|
|
||||||
|
Calendar endCal = Calendar.getInstance();
|
||||||
|
|
||||||
|
UsageStatsManager manager=(UsageStatsManager)getApplicationContext().getSystemService(USAGE_STATS_SERVICE);
|
||||||
|
List<UsageStats> stats=manager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,beginCal.getTimeInMillis(),endCal.getTimeInMillis());
|
||||||
|
Map<String, UsageStats> statsMap = manager.queryAndAggregateUsageStats(setBeginCal.getTimeInMillis(), setEndCal.getTimeInMillis());
|
||||||
|
|
||||||
|
ArrayList<HashMap<String, Object>> listItem = new ArrayList<>();
|
||||||
|
|
||||||
|
List<String> name = new ArrayList<>();
|
||||||
|
List<Drawable> icons = new ArrayList<>();
|
||||||
|
|
||||||
|
for(UsageStats us:stats){
|
||||||
|
try {
|
||||||
|
PackageManager pm=getApplicationContext().getPackageManager();
|
||||||
|
ApplicationInfo applicationInfo=pm.getApplicationInfo(us.getPackageName(),PackageManager.GET_META_DATA);
|
||||||
|
// Toast.makeText(this, appInfo.package_name, Toast.LENGTH_SHORT).show();
|
||||||
|
PackageInfo packageInfo = pm.getPackageInfo(applicationInfo.packageName, PackageManager.GET_CONFIGURATIONS);
|
||||||
|
int flags = packageInfo.applicationInfo.flags;
|
||||||
|
// SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
|
||||||
|
//
|
||||||
|
// String t=format.format(new Date(us.getLastTimeUsed()));
|
||||||
|
// long t1=us.getLastTimeStamp();
|
||||||
|
// String t2=format.format(new Date(us.getLastTimeUsed()));
|
||||||
|
String appName = "";
|
||||||
|
Drawable icon=null;
|
||||||
|
|
||||||
|
for(AppInfo app:mApplicationInfos){
|
||||||
|
if(applicationInfo.packageName.equals(app.getPackage_name())){
|
||||||
|
appName=app.getLabel();
|
||||||
|
name.add(appName);
|
||||||
|
icon=app.getIcon();
|
||||||
|
icons.add(icon);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(getTimeFromInt(us.getTotalTimeInForeground())!="已结束"){
|
||||||
|
UsageStats usageStats=statsMap.get(applicationInfo.packageName);
|
||||||
|
// 判断是否为系统应用系统
|
||||||
|
if((flags & ApplicationInfo.FLAG_SYSTEM) == 0){
|
||||||
|
AppInfo appInfo=new AppInfo();
|
||||||
|
HashMap<String,Object> map = new HashMap<>();
|
||||||
|
//加入图片
|
||||||
|
map.put("ItemImage", icon);
|
||||||
|
// 添加app名称
|
||||||
|
|
||||||
|
map.put("ItemLabel", appName);
|
||||||
|
appInfo.setLabel(appName);
|
||||||
|
// 添加app使用时间
|
||||||
|
map.put("ItemTime","今天使用时间:"+getTimeFromInt(usageStats.getTotalTimeInForeground()));
|
||||||
|
appInfo.setTimeString(getTimeFromInt(usageStats.getTotalTimeInForeground()));
|
||||||
|
appInfo.setTimeLong(usageStats.getTotalTimeInForeground());
|
||||||
|
appInfo.setPackage_name(applicationInfo.packageName);
|
||||||
|
|
||||||
|
|
||||||
|
// dbHelper=timeUseDBHelper.getInstance(this);
|
||||||
|
|
||||||
|
if(dbHelper.searchPacketName(appInfo.getPackage_name(),"appList")){
|
||||||
|
// 修改时间
|
||||||
|
dbHelper.updateAppUseTime(appInfo,date);
|
||||||
|
|
||||||
|
}else{
|
||||||
|
|
||||||
|
dbHelper.insertAppInfos(appInfo,date);
|
||||||
|
}
|
||||||
|
listItem.add(map);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return listItem;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue