From 25fac9e77337c349a0581aec701d87a8214776ab Mon Sep 17 00:00:00 2001 From: LooKeR Date: Tue, 28 Dec 2021 13:24:04 +0530 Subject: [PATCH] Improve: Added Sync automatically only when on wifi and plugged in --- .../com/looker/droidify/MainApplication.kt | 75 +++++++++++++------ .../looker/droidify/content/Preferences.kt | 9 ++- .../droidify/screen/SettingsFragment.kt | 2 +- src/main/res/values/strings.xml | 1 + 4 files changed, 60 insertions(+), 27 deletions(-) diff --git a/src/main/kotlin/com/looker/droidify/MainApplication.kt b/src/main/kotlin/com/looker/droidify/MainApplication.kt index 62ca454f..37ba2ba6 100644 --- a/src/main/kotlin/com/looker/droidify/MainApplication.kt +++ b/src/main/kotlin/com/looker/droidify/MainApplication.kt @@ -5,6 +5,7 @@ import android.app.Application import android.app.job.JobInfo import android.app.job.JobScheduler import android.content.* +import android.os.BatteryManager import coil.ImageLoader import coil.ImageLoaderFactory import com.looker.droidify.content.Cache @@ -21,10 +22,11 @@ import com.looker.droidify.utility.Utils.toInstalledItem import com.looker.droidify.utility.extension.android.Android import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.flow.collect import kotlinx.coroutines.launch import java.net.InetSocketAddress import java.net.Proxy +import kotlin.time.Duration.Companion.hours + @Suppress("unused") class MainApplication : Application(), ImageLoaderFactory { @@ -131,36 +133,63 @@ class MainApplication : Application(), ImageLoaderFactory { if (reschedule) { val autoSync = Preferences[Preferences.Key.AutoSync] when (autoSync) { - Preferences.AutoSync.Never -> { + is Preferences.AutoSync.Never -> { jobScheduler.cancel(JOB_ID_SYNC) } - Preferences.AutoSync.Wifi, Preferences.AutoSync.Always -> { - val period = 12 * 60 * 60 * 1000L // 12 hours - val wifiOnly = autoSync == Preferences.AutoSync.Wifi - jobScheduler.schedule(JobInfo - .Builder( - JOB_ID_SYNC, - ComponentName(this, SyncService.Job::class.java) + is Preferences.AutoSync.Wifi -> { + autoSync( + jobScheduler = jobScheduler, + connectionType = JobInfo.NETWORK_TYPE_UNMETERED + ) + } + is Preferences.AutoSync.WifiBattery -> { + if (isCharging(this)) { + autoSync( + jobScheduler = jobScheduler, + connectionType = JobInfo.NETWORK_TYPE_UNMETERED ) - .setRequiredNetworkType(if (wifiOnly) JobInfo.NETWORK_TYPE_UNMETERED else JobInfo.NETWORK_TYPE_ANY) - .apply { - if (Android.sdk(26)) { - setRequiresBatteryNotLow(true) - setRequiresStorageNotLow(true) - } - if (Android.sdk(24)) { - setPeriodic(period, JobInfo.getMinFlexMillis()) - } else { - setPeriodic(period) - } - } - .build()) + } Unit } + is Preferences.AutoSync.Always -> { + autoSync( + jobScheduler = jobScheduler, + connectionType = JobInfo.NETWORK_TYPE_ANY + ) + } }::class.java } } + private fun autoSync(jobScheduler: JobScheduler, connectionType: Int) { + val period = 12.hours.inWholeMilliseconds + jobScheduler.schedule( + JobInfo + .Builder( + JOB_ID_SYNC, + ComponentName(this, SyncService.Job::class.java) + ) + .setRequiredNetworkType(connectionType) + .apply { + if (Android.sdk(26)) { + setRequiresBatteryNotLow(true) + setRequiresStorageNotLow(true) + } + if (Android.sdk(24)) setPeriodic(period, JobInfo.getMinFlexMillis()) + else setPeriodic(period) + } + .build() + ) + } + + private fun isCharging(context: Context): Boolean { + val intent = context.registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED)) + val plugged = intent!!.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) + return plugged == BatteryManager.BATTERY_PLUGGED_AC + || plugged == BatteryManager.BATTERY_PLUGGED_USB + || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS + } + private fun updateProxy() { val type = Preferences[Preferences.Key.ProxyType].proxyType val host = Preferences[Preferences.Key.ProxyHost] @@ -178,7 +207,7 @@ class MainApplication : Application(), ImageLoaderFactory { } } } - val proxy = socketAddress?.let { Proxy(type, socketAddress) } + val proxy = socketAddress?.let { Proxy(type, it) } Downloader.proxy = proxy } diff --git a/src/main/kotlin/com/looker/droidify/content/Preferences.kt b/src/main/kotlin/com/looker/droidify/content/Preferences.kt index 3ae8dfa9..fd377550 100644 --- a/src/main/kotlin/com/looker/droidify/content/Preferences.kt +++ b/src/main/kotlin/com/looker/droidify/content/Preferences.kt @@ -38,8 +38,10 @@ object Preferences { fun init(context: Context) { preferences = - context.getSharedPreferences("${context.packageName}_preferences", - Context.MODE_PRIVATE) + context.getSharedPreferences( + "${context.packageName}_preferences", + Context.MODE_PRIVATE + ) preferences.registerOnSharedPreferenceChangeListener { _, keyString -> CoroutineScope(Dispatchers.Default).launch { keys[keyString]?.let { @@ -164,10 +166,11 @@ object Preferences { sealed class AutoSync(override val valueString: String) : Enumeration { override val values: List - get() = listOf(Never, Wifi, Always) + get() = listOf(Never, Wifi, WifiBattery, Always) object Never : AutoSync("never") object Wifi : AutoSync("wifi") + object WifiBattery : AutoSync("wifi-battery") object Always : AutoSync("always") } diff --git a/src/main/kotlin/com/looker/droidify/screen/SettingsFragment.kt b/src/main/kotlin/com/looker/droidify/screen/SettingsFragment.kt index d6fa1e5a..32efdc09 100644 --- a/src/main/kotlin/com/looker/droidify/screen/SettingsFragment.kt +++ b/src/main/kotlin/com/looker/droidify/screen/SettingsFragment.kt @@ -33,7 +33,6 @@ import com.looker.droidify.utility.Utils.languagesList import com.looker.droidify.utility.Utils.translateLocale import com.looker.droidify.utility.extension.resources.* import com.topjohnwu.superuser.Shell -import kotlinx.coroutines.flow.collect import kotlinx.coroutines.launch class SettingsFragment : ScreenFragment() { @@ -100,6 +99,7 @@ class SettingsFragment : ScreenFragment() { when (it) { Preferences.AutoSync.Never -> getString(R.string.never) Preferences.AutoSync.Wifi -> getString(R.string.only_on_wifi) + Preferences.AutoSync.WifiBattery -> getString(R.string.only_on_wifi_and_battery) Preferences.AutoSync.Always -> getString(R.string.always) } } diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 83f48423..12827b55 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -99,6 +99,7 @@ OK Only compatible with %s Only on Wi-Fi + Only on Wi-Fi and Plugged-In Open %s? Other Could not parse the index file.