public class OperationScheduler extends Object
This class does not directly perform or invoke any operations,
it only keeps track of the schedule. Somebody else needs to call
getNextTimeMillis(com.android.common.OperationScheduler.Options)
as appropriate and do the actual work.
Modifier and Type | Class and Description |
---|---|
static class |
OperationScheduler.Options
Tunable parameter options for
getNextTimeMillis(com.android.common.OperationScheduler.Options) . |
Constructor and Description |
---|
OperationScheduler(SharedPreferences storage)
Initialize the scheduler state.
|
Modifier and Type | Method and Description |
---|---|
protected long |
currentTimeMillis()
Gets the current time.
|
long |
getLastAttemptTimeMillis()
Return the last time the operation was attempted.
|
long |
getLastSuccessTimeMillis()
Return the last time the operation completed.
|
long |
getNextTimeMillis(OperationScheduler.Options options)
Compute the time of the next operation.
|
void |
onPermanentError()
Report a permanent error that will not go away until further notice.
|
void |
onSuccess()
Report successful completion of an operation.
|
void |
onTransientError()
Report a transient error (usually a network failure).
|
static OperationScheduler.Options |
parseOptions(String spec,
OperationScheduler.Options options)
Parse scheduler options supplied in this string form:
backoff=(fixed)+(incremental)[+(exponential)] max=(maxmoratorium) min=(mintrigger) [period=](interval)
All values are times in (possibly fractional) seconds (not milliseconds).
|
void |
resetPermanentError()
Reset any permanent error status set by
onPermanentError() ,
allowing operations to be scheduled as normal. |
void |
resetTransientError()
Reset all transient error counts, allowing the next operation to proceed
immediately without backoff.
|
void |
setEnabledState(boolean enabled)
Enable or disable all operations.
|
boolean |
setMoratoriumTimeHttp(String retryAfter)
Forbid any operations until after a certain time, as specified in
the format used by the HTTP "Retry-After" header.
|
void |
setMoratoriumTimeMillis(long millis)
Forbid any operations until after a certain (absolute) time.
|
void |
setTriggerTimeMillis(long millis)
Request an operation to be performed at a certain time.
|
String |
toString()
Return a string description of the scheduler state for debugging.
|
public OperationScheduler(SharedPreferences storage)
storage
- to use for recording the state of operations across restarts/rebootspublic static OperationScheduler.Options parseOptions(String spec, OperationScheduler.Options options) throws IllegalArgumentException
backoff=(fixed)+(incremental)[+(exponential)] max=(maxmoratorium) min=(mintrigger) [period=](interval)All values are times in (possibly fractional) seconds (not milliseconds). Omitted settings are left at whatever existing default value was passed in.
The default options: backoff=0+5 max=86400 min=0 period=0
Fractions are OK: backoff=+2.5 period=10.0
The "period=" can be omitted: 3600
spec
- describing some or all scheduler options.options
- to update with parsed values.IllegalArgumentException
- if the syntax is invalidpublic long getNextTimeMillis(OperationScheduler.Options options)
options
- to use for this computation.System.currentTimeMillis()
) when the
next operation should be attempted -- immediately, if the return value is
before the current time.public long getLastSuccessTimeMillis()
onSuccess()
was last called.public long getLastAttemptTimeMillis()
onSuccess()
or onTransientError()
was last called.public void setTriggerTimeMillis(long millis)
Long.MAX_VALUE
to disable triggering.millis
- wall clock time (System.currentTimeMillis()
) to
trigger another operation; 0 to trigger immediatelypublic void setMoratoriumTimeMillis(long millis)
OperationScheduler.Options.maxMoratoriumMillis
.millis
- wall clock time (System.currentTimeMillis()
)
when operations should be allowed again; 0 to remove moratoriumpublic boolean setMoratoriumTimeHttp(String retryAfter)
OperationScheduler.Options.maxMoratoriumMillis
.retryAfter
- moratorium time in HTTP formatpublic void setEnabledState(boolean enabled)
getNextTimeMillis(com.android.common.OperationScheduler.Options)
return Long.MAX_VALUE
.
Commonly used when data network availability goes up and down.enabled
- if operations can be performedpublic void onSuccess()
public void onTransientError()
public void resetTransientError()
public void onPermanentError()
resetPermanentError()
is called. Commonly used for authentication failures (which are reset
when the accounts database is updated).public void resetPermanentError()
onPermanentError()
,
allowing operations to be scheduled as normal.public String toString()
protected long currentTimeMillis()
System.currentTimeMillis()