From 402c66ae87c6f431f874d87f21e9404b21add8fb Mon Sep 17 00:00:00 2001 From: Koitharu Date: Sun, 15 Mar 2020 16:26:31 +0200 Subject: [PATCH] Option to disable app updates --- app/src/main/AndroidManifest.xml | 2 +- .../java/org/koitharu/kotatsu/KotatsuApp.kt | 38 ++++++++++--------- .../koitharu/kotatsu/core/github/VersionId.kt | 7 ++-- .../kotatsu/core/prefs/AppSettings.kt | 10 +++++ .../koitharu/kotatsu/ui/main/MainActivity.kt | 4 +- .../{UpdateService.kt => AppUpdateService.kt} | 23 ++++++----- .../delegates/prefs/LongPreferenceDelegate.kt | 20 ++++++++++ app/src/main/res/values-ru/strings.xml | 1 + app/src/main/res/values/constants.xml | 1 + app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/pref_about.xml | 12 +++++- 11 files changed, 81 insertions(+), 38 deletions(-) rename app/src/main/java/org/koitharu/kotatsu/ui/settings/{UpdateService.kt => AppUpdateService.kt} (82%) create mode 100644 app/src/main/java/org/koitharu/kotatsu/utils/delegates/prefs/LongPreferenceDelegate.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3cbcdd36c..2d54d7517 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -47,7 +47,7 @@ - + 1 - "b" -> 2 + "a", "alpha" -> 1 + "b", "beta" -> 2 "rc" -> 4 - else -> 8 + "" -> 8 + else -> 0 } @JvmStatic diff --git a/app/src/main/java/org/koitharu/kotatsu/core/prefs/AppSettings.kt b/app/src/main/java/org/koitharu/kotatsu/core/prefs/AppSettings.kt index af7f875fd..84980d598 100644 --- a/app/src/main/java/org/koitharu/kotatsu/core/prefs/AppSettings.kt +++ b/app/src/main/java/org/koitharu/kotatsu/core/prefs/AppSettings.kt @@ -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 diff --git a/app/src/main/java/org/koitharu/kotatsu/ui/main/MainActivity.kt b/app/src/main/java/org/koitharu/kotatsu/ui/main/MainActivity.kt index 302e2ac1f..2e8308e60 100644 --- a/app/src/main/java/org/koitharu/kotatsu/ui/main/MainActivity.kt +++ b/app/src/main/java/org/koitharu/kotatsu/ui/main/MainActivity.kt @@ -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) } } diff --git a/app/src/main/java/org/koitharu/kotatsu/ui/settings/UpdateService.kt b/app/src/main/java/org/koitharu/kotatsu/ui/settings/AppUpdateService.kt similarity index 82% rename from app/src/main/java/org/koitharu/kotatsu/ui/settings/UpdateService.kt rename to app/src/main/java/org/koitharu/kotatsu/ui/settings/AppUpdateService.kt index 9ff734e80..002dadec1 100644 --- a/app/src/main/java/org/koitharu/kotatsu/ui/settings/UpdateService.kt +++ b/app/src/main/java/org/koitharu/kotatsu/ui/settings/AppUpdateService.kt @@ -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) + } } } } diff --git a/app/src/main/java/org/koitharu/kotatsu/utils/delegates/prefs/LongPreferenceDelegate.kt b/app/src/main/java/org/koitharu/kotatsu/utils/delegates/prefs/LongPreferenceDelegate.kt new file mode 100644 index 000000000..34c4c34a3 --- /dev/null +++ b/app/src/main/java/org/koitharu/kotatsu/utils/delegates/prefs/LongPreferenceDelegate.kt @@ -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 { + + 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) + } + } +} \ No newline at end of file diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index eb4597986..c4c3fc5dd 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -107,4 +107,5 @@ Обновление приложения Доступно обновление приложения О программе + Показывать уведомление при наличии новой версии \ No newline at end of file diff --git a/app/src/main/res/values/constants.xml b/app/src/main/res/values/constants.xml index faba55904..ee418bb03 100644 --- a/app/src/main/res/values/constants.xml +++ b/app/src/main/res/values/constants.xml @@ -11,6 +11,7 @@ grid_size reader_switchers app_update + app_update_auto domain diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b96150489..fc33d53f9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -108,4 +108,5 @@ Application update Application update is available About + Show notification if update is available \ No newline at end of file diff --git a/app/src/main/res/xml/pref_about.xml b/app/src/main/res/xml/pref_about.xml index 5699e19c1..60d0c0a42 100644 --- a/app/src/main/res/xml/pref_about.xml +++ b/app/src/main/res/xml/pref_about.xml @@ -1,6 +1,14 @@ - - + + \ No newline at end of file