From d5f5ba95bb13e8d408d8c49afa0ca7851e860766 Mon Sep 17 00:00:00 2001 From: Eugene Date: Sat, 13 Jul 2019 13:36:30 -0400 Subject: [PATCH] Add automatic updates for dev builds (#2128) --- .../data/updater/GithubUpdateResult.kt | 7 ---- .../kanade/tachiyomi/data/updater/Release.kt | 13 ++++++ .../tachiyomi/data/updater/UpdateChecker.kt | 25 ++++++++++++ .../tachiyomi/data/updater/UpdateResult.kt | 8 ++++ .../tachiyomi/data/updater/UpdaterJob.kt | 10 ++--- .../data/updater/devrepo/DevRepoRelease.kt | 14 +++++++ .../updater/devrepo/DevRepoUpdateChecker.kt | 40 +++++++++++++++++++ .../updater/devrepo/DevRepoUpdateResult.kt | 10 +++++ .../updater/{ => github}/GithubRelease.kt | 11 ++--- .../updater/{ => github}/GithubService.kt | 4 +- .../{ => github}/GithubUpdateChecker.kt | 14 +++---- .../data/updater/github/GithubUpdateResult.kt | 10 +++++ .../ui/setting/SettingsAboutController.kt | 15 ++++--- 13 files changed, 147 insertions(+), 34 deletions(-) delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/updater/GithubUpdateResult.kt create mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/updater/Release.kt create mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdateChecker.kt create mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdateResult.kt create mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/updater/devrepo/DevRepoRelease.kt create mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/updater/devrepo/DevRepoUpdateChecker.kt create mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/updater/devrepo/DevRepoUpdateResult.kt rename app/src/main/java/eu/kanade/tachiyomi/data/updater/{ => github}/GithubRelease.kt (72%) rename app/src/main/java/eu/kanade/tachiyomi/data/updater/{ => github}/GithubService.kt (95%) rename app/src/main/java/eu/kanade/tachiyomi/data/updater/{ => github}/GithubUpdateChecker.kt (67%) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubUpdateResult.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/GithubUpdateResult.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/GithubUpdateResult.kt deleted file mode 100644 index 3f07d2da6d..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/GithubUpdateResult.kt +++ /dev/null @@ -1,7 +0,0 @@ -package eu.kanade.tachiyomi.data.updater - -sealed class GithubUpdateResult { - - class NewUpdate(val release: GithubRelease): GithubUpdateResult() - class NoNewUpdate : GithubUpdateResult() -} \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/Release.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/Release.kt new file mode 100644 index 0000000000..9ac138e980 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/Release.kt @@ -0,0 +1,13 @@ +package eu.kanade.tachiyomi.data.updater + +interface Release { + + val info: String + + /** + * Get download link of latest release. + * @return download link of latest release. + */ + val downloadLink: String + +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdateChecker.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdateChecker.kt new file mode 100644 index 0000000000..4d2a1de66c --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdateChecker.kt @@ -0,0 +1,25 @@ +package eu.kanade.tachiyomi.data.updater + +import eu.kanade.tachiyomi.BuildConfig +import eu.kanade.tachiyomi.data.updater.devrepo.DevRepoUpdateChecker +import eu.kanade.tachiyomi.data.updater.github.GithubUpdateChecker +import rx.Observable + +abstract class UpdateChecker { + + companion object { + fun getUpdateChecker(): UpdateChecker { + return if (BuildConfig.DEBUG) { + DevRepoUpdateChecker() + } else { + GithubUpdateChecker() + } + } + } + + /** + * Returns observable containing release information + */ + abstract fun checkForUpdate(): Observable + +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdateResult.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdateResult.kt new file mode 100644 index 0000000000..a59864f557 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdateResult.kt @@ -0,0 +1,8 @@ +package eu.kanade.tachiyomi.data.updater + +abstract class UpdateResult { + + open class NewUpdate(val release: T): UpdateResult() + open class NoNewUpdate: UpdateResult() + +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterJob.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterJob.kt index 59832bd2d2..594ecd31b9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterJob.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterJob.kt @@ -13,10 +13,10 @@ import eu.kanade.tachiyomi.util.notificationManager class UpdaterJob : Job() { override fun onRunJob(params: Params): Result { - return GithubUpdateChecker() + return UpdateChecker.getUpdateChecker() .checkForUpdate() .map { result -> - if (result is GithubUpdateResult.NewUpdate) { + if (result is UpdateResult.NewUpdate<*>) { val url = result.release.downloadLink val intent = Intent(context, UpdaterService::class.java).apply { @@ -33,9 +33,9 @@ class UpdaterJob : Job() { PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)) } } - Job.Result.SUCCESS + Result.SUCCESS } - .onErrorReturn { Job.Result.FAILURE } + .onErrorReturn { Result.FAILURE } // Sadly, the task needs to be synchronous. .toBlocking() .single() @@ -64,4 +64,4 @@ class UpdaterJob : Job() { } } -} \ No newline at end of file +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/devrepo/DevRepoRelease.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/devrepo/DevRepoRelease.kt new file mode 100644 index 0000000000..ea8a79a182 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/devrepo/DevRepoRelease.kt @@ -0,0 +1,14 @@ +package eu.kanade.tachiyomi.data.updater.devrepo + +import eu.kanade.tachiyomi.data.updater.Release + +class DevRepoRelease(override val info: String) : Release { + + override val downloadLink: String + get() = LATEST_URL + + companion object { + const val LATEST_URL = "https://tachiyomi.kanade.eu/latest" + } + +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/devrepo/DevRepoUpdateChecker.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/devrepo/DevRepoUpdateChecker.kt new file mode 100644 index 0000000000..a24036830d --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/devrepo/DevRepoUpdateChecker.kt @@ -0,0 +1,40 @@ +package eu.kanade.tachiyomi.data.updater.devrepo + +import eu.kanade.tachiyomi.BuildConfig +import eu.kanade.tachiyomi.data.updater.UpdateChecker +import eu.kanade.tachiyomi.data.updater.UpdateResult +import eu.kanade.tachiyomi.network.GET +import eu.kanade.tachiyomi.network.NetworkHelper +import eu.kanade.tachiyomi.network.asObservable +import okhttp3.OkHttpClient +import rx.Observable +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.get + +class DevRepoUpdateChecker : UpdateChecker() { + + private val client: OkHttpClient by lazy { + Injekt.get().client.newBuilder() + .followRedirects(false) + .build() + } + + private val versionRegex: Regex by lazy { + Regex("tachiyomi-r(\\d+).apk") + } + + override fun checkForUpdate(): Observable { + return client.newCall(GET(DevRepoRelease.LATEST_URL)).asObservable() + .map { response -> + // Get latest repo version number from header in format "Location: tachiyomi-r1512.apk" + val latestVersionNumber: String = versionRegex.find(response.header("Location")!!)!!.groupValues[1] + + if (latestVersionNumber.toInt() > BuildConfig.COMMIT_COUNT.toInt()) { + DevRepoUpdateResult.NewUpdate(DevRepoRelease("v$latestVersionNumber")) + } else { + DevRepoUpdateResult.NoNewUpdate() + } + } + } + +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/devrepo/DevRepoUpdateResult.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/devrepo/DevRepoUpdateResult.kt new file mode 100644 index 0000000000..1bda48b9c3 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/devrepo/DevRepoUpdateResult.kt @@ -0,0 +1,10 @@ +package eu.kanade.tachiyomi.data.updater.devrepo + +import eu.kanade.tachiyomi.data.updater.UpdateResult + +sealed class DevRepoUpdateResult : UpdateResult() { + + class NewUpdate(release: DevRepoRelease): UpdateResult.NewUpdate(release) + class NoNewUpdate: UpdateResult.NoNewUpdate() + +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/GithubRelease.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubRelease.kt similarity index 72% rename from app/src/main/java/eu/kanade/tachiyomi/data/updater/GithubRelease.kt rename to app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubRelease.kt index 8c20690e2a..f65bf39ba6 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/GithubRelease.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubRelease.kt @@ -1,24 +1,25 @@ -package eu.kanade.tachiyomi.data.updater +package eu.kanade.tachiyomi.data.updater.github import com.google.gson.annotations.SerializedName +import eu.kanade.tachiyomi.data.updater.Release /** * Release object. * Contains information about the latest release from Github. * * @param version version of latest release. - * @param changeLog log of latest release. + * @param info log of latest release. * @param assets assets of latest release. */ class GithubRelease(@SerializedName("tag_name") val version: String, - @SerializedName("body") val changeLog: String, - @SerializedName("assets") private val assets: List) { + @SerializedName("body") override val info: String, + @SerializedName("assets") private val assets: List): Release { /** * Get download link of latest release from the assets. * @return download link of latest release. */ - val downloadLink: String + override val downloadLink: String get() = assets[0].downloadLink /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/GithubService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubService.kt similarity index 95% rename from app/src/main/java/eu/kanade/tachiyomi/data/updater/GithubService.kt rename to app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubService.kt index 82befdc723..e19e3528d5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/GithubService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubService.kt @@ -1,4 +1,4 @@ -package eu.kanade.tachiyomi.data.updater +package eu.kanade.tachiyomi.data.updater.github import eu.kanade.tachiyomi.network.NetworkHelper import retrofit2.Retrofit @@ -30,4 +30,4 @@ interface GithubService { @GET("/repos/inorichi/tachiyomi/releases/latest") fun getLatestVersion(): Observable -} \ No newline at end of file +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/GithubUpdateChecker.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubUpdateChecker.kt similarity index 67% rename from app/src/main/java/eu/kanade/tachiyomi/data/updater/GithubUpdateChecker.kt rename to app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubUpdateChecker.kt index 7d321dc71a..6fc6297409 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/GithubUpdateChecker.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubUpdateChecker.kt @@ -1,16 +1,15 @@ -package eu.kanade.tachiyomi.data.updater +package eu.kanade.tachiyomi.data.updater.github import eu.kanade.tachiyomi.BuildConfig +import eu.kanade.tachiyomi.data.updater.UpdateChecker +import eu.kanade.tachiyomi.data.updater.UpdateResult import rx.Observable -class GithubUpdateChecker { +class GithubUpdateChecker : UpdateChecker() { private val service: GithubService = GithubService.create() - /** - * Returns observable containing release information - */ - fun checkForUpdate(): Observable { + override fun checkForUpdate(): Observable { return service.getLatestVersion().map { release -> val newVersion = release.version.replace("[^\\d.]".toRegex(), "") @@ -22,4 +21,5 @@ class GithubUpdateChecker { } } } -} \ No newline at end of file + +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubUpdateResult.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubUpdateResult.kt new file mode 100644 index 0000000000..fcb304604b --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubUpdateResult.kt @@ -0,0 +1,10 @@ +package eu.kanade.tachiyomi.data.updater.github + +import eu.kanade.tachiyomi.data.updater.UpdateResult + +sealed class GithubUpdateResult : UpdateResult() { + + class NewUpdate(release: GithubRelease): UpdateResult.NewUpdate(release) + class NoNewUpdate : UpdateResult.NoNewUpdate() + +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAboutController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAboutController.kt index a736da2a5e..bbd23da2cf 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAboutController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAboutController.kt @@ -9,8 +9,8 @@ import android.view.View import com.afollestad.materialdialogs.MaterialDialog import eu.kanade.tachiyomi.BuildConfig import eu.kanade.tachiyomi.R -import eu.kanade.tachiyomi.data.updater.GithubUpdateChecker -import eu.kanade.tachiyomi.data.updater.GithubUpdateResult +import eu.kanade.tachiyomi.data.updater.UpdateChecker +import eu.kanade.tachiyomi.data.updater.UpdateResult import eu.kanade.tachiyomi.data.updater.UpdaterJob import eu.kanade.tachiyomi.data.updater.UpdaterService import eu.kanade.tachiyomi.ui.base.controller.DialogController @@ -26,20 +26,19 @@ import java.util.Locale import java.util.TimeZone import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys - class SettingsAboutController : SettingsController() { /** * Checks for new releases */ - private val updateChecker by lazy { GithubUpdateChecker() } + private val updateChecker by lazy { UpdateChecker.getUpdateChecker() } /** * The subscribtion service of the obtained release object */ private var releaseSubscription: Subscription? = null - private val isUpdaterEnabled = !BuildConfig.DEBUG && BuildConfig.INCLUDE_UPDATER + private val isUpdaterEnabled = BuildConfig.INCLUDE_UPDATER override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) { titleRes = R.string.pref_category_about @@ -124,14 +123,14 @@ class SettingsAboutController : SettingsController() { .observeOn(AndroidSchedulers.mainThread()) .subscribe({ result -> when (result) { - is GithubUpdateResult.NewUpdate -> { - val body = result.release.changeLog + is UpdateResult.NewUpdate<*> -> { + val body = result.release.info val url = result.release.downloadLink // Create confirmation window NewUpdateDialogController(body, url).showDialog(router) } - is GithubUpdateResult.NoNewUpdate -> { + is UpdateResult.NoNewUpdate -> { activity?.toast(R.string.update_check_no_new_updates) } }