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

290 lines
11 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;
2019-03-17 18:06:51 +00:00
import android.app.NotificationChannelGroup;
import android.app.NotificationManager;
2018-12-09 17:49:52 +00:00
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
2019-03-13 13:42:33 +00:00
import android.webkit.CookieManager;
2018-08-03 18:07:12 +00:00
import androidx.preference.PreferenceManager;
2019-08-12 11:30:33 +00:00
import java.util.HashMap;
import java.util.Locale;
2019-08-12 11:30:33 +00:00
import java.util.Map;
2019-05-10 13:44:36 +00:00
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
protected void attachBaseContext(Context base) {
super.attachBaseContext(getLocalizedContext(base));
}
2018-08-03 18:07:12 +00:00
@Override
public void onCreate() {
super.onCreate();
2019-05-10 06:53:45 +00:00
2019-08-12 11:17:55 +00:00
Log.logMemory(this, "App create version=" + BuildConfig.VERSION_NAME);
2018-08-03 18:07:12 +00:00
2019-08-11 11:36:18 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
final boolean crash_reports = prefs.getBoolean("crash_reports", false);
2018-08-03 18:07:12 +00:00
prev = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
2019-08-12 14:18:41 +00:00
if (!crash_reports && Log.isOwnFault(ex)) {
2018-12-24 12:27:45 +00:00
Log.e(ex);
2018-12-28 09:56:40 +00:00
if (BuildConfig.BETA_RELEASE ||
2019-09-10 07:05:21 +00:00
!Helper.isPlayStoreInstall())
2019-08-12 14:18:41 +00:00
Log.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
}
}
});
2019-08-12 11:17:55 +00:00
Log.setupBugsnag(this);
2019-05-10 13:18:53 +00:00
upgrade(this);
createNotificationChannels();
if (Helper.hasWebView(this))
CookieManager.getInstance().setAcceptCookie(false);
2019-07-26 15:57:40 +00:00
MessageHelper.setSystemProperties(this);
2019-07-24 06:52:38 +00:00
ContactInfo.init(this);
2019-06-18 19:20:03 +00:00
2019-08-03 13:36:51 +00:00
WorkerWatchdog.init(this);
WorkerCleanup.queue(this);
2019-05-10 13:18:53 +00:00
}
@Override
public void onTrimMemory(int level) {
2019-08-12 11:17:55 +00:00
Log.logMemory(this, "Trim memory level=" + level);
2019-08-12 11:30:33 +00:00
Map<String, String> crumb = new HashMap<>();
crumb.put("level", Integer.toString(level));
crumb.put("free", Integer.toString(Log.getFreeMemMb()));
Log.breadcrumb("trim", crumb);
2019-05-10 13:18:53 +00:00
super.onTrimMemory(level);
}
@Override
public void onLowMemory() {
2019-08-12 11:17:55 +00:00
Log.logMemory(this, "Low memory");
2019-08-12 11:30:33 +00:00
Map<String, String> crumb = new HashMap<>();
crumb.put("free", Integer.toString(Log.getFreeMemMb()));
Log.breadcrumb("low", crumb);
2019-05-10 13:18:53 +00:00
super.onLowMemory();
}
static void upgrade(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
int version = prefs.getInt("version", BuildConfig.VERSION_CODE);
Log.i("Upgrading from " + version + " to " + BuildConfig.VERSION_CODE);
2019-07-22 10:31:30 +00:00
SharedPreferences.Editor editor = prefs.edit();
2019-05-21 12:28:38 +00:00
if (version < 468) {
editor.remove("notify_trash");
editor.remove("notify_archive");
editor.remove("notify_reply");
editor.remove("notify_flag");
editor.remove("notify_seen");
2019-07-22 10:31:30 +00:00
} else if (version < 601) {
editor.putBoolean("contact_images", prefs.getBoolean("autoimages", true));
editor.remove("autoimages");
} else if (version < 612) {
if (prefs.getBoolean("autonext", false))
editor.putString("onclose", "next");
editor.remove("autonext");
2019-09-09 07:31:55 +00:00
2019-09-07 15:53:06 +00:00
} else if (version < 693) {
editor.remove("message_swipe");
editor.remove("message_select");
2019-09-09 07:31:55 +00:00
} else if (version < 696) {
String theme = prefs.getString("theme", "light");
if ("grey".equals(theme))
editor.putString("theme", "grey_dark");
2019-09-09 07:46:54 +00:00
if (prefs.contains("ascending")) {
editor.putBoolean("ascending_list", prefs.getBoolean("ascending", false));
editor.remove("ascending");
}
} else if (version < 701) {
if (prefs.getBoolean("suggest_local", false)) {
editor.putBoolean("suggest_sent", true);
editor.remove("suggest_local");
}
2019-09-13 09:00:31 +00:00
} else if (version < 703) {
if (!prefs.getBoolean("style_toolbar", true)) {
editor.putBoolean("compose_media", false);
editor.remove("style_toolbar");
}
2019-09-17 09:31:28 +00:00
} else if (version < 709) {
if (prefs.getBoolean("swipe_reversed", false)) {
editor.putBoolean("reversed", true);
editor.remove("swipe_reversed");
}
2019-07-02 08:32:06 +00:00
} else if (version < 741)
editor.remove("send_dialog");
2019-10-06 19:01:01 +00:00
else if (version < 751) {
if (prefs.contains("notify_snooze_duration")) {
int minutes = prefs.getInt("notify_snooze_duration", 60);
int hours = (int) Math.ceil(minutes / 60.0);
editor.putInt("default_snooze", hours);
editor.remove("notify_snooze_duration");
}
2019-11-15 13:27:28 +00:00
} else if (version < 819) {
if (prefs.contains("no_history")) {
editor.putBoolean("secure", prefs.getBoolean("no_history", false));
editor.remove("no_history");
}
2019-11-15 15:08:03 +00:00
if (prefs.contains("zoom")) {
int zoom = prefs.getInt("zoom", 1);
editor.putInt("view_zoom", zoom);
editor.putInt("compose_zoom", zoom);
editor.remove("zoom");
}
2019-10-06 19:01:01 +00:00
}
2019-10-19 18:00:41 +00:00
if (version < BuildConfig.VERSION_CODE)
editor.putBoolean("vacuum", true);
if (BuildConfig.DEBUG && false) {
editor.remove("app_support");
editor.remove("notify_archive");
editor.remove("message_swipe");
editor.remove("message_select");
editor.remove("folder_actions");
editor.remove("folder_sync");
}
2019-07-22 10:31:30 +00:00
editor.putInt("version", BuildConfig.VERSION_CODE);
editor.apply();
}
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);
2019-07-08 18:00:19 +00:00
// Sync
NotificationChannel service = new NotificationChannel(
2019-06-09 18:51:02 +00:00
"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);
2019-07-08 18:00:19 +00:00
// Send
2019-06-09 18:51:02 +00:00
NotificationChannel send = new NotificationChannel(
"send", getString(R.string.channel_send),
2019-06-10 06:22:09 +00:00
NotificationManager.IMPORTANCE_DEFAULT);
2019-06-09 18:51:02 +00:00
send.setSound(null, Notification.AUDIO_ATTRIBUTES_DEFAULT);
send.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
nm.createNotificationChannel(send);
2019-07-08 18:00:19 +00:00
// Notify
NotificationChannel notification = new NotificationChannel(
2019-06-09 18:51:02 +00:00
"notification", getString(R.string.channel_notification),
NotificationManager.IMPORTANCE_HIGH);
notification.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
notification.enableLights(true);
nm.createNotificationChannel(notification);
2019-07-08 18:00:19 +00:00
// Update
2019-09-10 07:05:21 +00:00
if (!Helper.isPlayStoreInstall()) {
2019-06-23 13:17:45 +00:00
NotificationChannel update = new NotificationChannel(
"update", getString(R.string.channel_update),
NotificationManager.IMPORTANCE_HIGH);
2019-07-08 18:00:19 +00:00
update.setSound(null, Notification.AUDIO_ATTRIBUTES_DEFAULT);
2019-06-23 13:17:45 +00:00
update.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
nm.createNotificationChannel(update);
}
2019-12-10 10:51:03 +00:00
// Warnings
2019-01-23 08:44:25 +00:00
NotificationChannel warning = new NotificationChannel(
2019-06-09 18:51:02 +00:00
"warning", getString(R.string.channel_warning),
2019-01-23 08:44:25 +00:00
NotificationManager.IMPORTANCE_HIGH);
warning.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
nm.createNotificationChannel(warning);
2019-12-10 10:51:03 +00:00
// Errors
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);
2019-03-17 18:06:51 +00:00
2019-12-10 10:51:03 +00:00
// Server alerts
NotificationChannel alerts = new NotificationChannel(
"alerts",
getString(R.string.channel_alert),
NotificationManager.IMPORTANCE_HIGH);
alerts.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
nm.createNotificationChannel(alerts);
2019-07-08 18:00:19 +00:00
// Contacts grouping
2019-03-17 18:06:51 +00:00
NotificationChannelGroup group = new NotificationChannelGroup(
"contacts",
getString(R.string.channel_group_contacts));
nm.createNotificationChannelGroup(group);
}
2018-08-03 18:07:12 +00:00
}
2018-09-14 08:31:49 +00:00
2019-08-12 11:17:55 +00:00
static Context getLocalizedContext(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean english = prefs.getBoolean("english", false);
2018-09-14 08:31:49 +00:00
2019-08-12 11:17:55 +00:00
if (english) {
Configuration config = new Configuration(context.getResources().getConfiguration());
config.setLocale(Locale.US);
return context.createConfigurationContext(config);
} else
return context;
2018-09-14 08:31:49 +00:00
}
2018-08-02 13:33:06 +00:00
}