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

@@ -47,7 +47,7 @@
<service
android:name=".ui.download.DownloadService"
android:foregroundServiceType="dataSync" />
<service android:name=".ui.settings.UpdateService" />
<service android:name=".ui.settings.AppUpdateService" />
<provider
android:name=".ui.search.MangaSuggestionsProvider"

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)
}
}
}

View File

@@ -107,4 +107,5 @@
<string name="application_update">Обновление приложения</string>
<string name="app_update_available">Доступно обновление приложения</string>
<string name="about_app">О программе</string>
<string name="show_notification_app_update">Показывать уведомление при наличии новой версии</string>
</resources>

View File

@@ -11,6 +11,7 @@
<string name="key_grid_size">grid_size</string>
<string name="key_reader_switchers">reader_switchers</string>
<string name="key_app_update">app_update</string>
<string name="key_app_update_auto">app_update_auto</string>
<string name="key_parser_domain">domain</string>
<string-array name="values_theme">

View File

@@ -108,4 +108,5 @@
<string name="application_update">Application update</string>
<string name="app_update_available">Application update is available</string>
<string name="about_app">About</string>
<string name="show_notification_app_update">Show notification if update is available</string>
</resources>

View File

@@ -1,6 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<SwitchPreference
android:defaultValue="true"
android:key="@string/key_app_update_auto"
android:summary="@string/show_notification_app_update"
android:title="@string/application_update"
app:allowDividerBelow="true"
app:iconSpaceReserved="false" />
</PreferenceScreen>