Add auto-update feature

This commit is contained in:
xynngh 2019-06-09 19:48:08 +04:00
parent d33d881834
commit 7498fed8dc
6 changed files with 109 additions and 0 deletions

View File

@ -31,4 +31,5 @@ dependencies {
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:28.0.0'
implementation 'android.arch.work:work-runtime:1.0.1'
}

View File

@ -20,6 +20,13 @@ public class MainActivity extends AppCompatActivity {
notificationsSwitch.setOnCheckedChangeListener((buttonView, isChecked)
-> CallReceiver.setEnabled(MainActivity.this, isChecked));
SwitchCompat autoUpdateSwitch = findViewById(R.id.autoUpdateEnabledSwitch);
autoUpdateSwitch.setChecked(Updater.isAutoUpdateScheduled());
autoUpdateSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked) Updater.scheduleAutoUpdateWorker();
else Updater.cancelAutoUpdateWorker();
});
PermissionHelper.checkPermissions(this);
}

View File

@ -0,0 +1,32 @@
package dummydomain.yetanothercallblocker;
import android.content.Context;
import android.support.annotation.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
import dummydomain.yetanothercallblocker.sia.DatabaseSingleton;
public class UpdateWorker extends Worker {
private static final Logger LOG = LoggerFactory.getLogger(UpdateWorker.class);
public UpdateWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
}
@NonNull
@Override
public Result doWork() {
LOG.info("doWork() started");
DatabaseSingleton.getCommunityDatabase().updateSecondaryDb();
LOG.info("doWork() finished");
return Result.success();
}
}

View File

@ -0,0 +1,62 @@
package dummydomain.yetanothercallblocker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 {
private static final String AUTO_UPDATE_WORK_TAG = "autoUpdateWork";
private static final Logger LOG = LoggerFactory.getLogger(Updater.class);
public static void scheduleAutoUpdateWorker() {
if (isAutoUpdateScheduled()) return;
Constraints constraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build();
PeriodicWorkRequest updateRequest =
new PeriodicWorkRequest.Builder(UpdateWorker.class, 1, TimeUnit.DAYS)
.addTag(AUTO_UPDATE_WORK_TAG)
.setConstraints(constraints)
.build();
WorkManager.getInstance().enqueue(updateRequest);
}
public static void cancelAutoUpdateWorker() {
WorkManager.getInstance().cancelAllWorkByTag(AUTO_UPDATE_WORK_TAG);
}
public static boolean isAutoUpdateScheduled() {
return findScheduled() != null;
}
private static WorkInfo findScheduled() {
for (WorkInfo workInfo : getWorkInfoList()) {
if (workInfo.getState() == WorkInfo.State.ENQUEUED) return workInfo;
}
return null;
}
private static List<WorkInfo> getWorkInfoList() {
try {
return WorkManager.getInstance().getWorkInfosByTag(AUTO_UPDATE_WORK_TAG).get();
} catch (Exception e) {
LOG.warn("getWorkInfoList()", e);
}
return new ArrayList<>();
}
}

View File

@ -12,4 +12,10 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/incoming_call_notifications_enabled" />
<android.support.v7.widget.SwitchCompat
android:id="@+id/autoUpdateEnabledSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/auto_update_enabled" />
</LinearLayout>

View File

@ -42,6 +42,7 @@
<string name="title_activity_reviews">Reviews</string>
<string name="incoming_call_notifications_enabled">Incoming call notifications enabled</string>
<string name="auto_update_enabled">Auto-update enabled</string>
<string name="open_debug_activity">Open debug screen</string>
<string name="debug_activity_label">Debug</string>