From a2c38bd234a1cb61653427c3b02e0920ac6b91bc Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 12 May 2019 20:15:16 +0200 Subject: [PATCH] Added watchdog --- .../java/eu/faircode/email/ApplicationEx.java | 1 + .../faircode/email/FragmentOptionsMisc.java | 13 +++- .../eu/faircode/email/ServiceSynchronize.java | 19 +++++ .../eu/faircode/email/WorkerWatchdog.java | 74 +++++++++++++++++++ .../main/res/layout/fragment_options_misc.xml | 12 ++- app/src/main/res/values/strings.xml | 1 + 6 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/eu/faircode/email/WorkerWatchdog.java diff --git a/app/src/main/java/eu/faircode/email/ApplicationEx.java b/app/src/main/java/eu/faircode/email/ApplicationEx.java index 4c886dfb0a..fffa9ebc67 100644 --- a/app/src/main/java/eu/faircode/email/ApplicationEx.java +++ b/app/src/main/java/eu/faircode/email/ApplicationEx.java @@ -120,6 +120,7 @@ public class ApplicationEx extends Application { MessageHelper.setSystemProperties(); ContactInfo.init(this, new Handler()); Core.init(this); + WorkerWatchdog.init(this); } @Override diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java b/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java index 2190b1cae7..77f5640950 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java @@ -50,6 +50,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc private SwitchCompat swAuthentication; private SwitchCompat swParanoid; private TextView tvParanoidHint; + private SwitchCompat swWatchdog; private SwitchCompat swUpdates; private SwitchCompat swCrashReports; private SwitchCompat swDebug; @@ -59,7 +60,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc private Group grpSearchLocal; private final static String[] RESET_OPTIONS = new String[]{ - "badge", "subscriptions", "english", "authentication", "paranoid", "updates", "crash_reports", "debug" + "badge", "subscriptions", "english", "authentication", "paranoid", "watchdog", "updates", "crash_reports", "debug" }; private final static String[] RESET_QUESTIONS = new String[]{ @@ -82,6 +83,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc swAuthentication = view.findViewById(R.id.swAuthentication); swParanoid = view.findViewById(R.id.swParanoid); tvParanoidHint = view.findViewById(R.id.tvParanoidHint); + swWatchdog = view.findViewById(R.id.swWatchdog); swUpdates = view.findViewById(R.id.swUpdates); swCrashReports = view.findViewById(R.id.swCrashReports); swDebug = view.findViewById(R.id.swDebug); @@ -146,6 +148,14 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc }); } + swWatchdog.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { + prefs.edit().putBoolean("watchdog", checked).apply(); + WorkerWatchdog.init(getContext()); + } + }); + swUpdates.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { @@ -228,6 +238,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc swEnglish.setChecked(prefs.getBoolean("english", false)); swAuthentication.setChecked(prefs.getBoolean("authentication", false)); swParanoid.setChecked(prefs.getBoolean("paranoid", true)); + swWatchdog.setChecked(prefs.getBoolean("watchdog", true)); swUpdates.setChecked(prefs.getBoolean("updates", true)); swUpdates.setVisibility(Helper.isPlayStoreInstall(getContext()) ? View.GONE : View.VISIBLE); swCrashReports.setChecked(prefs.getBoolean("crash_reports", false)); diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index 7204828e49..c9fcceeefd 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -222,6 +222,10 @@ public class ServiceSynchronize extends LifecycleService { onOneshot(false); break; + case "watchdog": + onWatchdog(); + break; + default: Log.w("Unknown action: " + action); } @@ -317,6 +321,11 @@ public class ServiceSynchronize extends LifecycleService { onReload(true, "oneshot end"); } + private void onWatchdog() { + EntityLog.log(this, "Service watchdog"); + // Network events will manage the service + } + private void queue_reload(final boolean start, final boolean clear, final String reason) { final boolean doStop = started; final boolean doStart = (start && isEnabled() && networkState.isSuitable()); @@ -1387,4 +1396,14 @@ public class ServiceSynchronize extends LifecycleService { new Intent(context, ServiceSynchronize.class) .setAction("oneshot_start")); } + + static void watchdog(Context context) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + boolean enabled = prefs.getBoolean("enabled", true); + int pollInterval = prefs.getInt("poll_interval", 0); + if (enabled && pollInterval == 0) + ContextCompat.startForegroundService(context, + new Intent(context, ServiceSynchronize.class) + .setAction("watchdog")); + } } diff --git a/app/src/main/java/eu/faircode/email/WorkerWatchdog.java b/app/src/main/java/eu/faircode/email/WorkerWatchdog.java new file mode 100644 index 0000000000..eb23973dec --- /dev/null +++ b/app/src/main/java/eu/faircode/email/WorkerWatchdog.java @@ -0,0 +1,74 @@ +package eu.faircode.email; + +/* + This file is part of FairEmail. + + FairEmail is free software: you can redistribute it and/or modify + 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. + + FairEmail is distributed in the hope that it will be useful, + 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 + along with FairEmail. If not, see . + + Copyright 2018-2019 by Marcel Bokhorst (M66B) +*/ + +import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; + +import androidx.annotation.NonNull; +import androidx.work.ExistingPeriodicWorkPolicy; +import androidx.work.PeriodicWorkRequest; +import androidx.work.WorkManager; +import androidx.work.Worker; +import androidx.work.WorkerParameters; + +import java.util.concurrent.TimeUnit; + +public class WorkerWatchdog extends Worker { + private static final int WATCHDOG_INTERVAL = 60; // minutes + + public WorkerWatchdog(@NonNull Context context, @NonNull WorkerParameters workerParams) { + super(context, workerParams); + Log.i("Instance " + getName()); + } + + @NonNull + @Override + public Result doWork() { + Log.i("Running " + getName()); + ServiceSynchronize.watchdog(getApplicationContext()); + return Result.success(); + } + + static void init(Context context) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + boolean watchdog = prefs.getBoolean("watchdog", true); + if (watchdog) { + Log.i("Queuing " + getName() + " every " + WATCHDOG_INTERVAL + " minutes"); + + PeriodicWorkRequest workRequest = + new PeriodicWorkRequest.Builder(WorkerCleanup.class, WATCHDOG_INTERVAL, TimeUnit.MINUTES) + .build(); + WorkManager.getInstance(context) + .enqueueUniquePeriodicWork(getName(), ExistingPeriodicWorkPolicy.KEEP, workRequest); + + Log.i("Queued " + getName()); + } else { + Log.i("Cancelling " + getName()); + WorkManager.getInstance(context).cancelUniqueWork(getName()); + Log.i("Cancelled " + getName()); + } + } + + private static String getName() { + return WorkerWatchdog.class.getSimpleName(); + } +} diff --git a/app/src/main/res/layout/fragment_options_misc.xml b/app/src/main/res/layout/fragment_options_misc.xml index 3b724be279..2eb6ee6a2d 100644 --- a/app/src/main/res/layout/fragment_options_misc.xml +++ b/app/src/main/res/layout/fragment_options_misc.xml @@ -127,6 +127,16 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/swParanoid" /> + + Force English language Show a warning when the receiving server could not authenticate the message Extra privacy features + Periodically check if FairEmail is still active Check for updates Send error reports Debug mode