Commit 0428dc56 authored by Daniel Wolf's avatar Daniel Wolf
Browse files

Schedule the source refresh using WorkManager

parent 22eeddeb
......@@ -111,6 +111,7 @@ dependencies {
implementation 'com.frostnerd.utilskt:general:1.0.16' // https://git.frostnerd.com/AndroidUtils/generalkt (Accessible after logging in [free of charge])
implementation 'com.frostnerd.utilskt:adapters:1.1.1' // https://git.frostnerd.com/AndroidUtils/Adapters (Accessible after logging in [free of charge])
implementation 'androidx.work:work-runtime:2.2.0-rc01'
implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
implementation "androidx.preference:preference:1.1.0-rc01"
implementation "com.google.android.material:material:1.1.0-alpha09"
......
......@@ -27,7 +27,8 @@ import kotlinx.android.synthetic.main.dialog_host_source_refresh.view.*
* You can contact the developer at daniel.wolf@frostnerd.com.
*/
class HostSourceRefreshDialog(context:Context,
runRefresh:() -> Unit):AlertDialog(context, context.getPreferences().theme.dialogStyle) {
runRefresh:() -> Unit,
refreshConfigChanged:() -> Unit):AlertDialog(context, context.getPreferences().theme.dialogStyle) {
init {
setTitle(R.string.dialog_hostsourcerefresh_title)
......@@ -54,7 +55,7 @@ class HostSourceRefreshDialog(context:Context,
context.getPreferences().automaticHostRefreshWifiOnly = view.refreshWifiOnly.isChecked
context.getPreferences().automaticHostRefreshTimeAmount = view.timeAmount.text.toString().toInt()
context.getPreferences().automaticHostRefreshTimeUnit = TimeUnit.values().find { it.ordinal == view.timeUnit.selectedItemPosition }!!
refreshConfigChanged()
}
}
......
......@@ -12,6 +12,10 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.work.Constraints
import androidx.work.NetworkType
import androidx.work.PeriodicWorkRequest
import androidx.work.WorkManager
import com.frostnerd.cacheadapter.ListDataSource
import com.frostnerd.cacheadapter.ModelAdapterBuilder
import com.frostnerd.general.service.isServiceRunning
......@@ -28,8 +32,10 @@ import com.frostnerd.smokescreen.dialog.NewHostSourceDialog
import com.frostnerd.smokescreen.service.RuleExportService
import com.frostnerd.smokescreen.service.RuleImportService
import com.frostnerd.smokescreen.util.SpaceItemDecorator
import com.frostnerd.smokescreen.util.worker.RuleImportStartWorker
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.activity_dns_rules.*
import kotlinx.android.synthetic.main.dialog_host_source_refresh.*
import kotlinx.android.synthetic.main.item_datasource.view.*
import kotlinx.android.synthetic.main.item_datasource.view.cardContent
import kotlinx.android.synthetic.main.item_datasource.view.delete
......@@ -40,6 +46,8 @@ import kotlinx.android.synthetic.main.item_dnsrule_host.view.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import java.time.Duration
import java.util.concurrent.TimeUnit
/*
* Copyright (C) 2019 Daniel Wolf (Ch4t4r)
......@@ -119,7 +127,7 @@ class DnsRuleFragment : Fragment() {
}).show()
}
refresh.setOnClickListener {
HostSourceRefreshDialog(context!!) {
HostSourceRefreshDialog(context!!,runRefresh = {
if(context!!.isServiceRunning(RuleImportService::class.java)) {
context!!.startService(Intent(context!!, RuleImportService::class.java).putExtra("abort", true))
} else {
......@@ -127,7 +135,37 @@ class DnsRuleFragment : Fragment() {
refreshProgress.show()
refreshProgressShown = true
}
}.show()
}, refreshConfigChanged = {
getPreferences().apply {
val constraints = Constraints.Builder()
.setRequiresStorageNotLow(true)
.setRequiresBatteryNotLow(true)
.setRequiredNetworkType(if (this.automaticHostRefreshWifiOnly) NetworkType.UNMETERED else NetworkType.CONNECTED)
.build()
val mappedTimeAmount = automaticHostRefreshTimeAmount.let {
if (automaticHostRefreshTimeUnit == HostSourceRefreshDialog.TimeUnit.WEEKS) it * 7
else it
}.toLong()
val mappedTimeUnit = automaticHostRefreshTimeUnit.let {
when (it) {
HostSourceRefreshDialog.TimeUnit.WEEKS -> TimeUnit.DAYS
HostSourceRefreshDialog.TimeUnit.DAYS -> TimeUnit.DAYS
HostSourceRefreshDialog.TimeUnit.HOURS -> TimeUnit.HOURS
}
}
val workRequest = PeriodicWorkRequest.Builder(RuleImportStartWorker::class.java,
mappedTimeAmount,
mappedTimeUnit)
.setConstraints(constraints)
.setInitialDelay(mappedTimeAmount, mappedTimeUnit)
.addTag("hostSourceRefresh")
WorkManager.getInstance(context!!).apply {
cancelAllWorkByTag("hostSourceRefresh")
enqueue(workRequest.build())
}
}
}).show()
}
export.setOnClickListener {
if (context!!.isServiceRunning(RuleExportService::class.java)) {
......
package com.frostnerd.smokescreen.util.worker
import android.content.Context
import android.content.Intent
import androidx.work.Worker
import androidx.work.WorkerParameters
import com.frostnerd.smokescreen.service.RuleImportService
import com.frostnerd.smokescreen.startForegroundServiceCompat
/*
* Copyright (C) 2019 Daniel Wolf (Ch4t4r)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* You can contact the developer at daniel.wolf@frostnerd.com.
*/
class RuleImportStartWorker(appContext: Context, workerParams: WorkerParameters)
: Worker(appContext, workerParams) {
override fun doWork(): Result {
applicationContext.startForegroundServiceCompat(Intent(applicationContext, RuleImportService::class.java))
return Result.success()
}
}
\ No newline at end of file
Supports Markdown
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