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

Commit 23fcabc5 authored by Vincent Bourgmayer's avatar Vincent Bourgmayer
Browse files

Merge branch '000-refactor-remove-serviceExceptionHandler' into 'main'

chore: remove unused class: ServiceExceptionHandler

See merge request !289
parents e1887d94 6e6e0c0e
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
# eDrive

eDrive is a [persistent][doc-persistence] application provided by default with /e/OS.
eDrive is an application provided by default with /e/OS.
It synchronizes user's data files to /e/Cloud or a self-hosted cloud.

## Install
+0 −100
Original line number Diff line number Diff line
/*
 * Copyright © CLEUS SAS 2018-2019.
 * Copyright © ECORP SAS 2022.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 */
package foundation.e.drive.utils;
import android.annotation.SuppressLint;
import android.app.Service;
import android.os.Environment;

import androidx.annotation.NonNull;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.Thread.UncaughtExceptionHandler;

/**
 * todo: check if still usefull or if it can be remove
 * @author Vincent Bourgmayer
 */
public class ServiceExceptionHandler implements UncaughtExceptionHandler {
    public final static String CRASH_LOG_FOLDER = "crash-logs";
    public final static String LOG_FILE_NAME_PREFIX = "eDrive-crash-";
    public final static String LOG_FILE_EXTENSION = ".log";
    private final UncaughtExceptionHandler defaultUEH;

    Service service;

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

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

    @SuppressLint("SetWorldReadable")
    @Override
    public void uncaughtException(@NonNull Thread t, @NonNull Throwable exception) {

        if (isExternalStorageAvailable() && !isExternalStorageReadOnly()) {

            final long timestamp = System.currentTimeMillis();

            final String fileName = LOG_FILE_NAME_PREFIX+timestamp+LOG_FILE_EXTENSION;

            final File downloadDir = service.getApplication().getExternalFilesDir(CRASH_LOG_FOLDER);
            final File logFile = new File(downloadDir, fileName);
            try (FileOutputStream fos = new FileOutputStream(logFile)) {
                fos.write(service.getClass().getSimpleName().getBytes());
                fos.write(getStackTraceAsString(exception).getBytes());
                logFile.setReadable(true, false);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
        //source: https://stackoverflow.com/questions/9050962/rethrow-uncaughtexceptionhandler-exception-after-logging-it/9050990#9050990
        if (defaultUEH != null) {
            defaultUEH.uncaughtException(t, exception);
        } else {
            System.exit(1); //Kill /e/ Drive...
        }
    }

    //source: https://www.journaldev.com/9400/android-external-storage-read-write-save-file
    private static boolean isExternalStorageAvailable() {
        final String extStorageState = Environment.getExternalStorageState();
        return Environment.MEDIA_MOUNTED.equals(extStorageState);
    }

    //source: https://www.journaldev.com/9400/android-external-storage-read-write-save-file
    private static boolean isExternalStorageReadOnly() {
        final String extStorageState = Environment.getExternalStorageState();
        return Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState);
    }

    /**
     * Return the stackTrace of the exception as a String
     * @param exception the exception
     * @return the Stacktrace as a string
     */
    @NonNull
    private String getStackTraceAsString(Throwable exception) {
        final StringWriter sw = new StringWriter();
        final PrintWriter pw = new PrintWriter(sw);
        exception.printStackTrace(pw);
        return sw.toString();
    }
}
 No newline at end of file