Code example for SharedPreferences

Methods: editgetBooleangetIntgetLong

0
     * @return the wall clock time ({@link System#currentTimeMillis()}) when the 
     * next operation should be attempted -- immediately, if the return value is 
     * before the current time. 
     */ 
    public long getNextTimeMillis(Options options) {
        boolean enabledState = mStorage.getBoolean(PREFIX + "enabledState", true);
        if (!enabledState) return Long.MAX_VALUE;
 
        boolean permanentError = mStorage.getBoolean(PREFIX + "permanentError", false);
        if (permanentError) return Long.MAX_VALUE;
 
        // We do quite a bit of limiting to prevent a clock rollback from totally 
        // hosing the scheduler.  Times which are supposed to be in the past are 
        // clipped to the current time so we don't languish forever. 
 
        int errorCount = mStorage.getInt(PREFIX + "errorCount", 0);
        long now = currentTimeMillis();
        long lastSuccessTimeMillis = getTimeBefore(PREFIX + "lastSuccessTimeMillis", now);
        long lastErrorTimeMillis = getTimeBefore(PREFIX + "lastErrorTimeMillis", now);
        long triggerTimeMillis = mStorage.getLong(PREFIX + "triggerTimeMillis", Long.MAX_VALUE);
        long moratoriumSetMillis = getTimeBefore(PREFIX + "moratoriumSetTimeMillis", now);
        long moratoriumTimeMillis = getTimeBefore(PREFIX + "moratoriumTimeMillis",
                moratoriumSetMillis + options.maxMoratoriumMillis);
 
        long time = triggerTimeMillis;
        if (options.periodicIntervalMillis > 0) {
            time = Math.min(time, lastSuccessTimeMillis + options.periodicIntervalMillis);
        } 
 
        time = Math.max(time, moratoriumTimeMillis);
        time = Math.max(time, lastSuccessTimeMillis + options.minTriggerMillis);
        if (errorCount > 0) {
            time = Math.max(time, lastErrorTimeMillis + options.backoffFixedMillis +
                    options.backoffIncrementalMillis * errorCount);
        } 
        return time;
    } 
 
    /** 
     * Return the last time the operation completed.  Does not modify any state. 
     * 
     * @return the wall clock time when {@link #onSuccess()} was last called. 
     */ 
    public long getLastSuccessTimeMillis() { 
        return mStorage.getLong(PREFIX + "lastSuccessTimeMillis", 0);
    } 
 
    /** 
     * Return the last time the operation was attempted.  Does not modify any state. 
     * 
     * @return the wall clock time when {@link #onSuccess()} or {@link 
     * #onTransientError()} was last called. 
     */ 
    public long getLastAttemptTimeMillis() { 
        return Math.max(
                mStorage.getLong(PREFIX + "lastSuccessTimeMillis", 0),
                mStorage.getLong(PREFIX + "lastErrorTimeMillis", 0));
    } 
 
    /** 
     * Fetch a {@link SharedPreferences} property, but force it to be before 
     * a certain time, updating the value if necessary.  This is to recover 
     * gracefully from clock rollbacks which could otherwise strand our timers. 
     * 
     * @param name of SharedPreferences key 
     * @param max time to allow in result 
     * @return current value attached to key (default 0), limited by max 
     */ 
    private long getTimeBefore(String name, long max) {
        long time = mStorage.getLong(name, 0);
        if (time > max) mStorage.edit().putLong(name, (time = max)).commit();
        return time;
    } 
 
    /** 
     * Request an operation to be performed at a certain time.  The actual