Commit c10b076c authored by Toni Beier's avatar Toni Beier

Merge branch 'feature_system_background-data-update' into 'develop'

Moduldaten im Hintergrund und bei inaktiver App aktualisieren

See merge request OpenASiST/openasist4android_core!23
parents 0268a69e 7e424703
......@@ -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;
......@@ -151,7 +157,17 @@ 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