Check for updates each 12 hours

pull/125/head
M66B 6 years ago
parent e803e487ce
commit 96781e7e13

@ -25,6 +25,7 @@ import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
@ -97,6 +98,9 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
static final String ACTION_EDIT_ANSWER = BuildConfig.APPLICATION_ID + ".EDIT_ANSWER"; static final String ACTION_EDIT_ANSWER = BuildConfig.APPLICATION_ID + ".EDIT_ANSWER";
static final String ACTION_STORE_ATTACHMENT = BuildConfig.APPLICATION_ID + ".STORE_ATTACHMENT"; static final String ACTION_STORE_ATTACHMENT = BuildConfig.APPLICATION_ID + ".STORE_ATTACHMENT";
static final String UPDATE_LATEST_API = "https://api.github.com/repos/M66B/open-source-email/releases/latest";
static final long UPDATE_INTERVAL = 12 * 3600 * 1000L; // milliseconds
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -469,13 +473,18 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
} }
private void checkUpdate() { private void checkUpdate() {
final long now = new Date().getTime();
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (prefs.getLong("last_update_check", 0) + UPDATE_INTERVAL > now)
return;
new SimpleTask<UpdateInfo>() { new SimpleTask<UpdateInfo>() {
@Override @Override
protected UpdateInfo onLoad(Context context, Bundle args) throws Throwable { protected UpdateInfo onLoad(Context context, Bundle args) throws Throwable {
StringBuilder json = new StringBuilder(); StringBuilder json = new StringBuilder();
HttpsURLConnection urlConnection = null; HttpsURLConnection urlConnection = null;
try { try {
URL latest = new URL("https://api.github.com/repos/M66B/open-source-email/releases/latest"); URL latest = new URL(UPDATE_LATEST_API);
urlConnection = (HttpsURLConnection) latest.openConnection(); urlConnection = (HttpsURLConnection) latest.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
@ -487,6 +496,8 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
if (jroot.has("tag_name") && if (jroot.has("tag_name") &&
jroot.has("html_url") && jroot.has("html_url") &&
jroot.has("assets")) { jroot.has("assets")) {
prefs.edit().putLong("last_update_check", now).apply();
UpdateInfo info = new UpdateInfo(); UpdateInfo info = new UpdateInfo();
info.html_url = jroot.getString("html_url"); info.html_url = jroot.getString("html_url");
JSONArray jassets = jroot.getJSONArray("assets"); JSONArray jassets = jroot.getJSONArray("assets");
@ -496,7 +507,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
String name = jasset.getString("name"); String name = jasset.getString("name");
if (name != null && name.endsWith(".apk")) { if (name != null && name.endsWith(".apk")) {
info.tag_name = jroot.getString("tag_name"); info.tag_name = jroot.getString("tag_name");
Log.i(Helper.TAG, "Lastest version=" + info.tag_name); Log.i(Helper.TAG, "Latest version=" + info.tag_name);
if (BuildConfig.VERSION_NAME.equals(info.tag_name)) if (BuildConfig.VERSION_NAME.equals(info.tag_name))
break; break;
else else

Loading…
Cancel
Save