contextWeakReference;
+
+ private StillOnWifiAsyncTask(Context context) {
+ this.contextWeakReference = new WeakReference<>(context);
+ }
+
+ @Override
+ protected Void doInBackground(Void... voids) {
+ Context context = contextWeakReference.get();
+ try {
+ Thread.sleep(120000);
+ if (Preferences.get().isBackgroundDownloadAllowed()) {
+ Utils.debugLog(TAG, "scheduling update because there is good internet");
+ schedule(context);
+ }
+ } catch (Exception e) {
+ Utils.debugLog(TAG, e.getMessage());
+ }
+ isScheduleIfStillOnWifiRunning = false;
+ return null;
+ }
+ }
+
@Override
public void onCreate() {
super.onCreate();
@@ -279,35 +338,6 @@ public class UpdateService extends IntentService {
}
};
- /**
- * Check whether it is time to run the scheduled update.
- * We don't want to run if:
- * - The time between scheduled runs is set to zero (though don't know
- * when that would occur)
- * - Last update was too recent
- * - Not on wifi, but the property for "Only auto update on wifi" is set.
- *
- * @return True if we are due for a scheduled update.
- */
- private boolean verifyIsTimeForScheduledRun() {
- SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
- String sint = prefs.getString(Preferences.PREF_UPD_INTERVAL, "0");
- int interval = Integer.parseInt(sint);
- if (interval == 0) {
- Log.i(TAG, "Skipping update - disabled");
- return false;
- }
- long lastUpdate = prefs.getLong(STATE_LAST_UPDATED, 0);
- long elapsed = System.currentTimeMillis() - lastUpdate;
- if (elapsed < interval * 60 * 60 * 1000) {
- Log.i(TAG, "Skipping update - done " + elapsed
- + "ms ago, interval is " + interval + " hours");
- return false;
- }
-
- return true;
- }
-
/**
* In order to send a {@link Toast} from a {@link IntentService}, we have to do these tricks.
*/
@@ -339,6 +369,7 @@ public class UpdateService extends IntentService {
}
try {
+ final Preferences fdroidPrefs = Preferences.get();
// See if it's time to actually do anything yet...
int netState = ConnectivityMonitorService.getNetworkState(this);
if (address != null && address.startsWith(BluetoothDownloader.SCHEME)) {
@@ -349,12 +380,9 @@ public class UpdateService extends IntentService {
sendNoInternetToast();
}
return;
- }
-
- final Preferences fdroidPrefs = Preferences.get();
- if (manualUpdate || forcedUpdate) {
+ } else if (manualUpdate || forcedUpdate) {
Utils.debugLog(TAG, "manually requested or forced update");
- } else if (!verifyIsTimeForScheduledRun() || !fdroidPrefs.isBackgroundDownloadAllowed()) {
+ } else if (!fdroidPrefs.isBackgroundDownloadAllowed()) {
Utils.debugLog(TAG, "don't run update");
return;
}
diff --git a/app/src/main/java/org/fdroid/fdroid/net/WifiStateChangeService.java b/app/src/main/java/org/fdroid/fdroid/net/WifiStateChangeService.java
index c4280c3cc..cdc99524f 100644
--- a/app/src/main/java/org/fdroid/fdroid/net/WifiStateChangeService.java
+++ b/app/src/main/java/org/fdroid/fdroid/net/WifiStateChangeService.java
@@ -8,6 +8,7 @@ import android.net.DhcpInfo;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
+import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
@@ -15,6 +16,7 @@ import android.util.Log;
import org.apache.commons.net.util.SubnetUtils;
import org.fdroid.fdroid.FDroidApp;
import org.fdroid.fdroid.Preferences;
+import org.fdroid.fdroid.UpdateService;
import org.fdroid.fdroid.Utils;
import org.fdroid.fdroid.data.Repo;
import org.fdroid.fdroid.localrepo.LocalRepoKeyStore;
@@ -42,6 +44,11 @@ import java.util.Locale;
* changed. Having the {@code Thread} also makes it easy to kill work
* that is in progress.
*
+ * This also schedules an update to encourage updates happening on
+ * unmetered networks like typical WiFi rather than networks that can
+ * cost money or have caps. The logic for checking the state of the
+ * internet connection is in {@link org.fdroid.fdroid.UpdateService#onHandleIntent(Intent)}
+ *
* Some devices send multiple copies of given events, like a Moto G often
* sends three {@code CONNECTED} events. So they have to be debounced to
* keep the {@link #BROADCAST} useful.
@@ -92,6 +99,10 @@ public class WifiStateChangeService extends IntentService {
wifiInfoThread = new WifiInfoThread();
wifiInfoThread.start();
}
+
+ if (Build.VERSION.SDK_INT < 21 && wifiState == WifiManager.WIFI_STATE_ENABLED) {
+ UpdateService.scheduleIfStillOnWifi(this);
+ }
}
}