From 055f16368755031280e4a626059eef2e70570ef9 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 8 Sep 2018 09:41:05 +0000 Subject: [PATCH] Check for updates --- .../java/eu/faircode/email/ActivityView.java | 82 +++++++++++++++++++ app/src/main/res/values/strings.xml | 1 + 2 files changed, 83 insertions(+) diff --git a/app/src/main/java/eu/faircode/email/ActivityView.java b/app/src/main/java/eu/faircode/email/ActivityView.java index c02922867c..238721a0d8 100644 --- a/app/src/main/java/eu/faircode/email/ActivityView.java +++ b/app/src/main/java/eu/faircode/email/ActivityView.java @@ -43,11 +43,16 @@ import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; +import org.json.JSONArray; +import org.json.JSONObject; + import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; +import java.io.InputStreamReader; +import java.net.URL; import java.text.Collator; import java.util.ArrayList; import java.util.Collections; @@ -57,6 +62,7 @@ import java.util.List; import java.util.Locale; import javax.mail.Address; +import javax.net.ssl.HttpsURLConnection; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -325,6 +331,9 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB }.load(this, new Bundle()); checkIntent(getIntent()); + + if (!Helper.isPlayStoreInstall(this)) + checkUpdate(); } @Override @@ -454,6 +463,79 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB } } + private class UpdateInfo { + public String tag_name; // version + public String html_url; + } + + private void checkUpdate() { + new SimpleTask() { + @Override + protected UpdateInfo onLoad(Context context, Bundle args) throws Throwable { + StringBuilder json = new StringBuilder(); + HttpsURLConnection urlConnection = null; + try { + URL latest = new URL("https://api.github.com/repos/M66B/open-source-email/releases/latest"); + urlConnection = (HttpsURLConnection) latest.openConnection(); + BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); + + String line; + while ((line = br.readLine()) != null) + json.append(line); + + JSONObject jroot = new JSONObject(json.toString()); + if (jroot.has("tag_name") && + jroot.has("html_url") && + jroot.has("assets")) { + UpdateInfo info = new UpdateInfo(); + info.html_url = jroot.getString("html_url"); + 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")) { + info.tag_name = jroot.getString("tag_name"); + Log.i(Helper.TAG, "Lastest version=" + info.tag_name); + if (BuildConfig.VERSION_NAME.equals(info.tag_name)) + break; + else + return info; + } + } + } + } + } finally { + if (urlConnection != null) + urlConnection.disconnect(); + } + + return null; + } + + @Override + protected void onLoaded(Bundle args, UpdateInfo info) { + final Intent update = new Intent(Intent.ACTION_VIEW, Uri.parse(info.html_url)); + if (update.resolveActivity(getPackageManager()) != null) + new AlertDialog.Builder(ActivityView.this) + .setMessage(getString(R.string.title_updated, info.tag_name)) + .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + startActivity(update); + } + }) + .show(); + } + + @Override + protected void onException(Bundle args, Throwable ex) { + if (BuildConfig.DEBUG) + Toast.makeText(ActivityView.this, ex.toString(), Toast.LENGTH_LONG).show(); + } + }.load(this, new Bundle()); + } + private Intent getIntentPrivacy() { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("https://email.faircode.eu/privacy/")); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2f9a90cd74..50a970e9b0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -215,6 +215,7 @@ Swipe left to trash; swipe right to archive (if available); long press to mark read/unread Understood + There is an update to version %1$s available Do you have a question or problem? Yes No