Added auto update Disconnect's lists

This commit is contained in:
M66B 2021-05-02 08:37:34 +02:00
parent b0c281e909
commit 6e367fb420
5 changed files with 118 additions and 2 deletions

View File

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

View File

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

View File

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

View File

@ -367,6 +367,19 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/btnDisconnectBlacklist" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/swDisconnectAutoUpdate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="12dp"
android:enabled="true"
android:text="@string/title_advanced_disconnect_auto_update"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvDisconnectBlacklistTime"
app:switchPadding="12dp" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/swDisconnectLinks"
android:layout_width="0dp"
@ -377,7 +390,7 @@
android:text="@string/title_advanced_disconnect_links"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvDisconnectBlacklistTime"
app:layout_constraintTop_toBottomOf="@+id/swDisconnectAutoUpdate"
app:switchPadding="12dp" />
<androidx.appcompat.widget.SwitchCompat

View File

@ -513,6 +513,7 @@
<string name="title_advanced_biometrics_timeout">Biometric authentication timeout</string>
<string name="title_advanced_safe_browsing" translatable="false">Google Safe browsing (Android 8+)</string>
<string name="title_advanced_disconnect_blacklist" translatable="false">Disconnect\'s tracker protection lists</string>
<string name="title_advanced_disconnect_auto_update">Automatically update lists weekly</string>
<string name="title_advanced_disconnect_links">Use lists to warn about tracking links</string>
<string name="title_advanced_disconnect_images">Use lists to recognize tracking images</string>