diff --git a/app/src/main/java/com/gh/base/AppController.java b/app/src/main/java/com/gh/base/AppController.java index 5c63d9370c..628e39b41f 100644 --- a/app/src/main/java/com/gh/base/AppController.java +++ b/app/src/main/java/com/gh/base/AppController.java @@ -17,6 +17,7 @@ import com.gh.common.util.DataUtils; import com.gh.common.util.StringUtils; import com.gh.common.util.TokenUtils; import com.gh.gamecenter.BuildConfig; +import com.gh.gamecenter.statistics.AppTrafficUtils; import com.leon.channel.helper.ChannelReaderUtil; import com.lightgame.utils.Utils; import com.umeng.message.IUmengRegisterCallback; @@ -209,9 +210,11 @@ public class AppController extends Application { // } // } - // 启用EventBus3.0加速功能 - EventBus.builder().addIndex(new EventBusIndex()).installDefaultEventBus(); - } + // 启用EventBus3.0加速功能 + EventBus.builder().addIndex(new EventBusIndex()).installDefaultEventBus(); + + AppTrafficUtils.appTraffic(this); + } private boolean shouldInit() { ActivityManager am = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE)); diff --git a/app/src/main/java/com/gh/common/util/LoginUtils.java b/app/src/main/java/com/gh/common/util/LoginUtils.java index 1f7fe447d7..76b86f9da0 100644 --- a/app/src/main/java/com/gh/common/util/LoginUtils.java +++ b/app/src/main/java/com/gh/common/util/LoginUtils.java @@ -137,6 +137,9 @@ public class LoginUtils { @Override public void onFailure(HttpException e) { super.onFailure(e); + + if (e == null) return; + ResponseBody responseBody = e.response().errorBody(); try { String string = responseBody.string(); diff --git a/app/src/main/java/com/gh/gamecenter/db/AppTrafficDao.java b/app/src/main/java/com/gh/gamecenter/db/AppTrafficDao.java new file mode 100644 index 0000000000..c08df2d2ec --- /dev/null +++ b/app/src/main/java/com/gh/gamecenter/db/AppTrafficDao.java @@ -0,0 +1,121 @@ +package com.gh.gamecenter.db; + +import android.content.Context; + +import com.gh.gamecenter.db.info.AppTrafficInfo; +import com.j256.ormlite.dao.Dao; + +import java.sql.SQLException; +import java.util.HashMap; +import java.util.List; + +/** + * Created by khy on 23/07/17. + */ + +public class AppTrafficDao { + private DatabaseHelper helper; + private Dao dao; + + public AppTrafficDao(Context context) { + try { + helper = DatabaseHelper.getHelper(context); + dao = helper.getDao(AppTrafficInfo.class); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + /** + * 添加一天的流量信息 + */ + public void add(AppTrafficInfo entity) { + try { + dao.create(entity); + } catch (SQLException e) { + + e.printStackTrace(); + } + } + + /** + * 获取前一天总的使用量 + */ + public void getTrafficByAll(AppTrafficInfo entity) { + try { + dao.create(entity); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + /** + * 获取最迟一天存储流量的时间戳 + */ + public long getLastTime() { + try { + List list = dao.queryBuilder().orderBy("time", false).query(); + if (list.size() > 0) { + return list.get(0).getTime(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + return 0; + } + + + /** + * 获取最迟一天的流量使用情况 + */ + public AppTrafficInfo getLastTraffic() { + try { + List list = dao.queryBuilder().orderBy("time", false).query(); + if (list.size() > 0) { + return list.get(0); + } + } catch (SQLException e) { + e.printStackTrace(); + } + return null; + } + + /** + * 获取平均每天使用的流量情况 + */ + public HashMap getMeanByDay() { + try { + HashMap mapCount = new HashMap<>(); + HashMap mapAll = new HashMap<>(); + List list = dao.queryForAll(); + int size = list.size(); + if (size > 0) { + for (AppTrafficInfo appTrafficInfo : list) { + HashMap trafficByDay = appTrafficInfo.getTrafficByDay(); + for (String s : trafficByDay.keySet()) { + Long value = trafficByDay.get(s); + if (value != null && value != 0 && value != -1) { + mapAll.put(s, mapAll.get(s) + value); + + Integer count = mapCount.get(s); + if (count == null) count = 0; + mapCount.put(s, count + 1); + } + } + } + + HashMap map = new HashMap<>(); + + for (String s : mapAll.keySet()) { + map.put(s, mapAll.get(s)/ mapCount.get(s)); + } + return map; + } + } catch (SQLException e) { + e.printStackTrace(); + } + return null; + + } + +} diff --git a/app/src/main/java/com/gh/gamecenter/db/DatabaseHelper.java b/app/src/main/java/com/gh/gamecenter/db/DatabaseHelper.java index 0679533ae2..6a8f65d1c7 100644 --- a/app/src/main/java/com/gh/gamecenter/db/DatabaseHelper.java +++ b/app/src/main/java/com/gh/gamecenter/db/DatabaseHelper.java @@ -4,6 +4,7 @@ import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.support.v4.util.ArrayMap; +import com.gh.gamecenter.db.info.AppTrafficInfo; import com.lightgame.utils.Utils; import com.gh.gamecenter.db.info.AppRunTimeInfo; import com.gh.gamecenter.db.info.CommentInfo; @@ -29,7 +30,7 @@ import java.sql.SQLException; public class DatabaseHelper extends OrmLiteSqliteOpenHelper { private static final String DATABASE_NAME = "gh_assist.db"; - private static final int DATABASE_VERSION = 6; + private static final int DATABASE_VERSION = 7; private static DatabaseHelper instance; private ArrayMap daos = new ArrayMap<>(); @@ -73,6 +74,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { TableUtils.createTable(connectionSource, KeFuMarkReadInfo.class); TableUtils.createTable(connectionSource, NoticeMarkReadInfo.class); TableUtils.createTable(connectionSource, VersionVoteInfo.class); + TableUtils.createTable(connectionSource, AppTrafficInfo.class); } catch (SQLException e) { e.printStackTrace(); } @@ -96,6 +98,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { TableUtils.dropTable(connectionSource, KeFuMarkReadInfo.class, true); TableUtils.dropTable(connectionSource, NoticeMarkReadInfo.class, true); TableUtils.dropTable(connectionSource, VersionVoteInfo.class, true); + TableUtils.dropTable(connectionSource, AppTrafficInfo.class, true); onCreate(database, connectionSource); } catch (SQLException e) { e.printStackTrace(); diff --git a/app/src/main/java/com/gh/gamecenter/db/LibaoDao.java b/app/src/main/java/com/gh/gamecenter/db/LibaoDao.java index 61b66ee8cb..c721604f0b 100644 --- a/app/src/main/java/com/gh/gamecenter/db/LibaoDao.java +++ b/app/src/main/java/com/gh/gamecenter/db/LibaoDao.java @@ -10,6 +10,7 @@ import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; +import java.util.Locale; /** * Created by khy on 2016/12/18. @@ -158,7 +159,7 @@ public class LibaoDao { List infos = dao.queryBuilder().orderBy("time", false).where().eq("libaoId", libaoId).query(); if (infos.size() > 0) { LibaoInfo libaoInfo = infos.get(0); - SimpleDateFormat formatDay = new SimpleDateFormat("dd"); + SimpleDateFormat formatDay = new SimpleDateFormat("dd", Locale.CHINA); long lingTime = libaoInfo.getTime() * 1000; long curTime = Utils.getTime(context) * 1000; int lingDay = Integer.parseInt(formatDay.format(lingTime)); diff --git a/app/src/main/java/com/gh/gamecenter/db/info/AppTrafficInfo.java b/app/src/main/java/com/gh/gamecenter/db/info/AppTrafficInfo.java new file mode 100644 index 0000000000..70141b2137 --- /dev/null +++ b/app/src/main/java/com/gh/gamecenter/db/info/AppTrafficInfo.java @@ -0,0 +1,51 @@ +package com.gh.gamecenter.db.info; + +import com.j256.ormlite.field.DataType; +import com.j256.ormlite.field.DatabaseField; +import com.j256.ormlite.table.DatabaseTable; + +import java.io.Serializable; +import java.util.HashMap; + +/** + * Created by khy on 23/07/17. + */ + +@DatabaseTable(tableName = "tb_traffic") +public class AppTrafficInfo implements Serializable { + + @DatabaseField(id = true, columnName = "time") + private long time; + + // packageName + traffic + @DatabaseField(columnName = "trafficByDay", dataType = DataType.SERIALIZABLE) + private HashMap trafficByDay; // 一天使用的流量 + + // packageName + traffic + @DatabaseField(columnName = "trafficByAll", dataType = DataType.SERIALIZABLE) + private HashMap trafficByAll; // 总使用的流量 + + public long getTime() { + return time; + } + + public void setTime(long time) { + this.time = time; + } + + public HashMap getTrafficByDay() { + return trafficByDay; + } + + public void setTrafficByDay(HashMap trafficByDay) { + this.trafficByDay = trafficByDay; + } + + public HashMap getTrafficByAll() { + return trafficByAll; + } + + public void setTrafficByAll(HashMap trafficByAll) { + this.trafficByAll = trafficByAll; + } +} diff --git a/app/src/main/java/com/gh/gamecenter/statistics/AppTrafficUtils.java b/app/src/main/java/com/gh/gamecenter/statistics/AppTrafficUtils.java new file mode 100644 index 0000000000..9d06ca482a --- /dev/null +++ b/app/src/main/java/com/gh/gamecenter/statistics/AppTrafficUtils.java @@ -0,0 +1,161 @@ +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 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 mapByDay = new HashMap<>(); // 记录一天的流量 + HashMap mapByAll = new HashMap<>(); // 记录总的流量 + AppTrafficInfo lastTraffic = trafficDao.getLastTraffic(); + HashMap 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 为-1 +// +// } +// mapByAll.put(pkgName, KB); +// } + + List 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 为-1 + + } + 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(); + } + } +}