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

119 lines
4.5 KiB
Java
Raw Normal View History

2023-01-16 14:09:53 +00:00
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/>.
2024-01-01 07:50:49 +00:00
Copyright 2018-2024 by Marcel Bokhorst (M66B)
2023-01-16 14:09:53 +00:00
*/
import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.preference.PreferenceManager;
2023-01-24 17:29:52 +00:00
import androidx.work.Constraints;
2023-01-16 14:09:53 +00:00
import androidx.work.ExistingPeriodicWorkPolicy;
2023-01-24 17:29:52 +00:00
import androidx.work.NetworkType;
2023-01-16 14:09:53 +00:00
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
import java.util.Calendar;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
public class WorkerSync extends Worker {
private static final Semaphore semaphore = new Semaphore(1);
public WorkerSync(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
Log.i("Instance " + getName());
}
@NonNull
@Override
public Result doWork() {
Thread.currentThread().setPriority(THREAD_PRIORITY_BACKGROUND);
final Context context = getApplicationContext();
try {
semaphore.acquire();
EntityLog.log(context, EntityLog.Type.Rules, "Cloud sync execute");
2023-01-16 21:33:16 +00:00
CloudSync.execute(context, "sync", false);
2023-01-16 14:09:53 +00:00
EntityLog.log(context, EntityLog.Type.Rules, "Cloud sync completed");
return Result.success();
} catch (Throwable ex) {
Log.e(ex);
return Result.failure();
} finally {
semaphore.release();
}
}
static void init(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String user = prefs.getString("cloud_user", null);
String password = prefs.getString("cloud_password", null);
2024-01-09 06:37:49 +00:00
boolean enabled = !(TextUtils.isEmpty(BuildConfig.CLOUD_URI) ||
TextUtils.isEmpty(user) ||
TextUtils.isEmpty(password));
2023-01-24 07:01:41 +00:00
Log.i("Cloud worker enabled=" + enabled);
2023-01-16 14:09:53 +00:00
try {
if (enabled) {
Calendar cal = Calendar.getInstance();
2023-01-24 07:01:41 +00:00
long now = cal.getTimeInMillis();
2023-01-16 14:09:53 +00:00
cal.set(Calendar.MILLISECOND, 0);
cal.set(Calendar.SECOND, 0);
2023-01-20 21:05:00 +00:00
cal.set(Calendar.MINUTE, 30);
2023-01-24 08:19:15 +00:00
cal.set(Calendar.HOUR_OF_DAY, 1);
2023-01-24 07:01:41 +00:00
long delay = cal.getTimeInMillis() - now;
if (delay < 0)
cal.add(Calendar.DATE, 1);
delay = cal.getTimeInMillis() - now;
2023-01-16 14:09:53 +00:00
2023-01-20 21:50:28 +00:00
EntityLog.log(context, EntityLog.Type.Cloud,
"Queuing " + getName() + " delay=" + (delay / (60 * 1000L)) + "m");
2023-01-16 14:09:53 +00:00
PeriodicWorkRequest.Builder builder =
new PeriodicWorkRequest.Builder(WorkerSync.class, 1, TimeUnit.DAYS)
2023-01-24 17:29:52 +00:00
.setInitialDelay(delay, TimeUnit.MILLISECONDS)
.setConstraints(new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED).build());
2023-01-16 14:09:53 +00:00
WorkManager.getInstance(context)
.enqueueUniquePeriodicWork(getName(), ExistingPeriodicWorkPolicy.KEEP, builder.build());
2023-01-16 14:09:53 +00:00
Log.i("Queued " + getName());
} else {
2023-01-20 21:50:28 +00:00
EntityLog.log(context, EntityLog.Type.Cloud,
"Cancelling " + getName());
2023-01-16 14:09:53 +00:00
WorkManager.getInstance(context).cancelUniqueWork(getName());
Log.i("Cancelled " + getName());
}
2023-01-24 10:00:52 +00:00
} catch (Throwable ex) {
2023-01-16 14:09:53 +00:00
// https://issuetracker.google.com/issues/138465476
Log.w(ex);
}
}
private static String getName() {
return WorkerSync.class.getSimpleName();
}
}