Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4639)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/precache/PrecacheServiceLauncher.java

Issue 1272443002: Only set precache.last_time when precache ran. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@filter_stats
Patch Set: Don't retry often unless backend pending. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/precache/PrecacheServiceLauncher.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/precache/PrecacheServiceLauncher.java b/chrome/android/java/src/org/chromium/chrome/browser/precache/PrecacheServiceLauncher.java
index f87edd24be64cfdfa4a71958a2b16ce6bd767079..bf3ce07a99b11c8d2c477d5471cdc62f997588a3 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/precache/PrecacheServiceLauncher.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/precache/PrecacheServiceLauncher.java
@@ -58,11 +58,13 @@ public class PrecacheServiceLauncher extends BroadcastReceiver {
* PrecacheService will be stopped, and this receiver will do nothing when it receives an
* intent.
*
- * @param context The Context to use.
+ * @param context The application context.
* @param enabled Whether or not precaching is enabled.
*/
public static void setIsPrecachingEnabled(Context context, boolean enabled) {
Log.v(TAG, "setIsPrecachingEnabled(%s)", enabled);
+ // |context| needs to be the application context so that the following call gets a
Yaron 2015/08/11 13:59:28 It's also generally preferred in non-ui code to av
+ // reference to a consistent SharedPreferences object.
Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
editor.putBoolean(PREF_IS_PRECACHING_ENABLED, enabled);
editor.apply();
@@ -75,6 +77,35 @@ public class PrecacheServiceLauncher extends BroadcastReceiver {
}
}
+ /**
+ * Handler for when precaching has finished. If precaching had successfully started, sets an
+ * alarm for a subsequent run, and updates the last precache time. If not (i.e. the precache
+ * attempt aborted), sets an alarm for another attempt.
+ *
+ * @param context The application context.
+ * @param tryAgainSoon Whether precaching should be attempted again.
+ */
+ public static void precachingFinished(Context context, boolean tryAgainSoon) {
+ new PrecacheServiceLauncher().precachingFinishedInternal(context, tryAgainSoon);
+ }
+
+ private void precachingFinishedInternal(Context context, boolean tryAgainSoon) {
+ Log.v(TAG, "precachingFinished(%s)", tryAgainSoon);
+ if (tryAgainSoon) {
+ setAlarm(context,
+ Math.max(INTERACTIVE_STATE_POLLING_PERIOD_MS,
+ WAIT_UNTIL_NEXT_PRECACHE_MS - timeSinceLastPrecacheMs(context)));
+ } else {
+ // Store a pref indicating that precaching finished just now.
+ setAlarm(context,
+ Math.max(INTERACTIVE_STATE_POLLING_PERIOD_MS, WAIT_UNTIL_NEXT_PRECACHE_MS));
+
+ Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
+ editor.putLong(PREF_PRECACHE_LAST_TIME, getElapsedRealtimeOnSystem());
+ editor.apply();
+ }
+ }
+
@VisibleForTesting
static boolean isPrecachingEnabled(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
@@ -83,14 +114,6 @@ public class PrecacheServiceLauncher extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
- SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
- long lastPrecacheTimeMs = prefs.getLong(PREF_PRECACHE_LAST_TIME, 0L);
- if (lastPrecacheTimeMs > getElapsedRealtimeOnSystem()) {
- // System.elapsedRealtime() counts milliseconds since boot, so if the device has been
- // rebooted since the last time precaching was performed, reset lastPrecacheTimeMs to 0.
- lastPrecacheTimeMs = 0L;
- }
-
// Do nothing if precaching is disabled.
if (!isPrecachingEnabled(context)) return;
@@ -99,9 +122,8 @@ public class PrecacheServiceLauncher extends BroadcastReceiver {
boolean isInteractive = mDeviceState.isInteractive(context);
boolean areConditionsGoodForPrecaching =
isPowerConnected && isWifiAvailable && !isInteractive;
- long timeSinceLastPrecacheMs = getElapsedRealtimeOnSystem() - lastPrecacheTimeMs;
boolean hasEnoughTimePassedSinceLastPrecache =
- timeSinceLastPrecacheMs >= WAIT_UNTIL_NEXT_PRECACHE_MS;
+ timeSinceLastPrecacheMs(context) >= WAIT_UNTIL_NEXT_PRECACHE_MS;
// Only start precaching when an alarm action is received. This is to prevent situations
// such as power being connected, precaching starting, then precaching being immediately
@@ -109,14 +131,6 @@ public class PrecacheServiceLauncher extends BroadcastReceiver {
if (ACTION_ALARM.equals(intent.getAction())
&& areConditionsGoodForPrecaching
&& hasEnoughTimePassedSinceLastPrecache) {
- // Store a pref indicating that precaching is starting now.
- Editor editor = prefs.edit();
- editor.putLong(PREF_PRECACHE_LAST_TIME, getElapsedRealtimeOnSystem());
- editor.apply();
-
- setAlarm(context, Math.max(
- INTERACTIVE_STATE_POLLING_PERIOD_MS, WAIT_UNTIL_NEXT_PRECACHE_MS));
-
acquireWakeLockAndStartService(context);
} else {
if (isPowerConnected && isWifiAvailable) {
@@ -127,8 +141,9 @@ public class PrecacheServiceLauncher extends BroadcastReceiver {
// SCREEN_ON/OFF intents are only delivered to BroadcastReceivers that are
// registered dynamically in code, but the PrecacheServiceLauncher is registered in
// the Android manifest.
- setAlarm(context, Math.max(INTERACTIVE_STATE_POLLING_PERIOD_MS,
- WAIT_UNTIL_NEXT_PRECACHE_MS - timeSinceLastPrecacheMs));
+ setAlarm(context,
+ Math.max(INTERACTIVE_STATE_POLLING_PERIOD_MS,
+ WAIT_UNTIL_NEXT_PRECACHE_MS - timeSinceLastPrecacheMs(context)));
} else {
// If the device doesn't have connected power or doesn't have Wi-Fi, then there's no
// point in setting an alarm.
@@ -225,5 +240,17 @@ public class PrecacheServiceLauncher extends BroadcastReceiver {
protected long getElapsedRealtimeOnSystem() {
return SystemClock.elapsedRealtime();
}
+
+ /** Returns the number of milliseconds since the last precache run completed. */
+ private long timeSinceLastPrecacheMs(Context context) {
+ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
+ long lastPrecacheTimeMs = prefs.getLong(PREF_PRECACHE_LAST_TIME, 0L);
+ if (lastPrecacheTimeMs > getElapsedRealtimeOnSystem()) {
+ // System.elapsedRealtime() counts milliseconds since boot, so if the device has been
+ // rebooted since the last time precaching was performed, reset lastPrecacheTimeMs to 0.
+ lastPrecacheTimeMs = 0L;
+ }
+ return getElapsedRealtimeOnSystem() - lastPrecacheTimeMs;
+ }
}

Powered by Google App Engine
This is Rietveld 408576698