Added watchdog

pull/156/head
M66B 6 years ago
parent d4da0bb223
commit a2c38bd234

@ -120,6 +120,7 @@ public class ApplicationEx extends Application {
MessageHelper.setSystemProperties();
ContactInfo.init(this, new Handler());
Core.init(this);
WorkerWatchdog.init(this);
}
@Override

@ -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));

@ -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"));
}
}

@ -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 <http://www.gnu.org/licenses/>.
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();
}
}

@ -127,6 +127,16 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swParanoid" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/swWatchdog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/title_advanced_watchdog"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvParanoidHint"
app:switchPadding="12dp" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/swUpdates"
android:layout_width="match_parent"
@ -134,7 +144,7 @@
android:layout_marginTop="12dp"
android:text="@string/title_advanced_updates"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvParanoidHint"
app:layout_constraintTop_toBottomOf="@id/swWatchdog"
app:switchPadding="12dp" />
<androidx.appcompat.widget.SwitchCompat

@ -223,6 +223,7 @@
<string name="title_advanced_english">Force English language</string>
<string name="title_advanced_authentication">Show a warning when the receiving server could not authenticate the message</string>
<string name="title_advanced_paranoid">Extra privacy features</string>
<string name="title_advanced_watchdog">Periodically check if FairEmail is still active</string>
<string name="title_advanced_updates">Check for updates</string>
<string name="title_advanced_crash_reports">Send error reports</string>
<string name="title_advanced_debug">Debug mode</string>

Loading…
Cancel
Save