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

397 lines
15 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
2020-01-05 17:32:53 +00:00
Copyright 2018-2020 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;
import android.content.BroadcastReceiver;
2018-12-09 17:49:52 +00:00
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.res.Configuration;
2020-07-12 18:24:21 +00:00
import android.graphics.Color;
import android.os.Build;
2020-04-04 07:19:19 +00:00
import android.util.Printer;
2019-03-13 13:42:33 +00:00
import android.webkit.CookieManager;
2018-08-03 18:07:12 +00:00
import androidx.preference.PreferenceManager;
2020-01-23 08:30:08 +00:00
import java.util.Date;
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));
}
2020-01-23 07:46:38 +00:00
static Context getLocalizedContext(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean english = prefs.getBoolean("english", false);
if (english) {
Configuration config = new Configuration(context.getResources().getConfiguration());
config.setLocale(Locale.US);
return context.createConfigurationContext(config);
} else
return context;
}
2018-08-03 18:07:12 +00:00
@Override
public void onCreate() {
super.onCreate();
2019-05-10 06:53:45 +00:00
2020-01-23 08:30:08 +00:00
long start = new Date().getTime();
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
2020-04-04 07:19:19 +00:00
getMainLooper().setMessageLogging(new Printer() {
@Override
public void println(String msg) {
2020-07-26 07:37:03 +00:00
if (BuildConfig.DEBUG)
Log.d("Loop: " + msg);
2020-04-04 07:19:19 +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
}
}
});
2020-04-05 12:10:42 +00:00
Log.setup(this);
2019-05-10 13:18:53 +00:00
upgrade(this);
createNotificationChannels();
2020-04-14 06:32:54 +00:00
DB.setupViewInvalidation(this);
2020-01-23 07:46:38 +00:00
2019-05-10 13:18:53 +00:00
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
2020-07-19 13:50:24 +00:00
DisconnectBlacklist.init(this);
2019-08-03 13:36:51 +00:00
WorkerWatchdog.init(this);
WorkerCleanup.queue(this);
2020-01-22 15:18:21 +00:00
registerReceiver(onScreenOff, new IntentFilter(Intent.ACTION_SCREEN_OFF));
2020-04-16 16:09:55 +00:00
if (BuildConfig.DEBUG)
new Thread(new Runnable() {
@Override
public void run() {
DnsHelper.test(ApplicationEx.this);
}
}).start();
2020-01-23 08:30:08 +00:00
long end = new Date().getTime();
Log.i("App created " + (end - start) + " ms");
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-12-12 16:51:39 +00:00
} else if (version < 844) {
if (prefs.getBoolean("schedule", false))
editor.putBoolean("enabled", true);
} else if (version < 874) {
if (prefs.contains("experiments") &&
prefs.getBoolean("experiments", false))
editor.putBoolean("quick_filter", true);
editor.remove("experiments");
} else if (version < 889) {
if (prefs.contains("autoresize")) {
boolean autoresize = prefs.getBoolean("autoresize", true);
editor.putBoolean("resize_images", autoresize);
editor.putBoolean("resize_attachments", autoresize);
editor.remove("autoresize");
}
2020-01-28 11:43:56 +00:00
} else if (version < 930) {
boolean large = context.getResources().getConfiguration()
.isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE);
editor.putBoolean("landscape3", large);
} else if (version < 949) {
if (prefs.contains("automove")) {
boolean automove = prefs.getBoolean("automove", false);
editor.putBoolean("move_1_confirmed", automove);
editor.remove("automove");
}
2020-02-16 12:29:19 +00:00
} else if (version < 972) {
if (prefs.contains("signature_end")) {
boolean signature_end = prefs.getBoolean("signature_end", false);
if (signature_end)
editor.putInt("signature_location", 2);
editor.remove("signature_end");
}
2020-02-23 17:39:42 +00:00
} else if (version < 978) {
if (!prefs.contains("poll_interval"))
editor.putInt("poll_interval", 0);
2020-02-23 18:16:50 +00:00
editor.remove("first");
} else if (version < 1021) {
boolean highlight_unread = prefs.getBoolean("highlight_unread", false);
if (!highlight_unread)
2020-03-20 07:25:01 +00:00
editor.putBoolean("highlight_unread", highlight_unread);
} else if (version < 1121) {
if (!Helper.isPlayStoreInstall())
editor.putBoolean("experiments", true);
2020-04-27 15:50:58 +00:00
} else if (version < 1124) {
editor.remove("experiments");
} else if (version < 1181) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !BuildConfig.DEBUG)
editor.remove("background_service");
} else if (version < 1195)
editor.remove("auto_optimize");
else if (version < 1229) {
boolean monospaced = prefs.getBoolean("monospaced", false);
if (monospaced && !BuildConfig.DEBUG)
editor.putBoolean("text_font", false);
2020-07-04 07:26:11 +00:00
} else if (version < 1238) {
if (!prefs.contains("subject_ellipsize"))
editor.putString("subject_ellipsize", "middle");
2020-07-04 19:01:04 +00:00
if (!prefs.contains("auto_optimize"))
editor.putBoolean("auto_optimize", false);
} else if (version < 1253) {
int threads = prefs.getInt("query_threads", 4);
if (threads == 4)
editor.remove("query_threads");
2020-07-22 06:26:05 +00:00
} else if (version < 1264) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N ||
"Blackview".equalsIgnoreCase(Build.MANUFACTURER) ||
"OnePlus".equalsIgnoreCase(Build.MANUFACTURER) ||
"HUAWEI".equalsIgnoreCase(Build.MANUFACTURER))
editor.putInt("query_threads", 2);
2020-07-28 15:23:07 +00:00
} else if (version < 1274)
ContactInfo.clearCache(context); // Favicon background
2019-10-06 19:01:01 +00:00
2019-12-22 07:47:32 +00:00
if (version < BuildConfig.VERSION_CODE)
editor.putInt("previous_version", version);
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);
2020-02-12 06:52:29 +00:00
send.setShowBadge(false);
2019-06-09 18:51:02 +00:00
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);
2020-07-12 18:24:21 +00:00
notification.setLightColor(Color.YELLOW);
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
private BroadcastReceiver onScreenOff = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("Received " + intent);
Log.logExtras(intent);
Helper.clearAuthentication(ApplicationEx.this);
}
};
2018-08-02 13:33:06 +00:00
}