Commit 0d37d4dc authored by Jonas L.'s avatar Jonas L.

Enable background updates

parent 69945f5c
......@@ -4,7 +4,6 @@ import android.app.NotificationManager
import android.content.Context
import android.os.SystemClock
import android.util.Log
import androidx.arch.core.util.Function
import androidx.core.app.NotificationCompat
import androidx.core.os.CancellationSignal
import androidx.lifecycle.LiveData
......@@ -28,6 +27,7 @@ import de.determapp.android.util.Async
import de.determapp.android.util.Progress
import de.determapp.android.util.ProgressListener
import java.io.File
import java.util.concurrent.TimeUnit
enum class UpdateContentStatus {
RunningPeriodically, RunningManually, ScheduledManually, Idle
......@@ -35,48 +35,34 @@ enum class UpdateContentStatus {
class UpdateContentWorker: Worker() {
companion object {
val TAG_PERIODICALLY = "UpdateContentWorkPeriodically"
val TAG_MANUALLY = "UpdateContentWorkManually"
val TAG = "UpdateContentWork"
val LOG_TAG = "UpdateContentWork"
val enqueueLock = Object()
const val TAG_PERIODICALLY = "UpdateContentWorkPeriodically"
const val TAG_MANUALLY = "UpdateContentWorkManually"
const val TAG = "UpdateContentWork"
const val LOG_TAG = "UpdateContentWork"
const val UNIQUE_WORK_ID = "UpdateContentWorkPeriodically"
val executionLock = Object()
val enqueueLock = Object()
fun enqueuePeriodicallyIfNotDone() {
// disabled because it does not work
// if you enable it:
// - enable the notice at the end of layout/viewer_info_dialog.xml
// - enable the notice at the end of layout/fragment_update_content.xml
/*Async.disk.submit {
synchronized(enqueueLock) {
val statuses = WorkManager.getInstance().synchronous().getStatusesByTagSync(TAG_PERIODICALLY)
if (!scheduledOrRunning(statuses)) {
if (BuildConfig.DEBUG) {
Log.d(LOG_TAG, "schedule periodically")
}
if (BuildConfig.DEBUG) {
Log.d(LOG_TAG, "schedule periodically")
}
WorkManager.getInstance().synchronous().enqueueSync(
PeriodicWorkRequestBuilder<UpdateContentWorker>(30L, TimeUnit.SECONDS)
.addTag(TAG)
.addTag(TAG_PERIODICALLY)
.setConstraints(
Constraints.Builder()
.setRequiresStorageNotLow(true)
.setRequiresBatteryNotLow(true)
.setRequiredNetworkType(NetworkType.UNMETERED)
.build()
)
.build()
)
} else {
if (BuildConfig.DEBUG) {
Log.d(LOG_TAG, "already scheduled")
}
}
}
}*/
WorkManager.getInstance().enqueueUniquePeriodicWork(
UNIQUE_WORK_ID,
ExistingPeriodicWorkPolicy.REPLACE,
PeriodicWorkRequestBuilder<UpdateContentWorker>(0, TimeUnit.DAYS)
.addTag(TAG)
.addTag(TAG_PERIODICALLY)
.setConstraints(
Constraints.Builder()
.setRequiresStorageNotLow(true)
.setRequiresBatteryNotLow(true)
.setRequiredNetworkType(NetworkType.UNMETERED)
.build()
)
.build()
)
}
fun enqueueManually() {
......@@ -115,28 +101,22 @@ class UpdateContentWorker: Worker() {
fun getStatus(): LiveData<UpdateContentStatus> {
return Transformations.map(
WorkManager.getInstance().getStatusesByTag(TAG),
Function {
if (it.find {
it.tags.contains(TAG_PERIODICALLY) &&
it.state == State.RUNNING
} != null) {
UpdateContentStatus.RunningPeriodically
} else if (it.find {
it.tags.contains(TAG_MANUALLY) &&
it.state == State.RUNNING
} != null) {
UpdateContentStatus.RunningManually
} else if (it.find {
it.tags.contains(TAG_MANUALLY) &&
(it.state == State.ENQUEUED || it.state == State.BLOCKED)
} != null) {
UpdateContentStatus.ScheduledManually
} else {
UpdateContentStatus.Idle
}
}
);
WorkManager.getInstance().getStatusesByTag(TAG)) {
statuses ->
val periodicEntries = statuses.filter { it.tags.contains(TAG_PERIODICALLY) }
val manuelEntries = statuses.filter { it.tags.contains(TAG_MANUALLY) }
if (periodicEntries.find { it.state == State.RUNNING } != null) {
UpdateContentStatus.RunningPeriodically
} else if (manuelEntries.find { it.state == State.RUNNING } != null) {
UpdateContentStatus.RunningManually
} else if (manuelEntries.find { (it.state == State.ENQUEUED || it.state == State.BLOCKED) } != null) {
UpdateContentStatus.ScheduledManually
} else {
UpdateContentStatus.Idle
}
}
}
}
......
......@@ -70,9 +70,6 @@
</LinearLayout>
</RelativeLayout>
<!--
hidden because not enabled
<TextView
android:gravity="center_horizontal"
android:textAppearance="?android:textAppearanceMedium"
......@@ -80,8 +77,7 @@
android:text="@string/viewer_info_dialog_notice_background_update"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
-->
</LinearLayout>
</layout>
\ No newline at end of file
</layout>
......@@ -139,17 +139,13 @@
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!--
hidden because not enabled
<TextView
android:visibility="@{mode == ViewerInfoMode.LocalNetworkPackage ? View.GONE : View.VISIBLE}"
android:text="@string/viewer_info_dialog_notice_background_update"
android:textAppearance="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
-->
</LinearLayout>
</ScrollView>
</layout>
\ No newline at end of file
</layout>
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