diff --git a/Clover/app/build.gradle b/Clover/app/build.gradle index 708a8b34..7f43c6fd 100644 --- a/Clover/app/build.gradle +++ b/Clover/app/build.gradle @@ -9,7 +9,7 @@ def getCommitHash = { -> commandLine 'git', 'rev-parse', '--short', 'HEAD' standardOutput = stdout } - return "-" + stdout.toString().trim() + return stdout.toString().trim() } android { @@ -47,6 +47,8 @@ android { keyPass=pass */ + def hash = getCommitHash(); + File f = file('keys.properties') boolean doSign = f.exists(); @@ -77,6 +79,7 @@ android { applicationId "org.floens.chan" resValue "string", "app_name", "Clover" resValue "string", "app_flavor_name", "" + buildConfigField "String", "BUILD_HASH", "\"$hash\"" buildConfigField "String", "UPDATE_API_ENDPOINT", "\"https://floens.github.io/Clover/api/update\"" buildConfigField "String", "CRASH_REPORT_ENDPOINT", "\"https://acra.floens.org/clover/report\"" } @@ -86,8 +89,9 @@ android { applicationId "org.floens.chan.dev" resValue "string", "app_name", "Clover dev" resValue "string", "app_flavor_name", "" - buildConfigField "String", "UPDATE_API_ENDPOINT", "\"\"" - buildConfigField "String", "CRASH_REPORT_ENDPOINT", "\"\"" + buildConfigField "String", "BUILD_HASH", "\"$hash\"" + buildConfigField "String", "UPDATE_API_ENDPOINT", "\"https://build.floens.org/clover/api/update_dev\"" + buildConfigField "String", "CRASH_REPORT_ENDPOINT", "\"https://acra.floens.org/clover/report\"" } fdroid { @@ -95,6 +99,7 @@ android { applicationId "org.floens.chan" resValue "string", "app_name", "Clover" resValue "string", "app_flavor_name", "F-Droid" + buildConfigField "String", "BUILD_HASH", "\"$hash\"" buildConfigField "String", "UPDATE_API_ENDPOINT", "\"https://floens.github.io/Clover/api/update\"" buildConfigField "String", "CRASH_REPORT_ENDPOINT", "\"https://acra.floens.org/clover/report\"" } @@ -110,7 +115,7 @@ android { } debug { - versionNameSuffix getCommitHash() + versionNameSuffix "-" + hash // minifyEnabled true // proguardFiles 'proguard.cfg' } diff --git a/Clover/app/src/main/java/org/floens/chan/core/net/UpdateApiRequest.java b/Clover/app/src/main/java/org/floens/chan/core/net/UpdateApiRequest.java index 226e5c59..8c7c26bf 100644 --- a/Clover/app/src/main/java/org/floens/chan/core/net/UpdateApiRequest.java +++ b/Clover/app/src/main/java/org/floens/chan/core/net/UpdateApiRequest.java @@ -42,7 +42,8 @@ public class UpdateApiRequest extends JsonReaderRequest listener, Response.ErrorListener errorListener) { + public UpdateApiRequest(Response.Listener listener, + Response.ErrorListener errorListener) { super(BuildConfig.UPDATE_API_ENDPOINT, listener, errorListener); forFlavor = BuildConfig.FLAVOR; } @@ -103,8 +104,12 @@ public class UpdateApiRequest extends JsonReaderRequest() { - @Override - public void onResponse(UpdateApiRequest.UpdateApiResponse response) { - if (!processUpdateApiResponse(response) && manual) { - callback.onManualCheckNone(); - } + volleyRequestQueue.add(new UpdateApiRequest(response -> { + if (!processUpdateApiResponse(response) && manual) { + callback.onManualCheckNone(); } - }, new Response.ErrorListener() { - @Override - public void onErrorResponse(VolleyError error) { - Logger.e(TAG, "Failed to process API call for updating", error); + }, error -> { + Logger.e(TAG, "Failed to process API call for updating", error); - if (manual) { - callback.onManualCheckFailed(); - } + if (manual) { + callback.onManualCheckFailed(); } })); } private boolean processUpdateApiResponse(UpdateApiRequest.UpdateApiResponse response) { if (response.newerApiVersion) { - Logger.e(TAG, "API endpoint reports a higher API version than we support, aborting update check."); + Logger.e(TAG, "API endpoint reports a higher API version than we support, " + + "aborting update check."); // ignore return false; @@ -129,27 +122,42 @@ public class UpdateManager { } private boolean processUpdateMessage(UpdateApiRequest.UpdateApiMessage message) { - if (message.code <= BuildConfig.VERSION_CODE) { - Logger.d(TAG, "No newer version available (" + BuildConfig.VERSION_CODE + " >= " + message.code + ")."); - // Our code is newer than the message - return false; - } + if (isMessageRelevantForThisVersion(message)) { + if (message.type.equals(UpdateApiRequest.TYPE_UPDATE)) { + if (message.apkUrl == null) { + Logger.i(TAG, "Update available but none for this build flavor."); + // Not for this flavor, discard. + return false; + } - if (message.type.equals(UpdateApiRequest.TYPE_UPDATE)) { - if (message.apkUrl == null) { - Logger.i(TAG, "Update available but none for this build flavor."); - // Not for this flavor, discard. - return false; + Logger.i(TAG, "Update available (" + message.code + ") with url \"" + + message.apkUrl + "\"."); + callback.showUpdateAvailableDialog(message); + return true; } - - Logger.i(TAG, "Update available (" + message.code + ") with url \"" + message.apkUrl + "\"."); - callback.showUpdateAvailableDialog(message); - return true; } return false; } + private boolean isMessageRelevantForThisVersion(UpdateApiRequest.UpdateApiMessage message) { + if (message.code != 0) { + if (message.code <= BuildConfig.VERSION_CODE) { + Logger.d(TAG, "No newer version available (" + + BuildConfig.VERSION_CODE + " >= " + message.code + ")."); + // Our code is newer than the message + return false; + } else { + return true; + } + } else if (message.hash != null) { + return !message.hash.equals(BuildConfig.BUILD_HASH); + } else { + Logger.w(TAG, "No code or hash found"); + return false; + } + } + /** * Install the APK file specified in {@code update}. This methods needs the storage permission. * @@ -180,7 +188,9 @@ public class UpdateManager { } private void copyToPublicDirectory(File cacheFile) { - File out = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), DOWNLOAD_FILE); + File out = new File(Environment.getExternalStoragePublicDirectory( + Environment.DIRECTORY_DOWNLOADS), + DOWNLOAD_FILE); try { IOUtils.copyFile(cacheFile, out); } catch (IOException e) { @@ -198,7 +208,8 @@ public class UpdateManager { // Then launch the APK install intent. Intent intent = new Intent(Intent.ACTION_VIEW); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - intent.setDataAndType(Uri.fromFile(install.installFile), "application/vnd.android.package-archive"); + intent.setDataAndType(Uri.fromFile(install.installFile), + "application/vnd.android.package-archive"); // The installer wants a content scheme from android N and up, // but I don't feel like implementing a content provider just for this feature.