Loading app/src/main/java/foundation/e/drive/utils/ServiceExceptionHandler.java +19 −5 Original line number Diff line number Diff line Loading @@ -7,6 +7,7 @@ */ package foundation.e.drive.utils; import android.app.Service; import android.content.Context; import android.os.Environment; import android.util.Log; Loading @@ -17,13 +18,14 @@ import java.io.PrintWriter; import java.io.StringWriter; import java.lang.Thread.UncaughtExceptionHandler; import foundation.e.drive.services.OperationManagerService; /** * @author Vincent Bourgmayer */ public class ServiceExceptionHandler implements UncaughtExceptionHandler{ private UncaughtExceptionHandler defaultUEH; private final static String TAG = ServiceExceptionHandler.class.getSimpleName(); Service service; Loading @@ -42,6 +44,14 @@ public class ServiceExceptionHandler implements UncaughtExceptionHandler{ @Override public void uncaughtException(Thread t, Throwable e) { Log.d(TAG, "Service class: "+service.getClass().getSimpleName()); //IF OMS is crashing, set settings that it runs to false; if(service.getClass().getSimpleName().equals(OperationManagerService.class.getSimpleName())){ service.getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE) .edit() .putBoolean(AppConstants.KEY_OMS_IS_WORKING, false) .apply(); } if(isExternalStorageAvailable() && !isExternalStorageReadOnly()){ //Get TimeStamp Loading @@ -49,22 +59,26 @@ public class ServiceExceptionHandler implements UncaughtExceptionHandler{ //Create a new file that user can sent to us String fileName = "eDrive-crash-"+timestamp+".log"; File downloadDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath()); File downloadDir = service.getApplication().getExternalFilesDir("Logs"); File logFile = new File(downloadDir, fileName); try { FileOutputStream fos = new FileOutputStream(logFile); fos.write(service.getClass().getSimpleName().getBytes()); fos.write(getStackTraceAsString(e).getBytes()); fos.close(); logFile.setReadable(true, false); } catch (IOException exception) { exception.printStackTrace(); } } //source: https://stackoverflow.com/questions/9050962/rethrow-uncaughtexceptionhandler-exception-after-logging-it/9050990#9050990 if(defaultUEH != null){ defaultUEH.uncaughtException(t, e); }else{ Log.d("ServiceExceptionHandler", "/e/ Drive has crashed and there is no ExceptionHandler"); Log.d(TAG, "/e/ Drive has crashed and there is no ExceptionHandler"); System.exit(1); //Kill /e/ Drive... } } Loading Loading
app/src/main/java/foundation/e/drive/utils/ServiceExceptionHandler.java +19 −5 Original line number Diff line number Diff line Loading @@ -7,6 +7,7 @@ */ package foundation.e.drive.utils; import android.app.Service; import android.content.Context; import android.os.Environment; import android.util.Log; Loading @@ -17,13 +18,14 @@ import java.io.PrintWriter; import java.io.StringWriter; import java.lang.Thread.UncaughtExceptionHandler; import foundation.e.drive.services.OperationManagerService; /** * @author Vincent Bourgmayer */ public class ServiceExceptionHandler implements UncaughtExceptionHandler{ private UncaughtExceptionHandler defaultUEH; private final static String TAG = ServiceExceptionHandler.class.getSimpleName(); Service service; Loading @@ -42,6 +44,14 @@ public class ServiceExceptionHandler implements UncaughtExceptionHandler{ @Override public void uncaughtException(Thread t, Throwable e) { Log.d(TAG, "Service class: "+service.getClass().getSimpleName()); //IF OMS is crashing, set settings that it runs to false; if(service.getClass().getSimpleName().equals(OperationManagerService.class.getSimpleName())){ service.getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE) .edit() .putBoolean(AppConstants.KEY_OMS_IS_WORKING, false) .apply(); } if(isExternalStorageAvailable() && !isExternalStorageReadOnly()){ //Get TimeStamp Loading @@ -49,22 +59,26 @@ public class ServiceExceptionHandler implements UncaughtExceptionHandler{ //Create a new file that user can sent to us String fileName = "eDrive-crash-"+timestamp+".log"; File downloadDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath()); File downloadDir = service.getApplication().getExternalFilesDir("Logs"); File logFile = new File(downloadDir, fileName); try { FileOutputStream fos = new FileOutputStream(logFile); fos.write(service.getClass().getSimpleName().getBytes()); fos.write(getStackTraceAsString(e).getBytes()); fos.close(); logFile.setReadable(true, false); } catch (IOException exception) { exception.printStackTrace(); } } //source: https://stackoverflow.com/questions/9050962/rethrow-uncaughtexceptionhandler-exception-after-logging-it/9050990#9050990 if(defaultUEH != null){ defaultUEH.uncaughtException(t, e); }else{ Log.d("ServiceExceptionHandler", "/e/ Drive has crashed and there is no ExceptionHandler"); Log.d(TAG, "/e/ Drive has crashed and there is no ExceptionHandler"); System.exit(1); //Kill /e/ Drive... } } Loading