Created
October 31, 2017 02:24
-
-
Save 5ZSQ/860440a5e23721b0ef736f7d006988d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Source code recreated from a .class file by IntelliJ IDEA | |
// (powered by Fernflower decompiler) | |
// | |
import android.content.Context; | |
import android.content.pm.PackageInfo; | |
import android.content.pm.PackageManager; | |
import android.content.pm.PackageManager.NameNotFoundException; | |
import android.net.ConnectivityManager; | |
import android.net.NetworkInfo; | |
import android.net.NetworkInfo.State; | |
import android.os.Build; | |
import android.os.Environment; | |
import android.os.Looper; | |
import android.os.Process; | |
import android.util.Log; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.FilenameFilter; | |
import java.io.PrintWriter; | |
import java.io.StringWriter; | |
import java.io.Writer; | |
import java.lang.Thread.UncaughtExceptionHandler; | |
import java.lang.reflect.Field; | |
import java.sql.Date; | |
import java.text.SimpleDateFormat; | |
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.Iterator; | |
import java.util.Map; | |
import java.util.Properties; | |
import java.util.TreeSet; | |
import java.util.Map.Entry; | |
public class CrashHandle implements UncaughtExceptionHandler { | |
public static final String TAG = "CrashHandle"; | |
private UncaughtExceptionHandler mDefaultHandler; | |
private static CrashHandle instance = new CrashHandle(); | |
private Context mcontext; | |
private Properties mDeviceCrashInfo = new Properties(); | |
private Map<String, String> mDevInfos = new HashMap(); | |
private SimpleDateFormat formatdate = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); | |
private static final String CRASH_REPORTER_EXTENSION = ".cr"; | |
private CrashHandle() { | |
} | |
public static CrashHandle getInstance() { | |
if(instance == null) { | |
instance = new CrashHandle(); | |
} | |
return instance; | |
} | |
public void init(Context context) { | |
this.mcontext = context; | |
this.mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler(); | |
Thread.setDefaultUncaughtExceptionHandler(this); | |
} | |
public void uncaughtException(Thread thread, Throwable ex) { | |
if(!this.handleException(ex) && this.mDefaultHandler != null) { | |
this.mDefaultHandler.uncaughtException(thread, ex); | |
} else { | |
try { | |
Thread.sleep(3000L); | |
} catch (InterruptedException var4) { | |
Log.e("CrashHandle", "error"); | |
} | |
Process.killProcess(Process.myPid()); | |
System.exit(1); | |
} | |
} | |
private boolean handleException(Throwable ex) { | |
if(ex == null) { | |
return false; | |
} else { | |
String msg = ex.getLocalizedMessage(); | |
(new Thread() { | |
public void run() { | |
Looper.prepare(); | |
Looper.loop(); | |
} | |
}).start(); | |
this.collectDeviceInfo(this.mcontext); | |
this.saveCrashInfo2File(ex); | |
this.sendCrashReportsToServer(this.mcontext); | |
return true; | |
} | |
} | |
public void sendPreviousReportsToServer() { | |
this.sendCrashReportsToServer(this.mcontext); | |
} | |
private void sendCrashReportsToServer(Context mcontext) { | |
String[] crFiles = this.getCrashReportFiles(mcontext); | |
if(crFiles != null && crFiles.length > 0) { | |
TreeSet<String> sortedFiles = new TreeSet(); | |
sortedFiles.addAll(Arrays.asList(crFiles)); | |
Iterator var5 = sortedFiles.iterator(); | |
while(var5.hasNext()) { | |
String fileName = (String)var5.next(); | |
File cr = new File(mcontext.getFilesDir(), fileName); | |
this.postReport(cr); | |
} | |
} | |
} | |
private void postReport(File cr) { | |
} | |
private String[] getCrashReportFiles(Context mcontext) { | |
File filesDir = mcontext.getFilesDir(); | |
FilenameFilter filter = new FilenameFilter() { | |
public boolean accept(File dir, String filename) { | |
return filename.endsWith(".cr"); | |
} | |
}; | |
return filesDir.list(filter); | |
} | |
private String saveCrashInfo2File(Throwable ex) { | |
StringBuffer buffer = new StringBuffer(); | |
Iterator var4 = this.mDevInfos.entrySet().iterator(); | |
String result; | |
while(var4.hasNext()) { | |
Entry<String, String> entry = (Entry)var4.next(); | |
String key = (String)entry.getKey(); | |
result = (String)entry.getValue(); | |
buffer.append(key + "=" + result + '\n'); | |
} | |
Writer writer = new StringWriter(); | |
PrintWriter printWriter = new PrintWriter(writer); | |
ex.printStackTrace(printWriter); | |
for(Throwable cause = ex.getCause(); cause != null; cause = cause.getCause()) { | |
cause.printStackTrace(); | |
} | |
printWriter.close(); | |
result = writer.toString(); | |
buffer.append(result); | |
try { | |
long timestamp = System.currentTimeMillis(); | |
String time = this.formatdate.format(new Date(timestamp)); | |
String fileName = "crash-" + time + "-" + timestamp + ".log"; | |
if(Environment.getExternalStorageState().equals("mounted")) { | |
String path = "/sdcard/crash/"; | |
File file = new File(path); | |
if(!file.exists()) { | |
file.mkdirs(); | |
} | |
FileOutputStream trace = this.mcontext.openFileOutput(path + fileName, 0); | |
this.mDeviceCrashInfo.store(trace, ""); | |
trace.flush(); | |
trace.close(); | |
FileOutputStream fos = new FileOutputStream(path + fileName); | |
fos.write(buffer.toString().getBytes()); | |
fos.flush(); | |
fos.close(); | |
} | |
return fileName; | |
} catch (Exception var15) { | |
Log.e("CrashHandle", "an error occured while writing file...", var15); | |
return null; | |
} | |
} | |
private void collectDeviceInfo(Context ctx) { | |
try { | |
PackageManager pm = ctx.getPackageManager(); | |
PackageInfo pi = pm.getPackageInfo(ctx.getPackageName(), 1); | |
if(pi != null) { | |
String versionName = pi.versionName == null?"null":pi.versionName; | |
String versionCode = String.valueOf(pi.versionCode); | |
this.mDevInfos.put("versionName", versionName); | |
this.mDevInfos.put("versionCode", versionCode); | |
} | |
} catch (NameNotFoundException var9) { | |
Log.v("CrashHandle", "NameNotFoundException"); | |
} | |
Field[] fields = Build.class.getDeclaredFields(); | |
Field[] var6 = fields; | |
int var13 = fields.length; | |
for(int var12 = 0; var12 < var13; ++var12) { | |
Field field = var6[var12]; | |
try { | |
field.setAccessible(true); | |
this.mDevInfos.put(field.getName(), field.get((Object)null).toString()); | |
Log.v("CrashHandle", field.getName() + ":" + field.get((Object)null).toString()); | |
} catch (Exception var8) { | |
Log.e("CrashHandle", "an error occured when collect crash info", var8); | |
} | |
} | |
} | |
public static boolean isNetworkAvailable(Context context) { | |
ConnectivityManager mgr = (ConnectivityManager)context.getSystemService("connectivity"); | |
NetworkInfo[] info = mgr.getAllNetworkInfo(); | |
if(info != null) { | |
for(int i = 0; i < info.length; ++i) { | |
if(info[i].getState() == State.CONNECTED) { | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment