add current git hash to the build config

add support for hash in the version check
refactor-toolbar
Floens 8 years ago
parent 141c1fe73e
commit 6d1ff66c00
  1. 13
      Clover/app/build.gradle
  2. 10
      Clover/app/src/main/java/org/floens/chan/core/net/UpdateApiRequest.java
  3. 75
      Clover/app/src/main/java/org/floens/chan/core/update/UpdateManager.java

@ -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'
}

@ -42,7 +42,8 @@ public class UpdateApiRequest extends JsonReaderRequest<UpdateApiRequest.UpdateA
private String forFlavor;
public UpdateApiRequest(Response.Listener<UpdateApiResponse> listener, Response.ErrorListener errorListener) {
public UpdateApiRequest(Response.Listener<UpdateApiResponse> listener,
Response.ErrorListener errorListener) {
super(BuildConfig.UPDATE_API_ENDPOINT, listener, errorListener);
forFlavor = BuildConfig.FLAVOR;
}
@ -103,8 +104,12 @@ public class UpdateApiRequest extends JsonReaderRequest<UpdateApiRequest.UpdateA
case "code":
message.code = reader.nextInt();
break;
case "hash":
message.hash = reader.nextString();
break;
case "date":
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US);
DateFormat format = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss", Locale.US);
try {
message.date = format.parse(reader.nextString());
} catch (ParseException ignore) {
@ -155,6 +160,7 @@ public class UpdateApiRequest extends JsonReaderRequest<UpdateApiRequest.UpdateA
public static class UpdateApiMessage {
public String type;
public int code;
public String hash;
public Date date;
public String messageHtml;
public HttpUrl apkUrl;

@ -25,8 +25,6 @@ import android.os.StrictMode;
import android.text.TextUtils;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import org.floens.chan.BuildConfig;
import org.floens.chan.core.cache.FileCache;
@ -88,28 +86,23 @@ public class UpdateManager {
}
Logger.d(TAG, "Calling update API");
volleyRequestQueue.add(new UpdateApiRequest(new Response.Listener<UpdateApiRequest.UpdateApiResponse>() {
@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.

Loading…
Cancel
Save