Browse Source

Only check for app (and search module) updates once a day. That should be enough, right?

pull/82/head
Eric Kok 11 years ago
parent
commit
b1881f0cf4
  1. 20
      core/src/org/transdroid/core/app/settings/SystemSettings.java
  2. 19
      core/src/org/transdroid/core/service/AppUpdateService.java

20
core/src/org/transdroid/core/app/settings/SystemSettings.java

@ -16,9 +16,12 @@ @@ -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;
@ -47,4 +50,21 @@ public class SystemSettings { @@ -47,4 +50,21 @@ public class SystemSettings {
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();
}
}

19
core/src/org/transdroid/core/service/AppUpdateService.java

@ -18,6 +18,8 @@ package org.transdroid.core.service; @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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());

Loading…
Cancel
Save