Improved update check

pull/147/head
M66B 6 years ago
parent 4668a44b85
commit cd3bf886f2

@ -620,31 +620,47 @@ 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());
JSONObject jroot = new JSONObject(response.toString());
if (!jroot.has("tag_name"))
throw new IOException("tag_name field missing");
if (!jroot.has("html_url"))
throw new IOException("html_url field missing");
if (!jroot.has("assets"))
throw new IOException("assets section missing");
if (jroot.has("tag_name") &&
jroot.has("html_url") &&
jroot.has("assets")) {
// Get update info // Get update info
UpdateInfo info = new UpdateInfo(); UpdateInfo info = new UpdateInfo();
info.tag_name = jroot.getString("tag_name"); info.tag_name = jroot.getString("tag_name");
info.html_url = jroot.getString("html_url"); info.html_url = jroot.getString("html_url");
if (TextUtils.isEmpty(info.html_url))
return null;
// Check if new release // Check if new release
JSONArray jassets = jroot.getJSONArray("assets"); JSONArray jassets = jroot.getJSONArray("assets");
@ -653,20 +669,16 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
if (jasset.has("name")) { if (jasset.has("name")) {
String name = jasset.getString("name"); String name = jasset.getString("name");
if (name != null && name.endsWith(".apk")) { if (name != null && name.endsWith(".apk")) {
if (TextUtils.isEmpty(info.tag_name))
info.tag_name = name;
Log.i("Latest version=" + info.tag_name); Log.i("Latest version=" + info.tag_name);
if (BuildConfig.VERSION_NAME.equals(info.tag_name)) if (BuildConfig.VERSION_NAME.equals(info.tag_name))
break; return null;
else else
return info; 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() {

Loading…
Cancel
Save