Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.sync; |
michael@0 | 6 | |
michael@0 | 7 | import android.content.SharedPreferences; |
michael@0 | 8 | import android.content.SharedPreferences.Editor; |
michael@0 | 9 | |
michael@0 | 10 | public class PrefsBackoffHandler implements BackoffHandler { |
michael@0 | 11 | public static final String PREF_EARLIEST_NEXT = "earliestnext"; |
michael@0 | 12 | |
michael@0 | 13 | private final SharedPreferences prefs; |
michael@0 | 14 | private final String prefEarliest; |
michael@0 | 15 | |
michael@0 | 16 | public PrefsBackoffHandler(final SharedPreferences prefs, final String prefSuffix) { |
michael@0 | 17 | if (prefs == null) { |
michael@0 | 18 | throw new IllegalArgumentException("prefs must not be null."); |
michael@0 | 19 | } |
michael@0 | 20 | this.prefs = prefs; |
michael@0 | 21 | this.prefEarliest = PREF_EARLIEST_NEXT + "." + prefSuffix; |
michael@0 | 22 | } |
michael@0 | 23 | |
michael@0 | 24 | @Override |
michael@0 | 25 | public synchronized long getEarliestNextRequest() { |
michael@0 | 26 | return prefs.getLong(prefEarliest, 0); |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | @Override |
michael@0 | 30 | public synchronized void setEarliestNextRequest(final long next) { |
michael@0 | 31 | final Editor edit = prefs.edit(); |
michael@0 | 32 | edit.putLong(prefEarliest, next); |
michael@0 | 33 | edit.commit(); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | @Override |
michael@0 | 37 | public synchronized void extendEarliestNextRequest(final long next) { |
michael@0 | 38 | if (prefs.getLong(prefEarliest, 0) >= next) { |
michael@0 | 39 | return; |
michael@0 | 40 | } |
michael@0 | 41 | final Editor edit = prefs.edit(); |
michael@0 | 42 | edit.putLong(prefEarliest, next); |
michael@0 | 43 | edit.commit(); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | /** |
michael@0 | 47 | * Return the number of milliseconds until we're allowed to touch the server again, |
michael@0 | 48 | * or 0 if now is fine. |
michael@0 | 49 | */ |
michael@0 | 50 | @Override |
michael@0 | 51 | public long delayMilliseconds() { |
michael@0 | 52 | long earliestNextRequest = getEarliestNextRequest(); |
michael@0 | 53 | if (earliestNextRequest <= 0) { |
michael@0 | 54 | return 0; |
michael@0 | 55 | } |
michael@0 | 56 | long now = System.currentTimeMillis(); |
michael@0 | 57 | return Math.max(0, earliestNextRequest - now); |
michael@0 | 58 | } |
michael@0 | 59 | } |