Option to disable app updates

This commit is contained in:
Koitharu
2020-03-15 16:26:31 +02:00
parent 64e17d4086
commit 402c66ae87
11 changed files with 81 additions and 38 deletions

View File

@@ -40,25 +40,27 @@ class KotatsuApp : Application() {
startKoin {
androidLogger()
androidContext(applicationContext)
module {
factory {
okHttp()
.cache(CacheUtils.createHttpCache(applicationContext))
.build()
modules(
module {
factory {
okHttp()
.cache(CacheUtils.createHttpCache(applicationContext))
.build()
}
single {
mangaDb().build()
}
single {
MangaLoaderContext()
}
factory {
AppSettings(applicationContext)
}
single {
PagesCache(applicationContext)
}
}
single {
mangaDb().build()
}
single {
MangaLoaderContext()
}
factory {
AppSettings(applicationContext)
}
single {
PagesCache(applicationContext)
}
}
)
}
}

View File

@@ -35,10 +35,11 @@ data class VersionId(
@JvmStatic
private fun variantWeight(variantType: String) =
when (variantType.toLowerCase(Locale.ROOT)) {
"a" -> 1
"b" -> 2
"a", "alpha" -> 1
"b", "beta" -> 2
"rc" -> 4
else -> 8
"" -> 8
else -> 0
}
@JvmStatic

View File

@@ -42,6 +42,16 @@ class AppSettings private constructor(resources: Resources, private val prefs: S
true
)
val appUpdateAuto by BoolPreferenceDelegate(
resources.getString(R.string.key_app_update_auto),
true
)
var appUpdate by LongPreferenceDelegate(
resources.getString(R.string.key_app_update),
0L
)
private var sourcesOrderStr by NullableStringPreferenceDelegate(resources.getString(R.string.key_sources_order))
var sourcesOrder: List<Int>

View File

@@ -29,7 +29,7 @@ import org.koitharu.kotatsu.ui.main.list.remote.RemoteListFragment
import org.koitharu.kotatsu.ui.reader.ReaderActivity
import org.koitharu.kotatsu.ui.reader.ReaderState
import org.koitharu.kotatsu.ui.settings.SettingsActivity
import org.koitharu.kotatsu.ui.settings.UpdateService
import org.koitharu.kotatsu.ui.settings.AppUpdateService
import org.koitharu.kotatsu.utils.ext.getDisplayMessage
import org.koitharu.kotatsu.utils.ext.resolveDp
@@ -67,7 +67,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
setPrimaryFragment(HistoryListFragment.newInstance())
}
drawer.postDelayed(4000) {
UpdateService.startIfRequired(applicationContext)
AppUpdateService.startIfRequired(applicationContext)
}
}

View File

@@ -9,8 +9,6 @@ import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Build
import androidx.core.app.NotificationCompat
import androidx.core.content.edit
import androidx.preference.PreferenceManager
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@@ -20,11 +18,12 @@ import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.github.AppVersion
import org.koitharu.kotatsu.core.github.GithubRepository
import org.koitharu.kotatsu.core.github.VersionId
import org.koitharu.kotatsu.core.prefs.AppSettings
import org.koitharu.kotatsu.ui.common.BaseService
import org.koitharu.kotatsu.utils.FileSizeUtils
import java.util.concurrent.TimeUnit
class UpdateService : BaseService() {
class AppUpdateService : BaseService() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
launch(Dispatchers.IO) {
@@ -38,9 +37,7 @@ class UpdateService : BaseService() {
showUpdateNotification(version)
}
}
PreferenceManager.getDefaultSharedPreferences(this@UpdateService).edit(true) {
putLong(getString(R.string.key_app_update), System.currentTimeMillis())
}
AppSettings(this@AppUpdateService).appUpdate = System.currentTimeMillis()
} catch (_: CancellationException) {
} catch (e: Throwable) {
if (BuildConfig.DEBUG) {
@@ -72,7 +69,7 @@ class UpdateService : BaseService() {
builder.setContentText(buildString {
append(newVersion.name)
append(" (")
append(FileSizeUtils.formatBytes(this@UpdateService, newVersion.apkSize))
append(FileSizeUtils.formatBytes(this@AppUpdateService, newVersion.apkSize))
append(')')
})
builder.setContentIntent(
@@ -96,13 +93,15 @@ class UpdateService : BaseService() {
private val PERIOD = TimeUnit.HOURS.toMillis(10)
fun start(context: Context) =
context.startService(Intent(context, UpdateService::class.java))
context.startService(Intent(context, AppUpdateService::class.java))
fun startIfRequired(context: Context) {
val lastUpdate = PreferenceManager.getDefaultSharedPreferences(context)
.getLong(context.getString(R.string.key_app_update), 0)
if (lastUpdate + PERIOD < System.currentTimeMillis()) {
start(context)
val settings = AppSettings(context)
if (settings.appUpdateAuto) {
val lastUpdate = settings.appUpdate
if (lastUpdate + PERIOD < System.currentTimeMillis()) {
start(context)
}
}
}
}

View File

@@ -0,0 +1,20 @@
package org.koitharu.kotatsu.utils.delegates.prefs
import android.content.SharedPreferences
import androidx.core.content.edit
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
class LongPreferenceDelegate(private val key: String, private val defValue: Long) :
ReadWriteProperty<SharedPreferences, Long> {
override fun getValue(thisRef: SharedPreferences, property: KProperty<*>): Long {
return thisRef.getLong(key, defValue)
}
override fun setValue(thisRef: SharedPreferences, property: KProperty<*>, value: Long) {
thisRef.edit {
putLong(key, value)
}
}
}