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

Commit 135708d1 authored by vince-bourgmayer's avatar vince-bourgmayer
Browse files

add method in CommonUtils to set the ServiceUncaughtExceptionHandler

parent 74821d3e
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ package foundation.e.drive.utils;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Service;
import android.content.ContentResolver;
import android.content.Context;
import android.media.MediaScannerConnection;
@@ -39,6 +40,21 @@ 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
+10 −0
Original line number Diff line number Diff line
@@ -23,8 +23,18 @@ import java.lang.Thread.UncaughtExceptionHandler;
public class ServiceExceptionHandler implements UncaughtExceptionHandler{
    private UncaughtExceptionHandler defaultUEH;



    Service service;

    /**
     * Update the service which use this handler
     * @param service current running service
     */
    public void setService(Service service) {
        this.service = service;
    }

    public ServiceExceptionHandler(Service service) {
        this.service = service;
        defaultUEH = Thread.getDefaultUncaughtExceptionHandler();