11package org .domogik .butler ;
22
33import android .app .Application ;
4+ import android .os .Environment ;
45
56import com .github .anrwatchdog .ANRWatchDog ;
67
78import org .acra .ACRA ;
89import org .acra .ReportingInteractionMode ;
910import org .acra .annotation .ReportsCrashes ;
1011
12+ import java .io .File ;
13+ import java .io .IOException ;
14+
1115import static org .acra .ReportField .ANDROID_VERSION ;
1216import static org .acra .ReportField .LOGCAT ;
1317import static org .acra .ReportField .PHONE_MODEL ;
@@ -29,11 +33,67 @@ public class ButlerCrashReporter extends Application {
2933 @ Override
3034 public void onCreate () {
3135 super .onCreate ();
36+
37+ /******************************************************************
38+ Handle the crashes to send a report email with the logs
39+ ******************************************************************/
3240 // The following line triggers the initialization of ACRA
3341 ACRA .init (this );
3442 ACRA .getErrorReporter ().putCustomData ("Release" , getResources ().getString (R .string .dummy_content ));
3543 new ANRWatchDog ().start ();
3644
45+ /******************************************************************
46+ Log to a file
47+ ******************************************************************/
48+
49+ if ( isExternalStorageWritable () ) {
50+
51+ File appDirectory = new File ( Environment .getExternalStorageDirectory () + "/ButlerAndroid" );
52+ File logDirectory = new File ( appDirectory + "/log" );
53+ File logFile = new File ( logDirectory , "logcat" + System .currentTimeMillis () + ".txt" );
54+
55+ // create app folder
56+ if ( !appDirectory .exists () ) {
57+ appDirectory .mkdir ();
58+ }
59+
60+ // create log folder
61+ if ( !logDirectory .exists () ) {
62+ logDirectory .mkdir ();
63+ }
3764
65+ // clear the previous logcat and then write the new one to the file
66+ try {
67+ Process process = Runtime .getRuntime ().exec ( "logcat -c" );
68+ process = Runtime .getRuntime ().exec ( "logcat -f " + logFile + " *:I " );
69+ } catch ( IOException e ) {
70+ e .printStackTrace ();
71+ }
72+
73+ } else if ( isExternalStorageReadable () ) {
74+ // only readable
75+ } else {
76+ // not accessible
77+ }
78+ }
79+
80+ /* Checks if external storage is available for read and write */
81+ public boolean isExternalStorageWritable () {
82+ String state = Environment .getExternalStorageState ();
83+ if ( Environment .MEDIA_MOUNTED .equals ( state ) ) {
84+ return true ;
85+ }
86+ return false ;
3887 }
88+
89+ /* Checks if external storage is available to at least read */
90+ public boolean isExternalStorageReadable () {
91+ String state = Environment .getExternalStorageState ();
92+ if ( Environment .MEDIA_MOUNTED .equals ( state ) ||
93+ Environment .MEDIA_MOUNTED_READ_ONLY .equals ( state ) ) {
94+ return true ;
95+ }
96+ return false ;
97+ }
98+
3999}
0 commit comments