Use frequent update checks only on modern Android versions

Also fix @RequiresApi(23) method call on older versions
This commit is contained in:
xynngh 2020-06-02 16:05:10 +04:00
parent 77f3302ea0
commit 751472d27c
1 changed files with 15 additions and 12 deletions

View File

@ -1,6 +1,7 @@
package dummydomain.yetanothercallblocker.work; package dummydomain.yetanothercallblocker.work;
import android.content.Context; import android.content.Context;
import android.os.Build;
import androidx.work.Constraints; import androidx.work.Constraints;
import androidx.work.NetworkType; import androidx.work.NetworkType;
@ -47,20 +48,22 @@ public class UpdateScheduler {
getWorkManager().enqueue(updateRequest); getWorkManager().enqueue(updateRequest);
constraints = new Constraints.Builder() if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
.setRequiredNetworkType(NetworkType.UNMETERED) constraints = new Constraints.Builder()
.setRequiresBatteryNotLow(true) .setRequiredNetworkType(NetworkType.UNMETERED)
.setRequiresDeviceIdle(true) .setRequiresBatteryNotLow(true)
.build(); .setRequiresDeviceIdle(true)
.build();
updateRequest = updateRequest =
new PeriodicWorkRequest.Builder(UpdateWorker.class, 6, TimeUnit.HOURS) new PeriodicWorkRequest.Builder(UpdateWorker.class, 6, TimeUnit.HOURS)
.addTag(AUTO_UPDATE_WORK_TAG) .addTag(AUTO_UPDATE_WORK_TAG)
.setInitialDelay(6, TimeUnit.HOURS) .setInitialDelay(6, TimeUnit.HOURS)
.setConstraints(constraints) .setConstraints(constraints)
.build(); .build();
getWorkManager().enqueue(updateRequest); getWorkManager().enqueue(updateRequest);
}
} }
public void cancelAutoUpdateWorker() { public void cancelAutoUpdateWorker() {