Commit 7e424703 authored by Toni Beier's avatar Toni Beier

Feature: Module data are updated, while app is in background

Use WorkerManager, which update module data every hour, if device is connected with the internet and has enough power.
If the update failed, the WorkManager retry the update.
parent 0268a69e
......@@ -18,7 +18,13 @@ import java.io.File;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import androidx.work.Constraints;
import androidx.work.ExistingPeriodicWorkPolicy;
import androidx.work.NetworkType;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;
import de.bps.asist.R;
import de.bps.asist.core.manager.ASiSTModuleManager;
import de.bps.asist.core.manager.environment.ASiSTCoreDataManager;
......@@ -152,6 +158,16 @@ public class ASiSTApplication extends Application {
manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
//Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
PeriodicWorkRequest moduleDataUpdateWorkRequest = new PeriodicWorkRequest.Builder(ModuleDataUpdateWorker.class, 1, TimeUnit.HOURS)
.addTag("module_data_update")
.setConstraints(
new Constraints.Builder()
.setRequiresBatteryNotLow(true)
.setRequiredNetworkType(NetworkType.CONNECTED)
.build())
.build();
WorkManager.getInstance().enqueueUniquePeriodicWork("module_data_update", ExistingPeriodicWorkPolicy.KEEP, moduleDataUpdateWorkRequest);
}
......
package de.bps.asist.core;
import android.content.Context;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
import de.bps.asist.core.manager.ASiSTModuleManager;
import de.bps.asist.core.manager.update.UpdateManager;
import de.bps.asist.module.AbstractAsistModule;
public class ModuleDataUpdateWorker extends Worker {
public ModuleDataUpdateWorker(@NonNull Context context, @NonNull WorkerParameters workerParameters) {
super(context, workerParameters);
Log.d("module_data_update", this.getClass().getName() + "initialized");
}
@NonNull
@Override
public Result doWork() {
try {
Log.d("module_data_update", this.getClass().getName() + " starts module update");
for (final AbstractAsistModule module : ASiSTModuleManager.getInstance().getModules()) {
Log.d("module_data_update", this.getClass().getName() + " updating module: " + module.getClass().getSimpleName() );
module.updateData(this.getApplicationContext());
}
UpdateManager.getInstance().setUpdateDone(this.getApplicationContext());
return Result.success();
} catch (Exception exception) {
Log.e("module_data_update", "Error while updating", exception);
return Result.retry();
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment