Refactor and move Updater classes
This commit is contained in:
parent
9eed9a1a58
commit
ef4b018e87
|
@ -28,6 +28,7 @@ import dummydomain.yetanothercallblocker.event.MainDbDownloadingEvent;
|
|||
import dummydomain.yetanothercallblocker.sia.DatabaseSingleton;
|
||||
import dummydomain.yetanothercallblocker.sia.model.NumberInfo;
|
||||
import dummydomain.yetanothercallblocker.work.TaskService;
|
||||
import dummydomain.yetanothercallblocker.work.UpdateScheduler;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
|
@ -56,11 +57,12 @@ public class MainActivity extends AppCompatActivity {
|
|||
blockCallsSwitch.setOnCheckedChangeListener((buttonView, isChecked)
|
||||
-> new Settings(this).setBlockCalls(isChecked));
|
||||
|
||||
UpdateScheduler updateScheduler = UpdateScheduler.get(this);
|
||||
SwitchCompat autoUpdateSwitch = findViewById(R.id.autoUpdateEnabledSwitch);
|
||||
autoUpdateSwitch.setChecked(Updater.isAutoUpdateScheduled());
|
||||
autoUpdateSwitch.setChecked(updateScheduler.isAutoUpdateScheduled());
|
||||
autoUpdateSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
|
||||
if (isChecked) Updater.scheduleAutoUpdateWorker();
|
||||
else Updater.cancelAutoUpdateWorker();
|
||||
if (isChecked) updateScheduler.scheduleAutoUpdates();
|
||||
else updateScheduler.cancelAutoUpdateWorker();
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,12 @@
|
|||
package dummydomain.yetanothercallblocker;
|
||||
package dummydomain.yetanothercallblocker.work;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.work.Constraints;
|
||||
import androidx.work.NetworkType;
|
||||
import androidx.work.PeriodicWorkRequest;
|
||||
import androidx.work.WorkInfo;
|
||||
import androidx.work.WorkManager;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -7,19 +15,23 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import androidx.work.Constraints;
|
||||
import androidx.work.NetworkType;
|
||||
import androidx.work.PeriodicWorkRequest;
|
||||
import androidx.work.WorkInfo;
|
||||
import androidx.work.WorkManager;
|
||||
|
||||
public class Updater {
|
||||
public class UpdateScheduler {
|
||||
|
||||
private static final String AUTO_UPDATE_WORK_TAG = "autoUpdateWork";
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Updater.class);
|
||||
private static final Logger LOG = LoggerFactory.getLogger(UpdateScheduler.class);
|
||||
|
||||
public static void scheduleAutoUpdateWorker() {
|
||||
private Context context;
|
||||
|
||||
public static UpdateScheduler get(Context context) {
|
||||
return new UpdateScheduler(context);
|
||||
}
|
||||
|
||||
private UpdateScheduler(Context context) {
|
||||
this.context = context.getApplicationContext();
|
||||
}
|
||||
|
||||
public void scheduleAutoUpdates() {
|
||||
if (isAutoUpdateScheduled()) return;
|
||||
|
||||
Constraints constraints = new Constraints.Builder()
|
||||
|
@ -32,31 +44,35 @@ public class Updater {
|
|||
.setConstraints(constraints)
|
||||
.build();
|
||||
|
||||
WorkManager.getInstance().enqueue(updateRequest);
|
||||
getWorkManager().enqueue(updateRequest);
|
||||
}
|
||||
|
||||
public static void cancelAutoUpdateWorker() {
|
||||
WorkManager.getInstance().cancelAllWorkByTag(AUTO_UPDATE_WORK_TAG);
|
||||
public void cancelAutoUpdateWorker() {
|
||||
getWorkManager().cancelAllWorkByTag(AUTO_UPDATE_WORK_TAG);
|
||||
}
|
||||
|
||||
public static boolean isAutoUpdateScheduled() {
|
||||
public boolean isAutoUpdateScheduled() {
|
||||
return findScheduled() != null;
|
||||
}
|
||||
|
||||
private static WorkInfo findScheduled() {
|
||||
private WorkInfo findScheduled() {
|
||||
for (WorkInfo workInfo : getWorkInfoList()) {
|
||||
if (workInfo.getState() == WorkInfo.State.ENQUEUED) return workInfo;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static List<WorkInfo> getWorkInfoList() {
|
||||
private List<WorkInfo> getWorkInfoList() {
|
||||
try {
|
||||
return WorkManager.getInstance().getWorkInfosByTag(AUTO_UPDATE_WORK_TAG).get();
|
||||
return getWorkManager().getWorkInfosByTag(AUTO_UPDATE_WORK_TAG).get();
|
||||
} catch (Exception e) {
|
||||
LOG.warn("getWorkInfoList()", e);
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
private WorkManager getWorkManager() {
|
||||
return WorkManager.getInstance(context);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package dummydomain.yetanothercallblocker;
|
||||
package dummydomain.yetanothercallblocker.work;
|
||||
|
||||
import android.content.Context;
|
||||
|
Loading…
Reference in New Issue