Created
March 2, 2016 15:21
-
-
Save schmittsfn/afe33227383db9e0de8e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package net.hockeyapp.android; | |
| import java.io.BufferedWriter; | |
| import java.io.File; | |
| import java.io.FileNotFoundException; | |
| import java.io.FileWriter; | |
| import java.io.FilenameFilter; | |
| import java.util.Date; | |
| import java.util.UUID; | |
| import org.apache.http.Header; | |
| import org.json.JSONObject; | |
| import com.loopj.android.http.*; | |
| import android.app.Activity; | |
| import android.util.Log; | |
| /** | |
| * Created by stefan on 2/26/16. | |
| */ | |
| public class NativeCrashManager { | |
| private static String TAG = "NativeCrashManager"; | |
| public static void handleDumpFiles(Activity activity, String identifier) { | |
| String[] filenames = searchForDumpFiles(); | |
| for (String dumpFilename : filenames) { | |
| String logFilename = createLogFile(); | |
| if (logFilename != null) { | |
| uploadDumpAndLog(activity, identifier, dumpFilename, logFilename); | |
| } | |
| } | |
| } | |
| public static String createLogFile() { | |
| final Date now = new Date(); | |
| try { | |
| // Create filename from a random uuid | |
| String filename = UUID.randomUUID().toString(); | |
| String path = Constants.FILES_PATH + "/" + filename + ".faketrace"; | |
| Log.d(TAG, "Writing unhandled exception to: " + path); | |
| // Write the stacktrace to disk | |
| BufferedWriter write = new BufferedWriter(new FileWriter(path)); | |
| write.write("Package: " + Constants.APP_PACKAGE + "\n"); | |
| write.write("Version Code: " + Constants.APP_VERSION + "\n"); | |
| write.write("Version Name: " + Constants.APP_VERSION_NAME + "\n"); | |
| write.write("Android: " + Constants.ANDROID_VERSION + "\n"); | |
| write.write("Manufacturer: " + Constants.PHONE_MANUFACTURER + "\n"); | |
| write.write("Model: " + Constants.PHONE_MODEL + "\n"); | |
| write.write("Date: " + now + "\n"); | |
| write.write("\n"); | |
| write.write("MinidumpContainer"); | |
| write.flush(); | |
| write.close(); | |
| return filename + ".faketrace"; | |
| } | |
| catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| return null; | |
| } | |
| public static void uploadDumpAndLog(final Activity activity, final String identifier, final String dumpFilename, final String logFilename) { | |
| try { | |
| RequestParams params = new RequestParams(); | |
| File dumpFile = new File(Constants.FILES_PATH, dumpFilename); | |
| params.put("attachment0", dumpFile); | |
| File logFile = new File(Constants.FILES_PATH, logFilename); | |
| params.put("log", logFile); | |
| AsyncHttpClient client = new AsyncHttpClient(); | |
| client.post("https://rink.hockeyapp.net/api/2/apps/" + identifier + "/crashes/upload",params, new AsyncHttpResponseHandler() { | |
| @Override | |
| public void onStart() { | |
| // called before request is started | |
| } | |
| @Override | |
| public void onSuccess(int statusCode, Header[] headers, byte[] response) { | |
| activity.deleteFile(logFilename); | |
| activity.deleteFile(dumpFilename); | |
| } | |
| @Override | |
| public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) { | |
| // called when response HTTP status is "4XX" (eg. 401, 403, 404) | |
| Log.e(TAG,e.getLocalizedMessage()); | |
| } | |
| @Override | |
| public void onRetry(int retryNo) { | |
| // called when request is retried | |
| } | |
| }); | |
| } | |
| catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| private static String[] searchForDumpFiles() { | |
| if (Constants.FILES_PATH != null) { | |
| // Try to create the files folder if it doesn't exist | |
| File dir = new File(Constants.FILES_PATH + "/"); | |
| boolean created = dir.mkdir(); | |
| if (!created && !dir.exists()) { | |
| return new String[0]; | |
| } | |
| // Filter for ".dmp" files | |
| FilenameFilter filter = new FilenameFilter() { | |
| public boolean accept(File dir, String name) { | |
| return name.endsWith(".dmp"); | |
| } | |
| }; | |
| return dir.list(filter); | |
| } | |
| else { | |
| Log.d(TAG, "Can't search for exception as file path is null."); | |
| return new String[0]; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment