Commit c7ea34e3 authored by Nihar Thakkar's avatar Nihar Thakkar
Browse files

Schedule updates worker based on preference, enable/disable dependant preferences

parent aebc1cc8
......@@ -10,7 +10,6 @@ import android.support.design.widget.Snackbar
import android.support.v4.app.Fragment
import android.support.v7.app.AppCompatActivity
import android.view.MenuItem
import androidx.work.*
import io.eelo.appinstaller.applicationmanager.ApplicationManager
import io.eelo.appinstaller.applicationmanager.ApplicationManagerServiceConnection
import io.eelo.appinstaller.applicationmanager.ApplicationManagerServiceConnectionCallback
......@@ -19,11 +18,9 @@ import io.eelo.appinstaller.home.HomeFragment
import io.eelo.appinstaller.search.SearchFragment
import io.eelo.appinstaller.settings.SettingsFragment
import io.eelo.appinstaller.updates.UpdatesFragment
import io.eelo.appinstaller.updates.model.UpdatesWorker
import io.eelo.appinstaller.utils.Constants
import io.eelo.appinstaller.utils.Constants.CURRENTLY_SELECTED_FRAGMENT_KEY
import kotlinx.android.synthetic.main.activity_main.*
import java.util.concurrent.TimeUnit
class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemSelectedListener,
ApplicationManagerServiceConnectionCallback {
......@@ -55,16 +52,7 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS
}
private fun initialiseUpdatesWorker() {
val constraints = Constraints.Builder().apply {
setRequiresBatteryNotLow(true)
setRequiredNetworkType(NetworkType.CONNECTED)
}.build()
val updatesCheckBuilder = PeriodicWorkRequest
.Builder(UpdatesWorker::class.java, 15, TimeUnit.MINUTES).apply {
setConstraints(constraints)
}
WorkManager.getInstance().enqueueUniquePeriodicWork(Constants.UPDATES_WORK_NAME,
ExistingPeriodicWorkPolicy.KEEP, updatesCheckBuilder.build())
UpdatesManager(applicationContext).startWorker()
}
override fun onServiceBind(applicationManager: ApplicationManager) {
......
package io.eelo.appinstaller
import android.content.Context
import android.preference.PreferenceManager
import android.util.Log
import androidx.work.*
import io.eelo.appinstaller.updates.model.UpdatesWorker
import io.eelo.appinstaller.utils.Constants
import java.util.concurrent.TimeUnit
class UpdatesManager(applicationContext: Context) {
private val TAG = "UpdatesManager"
private var automaticUpdateInterval: Int
init {
val preferences = PreferenceManager.getDefaultSharedPreferences(applicationContext)
automaticUpdateInterval =
preferences.getString(
applicationContext.getString(R.string.pref_update_interval_key),
applicationContext.getString(R.string.preference_update_interval_default))
.toInt()
}
private fun getWorkerConstraints() = Constraints.Builder().apply {
setRequiresBatteryNotLow(true)
setRequiredNetworkType(NetworkType.CONNECTED)
}.build()
private fun getPeriodicWorkRequest() = PeriodicWorkRequest.Builder(
UpdatesWorker::class.java,
automaticUpdateInterval.toLong(),
TimeUnit.HOURS).apply {
setConstraints(getWorkerConstraints())
}.build()
fun startWorker() {
Log.i(TAG, "UpdatesWorker interval: ${automaticUpdateInterval.toLong()} hours")
WorkManager.getInstance().enqueueUniquePeriodicWork(Constants.UPDATES_WORK_NAME,
ExistingPeriodicWorkPolicy.KEEP, getPeriodicWorkRequest())
Log.i(TAG, "UpdatesWorker started")
}
fun replaceWorker(automaticUpdateInterval: Int) {
this.automaticUpdateInterval = automaticUpdateInterval
Log.i(TAG, "UpdatesWorker interval: ${automaticUpdateInterval.toLong()} hours")
WorkManager.getInstance().enqueueUniquePeriodicWork(Constants.UPDATES_WORK_NAME,
ExistingPeriodicWorkPolicy.REPLACE, getPeriodicWorkRequest())
Log.i(TAG, "UpdatesWorker started")
}
}
package io.eelo.appinstaller.settings
import android.os.Bundle
import android.support.v7.preference.CheckBoxPreference
import android.support.v7.preference.ListPreference
import android.support.v7.preference.PreferenceFragmentCompat
import io.eelo.appinstaller.R
import io.eelo.appinstaller.UpdatesManager
class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
// Create preferences
addPreferencesFromResource(R.xml.preferences)
// Handle update check interval changes
val updateCheckInterval =
preferenceManager.findPreference(getString(R.string.pref_update_interval_key)) as ListPreference
updateCheckInterval.setOnPreferenceChangeListener { _, newValue ->
UpdatesManager(activity!!.applicationContext).replaceWorker(newValue.toString().toInt())
true
}
// Disable auto update on WiFi preference if auto update is un-checked
val automaticallyInstallUpdates =
preferenceManager.findPreference(
getString(R.string.pref_update_install_automatically_key)) as CheckBoxPreference
val onlyOnWifi =
preferenceManager.findPreference(getString(R.string.pref_update_wifi_only_key)) as CheckBoxPreference
onlyOnWifi.isEnabled = automaticallyInstallUpdates.isChecked
automaticallyInstallUpdates.setOnPreferenceChangeListener { _, newValue ->
onlyOnWifi.isEnabled = newValue.toString().toBoolean()
true
}
}
}
......@@ -58,16 +58,13 @@
<!-- Search fragment -->
<string name="search_splash_title">Search for an app</string>
<!-- Search Fragment -->
<string name="search_description">Search for an app</string>
<!-- Updates -->
<string name="updates_splash_title">All apps are up-to-date</string>
<!-- Settings Fragment -->
<string name="preference_updates_title">Updates</string>
<string name="preference_update_interval_default">24</string>
<string name="preference_update_interval_title">Automatic update interval</string>
<string name="preference_update_interval_title">Update check interval</string>
<string name="preference_update_wifi_only_title">Only on Wi-Fi</string>
<string name="preference_update_wifi_only_description">Update apps automatically only on
un-metered networks such as Wi-Fi</string>
......@@ -76,6 +73,10 @@
updates in the background</string>
<string name="preference_update_notify_available_title">Show available updates</string>
<string name="preference_update_notify_available_description">Show a notification when app updates are available</string>
<string name="pref_update_interval_key">pref_update_interval</string>
<string name="pref_update_wifi_only_key">pref_update_wifi_only</string>
<string name="pref_update_install_automatically_key">pref_update_install_automatically</string>
<string name="pref_update_notify_key">pref_update_notify</string>
<!-- Errors -->
<string name="error_storage_permission_denied">Storage access is required for downloading apps.</string>
......
......@@ -10,27 +10,27 @@
android:dialogTitle="@string/preference_update_interval_title"
android:entries="@array/update_interval_names"
android:entryValues="@array/update_interval_values"
android:key="pref_update_interval"
android:key="@string/pref_update_interval_key"
android:summary="%s"
android:title="@string/preference_update_interval_title" />
<CheckBoxPreference
android:defaultValue="false"
android:key="pref_update_wifi_only"
android:summary="@string/preference_update_wifi_only_description"
android:title="@string/preference_update_wifi_only_title" />
android:defaultValue="true"
android:key="@string/pref_update_notify_key"
android:summary="@string/preference_update_notify_available_description"
android:title="@string/preference_update_notify_available_title" />
<CheckBoxPreference
android:defaultValue="true"
android:key="pref_update_install_automatically"
android:key="@string/pref_update_install_automatically_key"
android:summary="@string/preference_update_install_automatically_description"
android:title="@string/preference_update_install_automatically_title" />
<CheckBoxPreference
android:defaultValue="true"
android:key="pref_update_notify"
android:summary="@string/preference_update_notify_available_description"
android:title="@string/preference_update_notify_available_title" />
android:defaultValue="false"
android:key="@string/pref_update_wifi_only_key"
android:summary="@string/preference_update_wifi_only_description"
android:title="@string/preference_update_wifi_only_title" />
</PreferenceCategory>
</PreferenceScreen>
\ 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