Preferences for the background tasks.

This commit is contained in:
Amine 2018-11-03 18:14:22 +01:00
parent fec6683701
commit 363eaf9bf9
3 changed files with 53 additions and 13 deletions

View File

@ -27,6 +27,7 @@ import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.work.Constraints
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.NetworkType
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager
@ -118,6 +119,10 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
private var itemsCaching: Boolean = false
private var hiddenTags: List<String> = emptyList()
private var periodicRefresh = false
private var refreshMinutes: Long = 360L
private var refreshWhenChargingOnly = false
private lateinit var tabNewBadge: TextBadgeItem
private lateinit var tabArchiveBadge: TextBadgeItem
private lateinit var tabStarredBadge: TextBadgeItem
@ -393,6 +398,13 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
} else {
emptyList()
}
periodicRefresh = sharedPref.getBoolean("periodic_refresh", false)
refreshWhenChargingOnly = sharedPref.getBoolean("refresh_when_charging", false)
refreshMinutes = sharedPref.getString("periodic_refresh_minutes", "360").toLong()
if (refreshMinutes <= 15) {
refreshMinutes = 15
}
}
private fun handleThemeBinding() {
@ -1399,23 +1411,22 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
}
private fun handleRecurringTask() {
// TODO: add network type
val myConstraints = Constraints.Builder()
.setRequiresBatteryNotLow(true)
.setRequiresStorageNotLow(true)
.build()
// TODO: make the time variable from the settings.
val backgroundWork =
PeriodicWorkRequestBuilder<LoadingWorker>(4, TimeUnit.HOURS)
.setConstraints(myConstraints)
.addTag("selfoss-loading")
if (periodicRefresh) {
val myConstraints = Constraints.Builder()
.setRequiresBatteryNotLow(true)
.setRequiresCharging(refreshWhenChargingOnly)
.setRequiresStorageNotLow(true)
.build()
WorkManager.getInstance().enqueueUniquePeriodicWork("selfoss-loading", ExistingPeriodicWorkPolicy.REPLACE, backgroundWork)
val backgroundWork =
PeriodicWorkRequestBuilder<LoadingWorker>(refreshMinutes, TimeUnit.MINUTES)
.setConstraints(myConstraints)
.addTag("selfoss-loading")
.build()
WorkManager.getInstance().enqueueUniquePeriodicWork("selfoss-loading", ExistingPeriodicWorkPolicy.KEEP, backgroundWork)
}
}
}

View File

@ -153,4 +153,9 @@
<string name="pref_switch_items_caching_on">Articles will be saved to the device memory and will be used for offline use.</string>
<string name="pref_switch_items_caching">Save items for offline use</string>
<string name="no_network_connectivity">Not connected !</string>
<string name="pref_switch_periodic_refresh">Sync articles</string>
<string name="pref_switch_periodic_refresh_off">Articles will not be synced in the background</string>
<string name="pref_switch_periodic_refresh_on">Articles will periodically be synced</string>
<string name="pref_periodic_refresh_minutes_title"><![CDATA[Sync interval ( >= 15 minutes)]]></string>
<string name="pref_switch_refresh_when_charging">Only refresh when phone is charging</string>
</resources>

View File

@ -5,4 +5,28 @@
android:summaryOff="@string/pref_switch_items_caching_off"
android:summaryOn="@string/pref_switch_items_caching_on"
android:title="@string/pref_switch_items_caching" />
<SwitchPreference
android:defaultValue="false"
android:key="periodic_refresh"
android:dependency="items_caching"
android:summaryOff="@string/pref_switch_periodic_refresh_off"
android:summaryOn="@string/pref_switch_periodic_refresh_on"
android:title="@string/pref_switch_periodic_refresh" />
<EditTextPreference
android:dependency="periodic_refresh"
android:defaultValue="360"
android:inputType="number"
android:key="periodic_refresh_minutes"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/pref_periodic_refresh_minutes_title" />
<SwitchPreference
android:defaultValue="false"
android:key="refresh_when_charging"
android:dependency="items_caching"
android:title="@string/pref_switch_refresh_when_charging" />
</PreferenceScreen>