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