Files
FairEmail/app/src/main/java/eu/faircode/email/ApplicationEx.java

175 lines
6.0 KiB
Java
Raw Normal View History

2018-08-02 13:33:06 +00:00
package eu.faircode.email;
/*
2018-08-14 05:53:24 +00:00
This file is part of FairEmail.
2018-08-02 13:33:06 +00:00
2018-08-14 05:53:24 +00:00
FairEmail is free software: you can redistribute it and/or modify
2018-08-02 13:33:06 +00:00
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2018-10-29 10:46:49 +00:00
FairEmail is distributed in the hope that it will be useful,
2018-08-02 13:33:06 +00:00
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
2018-10-29 10:46:49 +00:00
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2018-08-02 13:33:06 +00:00
Copyright 2018 by Marcel Bokhorst (M66B)
*/
import android.app.Application;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
2018-10-15 06:02:56 +00:00
import android.os.Build;
import android.os.DeadSystemException;
import android.os.RemoteException;
2018-08-03 18:07:12 +00:00
import android.util.Log;
2018-12-07 15:35:03 +01:00
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
2018-08-11 04:41:26 +00:00
import java.io.File;
2018-12-07 15:35:03 +01:00
import java.io.FileOutputStream;
2018-08-11 04:41:26 +00:00
import java.io.FileWriter;
import java.io.IOException;
2018-12-07 15:35:03 +01:00
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Date;
2018-08-02 13:33:06 +00:00
public class ApplicationEx extends Application {
2018-08-03 18:07:12 +00:00
private Thread.UncaughtExceptionHandler prev = null;
@Override
public void onCreate() {
super.onCreate();
prev = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
2018-09-14 08:31:49 +00:00
if (ownFault(ex)) {
Log.e(Helper.TAG, ex + "\r\n" + Log.getStackTraceString(ex));
2018-12-01 13:17:33 +01:00
writeCrashLog(ex);
2018-08-08 13:05:43 +00:00
2018-09-14 08:31:49 +00:00
if (prev != null)
prev.uncaughtException(thread, ex);
} else {
Log.w(Helper.TAG, ex + "\r\n" + Log.getStackTraceString(ex));
System.exit(1);
2018-08-03 18:07:12 +00:00
}
}
});
2018-09-14 08:31:49 +00:00
createNotificationChannels();
}
private void createNotificationChannels() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationManager nm = getSystemService(NotificationManager.class);
NotificationChannel service = new NotificationChannel(
"service",
getString(R.string.channel_service),
NotificationManager.IMPORTANCE_MIN);
service.setSound(null, Notification.AUDIO_ATTRIBUTES_DEFAULT);
service.setShowBadge(false);
service.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
nm.createNotificationChannel(service);
NotificationChannel notification = new NotificationChannel(
"notification",
getString(R.string.channel_notification),
NotificationManager.IMPORTANCE_HIGH);
notification.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
nm.createNotificationChannel(notification);
NotificationChannel error = new NotificationChannel(
"error",
getString(R.string.channel_error),
NotificationManager.IMPORTANCE_HIGH);
error.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
nm.createNotificationChannel(error);
}
2018-08-03 18:07:12 +00:00
}
2018-09-14 08:31:49 +00:00
public boolean ownFault(Throwable ex) {
2018-09-30 15:43:16 +00:00
//if (!Helper.isPlayStoreInstall(this))
// return true;
2018-09-14 08:31:49 +00:00
if (ex instanceof OutOfMemoryError)
return false;
2018-09-14 08:31:49 +00:00
if (ex instanceof RemoteException)
return false;
2018-10-15 06:02:56 +00:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
if (ex instanceof RuntimeException && ex.getCause() instanceof DeadSystemException)
return false;
2018-09-14 08:31:49 +00:00
while (ex != null) {
for (StackTraceElement ste : ex.getStackTrace())
if (ste.getClassName().startsWith(getPackageName()))
return true;
ex = ex.getCause();
}
return false;
}
2018-12-01 13:17:33 +01:00
private void writeCrashLog(Throwable ex) {
File file = new File(getCacheDir(), "crash.log");
2018-09-14 08:31:49 +00:00
Log.w(Helper.TAG, "Writing exception to " + file);
FileWriter out = null;
try {
out = new FileWriter(file);
2018-12-07 15:35:03 +01:00
out.write(new Date() + "\r\n");
out.write(ex.toString() + "\r\n");
out.write(Log.getStackTraceString(ex) + "\r\n");
out.write("\r\n");
Process proc = null;
BufferedReader br = null;
OutputStream os = null;
try {
os = new BufferedOutputStream(new FileOutputStream(file));
String[] cmd = new String[]{"logcat",
"-d",
"-v", "threadtime",
//"-t", "1000",
Helper.TAG + ":I"};
proc = Runtime.getRuntime().exec(cmd);
br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line;
while ((line = br.readLine()) != null)
out.write(line + "\r\n");
} catch (Throwable exex) {
out.write(exex.toString());
} finally {
if (os != null)
os.close();
if (br != null)
br.close();
if (proc != null)
proc.destroy();
}
2018-09-14 08:31:49 +00:00
} catch (IOException e) {
Log.e(Helper.TAG, e + "\n" + Log.getStackTraceString(e));
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
Log.e(Helper.TAG, e + "\n" + Log.getStackTraceString(e));
}
}
}
}
2018-08-02 13:33:06 +00:00
}