Files
assistant-android/app/src/main/java/com/gh/gamecenter/statistics/AppTrafficUtils.java
2017-07-23 18:36:45 +08:00

162 lines
7.4 KiB
Java
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.gh.gamecenter.statistics;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.TrafficStats;
import com.gh.gamecenter.db.AppTrafficDao;
import com.gh.gamecenter.db.info.AppTrafficInfo;
import com.lightgame.utils.Utils;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
/**
* Created by khy on 23/07/17.
*
* 流量统计
*/
public class AppTrafficUtils {
public static void appTraffic(Context context) {
try {
// ConcernManager cManager = new ConcernManager(context);
// List<ConcernInfo> runnableGame = cManager.getInstalledGame();
// if (runnableGame.size() == 0) return; // 没有安装光环助手已有的游戏
AppTrafficDao trafficDao = new AppTrafficDao(context);
long lastTime = trafficDao.getLastTime();
long curTime = Utils.getTime(context) * 1000;
int offsetDay = 1; // 默认
if (lastTime != 0) {
Calendar calendar = Calendar.getInstance();
Date lastDate = new Date(lastTime);
calendar.setTime(lastDate);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.MILLISECOND, 0);
long lastTimeZero = calendar.getTimeInMillis(); // 最后一次保存零点
Date curDate = new Date(curTime);
calendar.setTime(curDate);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.MILLISECOND, 0);
long curTimeZero = calendar.getTimeInMillis(); // 当前时间零点
long zeroOffset = curTimeZero - lastTimeZero;
offsetDay = (int) (zeroOffset / (86400 * 1000));
}
Utils.log("=======偏移天数" + offsetDay);
if (offsetDay == 0) return;
HashMap<String, Long> mapByDay = new HashMap<>(); // 记录一天的流量
HashMap<String, Long> mapByAll = new HashMap<>(); // 记录总的流量
AppTrafficInfo lastTraffic = trafficDao.getLastTraffic();
HashMap<String, Long> lastMapByAll = null;
if (lastTraffic != null) {
lastMapByAll = lastTraffic.getTrafficByAll();
}
PackageManager pm = context.getPackageManager();
// for (ConcernInfo concernInfo : runnableGame) { // 记录光环助手包含的已安装的游戏流量使用情况
// String pkgName = concernInfo.getPackageNames().keySet().iterator().next();
// ApplicationInfo applicationInfo = pm.getApplicationInfo(pkgName, PackageManager.GET_META_DATA);
// int uid = applicationInfo.uid;
//
// long uidRxBytes = TrafficStats.getUidRxBytes(uid);
// long KB = uidRxBytes / 1024;
// Utils.log(pkgName + "==总流量::" + KB + "KB");
//
// if (lastTraffic != null) {
//
// long trafficByDay; // 统计某段时间流量使用情况
// Long lastTrafficByAll = lastMapByAll.get(pkgName); // 最后一次记录的流量使用总数
// if (lastTrafficByAll != null && lastMapByAll.get(pkgName) > KB) { // 特殊情况,本次总的流量比上次记录的小(有可能是手机重启,导致流量表被清空)
// trafficByDay = KB;
// } else if (lastTrafficByAll != null) { // 正常情况,上次有记录,本次总数减去最后一次记录总数得出某段时间使用值
// trafficByDay = KB - lastTrafficByAll;
// } else { // 没有流量记录(并不是所有记录为空而是默认APP没有记录有可能是新安装的),默认为零
// trafficByDay = 0;
// }
//
// Utils.log(pkgName + "==当天流量::" + trafficByDay + "KB");
// mapByDay.put(pkgName, trafficByDay); // 如果上一次没有记录 trafficByDay 为-
//
// }
// mapByAll.put(pkgName, KB);
// }
List<PackageInfo> installedPackages = pm.getInstalledPackages(0);
for (PackageInfo installedPackage : installedPackages) { // 获取第三方应用流量使用情况
if ((installedPackage.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
String pkgName = installedPackage.applicationInfo.packageName;
int uid = installedPackage.applicationInfo.uid;
long uidRxBytes = TrafficStats.getUidRxBytes(uid);
long KB = uidRxBytes / 1024;
Utils.log(installedPackage.applicationInfo.loadLabel(pm).toString() + "==总流量::" + KB + "KB");
if (lastTraffic != null) {
long trafficByDay; // 统计某段时间流量使用情况
Long lastTrafficByAll = lastMapByAll.get(pkgName); // 最后一次记录的流量使用总数
if (lastTrafficByAll != null && lastMapByAll.get(pkgName) > KB) { // 特殊情况,本次总的流量比上次记录的小(有可能是手机重启,导致流量表被清空)
trafficByDay = KB;
} else if (lastTrafficByAll != null) { // 正常情况,上次有记录,本次总数减去最后一次记录总数得出某段时间使用值
trafficByDay = KB - lastTrafficByAll;
} else { // 没有流量记录(并不是所有记录为空而是默认APP没有记录有可能是新安装的),默认为零
trafficByDay = 0;
}
Utils.log(installedPackage.applicationInfo.loadLabel(pm).toString() + "==当天流量::" + trafficByDay + "KB");
mapByDay.put(pkgName, trafficByDay); // 如果上一次没有记录 trafficByDay 为-
}
mapByAll.put(pkgName, KB);
}
}
if (offsetDay == 1) { // 一天统计一次
AppTrafficInfo entity = new AppTrafficInfo();
entity.setTime(curTime);
entity.setTrafficByAll(mapByAll);
entity.setTrafficByDay(mapByDay);
trafficDao.add(entity);
return;
}
if (offsetDay > 1) { // 过程由于种种原因无法一天统计一次, 存储的流量按每天平均存储
for (String s : mapByDay.keySet()) {
mapByDay.put(s, mapByDay.get(s) / offsetDay);
}
for (int i = 0; i < offsetDay; i++) {
AppTrafficInfo entity = new AppTrafficInfo();
long targetTime = curTime - (86400 * 1000) * i;
entity.setTime(targetTime);
entity.setTrafficByAll(mapByAll);
entity.setTrafficByDay(mapByDay);
trafficDao.add(entity);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}