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' commandLine 'git', 'rev-parse', '--short', 'HEAD'
standardOutput = stdout standardOutput = stdout
} }
return "-" + stdout.toString().trim() return stdout.toString().trim()
} }
android { android {
@ -47,6 +47,8 @@ android {
keyPass=pass keyPass=pass
*/ */
def hash = getCommitHash();
File f = file('keys.properties') File f = file('keys.properties')
boolean doSign = f.exists(); boolean doSign = f.exists();
@ -77,6 +79,7 @@ android {
applicationId "org.floens.chan" applicationId "org.floens.chan"
resValue "string", "app_name", "Clover" resValue "string", "app_name", "Clover"
resValue "string", "app_flavor_name", "" resValue "string", "app_flavor_name", ""
buildConfigField "String", "BUILD_HASH", "\"$hash\""
buildConfigField "String", "UPDATE_API_ENDPOINT", "\"https://floens.github.io/Clover/api/update\"" buildConfigField "String", "UPDATE_API_ENDPOINT", "\"https://floens.github.io/Clover/api/update\""
buildConfigField "String", "CRASH_REPORT_ENDPOINT", "\"https://acra.floens.org/clover/report\"" buildConfigField "String", "CRASH_REPORT_ENDPOINT", "\"https://acra.floens.org/clover/report\""
} }
@ -86,8 +89,9 @@ android {
applicationId "org.floens.chan.dev" applicationId "org.floens.chan.dev"
resValue "string", "app_name", "Clover dev" resValue "string", "app_name", "Clover dev"
resValue "string", "app_flavor_name", "" resValue "string", "app_flavor_name", ""
buildConfigField "String", "UPDATE_API_ENDPOINT", "\"\"" buildConfigField "String", "BUILD_HASH", "\"$hash\""
buildConfigField "String", "CRASH_REPORT_ENDPOINT", "\"\"" 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 { fdroid {
@ -95,6 +99,7 @@ android {
applicationId "org.floens.chan" applicationId "org.floens.chan"
resValue "string", "app_name", "Clover" resValue "string", "app_name", "Clover"
resValue "string", "app_flavor_name", "F-Droid" 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", "UPDATE_API_ENDPOINT", "\"https://floens.github.io/Clover/api/update\""
buildConfigField "String", "CRASH_REPORT_ENDPOINT", "\"https://acra.floens.org/clover/report\"" buildConfigField "String", "CRASH_REPORT_ENDPOINT", "\"https://acra.floens.org/clover/report\""
} }
@ -110,7 +115,7 @@ android {
} }
debug { debug {
versionNameSuffix getCommitHash() versionNameSuffix "-" + hash
// minifyEnabled true // minifyEnabled true
// proguardFiles 'proguard.cfg' // proguardFiles 'proguard.cfg'
} }

@ -42,7 +42,8 @@ public class UpdateApiRequest extends JsonReaderRequest<UpdateApiRequest.UpdateA
private String forFlavor; 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); super(BuildConfig.UPDATE_API_ENDPOINT, listener, errorListener);
forFlavor = BuildConfig.FLAVOR; forFlavor = BuildConfig.FLAVOR;
} }
@ -103,8 +104,12 @@ public class UpdateApiRequest extends JsonReaderRequest<UpdateApiRequest.UpdateA
case "code": case "code":
message.code = reader.nextInt(); message.code = reader.nextInt();
break; break;
case "hash":
message.hash = reader.nextString();
break;
case "date": 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 { try {
message.date = format.parse(reader.nextString()); message.date = format.parse(reader.nextString());
} catch (ParseException ignore) { } catch (ParseException ignore) {
@ -155,6 +160,7 @@ public class UpdateApiRequest extends JsonReaderRequest<UpdateApiRequest.UpdateA
public static class UpdateApiMessage { public static class UpdateApiMessage {
public String type; public String type;
public int code; public int code;
public String hash;
public Date date; public Date date;
public String messageHtml; public String messageHtml;
public HttpUrl apkUrl; public HttpUrl apkUrl;

@ -25,8 +25,6 @@ import android.os.StrictMode;
import android.text.TextUtils; import android.text.TextUtils;
import com.android.volley.RequestQueue; import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import org.floens.chan.BuildConfig; import org.floens.chan.BuildConfig;
import org.floens.chan.core.cache.FileCache; import org.floens.chan.core.cache.FileCache;
@ -88,28 +86,23 @@ public class UpdateManager {
} }
Logger.d(TAG, "Calling update API"); Logger.d(TAG, "Calling update API");
volleyRequestQueue.add(new UpdateApiRequest(new Response.Listener<UpdateApiRequest.UpdateApiResponse>() { volleyRequestQueue.add(new UpdateApiRequest(response -> {
@Override if (!processUpdateApiResponse(response) && manual) {
public void onResponse(UpdateApiRequest.UpdateApiResponse response) { callback.onManualCheckNone();
if (!processUpdateApiResponse(response) && manual) {
callback.onManualCheckNone();
}
} }
}, new Response.ErrorListener() { }, error -> {
@Override Logger.e(TAG, "Failed to process API call for updating", error);
public void onErrorResponse(VolleyError error) {
Logger.e(TAG, "Failed to process API call for updating", error);
if (manual) { if (manual) {
callback.onManualCheckFailed(); callback.onManualCheckFailed();
}
} }
})); }));
} }
private boolean processUpdateApiResponse(UpdateApiRequest.UpdateApiResponse response) { private boolean processUpdateApiResponse(UpdateApiRequest.UpdateApiResponse response) {
if (response.newerApiVersion) { 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 // ignore
return false; return false;
@ -129,27 +122,42 @@ public class UpdateManager {
} }
private boolean processUpdateMessage(UpdateApiRequest.UpdateApiMessage message) { private boolean processUpdateMessage(UpdateApiRequest.UpdateApiMessage message) {
if (message.code <= BuildConfig.VERSION_CODE) { if (isMessageRelevantForThisVersion(message)) {
Logger.d(TAG, "No newer version available (" + BuildConfig.VERSION_CODE + " >= " + message.code + ")."); if (message.type.equals(UpdateApiRequest.TYPE_UPDATE)) {
// Our code is newer than the message if (message.apkUrl == null) {
return false; 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)) { Logger.i(TAG, "Update available (" + message.code + ") with url \"" +
if (message.apkUrl == null) { message.apkUrl + "\".");
Logger.i(TAG, "Update available but none for this build flavor."); callback.showUpdateAvailableDialog(message);
// Not for this flavor, discard. return true;
return false;
} }
Logger.i(TAG, "Update available (" + message.code + ") with url \"" + message.apkUrl + "\".");
callback.showUpdateAvailableDialog(message);
return true;
} }
return false; 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. * 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) { 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 { try {
IOUtils.copyFile(cacheFile, out); IOUtils.copyFile(cacheFile, out);
} catch (IOException e) { } catch (IOException e) {
@ -198,7 +208,8 @@ public class UpdateManager {
// Then launch the APK install intent. // Then launch the APK install intent.
Intent intent = new Intent(Intent.ACTION_VIEW); Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 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, // 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. // but I don't feel like implementing a content provider just for this feature.

Loading…
Cancel
Save