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.room.RoomDatabase
import androidx.work.Constraints import androidx.work.Constraints
import androidx.work.ExistingPeriodicWorkPolicy import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.NetworkType
import androidx.work.OneTimeWorkRequestBuilder import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.PeriodicWorkRequestBuilder import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager import androidx.work.WorkManager
@ -118,6 +119,10 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
private var itemsCaching: Boolean = false private var itemsCaching: Boolean = false
private var hiddenTags: List<String> = emptyList() 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 tabNewBadge: TextBadgeItem
private lateinit var tabArchiveBadge: TextBadgeItem private lateinit var tabArchiveBadge: TextBadgeItem
private lateinit var tabStarredBadge: TextBadgeItem private lateinit var tabStarredBadge: TextBadgeItem
@ -393,6 +398,13 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
} else { } else {
emptyList() 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() { private fun handleThemeBinding() {
@ -1399,23 +1411,22 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
} }
private fun handleRecurringTask() { private fun handleRecurringTask() {
// TODO: add network type if (periodicRefresh) {
val myConstraints = Constraints.Builder() val myConstraints = Constraints.Builder()
.setRequiresBatteryNotLow(true) .setRequiresBatteryNotLow(true)
.setRequiresCharging(refreshWhenChargingOnly)
.setRequiresStorageNotLow(true) .setRequiresStorageNotLow(true)
.build() .build()
// TODO: make the time variable from the settings.
val backgroundWork = val backgroundWork =
PeriodicWorkRequestBuilder<LoadingWorker>(4, TimeUnit.HOURS) PeriodicWorkRequestBuilder<LoadingWorker>(refreshMinutes, TimeUnit.MINUTES)
.setConstraints(myConstraints) .setConstraints(myConstraints)
.addTag("selfoss-loading") .addTag("selfoss-loading")
.build() .build()
WorkManager.getInstance().enqueueUniquePeriodicWork("selfoss-loading", ExistingPeriodicWorkPolicy.REPLACE, backgroundWork) 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_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="pref_switch_items_caching">Save items for offline use</string>
<string name="no_network_connectivity">Not connected !</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> </resources>

View File

@ -5,4 +5,28 @@
android:summaryOff="@string/pref_switch_items_caching_off" android:summaryOff="@string/pref_switch_items_caching_off"
android:summaryOn="@string/pref_switch_items_caching_on" android:summaryOn="@string/pref_switch_items_caching_on"
android:title="@string/pref_switch_items_caching" /> 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> </PreferenceScreen>