Improved update check

This commit is contained in:
M66B 2019-01-11 06:25:06 +00:00
parent 4668a44b85
commit cd3bf886f2
1 changed files with 47 additions and 32 deletions

View File

@ -620,53 +620,65 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
return; return;
prefs.edit().putLong("last_update_check", now).apply(); prefs.edit().putLong("last_update_check", now).apply();
Bundle args = new Bundle();
args.putBoolean("always", always);
new SimpleTask<UpdateInfo>() { new SimpleTask<UpdateInfo>() {
@Override @Override
protected UpdateInfo onExecute(Context context, Bundle args) throws Throwable { protected UpdateInfo onExecute(Context context, Bundle args) throws Throwable {
StringBuilder json = new StringBuilder(); StringBuilder response = new StringBuilder();
HttpsURLConnection urlConnection = null; HttpsURLConnection urlConnection = null;
try { try {
URL latest = new URL(BuildConfig.GITHUB_LATEST_API); URL latest = new URL(BuildConfig.GITHUB_LATEST_API);
urlConnection = (HttpsURLConnection) latest.openConnection(); urlConnection = (HttpsURLConnection) latest.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(false);
urlConnection.connect();
int status = urlConnection.getResponseCode();
InputStream inputStream = (status == HttpsURLConnection.HTTP_OK
? urlConnection.getInputStream() : urlConnection.getErrorStream());
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String line; String line;
while ((line = br.readLine()) != null) while ((line = br.readLine()) != null)
json.append(line); response.append(line);
JSONObject jroot = new JSONObject(json.toString()); if (status != HttpsURLConnection.HTTP_OK)
throw new IOException("HTTP " + status + ": " + response.toString());
if (jroot.has("tag_name") && JSONObject jroot = new JSONObject(response.toString());
jroot.has("html_url") &&
jroot.has("assets")) {
// Get update info
UpdateInfo info = new UpdateInfo();
info.tag_name = jroot.getString("tag_name");
info.html_url = jroot.getString("html_url");
if (TextUtils.isEmpty(info.html_url))
return null;
// Check if new release if (!jroot.has("tag_name"))
JSONArray jassets = jroot.getJSONArray("assets"); throw new IOException("tag_name field missing");
for (int i = 0; i < jassets.length(); i++) { if (!jroot.has("html_url"))
JSONObject jasset = jassets.getJSONObject(i); throw new IOException("html_url field missing");
if (jasset.has("name")) { if (!jroot.has("assets"))
String name = jasset.getString("name"); throw new IOException("assets section missing");
if (name != null && name.endsWith(".apk")) {
if (TextUtils.isEmpty(info.tag_name))
info.tag_name = name;
Log.i("Latest version=" + info.tag_name); // Get update info
if (BuildConfig.VERSION_NAME.equals(info.tag_name)) UpdateInfo info = new UpdateInfo();
break; info.tag_name = jroot.getString("tag_name");
else info.html_url = jroot.getString("html_url");
return info;
} // Check if new release
JSONArray jassets = jroot.getJSONArray("assets");
for (int i = 0; i < jassets.length(); i++) {
JSONObject jasset = jassets.getJSONObject(i);
if (jasset.has("name")) {
String name = jasset.getString("name");
if (name != null && name.endsWith(".apk")) {
Log.i("Latest version=" + info.tag_name);
if (BuildConfig.VERSION_NAME.equals(info.tag_name))
return null;
else
return info;
} }
} }
} }
return null; throw new IOException("Asset name field missing");
} finally { } finally {
if (urlConnection != null) if (urlConnection != null)
urlConnection.disconnect(); urlConnection.disconnect();
@ -675,8 +687,11 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
@Override @Override
protected void onExecuted(Bundle args, UpdateInfo info) { protected void onExecuted(Bundle args, UpdateInfo info) {
if (info == null) if (info == null) {
if (args.getBoolean("always"))
Toast.makeText(ActivityView.this, BuildConfig.VERSION_NAME, Toast.LENGTH_LONG).show();
return; return;
}
final Intent update = new Intent(Intent.ACTION_VIEW, Uri.parse(info.html_url)); final Intent update = new Intent(Intent.ACTION_VIEW, Uri.parse(info.html_url));
if (update.resolveActivity(getPackageManager()) != null) if (update.resolveActivity(getPackageManager()) != null)
@ -693,10 +708,10 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
@Override @Override
protected void onException(Bundle args, Throwable ex) { protected void onException(Bundle args, Throwable ex) {
if (BuildConfig.DEBUG) if (args.getBoolean("always"))
Helper.unexpectedError(ActivityView.this, ActivityView.this, ex); Helper.unexpectedError(ActivityView.this, ActivityView.this, ex);
} }
}.execute(this, new Bundle(), "update:check"); }.execute(this, args, "update:check");
} }
private void updateShortcuts() { private void updateShortcuts() {