package com.tencent.bugly.agent;

 import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Process;
import android.text.TextUtils;
import android.util.Log;
import java.lang.ref.WeakReference;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Locale; public class GameAgent
{
private static final String VERSION = "2.0";
private static final String LOG_TAG = "CrashReport-GameAgent";
private static final int GAME_TYPE_COCOS = 1;
private static final int GAME_TYPE_UNITY = 2;
private static final int LOG_LEVEL_VERBOSE = 0;
private static final int LOG_LEVEL_DEBUG = 1;
private static final int LOG_LEVEL_INFO = 2;
private static final int LOG_LEVEL_WARN = 3;
private static final int LOG_LEVEL_ERROR = 4;
private static final int TYPE_U3D_CRASH = 4;
private static final int TYPE_COCOS2DX_JS_CRASH = 5;
private static final int TYPE_COCOS2DX_LUA_CRASH = 6;
private static final String STRATEGY_CLASS_SUFFIX = "BuglyStrategy";
private static final String OLD_STRATEGY_CLASS_SUFFIX = "crashreport.CrashReport$UserStrategy";
private static final String CRASH_REPORT_CLASS_SUFFIX = "crashreport.CrashReport";
private static final String CLASS_UNITY_PLAYER = "com.unity3d.player.UnityPlayer";
private static final String CLASS_COCOS_ACTIVITY = "org.cocos2dx.lib.Cocos2dxActivity";
private static String sdkPackageName = "com.tencent.bugly";
private static Handler sHandler = null;
private static String sAppVersion = null;
private static String sAppChannel = null;
private static String sUserId = null;
private static boolean sIsDebug = false;
private static int sGameType = 0;
private static WeakReference<Activity> sContext = null; public static String getVersion()
{
return "2.0";
} public static void printLog(String paramString)
{
if (TextUtils.isEmpty(paramString)) {
return;
}
if (paramString.startsWith("<Log>"))
printLog(2, paramString);
else if (paramString.startsWith("<LogDebug>"))
printLog(1, paramString);
else if (paramString.startsWith("<LogInfo>"))
printLog(2, paramString);
else if (paramString.startsWith("<LogWarning>"))
printLog(3, paramString);
else if (paramString.startsWith("<LogAssert>"))
printLog(3, paramString);
else if (paramString.startsWith("<LogError>"))
printLog(4, paramString);
else if (paramString.startsWith("<LogException>"))
printLog(4, paramString);
else
printLog(0, paramString);
} private static void printLog(int paramInt, String paramString)
{
setLog(paramInt, "CrashReport-GameAgent", paramString);
} public static Activity getUnityActivity()
{
try
{
Object localObject = Reflection.getStaticField("com.unity3d.player.UnityPlayer", "currentActivity", null);
if ((localObject != null) && ((localObject instanceof Activity)))
return (Activity)localObject;
}
catch (Exception localException) {
Log.w("CrashReport-GameAgent", "Failed to get the current activity from UnityPlayer");
localException.printStackTrace();
} return null;
} public static Activity getCocosActivity()
{
try
{
Object localObject = Reflection.invokeStaticMethod("org.cocos2dx.lib.Cocos2dxActivity", "getContext", null, new Class[0]);
if ((localObject != null) && ((localObject instanceof Activity)))
return (Activity)localObject;
}
catch (Exception localException) {
Log.w("CrashReport-GameAgent", "Failed to get the current activity from UnityPlayer");
localException.printStackTrace();
} return null;
} private static Activity getActivity() {
Activity localActivity = null;
if ((sContext != null) && (sContext.get() != null)) {
return (Activity)sContext.get();
}
switch (sGameType) {
case 1:
localActivity = getCocosActivity();
break;
case 2:
localActivity = getUnityActivity();
break;
default:
Log.w("CrashReport-GameAgent", "Game type has not been set.");
} return localActivity;
} private static Context getApplicationContext() {
Activity localActivity = getActivity();
if (localActivity != null) {
return localActivity.getApplicationContext();
}
return null;
} private static void runTaskInUiThread(Runnable paramRunnable) {
Activity localActivity = getActivity();
if (localActivity != null)
localActivity.runOnUiThread(paramRunnable);
else
new Thread(paramRunnable).start();
} private static void exitApplication()
{
int i = Process.myPid();
printLog(3, String.format(Locale.US, "Exit application by kill process[%d]", new Object[] { Integer.valueOf(i) })); Process.killProcess(i);
} private static void delayExit(long paramLong)
{
paramLong = Math.max(0L, paramLong); if (sHandler != null) {
sHandler.postDelayed(new Runnable()
{
public void run() {
GameAgent.access$200();
}
}
, paramLong);
}
else
{
try
{
Thread.sleep(paramLong);
exitApplication();
} catch (InterruptedException localInterruptedException) {
localInterruptedException.printStackTrace();
}
}
} private static String convertToCanonicalName(String paramString)
{
StringBuilder localStringBuilder = new StringBuilder();
if (sdkPackageName == null) {
sdkPackageName = "com.tencent.bugly";
}
localStringBuilder.append(sdkPackageName);
localStringBuilder.append(".");
localStringBuilder.append(paramString);
return localStringBuilder.toString();
} public static void setSdkPackageName(String paramString)
{
if (TextUtils.isEmpty(paramString)) {
return;
}
sdkPackageName = paramString;
} public static void setGameType(int paramInt) {
sGameType = paramInt;
} public static void setLogEnable(boolean paramBoolean)
{
sIsDebug = paramBoolean;
} private static Object newStrategy(Context paramContext, String paramString1, String paramString2, long paramLong)
{
if ((paramContext == null) || ((TextUtils.isEmpty(paramString1)) && (TextUtils.isEmpty(paramString2)))) {
return null;
}
Object localObject = Reflection.newInstance(convertToCanonicalName("crashreport.CrashReport$UserStrategy"), new Object[] { paramContext }, new Class[] { Context.class }); if (localObject != null) {
try {
Class localClass = Class.forName(convertToCanonicalName("BuglyStrategy"));
Method localMethod1 = localClass.getDeclaredMethod("setAppChannel", new Class[] { String.class });
localMethod1.invoke(localObject, new Object[] { paramString1 }); Method localMethod2 = localClass.getDeclaredMethod("setAppVersion", new Class[] { String.class });
localMethod2.invoke(localObject, new Object[] { paramString2 }); Method localMethod3 = localClass.getDeclaredMethod("setAppReportDelay", new Class[] { Long.TYPE });
localMethod3.invoke(localObject, new Object[] { Long.valueOf(paramLong) }); return localObject;
} catch (NoSuchMethodException localNoSuchMethodException) {
localNoSuchMethodException.printStackTrace();
} catch (IllegalAccessException localIllegalAccessException) {
localIllegalAccessException.printStackTrace();
} catch (IllegalArgumentException localIllegalArgumentException) {
localIllegalArgumentException.printStackTrace();
} catch (InvocationTargetException localInvocationTargetException) {
localInvocationTargetException.printStackTrace();
} catch (Exception localException) {
localException.printStackTrace();
}
}
return null;
} public static void initCrashReport(String paramString, boolean paramBoolean) {
setLogEnable(paramBoolean);
initCrashReport(paramString, sAppChannel, sAppVersion, sUserId, 0L);
} private static void initCrashReport(final String paramString1, String paramString2, String paramString3, final String paramString4, long paramLong)
{
final Context localContext = getApplicationContext();
if (localContext == null) {
printLog(4, "Context is null. bugly initialize terminated.");
return;
}
if (TextUtils.isEmpty(paramString1)) {
printLog(4, "Please input appid when initCrashReport.");
return;
}
sHandler = new Handler(Looper.getMainLooper());
Object localObject = newStrategy(localContext, paramString2, paramString3, paramLong);
runTaskInUiThread(new Runnable()
{
public void run()
{
int i = 0;
boolean bool = GameAgent.sIsDebug;
if (this.val$strategy != null) {
Class localClass = null;
try {
localClass = Class.forName(GameAgent.convertToCanonicalName("crashreport.CrashReport$UserStrategy"));
} catch (ClassNotFoundException localClassNotFoundException) {
localClassNotFoundException.printStackTrace();
} catch (Exception localException) {
localException.printStackTrace();
}
if (localClass != null) {
GameAgent.Reflection.invokeStaticMethod(GameAgent.access$500("crashreport.CrashReport"), "initCrashReport", new Object[] { localContext, paramString1, Boolean.valueOf(bool), this.val$strategy }, new Class[] { Context.class, String.class, Boolean.TYPE, localClass }); i = 1;
}
} if (i == 0) {
GameAgent.Reflection.invokeStaticMethod(GameAgent.access$500("crashreport.CrashReport"), "initCrashReport", new Object[] { localContext, paramString1, Boolean.valueOf(bool) }, new Class[] { Context.class, String.class, Boolean.TYPE });
} GameAgent.setUserId(paramString4);
}
});
} public static void setAppVersion(String paramString)
{
if (TextUtils.isEmpty(paramString)) {
return;
}
sAppVersion = paramString;
runTaskInUiThread(new Runnable()
{
public void run() {
GameAgent.Reflection.invokeStaticMethod(GameAgent.access$500("crashreport.CrashReport"), "setAppVersion", new Object[] { GameAgent.access$600(), this.val$version }, new Class[] { Context.class, String.class });
}
});
} public static void setAppChannel(String paramString)
{
if (TextUtils.isEmpty(paramString)) {
return;
}
sAppChannel = paramString;
runTaskInUiThread(new Runnable()
{
public void run() {
GameAgent.Reflection.invokeStaticMethod(GameAgent.access$500("crashreport.CrashReport"), "setAppChannel", new Object[] { GameAgent.access$600(), this.val$channel }, new Class[] { Context.class, String.class });
}
});
} public static void setUserId(String paramString)
{
if (TextUtils.isEmpty(paramString)) {
return;
}
sUserId = paramString;
runTaskInUiThread(new Runnable()
{
public void run() {
GameAgent.Reflection.invokeStaticMethod(GameAgent.access$500("crashreport.CrashReport"), "setUserId", new Object[] { GameAgent.access$600(), this.val$userId }, new Class[] { Context.class, String.class });
}
});
} public static void setUserSceneTag(int paramInt)
{
runTaskInUiThread(new Runnable()
{
public void run() {
GameAgent.Reflection.invokeStaticMethod(GameAgent.access$500("crashreport.CrashReport"), "setUserSceneTag", new Object[] { GameAgent.access$600(), Integer.valueOf(this.val$sceneId) }, new Class[] { Context.class, Integer.TYPE });
}
});
} public static void putUserData(String paramString1, final String paramString2)
{
if ((TextUtils.isEmpty(paramString1)) || (TextUtils.isEmpty(paramString2))) {
return;
}
runTaskInUiThread(new Runnable()
{
public void run() {
GameAgent.Reflection.invokeStaticMethod(GameAgent.access$500("crashreport.CrashReport"), "putUserData", new Object[] { GameAgent.access$600(), this.val$key, paramString2 }, new Class[] { Context.class, String.class, String.class });
}
});
} public static void removeUserData(String paramString)
{
if (TextUtils.isEmpty(paramString)) {
return;
}
runTaskInUiThread(new Runnable()
{
public void run() {
GameAgent.Reflection.invokeStaticMethod(GameAgent.access$500("crashreport.CrashReport"), "removeUserData", new Object[] { GameAgent.access$600(), this.val$key }, new Class[] { Context.class, String.class });
}
});
} public static void setSdkConfig(String paramString1, final String paramString2)
{
if ((TextUtils.isEmpty(paramString1)) || (TextUtils.isEmpty(paramString2))) {
return;
}
runTaskInUiThread(new Runnable()
{
public void run() {
GameAgent.Reflection.invokeStaticMethod(GameAgent.access$500("crashreport.CrashReport"), "putSdkData", new Object[] { GameAgent.access$600(), "SDK_" + this.val$key, paramString2 }, new Class[] { Context.class, String.class, String.class });
}
});
} public static void setLog(int paramInt, final String paramString1, final String paramString2)
{
if (TextUtils.isEmpty(paramString1))
return;
String str;
switch (paramInt) {
case 0:
str = "v";
break;
case 1:
str = "d";
break;
case 2:
str = "i";
break;
case 3:
str = "w";
break;
case 4:
str = "e";
break;
default:
str = null;
} if (str != null)
runTaskInUiThread(new Runnable()
{
public void run() {
GameAgent.Reflection.invokeStaticMethod(GameAgent.access$500("crashreport.BuglyLog"), this.val$method, new Object[] { paramString1, paramString2 }, new Class[] { String.class, String.class });
}
});
} private static void postCocosException(int paramInt, String paramString1, final String paramString2, String paramString3, final boolean paramBoolean)
{
try
{
if (paramString3.startsWith("stack traceback")) {
paramString3 = paramString3.substring(paramString3.indexOf("\n") + 1, paramString3.length()).trim();
} int i = paramString3.indexOf("\n");
if (i > 0) {
paramString3 = paramString3.substring(i + 1, paramString3.length());
} i = paramString3.indexOf("\n");
str2 = paramString3;
if (i > 0) {
str2 = paramString3.substring(0, i);
} int j = str2.indexOf("]:");
if ((paramString1 == null) || (paramString1.length() == 0)) {
if (j != -1)
paramString1 = str2.substring(0, j + 1);
else
paramString1 = paramString2;
}
}
catch (Throwable localThrowable)
{
if ((paramString1 == null) || (paramString1.length() == 0)) {
paramString1 = paramString2;
}
}
final String str1 = paramString1;
final String str2 = paramString3;
runTaskInUiThread(new Runnable()
{
public void run() {
GameAgent.Reflection.invokeStaticMethod(GameAgent.access$500("crashreport.inner.InnerApi"), "postCocos2dxCrashAsync", new Object[] { Integer.valueOf(this.val$category), str1, paramString2, str2 }, new Class[] { Integer.TYPE, String.class, String.class, String.class }); if (paramBoolean)
GameAgent.delayExit(3000L);
}
});
} private static void postUnityException(String paramString1, final String paramString2, final String paramString3, final boolean paramBoolean)
{
runTaskInUiThread(new Runnable()
{
public void run() {
GameAgent.Reflection.invokeStaticMethod(GameAgent.access$500("crashreport.inner.InnerApi"), "postU3dCrashAsync", new Object[] { this.val$type, paramString2, paramString3 }, new Class[] { String.class, String.class, String.class }); if (paramBoolean)
GameAgent.delayExit(3000L);
}
});
} public static void postException(int paramInt, String paramString1, String paramString2, String paramString3, boolean paramBoolean)
{
switch (paramInt) {
case 5:
case 6:
postCocosException(paramInt, paramString1, paramString2, paramString3, paramBoolean);
break;
case 4:
postUnityException(paramString1, paramString2, paramString3, paramBoolean);
break;
default:
printLog(4, new StringBuilder().append("The category of exception posted is unknown: ").append(String.valueOf(paramInt)).toString());
}
} private static class Reflection
{
private static Object getStaticField(String paramString1, String paramString2, Object paramObject)
{
try
{
Class localClass = Class.forName(paramString1); Field localField = localClass.getDeclaredField(paramString2);
localField.setAccessible(true);
return localField.get(paramObject);
} catch (ClassNotFoundException localClassNotFoundException) {
localClassNotFoundException.printStackTrace();
} catch (NoSuchFieldException localNoSuchFieldException) {
localNoSuchFieldException.printStackTrace();
} catch (IllegalAccessException localIllegalAccessException) {
localIllegalAccessException.printStackTrace();
} return null;
} private static Object invokeStaticMethod(String paramString1, String paramString2, Object[] paramArrayOfObject, Class<?>[] paramArrayOfClass)
{
try
{
Class localClass = Class.forName(paramString1);
Method localMethod = localClass.getDeclaredMethod(paramString2, paramArrayOfClass);
localMethod.setAccessible(true);
return localMethod.invoke(null, paramArrayOfObject);
} catch (ClassNotFoundException localClassNotFoundException) {
localClassNotFoundException.printStackTrace();
} catch (NoSuchMethodException localNoSuchMethodException) {
localNoSuchMethodException.printStackTrace();
} catch (InvocationTargetException localInvocationTargetException) {
localInvocationTargetException.printStackTrace();
} catch (IllegalAccessException localIllegalAccessException) {
localIllegalAccessException.printStackTrace();
} catch (Exception localException) {
localException.printStackTrace();
}
return null;
} private static Object newInstance(String paramString, Object[] paramArrayOfObject, Class<?>[] paramArrayOfClass)
{
try
{
Class localClass = Class.forName(paramString);
if (paramArrayOfObject == null) {
return localClass.newInstance();
}
Constructor localConstructor = localClass.getConstructor(paramArrayOfClass);
return localConstructor.newInstance(paramArrayOfObject);
}
catch (ClassNotFoundException localClassNotFoundException) {
localClassNotFoundException.printStackTrace();
} catch (NoSuchMethodException localNoSuchMethodException) {
localNoSuchMethodException.printStackTrace();
} catch (InstantiationException localInstantiationException) {
localInstantiationException.printStackTrace();
} catch (IllegalAccessException localIllegalAccessException) {
localIllegalAccessException.printStackTrace();
} catch (InvocationTargetException localInvocationTargetException) {
localInvocationTargetException.printStackTrace();
} catch (Exception localException) {
localException.printStackTrace();
}
return null;
}
}
}
 private static readonly string GAME_AGENT_CLASS = "com.tencent.bugly.agent.GameAgent";
private static readonly int TYPE_U3D_CRASH = ;
private static readonly int GAME_TYPE_UNITY = ;
private static bool hasSetGameType = false;
private static AndroidJavaClass _gameAgentClass = null; public static AndroidJavaClass GameAgent {
get {
if (_gameAgentClass == null) {
_gameAgentClass = new AndroidJavaClass(GAME_AGENT_CLASS);
// using (AndroidJavaClass clazz = new AndroidJavaClass(CLASS_UNITYAGENT)) {
// _gameAgentClass = clazz.CallStatic<AndroidJavaObject> ("getInstance");
// }
}
if (!hasSetGameType) {
// set game type: unity(2).
_gameAgentClass.CallStatic ("setGameType", GAME_TYPE_UNITY);
hasSetGameType = true;
}
return _gameAgentClass;
}
}
05-11 19:26