diff --git a/core/src/org/transdroid/core/app/settings/SystemSettings.java b/core/src/org/transdroid/core/app/settings/SystemSettings.java index 0ef58d0d..3264d375 100644 --- a/core/src/org/transdroid/core/app/settings/SystemSettings.java +++ b/core/src/org/transdroid/core/app/settings/SystemSettings.java @@ -16,9 +16,12 @@ */ package org.transdroid.core.app.settings; +import java.util.Date; + import org.androidannotations.annotations.EBean; import org.androidannotations.annotations.RootContext; import org.androidannotations.annotations.EBean.Scope; +import org.transdroid.core.service.AppUpdateService; import android.content.Context; import android.content.SharedPreferences; @@ -34,7 +37,7 @@ public class SystemSettings { @RootContext protected Context context; private SharedPreferences prefs; - + protected SystemSettings(Context context) { prefs = PreferenceManager.getDefaultSharedPreferences(context); } @@ -46,5 +49,22 @@ public class SystemSettings { public boolean useDarkTheme() { return prefs.getBoolean("system_usedarktheme", false); } - + + /** + * Returns the date when we last checked transdroid.org for the latest app version. + * @return The date/time when the {@link AppUpdateService} checked on the server for updates + */ + public Date getLastCheckedForAppUpdates() { + long lastChecked = prefs.getLong("system_lastappupdatecheck", -1L); + return lastChecked == -1 ? null : new Date(lastChecked); + } + + /** + * Stores the date at which was last successfully, fully checked for new updates to the app. + * @param lastChecked The date/time at which the {@link AppUpdateService} last checked the server for updates + */ + public void setLastCheckedForAppUpdates(Date lastChecked) { + prefs.edit().putLong("system_lastappupdatecheck", lastChecked == null ? -1L : lastChecked.getTime()).commit(); + } + } diff --git a/core/src/org/transdroid/core/service/AppUpdateService.java b/core/src/org/transdroid/core/service/AppUpdateService.java index 4296e450..7cdf6975 100644 --- a/core/src/org/transdroid/core/service/AppUpdateService.java +++ b/core/src/org/transdroid/core/service/AppUpdateService.java @@ -18,6 +18,8 @@ package org.transdroid.core.service; import java.io.IOException; import java.io.InputStream; +import java.util.Calendar; +import java.util.Date; import java.util.Random; import org.androidannotations.annotations.Bean; @@ -74,6 +76,15 @@ public class AppUpdateService extends IntentService { return; } + Date lastChecked = systemSettings.getLastCheckedForAppUpdates(); + Calendar lastDay = Calendar.getInstance(); + lastDay.add(Calendar.DAY_OF_MONTH, -1); + if (lastChecked == null || lastChecked.after(lastDay.getTime())) { + Log.d(this, "Ship the update service, as we already checked the last 24 hours (or to be exact at " + + lastChecked.toString() + ")."); + return; + } + DefaultHttpClient httpclient = new DefaultHttpClient(); Random random = new Random(); @@ -88,6 +99,8 @@ public class AppUpdateService extends IntentService { // New version of the app? try { PackageInfo appPackage = getPackageManager().getPackageInfo(getPackageName(), 0); + Log.d(this, "Local Transdroid is at " + appPackage.versionCode + " and the reported latest version is " + + appVersion); if (appPackage.versionCode < appVersion) { // New version available! Notify the user. newNotification(getString(R.string.update_app_newversion), @@ -102,6 +115,8 @@ public class AppUpdateService extends IntentService { // New version of the search module? try { PackageInfo searchPackage = getPackageManager().getPackageInfo("org.transdroid.search", 0); + Log.d(this, "Local Transdroid Seach is at " + searchPackage.versionCode + + " and the reported latest version is " + searchVersion); if (searchPackage.versionCode < searchVersion) { // New version available! Notify the user. newNotification(getString(R.string.update_search_newversion), @@ -114,6 +129,10 @@ public class AppUpdateService extends IntentService { // install it (when the first search is initiated) } + // Save that we successfully checked for app updates (and notified the user) + // This prevents checking again for 1 day + systemSettings.setLastCheckedForAppUpdates(new Date()); + } catch (Exception e) { // Cannot check right now for some reason; log and ignore Log.d(this, "Cannot retrieve latest app or search module version code from the site: " + e.toString());