From 6e367fb420cecab46748c6917e1e182e1d02f6db Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 2 May 2021 08:37:34 +0200 Subject: [PATCH] Added auto update Disconnect's lists --- .../java/eu/faircode/email/ApplicationEx.java | 1 + .../email/FragmentOptionsPrivacy.java | 13 ++- .../eu/faircode/email/WorkerAutoUpdate.java | 90 +++++++++++++++++++ .../res/layout/fragment_options_privacy.xml | 15 +++- app/src/main/res/values/strings.xml | 1 + 5 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/eu/faircode/email/WorkerAutoUpdate.java diff --git a/app/src/main/java/eu/faircode/email/ApplicationEx.java b/app/src/main/java/eu/faircode/email/ApplicationEx.java index 7c2b650374..c4b895a7e7 100644 --- a/app/src/main/java/eu/faircode/email/ApplicationEx.java +++ b/app/src/main/java/eu/faircode/email/ApplicationEx.java @@ -182,6 +182,7 @@ public class ApplicationEx extends Application ServiceSynchronize.scheduleWatchdog(this); WorkManager.getInstance(this).cancelUniqueWork("WorkerWatchdog"); + WorkerAutoUpdate.init(this); WorkerCleanup.init(this); registerReceiver(onScreenOff, new IntentFilter(Intent.ACTION_SCREEN_OFF)); diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsPrivacy.java b/app/src/main/java/eu/faircode/email/FragmentOptionsPrivacy.java index 9a5042a533..20c983859f 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsPrivacy.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsPrivacy.java @@ -77,6 +77,7 @@ public class FragmentOptionsPrivacy extends FragmentBase implements SharedPrefer private ImageButton ibDisconnectBlacklist; private Button btnDisconnectBlacklist; private TextView tvDisconnectBlacklistTime; + private SwitchCompat swDisconnectAutoUpdate; private SwitchCompat swDisconnectLinks; private SwitchCompat swDisconnectImages; @@ -87,7 +88,7 @@ public class FragmentOptionsPrivacy extends FragmentBase implements SharedPrefer "disable_tracking", "hide_timezone", "pin", "biometrics", "biometrics_timeout", "display_hidden", "incognito_keyboard", "secure", "safe_browsing", - "disconnect_links", "disconnect_images" + "disconnect_auto_update", "disconnect_links", "disconnect_images" }; @Override @@ -118,6 +119,7 @@ public class FragmentOptionsPrivacy extends FragmentBase implements SharedPrefer ibDisconnectBlacklist = view.findViewById(R.id.ibDisconnectBlacklist); btnDisconnectBlacklist = view.findViewById(R.id.btnDisconnectBlacklist); tvDisconnectBlacklistTime = view.findViewById(R.id.tvDisconnectBlacklistTime); + swDisconnectAutoUpdate = view.findViewById(R.id.swDisconnectAutoUpdate); swDisconnectLinks = view.findViewById(R.id.swDisconnectLinks); swDisconnectImages = view.findViewById(R.id.swDisconnectImages); @@ -309,6 +311,14 @@ public class FragmentOptionsPrivacy extends FragmentBase implements SharedPrefer } }); + swDisconnectAutoUpdate.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { + prefs.edit().putBoolean("disconnect_auto_update", checked).apply(); + WorkerAutoUpdate.init(compoundButton.getContext()); + } + }); + swDisconnectLinks.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { @@ -394,6 +404,7 @@ public class FragmentOptionsPrivacy extends FragmentBase implements SharedPrefer tvDisconnectBlacklistTime.setText(time < 0 ? null : DF.format(time)); tvDisconnectBlacklistTime.setVisibility(time < 0 ? View.GONE : View.VISIBLE); + swDisconnectAutoUpdate.setChecked(prefs.getBoolean("disconnect_auto_update", false)); swDisconnectLinks.setChecked(prefs.getBoolean("disconnect_links", true)); swDisconnectImages.setChecked(prefs.getBoolean("disconnect_images", false)); } diff --git a/app/src/main/java/eu/faircode/email/WorkerAutoUpdate.java b/app/src/main/java/eu/faircode/email/WorkerAutoUpdate.java new file mode 100644 index 0000000000..63f55c33f3 --- /dev/null +++ b/app/src/main/java/eu/faircode/email/WorkerAutoUpdate.java @@ -0,0 +1,90 @@ +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-2021 by Marcel Bokhorst (M66B) +*/ + +import android.content.Context; +import android.content.SharedPreferences; + +import androidx.annotation.NonNull; +import androidx.preference.PreferenceManager; +import androidx.work.Constraints; +import androidx.work.ExistingPeriodicWorkPolicy; +import androidx.work.NetworkType; +import androidx.work.PeriodicWorkRequest; +import androidx.work.WorkManager; +import androidx.work.Worker; +import androidx.work.WorkerParameters; + +import java.util.concurrent.TimeUnit; + +import static android.os.Process.THREAD_PRIORITY_BACKGROUND; + +public class WorkerAutoUpdate extends Worker { + private static final long UPDATE_INTERVAL = 7; // Days + + public WorkerAutoUpdate(@NonNull Context context, @NonNull WorkerParameters workerParams) { + super(context, workerParams); + Log.i("Instance " + getName()); + } + + @NonNull + @Override + public Result doWork() { + Thread.currentThread().setPriority(THREAD_PRIORITY_BACKGROUND); + + try { + Log.i("Auto updating"); + DisconnectBlacklist.download(getApplicationContext()); + Log.i("Auto updated"); + return Result.success(); + } catch (Throwable ex) { + Log.e(ex); + return Result.failure(); + } + } + + static void init(Context context) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + boolean auto_update = prefs.getBoolean("disconnect_auto_update", false); + try { + if (auto_update) { + Log.i("Queuing " + getName()); + PeriodicWorkRequest.Builder builder = + new PeriodicWorkRequest.Builder(WorkerAutoUpdate.class, UPDATE_INTERVAL, TimeUnit.DAYS) + .setConstraints(new Constraints.Builder() + .setRequiredNetworkType(NetworkType.CONNECTED).build()); + WorkManager.getInstance(context) + .enqueueUniquePeriodicWork(getName(), ExistingPeriodicWorkPolicy.KEEP, builder.build()); + Log.i("Queued " + getName()); + } else { + Log.i("Cancelling " + getName()); + WorkManager.getInstance(context).cancelUniqueWork(getName()); + Log.i("Cancelled " + getName()); + } + } catch (IllegalStateException ex) { + // https://issuetracker.google.com/issues/138465476 + Log.w(ex); + } + } + + private static String getName() { + return WorkerAutoUpdate.class.getSimpleName(); + } +} diff --git a/app/src/main/res/layout/fragment_options_privacy.xml b/app/src/main/res/layout/fragment_options_privacy.xml index ab06ef4a7d..f739653c09 100644 --- a/app/src/main/res/layout/fragment_options_privacy.xml +++ b/app/src/main/res/layout/fragment_options_privacy.xml @@ -367,6 +367,19 @@ app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@id/btnDisconnectBlacklist" /> + + Biometric authentication timeout Google Safe browsing (Android 8+) Disconnect\'s tracker protection lists + Automatically update lists weekly Use lists to warn about tracking links Use lists to recognize tracking images