This repository has been archived by the owner on Jul 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
88 additions
and
4 deletions.
There are no files selected for viewing
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
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
77 changes: 77 additions & 0 deletions
77
app/src/main/java/ar/rulosoft/mimanganu/utils/CustomExceptionHandler.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package ar.rulosoft.mimanganu.utils; | ||
|
||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
import android.os.Environment; | ||
import android.preference.PreferenceManager; | ||
import android.text.format.DateFormat; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.io.Writer; | ||
|
||
// taken from https://stackoverflow.com/questions/601503/how-do-i-obtain-crash-data-from-my-android-application | ||
// rrainn / Prags | ||
// only minimal changes | ||
|
||
|
||
public class CustomExceptionHandler implements Thread.UncaughtExceptionHandler { | ||
|
||
private Thread.UncaughtExceptionHandler defaultUEH; | ||
|
||
private String localPath; | ||
private Context ctx; | ||
|
||
|
||
/* | ||
* if any of the parameters is null, the respective functionality | ||
* will not be used | ||
*/ | ||
public CustomExceptionHandler(String localPath) { | ||
this.localPath = localPath; | ||
this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler(); | ||
} | ||
|
||
public static void Attach(Context ctx) { | ||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx); | ||
String dir = prefs.getString("directorio", Environment.getExternalStorageDirectory().getAbsolutePath()) + "/MiMangaNu/logs/"; | ||
if (!new File(dir).exists()) { | ||
new File(dir).mkdir(); | ||
} | ||
if (!(Thread.getDefaultUncaughtExceptionHandler() instanceof CustomExceptionHandler)) { | ||
Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler(dir)); | ||
} | ||
} | ||
|
||
public void uncaughtException(Thread t, Throwable e) { | ||
String timestamp = (DateFormat.format("dd-MM-yyyy--hh-mm-ss", new java.util.Date()).toString()); | ||
final Writer result = new StringWriter(); | ||
final PrintWriter printWriter = new PrintWriter(result); | ||
e.printStackTrace(printWriter); | ||
String stacktrace = result.toString(); | ||
printWriter.close(); | ||
String filename = timestamp + ".txt"; | ||
|
||
if (localPath != null) { | ||
writeToFile(stacktrace, filename); | ||
} | ||
defaultUEH.uncaughtException(t, e); | ||
} | ||
|
||
private void writeToFile(String stacktrace, String filename) { | ||
try { | ||
BufferedWriter bos = new BufferedWriter(new FileWriter( | ||
localPath + "/" + filename)); | ||
bos.write(stacktrace); | ||
bos.flush(); | ||
bos.close(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
|
||
} |
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
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