143 lines
4.6 KiB
Java
143 lines
4.6 KiB
Java
package com.gh.common.util;
|
||
|
||
import android.app.Activity;
|
||
import android.app.Application;
|
||
import android.content.Context;
|
||
|
||
import com.gh.common.constant.Config;
|
||
import com.tencent.stat.MtaSDkException;
|
||
import com.tencent.stat.StatConfig;
|
||
import com.tencent.stat.StatCrashReporter;
|
||
import com.tencent.stat.StatReportStrategy;
|
||
import com.tencent.stat.StatService;
|
||
import com.tendcloud.tenddata.TCAgent;
|
||
|
||
import java.util.HashMap;
|
||
import java.util.Map;
|
||
import java.util.Properties;
|
||
|
||
/**
|
||
* Created by LGT on 2016/6/15.
|
||
* 数据收集 工具类 (TalkingData、MTA)
|
||
*/
|
||
public class DataUtils {
|
||
|
||
/**
|
||
* 初始化各种统计工具,仅在release build(非debug)模式启用统计
|
||
*
|
||
* @param context
|
||
* @param debug 是否debug模式
|
||
* @param channel
|
||
*/
|
||
public static void init(Application context, final boolean debug, String channel) {
|
||
|
||
//TalkingData
|
||
TCAgent.LOG_ON = debug;
|
||
if (!debug) {
|
||
TCAgent.init(context, Config.TD_APPID, channel);
|
||
//TODO 去除为了测试MTA的问题,这个版本不启用
|
||
TCAgent.setReportUncaughtExceptions(true);
|
||
}
|
||
|
||
// 打开debug开关,可查看mta上报日志或错误
|
||
// debug true release false
|
||
StatConfig.setDebugEnable(debug);
|
||
|
||
//MTA
|
||
if (!debug) {
|
||
|
||
//TODO 加入账号之后设置用户
|
||
// StatConfig.setCustomUserId(context, "userid");
|
||
|
||
// 收集未处理的异常
|
||
StatConfig.setAutoExceptionCaught(true);
|
||
|
||
StatConfig.setAntoActivityLifecycleStat(true);
|
||
|
||
// 设置数据上报策略
|
||
StatConfig.setStatSendStrategy(StatReportStrategy.PERIOD);
|
||
StatConfig.setSendPeriodMinutes(5);
|
||
|
||
StatConfig.init(context);
|
||
StatConfig.setInstallChannel(channel);
|
||
StatService.setContext(context);
|
||
StatService.registerActivityLifecycleCallbacks(context);
|
||
|
||
StatCrashReporter crashReporter = StatCrashReporter.getStatCrashReporter(context);
|
||
// 开启异常时的实时上报
|
||
crashReporter.setEnableInstantReporting(true);
|
||
// 开启java异常捕获
|
||
crashReporter.setJavaCrashHandlerStatus(true);
|
||
|
||
try {
|
||
// 开启收集服务
|
||
StatService.startStatService(context, Config.MTA_APPKEY, com.tencent.stat.common.StatConstants.VERSION);
|
||
} catch (MtaSDkException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
public static void onEvent(Context var0, String var1, String var2) {
|
||
TCAgent.onEvent(var0, var1, var2);
|
||
StatService.trackCustomEvent(var0, var1, var2);
|
||
}
|
||
|
||
public static void onPause(Activity var0) {
|
||
TCAgent.onPause(var0);
|
||
StatService.onPause(var0);
|
||
}
|
||
|
||
public static void onResume(Activity var0) {
|
||
TCAgent.onResume(var0);
|
||
StatService.onResume(var0);
|
||
}
|
||
|
||
// 游戏启动
|
||
public static void onGameLaunchEvent(Context context, String gameName, String platform, String page) {
|
||
Map<String, Object> kv = new HashMap<>();
|
||
kv.put("版本", platform);
|
||
kv.put("页面", page);
|
||
onEvent(context, "游戏启动", gameName, kv);
|
||
}
|
||
|
||
public static void onEvent(Context var0, String var1, String var2, Map<String, Object> var3) {
|
||
TCAgent.onEvent(var0, var1, var2, var3);
|
||
Properties prop = new Properties();
|
||
prop.setProperty("label", var2);
|
||
for (String key : var3.keySet()) {
|
||
prop.setProperty(key, var3.get(key) + "");
|
||
}
|
||
StatService.trackCustomKVEvent(var0, var1, prop);
|
||
}
|
||
|
||
// 游戏下载
|
||
public static void onGameDownloadEvent(Context context, String gameName, String platform, String entrance, String status) {
|
||
Map<String, Object> kv = new HashMap<>();
|
||
kv.put("版本", platform);
|
||
kv.put("状态", status);
|
||
onEvent(context, "游戏下载", gameName, kv);
|
||
|
||
Map<String, Object> kv2 = new HashMap<>();
|
||
kv2.put("版本", platform);
|
||
kv2.put("状态", status);
|
||
kv2.put("位置", entrance);
|
||
onEvent(context, "游戏下载位置", gameName, kv2);
|
||
|
||
Map<String, Object> kv3 = new HashMap<>();
|
||
kv3.put(entrance, "下载数");
|
||
kv3.put(entrance, status);
|
||
onEvent(context, "应用数据", gameName, kv3);
|
||
}
|
||
|
||
// 游戏更新
|
||
public static void onGameUpdateEvent(Context context, String gameName, String paltform, String status) {
|
||
Map<String, Object> kv = new HashMap<>();
|
||
kv.put("版本", paltform);
|
||
kv.put("状态", status);
|
||
onEvent(context, "游戏更新", gameName, kv);
|
||
}
|
||
|
||
}
|