From 5801f5835ea8192778dc9a0f28aa80b7db240d60 Mon Sep 17 00:00:00 2001 From: astarivi Date: Sun, 25 Jun 2023 18:40:43 -0500 Subject: [PATCH] Prepare for update --- app/build.gradle | 244 +++++----- .../kaizoyu/core/updater/UpdateManager.java | 446 +++++++++--------- 2 files changed, 345 insertions(+), 345 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 3689bc5..5721e14 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,123 +1,123 @@ -plugins { - id 'com.android.application' -} - -repositories { - mavenCentral() - maven { - url 'https://jitpack.io' - } - maven { - url 'https://gitlab.com/api/v4/projects/42572321/packages/maven' - } -} - -android { - compileSdk 33 - buildToolsVersion = "31.0.0" - - packagingOptions { - resources { - excludes += ['META-INF/DEPENDENCIES', 'mozilla/public-suffix-list.txt', 'META-INF/LICENSE', 'META-INF/LICENSE.txt', 'META-INF/license.txt', 'META-INF/NOTICE', 'META-INF/NOTICE.txt', 'META-INF/notice.txt', 'META-INF/ASL2.0', 'META-INF/*.kotlin_module'] - } - } - - defaultConfig { - applicationId "com.astarivi.kaizoyu" - minSdkVersion 23 - targetSdkVersion 33 - versionCode 4 - versionName "0.3" - testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" - javaCompileOptions { - annotationProcessorOptions { - arguments += ["room.schemaLocation": "$projectDir/schemas".toString()] - } - } - } - - buildTypes { - release { - minifyEnabled false - shrinkResources false - proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' - } - } - - compileOptions { - sourceCompatibility JavaVersion.VERSION_1_8 - targetCompatibility JavaVersion.VERSION_1_8 - coreLibraryDesugaringEnabled true - } - - buildFeatures { - viewBinding true - } - - namespace 'com.astarivi.kaizoyu' - - splits { - abi { - enable true - reset() - - include "armeabi-v7a", "arm64-v8a", "x86", "x86_64" - universalApk true - } - } -} - -dependencies { - // Core library - implementation 'com.astarivi.kaizolib:KaizoLib:1.4' - - // External libraries (third party) - implementation 'io.github.tonnyl:spark:0.1.0-alpha' - implementation 'org.videolan.android:libvlc-all:4.0.0-eap11' - implementation 'com.airbnb.android:lottie:6.0.0' - implementation 'com.github.bumptech.glide:glide:4.15.1' - implementation 'com.github.bumptech.glide:recyclerview-integration:4.15.1' - implementation 'com.github.bumptech.glide:okhttp3-integration:4.15.1' - - // Analytics - implementation 'com.flurry.android:analytics:14.2.0' - - // Android X - implementation 'androidx.appcompat:appcompat:1.6.1' - implementation 'androidx.constraintlayout:constraintlayout:2.1.4' - implementation "androidx.work:work-runtime:2.8.0" - implementation "androidx.room:room-runtime:2.5.0" - implementation 'androidx.lifecycle:lifecycle-viewmodel:2.5.1' - implementation 'androidx.lifecycle:lifecycle-livedata:2.5.1' - implementation 'androidx.palette:palette:1.0.0' - annotationProcessor "androidx.room:room-compiler:2.5.0" - - // Logger - implementation 'org.tinylog:tinylog-api:2.6.1' - implementation 'org.tinylog:tinylog-impl:2.6.1' - runtimeOnly 'org.tinylog:slf4j-tinylog:2.6.1' - - // Jackson - // Don't update to 2.14 as Android minSdk support changes. - implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.5' - implementation 'com.fasterxml.jackson.core:jackson-core:2.13.5' - implementation 'com.fasterxml.jackson.core:jackson-annotations:2.13.5' - - // HTTP - implementation 'com.squareup.okhttp3:okhttp:4.10.0' - - // Google others - implementation 'com.google.android.material:material:1.9.0' - coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.2' - - // Testing - testImplementation 'junit:junit:4.13.2' - androidTestImplementation 'androidx.test.ext:junit:1.1.3' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' -} - -configurations.all { - resolutionStrategy.dependencySubstitution { - substitute module('org.hamcrest:hamcrest-core:1.1') with module('junit:junit:4.13.2') - } +plugins { + id 'com.android.application' +} + +repositories { + mavenCentral() + maven { + url 'https://jitpack.io' + } + maven { + url 'https://gitlab.com/api/v4/projects/42572321/packages/maven' + } +} + +android { + compileSdk 33 + buildToolsVersion = "31.0.0" + + packagingOptions { + resources { + excludes += ['META-INF/DEPENDENCIES', 'mozilla/public-suffix-list.txt', 'META-INF/LICENSE', 'META-INF/LICENSE.txt', 'META-INF/license.txt', 'META-INF/NOTICE', 'META-INF/NOTICE.txt', 'META-INF/notice.txt', 'META-INF/ASL2.0', 'META-INF/*.kotlin_module'] + } + } + + defaultConfig { + applicationId "com.astarivi.kaizoyu" + minSdkVersion 23 + targetSdkVersion 33 + versionCode 5 + versionName "0.4" + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + javaCompileOptions { + annotationProcessorOptions { + arguments += ["room.schemaLocation": "$projectDir/schemas".toString()] + } + } + } + + buildTypes { + release { + minifyEnabled false + shrinkResources false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + coreLibraryDesugaringEnabled true + } + + buildFeatures { + viewBinding true + } + + namespace 'com.astarivi.kaizoyu' + + splits { + abi { + enable true + reset() + + include "armeabi-v7a", "arm64-v8a", "x86", "x86_64" + universalApk true + } + } +} + +dependencies { + // Core library + implementation 'com.astarivi.kaizolib:KaizoLib:1.4' + + // External libraries (third party) + implementation 'io.github.tonnyl:spark:0.1.0-alpha' + implementation 'org.videolan.android:libvlc-all:4.0.0-eap11' + implementation 'com.airbnb.android:lottie:6.0.0' + implementation 'com.github.bumptech.glide:glide:4.15.1' + implementation 'com.github.bumptech.glide:recyclerview-integration:4.15.1' + implementation 'com.github.bumptech.glide:okhttp3-integration:4.15.1' + + // Analytics + implementation 'com.flurry.android:analytics:14.2.0' + + // Android X + implementation 'androidx.appcompat:appcompat:1.6.1' + implementation 'androidx.constraintlayout:constraintlayout:2.1.4' + implementation "androidx.work:work-runtime:2.8.0" + implementation "androidx.room:room-runtime:2.5.0" + implementation 'androidx.lifecycle:lifecycle-viewmodel:2.5.1' + implementation 'androidx.lifecycle:lifecycle-livedata:2.5.1' + implementation 'androidx.palette:palette:1.0.0' + annotationProcessor "androidx.room:room-compiler:2.5.0" + + // Logger + implementation 'org.tinylog:tinylog-api:2.6.1' + implementation 'org.tinylog:tinylog-impl:2.6.1' + runtimeOnly 'org.tinylog:slf4j-tinylog:2.6.1' + + // Jackson + // Don't update to 2.14 as Android minSdk support changes. + implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.5' + implementation 'com.fasterxml.jackson.core:jackson-core:2.13.5' + implementation 'com.fasterxml.jackson.core:jackson-annotations:2.13.5' + + // HTTP + implementation 'com.squareup.okhttp3:okhttp:4.10.0' + + // Google others + implementation 'com.google.android.material:material:1.9.0' + coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.2' + + // Testing + testImplementation 'junit:junit:4.13.2' + androidTestImplementation 'androidx.test.ext:junit:1.1.3' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' +} + +configurations.all { + resolutionStrategy.dependencySubstitution { + substitute module('org.hamcrest:hamcrest-core:1.1') with module('junit:junit:4.13.2') + } } \ No newline at end of file diff --git a/app/src/main/java/com/astarivi/kaizoyu/core/updater/UpdateManager.java b/app/src/main/java/com/astarivi/kaizoyu/core/updater/UpdateManager.java index b351cc8..8a8a5a8 100644 --- a/app/src/main/java/com/astarivi/kaizoyu/core/updater/UpdateManager.java +++ b/app/src/main/java/com/astarivi/kaizoyu/core/updater/UpdateManager.java @@ -1,223 +1,223 @@ -package com.astarivi.kaizoyu.core.updater; - -import android.os.Build; -import android.os.Parcel; -import android.os.Parcelable; - -import androidx.annotation.NonNull; - -import com.astarivi.kaizoyu.core.adapters.WebAdapter; -import com.astarivi.kaizoyu.core.annotations.ThreadedOnly; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; - -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.tinylog.Logger; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.time.ZoneId; -import java.util.Calendar; -import java.util.Date; -import java.util.Locale; -import java.util.Objects; -import java.util.TimeZone; - -import okhttp3.HttpUrl; - - -public class UpdateManager { - // TODO Change this for every release, doh. - public static String VERSION = "0.3"; - public static String VERSION_NAME = "fool"; - - @ThreadedOnly - public @Nullable LatestUpdate getLatestUpdate() throws ParseException { - final LatestCDNReleases latestReleaseCDN = getLatestReleases(); - - if (latestReleaseCDN == null || latestReleaseCDN.latest_release == null) - return null; - - float latestVersion; - float currentVersion; - - try { - latestVersion = Float.parseFloat(latestReleaseCDN.latest_release); - currentVersion = Float.parseFloat(VERSION); - } catch (NumberFormatException e) { - throw new ParseException("Couldn't parse latest version TAG", 0); - } - - if (Float.compare(latestVersion, currentVersion) <= 0) { - return null; - } - - GithubRelease latestRelease = getVersion(latestReleaseCDN.latest_release); - - if (latestRelease == null || latestRelease.assets == null || latestRelease.assets.length == 0) - return null; - - final String desiredVersion = String.format("app-%s-release.apk", Build.SUPPORTED_ABIS[0]); - - for (GithubReleaseAsset releaseAsset : latestRelease.assets) { - if (releaseAsset.name.equals(desiredVersion)) { - return new LatestUpdate( - releaseAsset, - latestRelease.tag_name, - latestRelease.body - ); - } - } - - return null; - } - - @ThreadedOnly - private @Nullable LatestCDNReleases getLatestReleases() { - String body = WebAdapter.getJSON( - new HttpUrl.Builder() - .scheme("https") - .host("raw.githubusercontent.com") - .addPathSegments("astarivi/KaizoDelivery/main/data/latest_release.json") - .build() - ); - - if (body == null) return null; - - LatestCDNReleases latestCDNReleases; - - try { - latestCDNReleases = new ObjectMapper().readValue(body, LatestCDNReleases.class); - } catch (JsonProcessingException e) { - Logger.error("Failed to decode Github KaizoDelivery latest releases"); - return null; - } - - return latestCDNReleases; - } - - @ThreadedOnly - private @Nullable GithubRelease getVersion(@NotNull final String tag) { - String body = WebAdapter.getJSON( - new HttpUrl.Builder() - .scheme("https") - .host("api.github.com") - .addPathSegments("repos/astarivi/KaizoDelivery/releases/tags") - .addPathSegment(tag) - .build() - ); - - if (body == null) return null; - - GithubRelease latestRelease; - - try { - latestRelease = new ObjectMapper().readValue(body, GithubRelease.class); - } catch (JsonProcessingException e) { - Logger.error("Failed to decode Github latest release"); - return null; - } - - return latestRelease; - } - - @JsonIgnoreProperties(ignoreUnknown = true) - public static class LatestCDNReleases { - public String latest_release; - public String latest_beta; - } - - @JsonIgnoreProperties(ignoreUnknown = true) - public static class GithubRelease { - public String tag_name; - public String published_at; - public String name; - public String body; - public GithubReleaseAsset[] assets; - - } - - @JsonIgnoreProperties(ignoreUnknown = true) - public static class GithubReleaseAsset { - public String url; - public String name; - public double size; - public String browser_download_url; - public String created_at; - } - - public static class LatestUpdate implements Parcelable { - public String version; - public String name; - public double size; - public Date releaseDate; - public String downloadUrl; - public String body; - - public LatestUpdate(@NotNull GithubReleaseAsset releaseAsset, String version, String body) throws ParseException { - Calendar calendar = Calendar.getInstance( - TimeZone.getTimeZone( - ZoneId.systemDefault() - ) - ); - - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.UK); - sdf.setCalendar(calendar); - try { - calendar.setTime( - Objects.requireNonNull(sdf.parse( - releaseAsset.created_at - )) - ); - } catch(NullPointerException ex) { - throw new ParseException("The parsed date was null", 0); - } - - this.releaseDate = calendar.getTime(); - this.name = releaseAsset.name; - this.size = releaseAsset.size; - this.downloadUrl = releaseAsset.browser_download_url; - this.version = version; - this.body = body; - } - - // Parcelable implementation - protected LatestUpdate(@NotNull Parcel parcel) { - version = parcel.readString(); - name = parcel.readString(); - size = parcel.readDouble(); - releaseDate = new Date(parcel.readLong()); - downloadUrl = parcel.readString(); - body = parcel.readString(); - } - - public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { - @Override - public LatestUpdate createFromParcel(Parcel parcel) { - return new LatestUpdate(parcel); - } - - @Override - public LatestUpdate[] newArray(int size) { - return new LatestUpdate[size]; - } - }; - - @Override - public int describeContents() { - return 0; - } - - @Override - public void writeToParcel(@NonNull Parcel dest, int flags) { - dest.writeString(version); - dest.writeString(name); - dest.writeDouble(size); - dest.writeLong(releaseDate.getTime()); - dest.writeString(downloadUrl); - dest.writeString(body); - } - } -} +package com.astarivi.kaizoyu.core.updater; + +import android.os.Build; +import android.os.Parcel; +import android.os.Parcelable; + +import androidx.annotation.NonNull; + +import com.astarivi.kaizoyu.core.adapters.WebAdapter; +import com.astarivi.kaizoyu.core.annotations.ThreadedOnly; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.tinylog.Logger; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.ZoneId; +import java.util.Calendar; +import java.util.Date; +import java.util.Locale; +import java.util.Objects; +import java.util.TimeZone; + +import okhttp3.HttpUrl; + + +public class UpdateManager { + // TODO Change this for every release, doh. + public static String VERSION = "0.4"; + public static String VERSION_NAME = "magician"; + + @ThreadedOnly + public @Nullable LatestUpdate getLatestUpdate() throws ParseException { + final LatestCDNReleases latestReleaseCDN = getLatestReleases(); + + if (latestReleaseCDN == null || latestReleaseCDN.latest_release == null) + return null; + + float latestVersion; + float currentVersion; + + try { + latestVersion = Float.parseFloat(latestReleaseCDN.latest_release); + currentVersion = Float.parseFloat(VERSION); + } catch (NumberFormatException e) { + throw new ParseException("Couldn't parse latest version TAG", 0); + } + + if (Float.compare(latestVersion, currentVersion) <= 0) { + return null; + } + + GithubRelease latestRelease = getVersion(latestReleaseCDN.latest_release); + + if (latestRelease == null || latestRelease.assets == null || latestRelease.assets.length == 0) + return null; + + final String desiredVersion = String.format("app-%s-release.apk", Build.SUPPORTED_ABIS[0]); + + for (GithubReleaseAsset releaseAsset : latestRelease.assets) { + if (releaseAsset.name.equals(desiredVersion)) { + return new LatestUpdate( + releaseAsset, + latestRelease.tag_name, + latestRelease.body + ); + } + } + + return null; + } + + @ThreadedOnly + private @Nullable LatestCDNReleases getLatestReleases() { + String body = WebAdapter.getJSON( + new HttpUrl.Builder() + .scheme("https") + .host("raw.githubusercontent.com") + .addPathSegments("astarivi/KaizoDelivery/main/data/latest_release.json") + .build() + ); + + if (body == null) return null; + + LatestCDNReleases latestCDNReleases; + + try { + latestCDNReleases = new ObjectMapper().readValue(body, LatestCDNReleases.class); + } catch (JsonProcessingException e) { + Logger.error("Failed to decode Github KaizoDelivery latest releases"); + return null; + } + + return latestCDNReleases; + } + + @ThreadedOnly + private @Nullable GithubRelease getVersion(@NotNull final String tag) { + String body = WebAdapter.getJSON( + new HttpUrl.Builder() + .scheme("https") + .host("api.github.com") + .addPathSegments("repos/astarivi/KaizoDelivery/releases/tags") + .addPathSegment(tag) + .build() + ); + + if (body == null) return null; + + GithubRelease latestRelease; + + try { + latestRelease = new ObjectMapper().readValue(body, GithubRelease.class); + } catch (JsonProcessingException e) { + Logger.error("Failed to decode Github latest release"); + return null; + } + + return latestRelease; + } + + @JsonIgnoreProperties(ignoreUnknown = true) + public static class LatestCDNReleases { + public String latest_release; + public String latest_beta; + } + + @JsonIgnoreProperties(ignoreUnknown = true) + public static class GithubRelease { + public String tag_name; + public String published_at; + public String name; + public String body; + public GithubReleaseAsset[] assets; + + } + + @JsonIgnoreProperties(ignoreUnknown = true) + public static class GithubReleaseAsset { + public String url; + public String name; + public double size; + public String browser_download_url; + public String created_at; + } + + public static class LatestUpdate implements Parcelable { + public String version; + public String name; + public double size; + public Date releaseDate; + public String downloadUrl; + public String body; + + public LatestUpdate(@NotNull GithubReleaseAsset releaseAsset, String version, String body) throws ParseException { + Calendar calendar = Calendar.getInstance( + TimeZone.getTimeZone( + ZoneId.systemDefault() + ) + ); + + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.UK); + sdf.setCalendar(calendar); + try { + calendar.setTime( + Objects.requireNonNull(sdf.parse( + releaseAsset.created_at + )) + ); + } catch(NullPointerException ex) { + throw new ParseException("The parsed date was null", 0); + } + + this.releaseDate = calendar.getTime(); + this.name = releaseAsset.name; + this.size = releaseAsset.size; + this.downloadUrl = releaseAsset.browser_download_url; + this.version = version; + this.body = body; + } + + // Parcelable implementation + protected LatestUpdate(@NotNull Parcel parcel) { + version = parcel.readString(); + name = parcel.readString(); + size = parcel.readDouble(); + releaseDate = new Date(parcel.readLong()); + downloadUrl = parcel.readString(); + body = parcel.readString(); + } + + public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { + @Override + public LatestUpdate createFromParcel(Parcel parcel) { + return new LatestUpdate(parcel); + } + + @Override + public LatestUpdate[] newArray(int size) { + return new LatestUpdate[size]; + } + }; + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(@NonNull Parcel dest, int flags) { + dest.writeString(version); + dest.writeString(name); + dest.writeDouble(size); + dest.writeLong(releaseDate.getTime()); + dest.writeString(downloadUrl); + dest.writeString(body); + } + } +}