mirror of
https://github.com/Aviortheking/Neo-Store.git
synced 2025-04-23 03:12:15 +00:00
Improve: Added Sync automatically only when on wifi and plugged in
This commit is contained in:
parent
b67a5fdc9f
commit
25fac9e773
@ -5,6 +5,7 @@ import android.app.Application
|
|||||||
import android.app.job.JobInfo
|
import android.app.job.JobInfo
|
||||||
import android.app.job.JobScheduler
|
import android.app.job.JobScheduler
|
||||||
import android.content.*
|
import android.content.*
|
||||||
|
import android.os.BatteryManager
|
||||||
import coil.ImageLoader
|
import coil.ImageLoader
|
||||||
import coil.ImageLoaderFactory
|
import coil.ImageLoaderFactory
|
||||||
import com.looker.droidify.content.Cache
|
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 com.looker.droidify.utility.extension.android.Android
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.flow.collect
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import java.net.InetSocketAddress
|
import java.net.InetSocketAddress
|
||||||
import java.net.Proxy
|
import java.net.Proxy
|
||||||
|
import kotlin.time.Duration.Companion.hours
|
||||||
|
|
||||||
|
|
||||||
@Suppress("unused")
|
@Suppress("unused")
|
||||||
class MainApplication : Application(), ImageLoaderFactory {
|
class MainApplication : Application(), ImageLoaderFactory {
|
||||||
@ -131,34 +133,61 @@ class MainApplication : Application(), ImageLoaderFactory {
|
|||||||
if (reschedule) {
|
if (reschedule) {
|
||||||
val autoSync = Preferences[Preferences.Key.AutoSync]
|
val autoSync = Preferences[Preferences.Key.AutoSync]
|
||||||
when (autoSync) {
|
when (autoSync) {
|
||||||
Preferences.AutoSync.Never -> {
|
is Preferences.AutoSync.Never -> {
|
||||||
jobScheduler.cancel(JOB_ID_SYNC)
|
jobScheduler.cancel(JOB_ID_SYNC)
|
||||||
}
|
}
|
||||||
Preferences.AutoSync.Wifi, Preferences.AutoSync.Always -> {
|
is Preferences.AutoSync.Wifi -> {
|
||||||
val period = 12 * 60 * 60 * 1000L // 12 hours
|
autoSync(
|
||||||
val wifiOnly = autoSync == Preferences.AutoSync.Wifi
|
jobScheduler = jobScheduler,
|
||||||
jobScheduler.schedule(JobInfo
|
connectionType = JobInfo.NETWORK_TYPE_UNMETERED
|
||||||
|
)
|
||||||
|
}
|
||||||
|
is Preferences.AutoSync.WifiBattery -> {
|
||||||
|
if (isCharging(this)) {
|
||||||
|
autoSync(
|
||||||
|
jobScheduler = jobScheduler,
|
||||||
|
connectionType = JobInfo.NETWORK_TYPE_UNMETERED
|
||||||
|
)
|
||||||
|
}
|
||||||
|
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(
|
.Builder(
|
||||||
JOB_ID_SYNC,
|
JOB_ID_SYNC,
|
||||||
ComponentName(this, SyncService.Job::class.java)
|
ComponentName(this, SyncService.Job::class.java)
|
||||||
)
|
)
|
||||||
.setRequiredNetworkType(if (wifiOnly) JobInfo.NETWORK_TYPE_UNMETERED else JobInfo.NETWORK_TYPE_ANY)
|
.setRequiredNetworkType(connectionType)
|
||||||
.apply {
|
.apply {
|
||||||
if (Android.sdk(26)) {
|
if (Android.sdk(26)) {
|
||||||
setRequiresBatteryNotLow(true)
|
setRequiresBatteryNotLow(true)
|
||||||
setRequiresStorageNotLow(true)
|
setRequiresStorageNotLow(true)
|
||||||
}
|
}
|
||||||
if (Android.sdk(24)) {
|
if (Android.sdk(24)) setPeriodic(period, JobInfo.getMinFlexMillis())
|
||||||
setPeriodic(period, JobInfo.getMinFlexMillis())
|
else setPeriodic(period)
|
||||||
} else {
|
|
||||||
setPeriodic(period)
|
|
||||||
}
|
}
|
||||||
|
.build()
|
||||||
|
)
|
||||||
}
|
}
|
||||||
.build())
|
|
||||||
Unit
|
private fun isCharging(context: Context): Boolean {
|
||||||
}
|
val intent = context.registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
|
||||||
}::class.java
|
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() {
|
private fun updateProxy() {
|
||||||
@ -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
|
Downloader.proxy = proxy
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,8 +38,10 @@ object Preferences {
|
|||||||
|
|
||||||
fun init(context: Context) {
|
fun init(context: Context) {
|
||||||
preferences =
|
preferences =
|
||||||
context.getSharedPreferences("${context.packageName}_preferences",
|
context.getSharedPreferences(
|
||||||
Context.MODE_PRIVATE)
|
"${context.packageName}_preferences",
|
||||||
|
Context.MODE_PRIVATE
|
||||||
|
)
|
||||||
preferences.registerOnSharedPreferenceChangeListener { _, keyString ->
|
preferences.registerOnSharedPreferenceChangeListener { _, keyString ->
|
||||||
CoroutineScope(Dispatchers.Default).launch {
|
CoroutineScope(Dispatchers.Default).launch {
|
||||||
keys[keyString]?.let {
|
keys[keyString]?.let {
|
||||||
@ -164,10 +166,11 @@ object Preferences {
|
|||||||
|
|
||||||
sealed class AutoSync(override val valueString: String) : Enumeration<AutoSync> {
|
sealed class AutoSync(override val valueString: String) : Enumeration<AutoSync> {
|
||||||
override val values: List<AutoSync>
|
override val values: List<AutoSync>
|
||||||
get() = listOf(Never, Wifi, Always)
|
get() = listOf(Never, Wifi, WifiBattery, Always)
|
||||||
|
|
||||||
object Never : AutoSync("never")
|
object Never : AutoSync("never")
|
||||||
object Wifi : AutoSync("wifi")
|
object Wifi : AutoSync("wifi")
|
||||||
|
object WifiBattery : AutoSync("wifi-battery")
|
||||||
object Always : AutoSync("always")
|
object Always : AutoSync("always")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,7 +33,6 @@ import com.looker.droidify.utility.Utils.languagesList
|
|||||||
import com.looker.droidify.utility.Utils.translateLocale
|
import com.looker.droidify.utility.Utils.translateLocale
|
||||||
import com.looker.droidify.utility.extension.resources.*
|
import com.looker.droidify.utility.extension.resources.*
|
||||||
import com.topjohnwu.superuser.Shell
|
import com.topjohnwu.superuser.Shell
|
||||||
import kotlinx.coroutines.flow.collect
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
class SettingsFragment : ScreenFragment() {
|
class SettingsFragment : ScreenFragment() {
|
||||||
@ -100,6 +99,7 @@ class SettingsFragment : ScreenFragment() {
|
|||||||
when (it) {
|
when (it) {
|
||||||
Preferences.AutoSync.Never -> getString(R.string.never)
|
Preferences.AutoSync.Never -> getString(R.string.never)
|
||||||
Preferences.AutoSync.Wifi -> getString(R.string.only_on_wifi)
|
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)
|
Preferences.AutoSync.Always -> getString(R.string.always)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -99,6 +99,7 @@
|
|||||||
<string name="ok">OK</string>
|
<string name="ok">OK</string>
|
||||||
<string name="only_compatible_with_FORMAT">Only compatible with %s</string>
|
<string name="only_compatible_with_FORMAT">Only compatible with %s</string>
|
||||||
<string name="only_on_wifi">Only on Wi-Fi</string>
|
<string name="only_on_wifi">Only on Wi-Fi</string>
|
||||||
|
<string name="only_on_wifi_and_battery">Only on Wi-Fi and Plugged-In</string>
|
||||||
<string name="open_DESC_FORMAT">Open %s?</string>
|
<string name="open_DESC_FORMAT">Open %s?</string>
|
||||||
<string name="other">Other</string>
|
<string name="other">Other</string>
|
||||||
<string name="parsing_index_error_DESC">Could not parse the index file.</string>
|
<string name="parsing_index_error_DESC">Could not parse the index file.</string>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user