Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit dbcfd22f authored by vince-bourgmayer's avatar vince-bourgmayer
Browse files

add method to setUEH for thread

parent 50eda989
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ContentResolver;
import android.content.Context;
import android.media.MediaScannerConnection;
@@ -40,6 +41,25 @@ import static foundation.e.drive.utils.AppConstants.SETTINGSYNC_PROVIDER_AUTHORI
public  abstract class CommonUtils {
    final private static String TAG = CommonUtils.class.getSimpleName();


    /**
     * Set ServiceUncaughtExceptionHandler to be the MainThread Exception Handler
     * Or update the service which use it
     * @param service current service
     */
    public static void setServiceUnCaughtExceptionHandler(Service service){

        Thread.UncaughtExceptionHandler defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
        if(defaultUEH.getClass().getSimpleName().equals(ServiceExceptionHandler.class.getSimpleName())){
            Log.d("ObserverService", "ServiceExceptionHandler already set!");
            ((ServiceExceptionHandler) defaultUEH).setService(service);
        }else{
            Thread.setDefaultUncaughtExceptionHandler(new ServiceExceptionHandler(service));
        }
    }



    /**
     * Unregister from screeOffReceiver component
     * @param context app context
+21 −5
Original line number Diff line number Diff line
@@ -7,6 +7,8 @@
 */
package foundation.e.drive.utils;
import android.app.Service;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.util.Log;

@@ -17,14 +19,23 @@ 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 final static String TAG = ServiceExceptionHandler.class.getSimpleName();
    private UncaughtExceptionHandler defaultUEH;
    private Service service;

    Service service;
    /**
     * Change the service that could trigger an UncaughtException
     * @param service service instance
     */
    public void setService(Service service){
        this.service = service;
    }

    public ServiceExceptionHandler(Service service) {
        this.service = service;
@@ -33,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
@@ -42,22 +61,19 @@ public class ServiceExceptionHandler implements UncaughtExceptionHandler{
            String fileName = "eDrive-crash-"+timestamp+".log";

            File downloadDir = service.getApplication().getExternalFilesDir("Logs");
            //File downloadDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath());
            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);

                //DID NOT WORKS: CommonUtils.doActionMediaScannerConnexionScanFile(service, logFile.getCanonicalPath());

            } 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);