Improved update check

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

@ -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") &&
jroot.has("html_url") && JSONObject jroot = new JSONObject(response.toString());
jroot.has("assets")) {
// Get update info if (!jroot.has("tag_name"))
UpdateInfo info = new UpdateInfo(); throw new IOException("tag_name field missing");
info.tag_name = jroot.getString("tag_name"); if (!jroot.has("html_url"))
info.html_url = jroot.getString("html_url"); throw new IOException("html_url field missing");
if (TextUtils.isEmpty(info.html_url)) if (!jroot.has("assets"))
return null; throw new IOException("assets section missing");
// Check if new release // Get update info
JSONArray jassets = jroot.getJSONArray("assets"); UpdateInfo info = new UpdateInfo();
for (int i = 0; i < jassets.length(); i++) { info.tag_name = jroot.getString("tag_name");
JSONObject jasset = jassets.getJSONObject(i); info.html_url = jroot.getString("html_url");
if (jasset.has("name")) {
String name = jasset.getString("name"); // Check if new release
if (name != null && name.endsWith(".apk")) { JSONArray jassets = jroot.getJSONArray("assets");
if (TextUtils.isEmpty(info.tag_name)) for (int i = 0; i < jassets.length(); i++) {
info.tag_name = name; JSONObject jasset = jassets.getJSONObject(i);
if (jasset.has("name")) {
Log.i("Latest version=" + info.tag_name); String name = jasset.getString("name");
if (BuildConfig.VERSION_NAME.equals(info.tag_name)) if (name != null && name.endsWith(".apk")) {
break; Log.i("Latest version=" + info.tag_name);
else if (BuildConfig.VERSION_NAME.equals(info.tag_name))
return info; 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() {

Loading…
Cancel
Save