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

141 lines
5.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
2018-12-31 08:04:33 +00:00
Copyright 2018-2019 by Marcel Bokhorst (M66B)
2018-08-02 13:33:06 +00:00
*/
import android.app.Application;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
2018-12-09 17:49:52 +00:00
import android.content.Context;
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
2018-08-11 04:41:26 +00:00
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
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)) {
2018-12-24 12:27:45 +00:00
Log.e(ex);
2018-12-28 09:56:40 +00:00
if (BuildConfig.BETA_RELEASE ||
!Helper.isPlayStoreInstall(ApplicationEx.this))
writeCrashLog(ApplicationEx.this, 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 {
2018-12-24 12:27:45 +00:00
Log.w(ex);
2018-09-14 08:31:49 +00:00
System.exit(1);
2018-08-03 18:07:12 +00:00
}
}
});
2018-09-14 08:31:49 +00:00
createNotificationChannels();
2019-02-06 13:51:41 +00:00
MessageHelper.setSystemProperties();
2019-03-02 15:03:09 +00:00
Core.init(this);
2018-09-14 08:31:49 +00:00
}
private void createNotificationChannels() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
2018-12-09 17:49:52 +00:00
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
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);
2019-01-23 08:44:25 +00:00
NotificationChannel warning = new NotificationChannel(
"warning",
getString(R.string.channel_warning),
NotificationManager.IMPORTANCE_HIGH);
warning.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
nm.createNotificationChannel(warning);
NotificationChannel error = new NotificationChannel(
"error",
getString(R.string.channel_error),
NotificationManager.IMPORTANCE_HIGH);
2019-01-23 08:44:25 +00:00
error.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
nm.createNotificationChannel(error);
}
2018-08-03 18:07:12 +00:00
}
2018-09-14 08:31:49 +00:00
public boolean ownFault(Throwable ex) {
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-12-30 07:55:31 +00:00
if (BuildConfig.BETA_RELEASE)
return true;
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();
}
2018-12-10 09:53:46 +00:00
2018-09-14 08:31:49 +00:00
return false;
}
2018-12-10 09:53:46 +00:00
static void writeCrashLog(Context context, Throwable ex) {
File file = new File(context.getCacheDir(), "crash.log");
2018-12-24 12:27:45 +00:00
Log.w("Writing exception to " + file);
2018-09-14 08:31:49 +00:00
2019-02-22 15:59:23 +00:00
try (FileWriter out = new FileWriter(file, true)) {
out.write(BuildConfig.VERSION_NAME + " " + new Date() + "\r\n");
2018-12-24 12:27:45 +00:00
out.write(ex + "\r\n" + android.util.Log.getStackTraceString(ex) + "\r\n");
2018-09-14 08:31:49 +00:00
} catch (IOException e) {
2018-12-24 12:27:45 +00:00
Log.e(e);
2018-09-14 08:31:49 +00:00
}
}
2018-08-02 13:33:06 +00:00
}