|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.octo.android.robospice.SpiceManager
public class SpiceManager
The instances of this class allow to acces the SpiceService
.
They are tied to activities and obtain a local binding to the
SpiceService
. When binding occurs, the SpiceManager
will send
commadnds to the SpiceService
, to execute requests, clear cache,
prevent listeners from being called and so on. Basically, all features of the
SpiceService
are accessible from the SpiceManager
. It acts as
an asynchronous proxy : every call to a SpiceService
method is
asynchronous and will occur as soon as possible when the SpiceManager
successfully binds to the service.
Nested Class Summary | |
---|---|
static class |
SpiceManager.SpiceManagerCommand<T>
|
class |
SpiceManager.SpiceServiceConnection
Reacts to binding/unbinding with SpiceService . |
Field Summary | |
---|---|
protected BlockingQueue<CachedSpiceRequest<?>> |
requestQueue
The queue of requests to be sent to the service. |
protected Thread |
runner
Thread running runnable code. |
protected static String |
SPICE_MANAGER_THREAD_NAME_PREFIX
The prefix of SpiceManager threads (used to send requests to the service). |
Constructor Summary | |
---|---|
SpiceManager(Class<? extends SpiceService> spiceServiceClass)
Creates a SpiceManager . |
Method Summary | ||
---|---|---|
|
addListenerIfPending(Class<T> clazz,
Object requestCacheKey,
PendingRequestListener<T> requestListener)
Add listener to a pending request if it exists. |
|
|
addListenerIfPending(Class<T> clazz,
Object requestCacheKey,
RequestListener<T> requestListener)
Deprecated. |
|
void |
addSpiceServiceListener(SpiceServiceListener spiceServiceListener)
|
|
|
cancel(Class<T> clazz,
Object requestCacheKey)
Cancel a pending request if it exists. |
|
void |
cancel(SpiceRequest<?> request)
Cancel a specific request. |
|
void |
cancelAllRequests()
Cancel all requests. |
|
void |
dontNotifyAnyRequestListeners()
Disable request listeners notifications for all requests. |
|
protected void |
dontNotifyAnyRequestListenersInternal()
Remove all listeners of requests. |
|
void |
dontNotifyRequestListenersForRequest(SpiceRequest<?> request)
Disable request listeners notifications for a specific request. |
|
protected void |
dontNotifyRequestListenersForRequestInternal(SpiceRequest<?> request)
Internal method to remove requests. |
|
void |
dumpState()
Dumps request processor state. |
|
|
execute(CachedSpiceRequest<T> cachedSpiceRequest,
RequestListener<T> requestListener)
Execute a request, put the result in cache and register listeners to notify when request is finished. |
|
|
execute(SpiceRequest<T> request,
Object requestCacheKey,
long cacheExpiryDuration,
RequestListener<T> requestListener)
Execute a request. |
|
|
execute(SpiceRequest<T> request,
RequestListener<T> requestListener)
Execute a request, without using cache. |
|
protected
|
executeCommand(SpiceManager.SpiceManagerCommand<T> spiceManagerCommand)
|
|
Future<List<Object>> |
getAllCacheKeys(Class<?> clazz)
|
|
|
getAllDataFromCache(Class<T> clazz)
|
|
|
getDataFromCache(Class<T> clazz,
Object cacheKey)
Get some data previously saved in cache with key requestCacheKey. |
|
Future<Date> |
getDateOfDataInCache(Class<?> clazz,
Object cacheKey)
Returns the last date of storage of a given data into the cache. |
|
|
getFromCache(Class<T> clazz,
Object requestCacheKey,
long cacheExpiryDuration,
RequestListener<T> requestListener)
Get some data previously saved in cache with key requestCacheKey with maximum time in cache : cacheDuration millisecond and register listeners to notify when request is finished. |
|
|
getFromCacheAndLoadFromNetworkIfExpired(SpiceRequest<T> request,
Object requestCacheKey,
long cacheExpiryDuration,
RequestListener<T> requestListener)
Gets data from cache, expired or not, and executes a request normaly. |
|
int |
getPendingRequestCount()
|
|
int |
getRequestToLaunchCount()
|
|
protected int |
getThreadCount()
Number of threads used internally by this spice manager to communicate commands to the SpiceService it is bound to |
|
protected boolean |
isBound()
For testing purpose. |
|
Future<Boolean> |
isDataInCache(Class<?> clazz,
Object cacheKey,
long cacheExpiryDuration)
Tests whether some data is present in cache or not. |
|
boolean |
isStarted()
Method is synchronized with start(Context) . |
|
|
putDataInCache(Object cacheKey,
T data)
Put some new data in cache using cache key requestCacheKey. |
|
|
putInCache(Class<? super T> clazz,
Object requestCacheKey,
T data)
Adds some data to the cache, asynchronously. |
|
|
putInCache(Class<U> clazz,
Object requestCacheKey,
T data,
RequestListener<U> listener)
Adds some data to the cache, asynchronously. |
|
|
putInCache(Object requestCacheKey,
T data)
Adds some data to the cache, asynchronously. |
|
|
putInCache(Object requestCacheKey,
T data,
RequestListener<T> listener)
Adds some data to the cache, asynchronously. |
|
Future<?> |
removeAllDataFromCache()
Remove all data from cache. |
|
|
removeDataFromCache(Class<T> clazz)
Remove some specific content from cache |
|
|
removeDataFromCache(Class<T> clazz,
Object cacheKey)
Remove some specific content from cache |
|
void |
removeSpiceServiceListener(SpiceServiceListener spiceServiceListener)
|
|
void |
run()
|
|
void |
setFailOnCacheError(boolean failOnCacheError)
Configure the behavior in case of error during reading/writing cache. |
|
void |
shouldStop()
Stops the SpiceManager . |
|
void |
shouldStopAndJoin(long timeOut)
This is mostly a testing method. |
|
void |
start(android.content.Context context)
Start the SpiceManager . |
|
protected void |
waitForServiceToBeBound()
Wait for acquiring binding to SpiceService . |
|
protected void |
waitForServiceToBeUnbound()
Wait for acquiring binding to SpiceService . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected static final String SPICE_MANAGER_THREAD_NAME_PREFIX
protected final BlockingQueue<CachedSpiceRequest<?>> requestQueue
protected Thread runner
Constructor Detail |
---|
public SpiceManager(Class<? extends SpiceService> spiceServiceClass)
SpiceManager
. Typically this occurs in the construction
of an Activity or Fragment. This method will check if the service to bind
to has been properly declared in AndroidManifest.
spiceServiceClass
- the service class to bind to.Method Detail |
---|
protected int getThreadCount()
DEFAULT_THREAD_COUNT
.public void start(android.content.Context context)
SpiceManager
. It will bind asynchronously to the
SpiceService
.
context
- a context that will be used to bind to the service. Typically,
the Activity or Fragment that needs to interact with the
SpiceService
.public boolean isStarted()
start(Context)
.
SpiceManager
is started.public int getRequestToLaunchCount()
public int getPendingRequestCount()
public void run()
run
in interface Runnable
public void shouldStop()
SpiceManager
. It will unbind from SpiceService
.
All request listeners that had been registered to listen to
SpiceRequest
s sent from this SpiceManager
will be
unregistered. None of them will be notified with the results of their
SpiceRequest
s. Unbinding will occur asynchronously.
public void shouldStopAndJoin(long timeOut) throws InterruptedException
SpiceManager
. It will
unbind from SpiceService
. All request listeners that had been
registered to listen to SpiceRequest
s sent from this
SpiceManager
will be unregistered. None of them will be notified
with the results of their SpiceRequest
s. Unbinding will occur
syncrhonously : the method returns when all events have been unregistered
and when main processing thread stops.
InterruptedException
public <T> void getFromCache(Class<T> clazz, Object requestCacheKey, long cacheExpiryDuration, RequestListener<T> requestListener)
clazz
- the class of the result to retrieve from cache.requestCacheKey
- the key used to store and retrieve the result of the request
in the cachecacheExpiryDuration
- duration in milliseconds after which the content of the cache
will be considered to be expired.
DurationInMillis.ALWAYS_RETURNED
means data in cache
is always returned if it exists.
DurationInMillis.ALWAYS_EXPIRED
means data in cache is
never returned.(see DurationInMillis
)requestListener
- the listener to notify when the request will finish. If
nothing is found in cache, listeners will receive a null
result on their
RequestListener.onRequestSuccess(Object)
method. If
something is found in cache, they will receive it in this
method. If an error occurs, they will be notified via their
RequestListener.onRequestFailure(com.octo.android.robospice.persistence.exception.SpiceException)
method.@Deprecated public <T> void addListenerIfPending(Class<T> clazz, Object requestCacheKey, RequestListener<T> requestListener)
public <T> void addListenerIfPending(Class<T> clazz, Object requestCacheKey, PendingRequestListener<T> requestListener)
clazz
- the class of the result of the pending request to look for.requestCacheKey
- the key used to store and retrieve the result of the request
in the cacherequestListener
- the listener to notify when the request will finish.public <T> void execute(SpiceRequest<T> request, RequestListener<T> requestListener)
SpiceRequest.loadDataFromNetwork()
will
always be invoked. The result will not be stored in cache.
request
- the request to execute.requestListener
- the listener to notify when the request will finish.public <T> void execute(SpiceRequest<T> request, Object requestCacheKey, long cacheExpiryDuration, RequestListener<T> requestListener)
SpiceRequest.loadDataFromNetwork()
, the cache will be checked :
if a result has been cached with the cache key requestCacheKey,
RoboSpice will consider the parameter cacheExpiryDuration to
determine whether the result in the cache is expired or not. If it is not
expired, then listeners will receive the data in cache. Otherwise, the
method SpiceRequest.loadDataFromNetwork()
will be invoked and the
result will be stored in cache using the cache key
requestCacheKey.
request
- the request to executerequestCacheKey
- the key used to store and retrieve the result of the request
in the cachecacheExpiryDuration
- duration in milliseconds after which the content of the cache
will be considered to be expired.
DurationInMillis.ALWAYS_RETURNED
means data in cache
is always returned if it exists.
DurationInMillis.ALWAYS_EXPIRED
means data in cache is
never returned.(see DurationInMillis
)requestListener
- the listener to notify when the request will finishpublic <T> void execute(CachedSpiceRequest<T> cachedSpiceRequest, RequestListener<T> requestListener)
cachedSpiceRequest
- the request to execute. CachedSpiceRequest
is a
wrapper of SpiceRequest
that contains cache key and
cache durationrequestListener
- the listener to notify when the request will finishpublic <T> void getFromCacheAndLoadFromNetworkIfExpired(SpiceRequest<T> request, Object requestCacheKey, long cacheExpiryDuration, RequestListener<T> requestListener)
SpiceRequest.loadDataFromNetwork()
,
the cache will be checked : if a result has been cached with the cache
key requestCacheKey, RoboSpice will consider the parameter
cacheExpiryDuration to determine whether the result in the cache
is expired or not. If it is not expired, then listeners will receive the
data in cache only. If the result is absent or expired, then
SpiceRequest.loadDataFromNetwork()
will be invoked and the result
will be stored in cache using the cache key requestCacheKey.
request
- the request to executerequestCacheKey
- the key used to store and retrieve the result of the request
in the cachecacheExpiryDuration
- duration in milliseconds after which the content of the cache
will be considered to be expired.
DurationInMillis.ALWAYS_RETURNED
means data in cache
is always returned if it exists.
DurationInMillis.ALWAYS_EXPIRED
doesn't make much
sense here.requestListener
- the listener to notify when the request will finishpublic <U,T extends U> void putInCache(Class<U> clazz, Object requestCacheKey, T data, RequestListener<U> listener)
clazz
- a super class or the class of data.requestCacheKey
- the request cache key that data will be stored in.data
- the data to store. Maybe null if supported by underlying
ObjectPersister.listener
- a listener that will be notified of this request's success or
failure. May be null.public <T> void putInCache(Class<? super T> clazz, Object requestCacheKey, T data)
putInCache(Class, Object, Object, RequestListener)
with a null
listener. Operation will take place but you won't be notified.
clazz
- a super class or the class of data.requestCacheKey
- the request cache key that data will be stored in.data
- the data to store. Maybe null if supported by underlying
ObjectPersister.public <T> void putInCache(Object requestCacheKey, T data, RequestListener<T> listener)
putInCache(Class, Object, Object, RequestListener)
where the
class used to identify the request is data.getClass().
requestCacheKey
- the request cache key that data will be stored in.data
- the data to store. Maybe null if supported by underlying
ObjectPersister.listener
- a listener that will be notified of this request's success or
failure. May be null.public <T> void putInCache(Object requestCacheKey, T data)
putInCache(Class, Object, Object, RequestListener)
where the
class used to identify the request is data.getClass() and with a null
listener. Operation will take place but you won't be notified.
requestCacheKey
- the request cache key that data will be stored in.data
- the data to store. Maybe null if supported by underlying
ObjectPersister.public <T> void cancel(Class<T> clazz, Object requestCacheKey)
clazz
- the class of the result of the pending request to look for.requestCacheKey
- the cache key associated to the request's results.public void dontNotifyRequestListenersForRequest(SpiceRequest<?> request)
SpiceService
to
remove listeners if requests have already been sent to the
SpiceService
if the request has already been sent to the service.
Otherwise, it will just remove listeners before passing the request to
the SpiceService
. Calling this method doesn't prevent request
from being executed (and put in cache) but will remove request's
listeners notification.
request
- Request for which listeners are to unregistered.protected void dontNotifyRequestListenersForRequestInternal(SpiceRequest<?> request)
SpiceService
yet, all listeners are unregistered locally before
being passed to the service. Otherwise, it will asynchronously ask to the
SpiceService
to remove the listeners of the request being
processed.
request
- Request for which listeners are to unregistered.public void dontNotifyAnyRequestListeners()
Activity.onStop()
protected void dontNotifyAnyRequestListenersInternal()
public void cancel(SpiceRequest<?> request)
request
- the request to cancelpublic void cancelAllRequests()
public void addSpiceServiceListener(SpiceServiceListener spiceServiceListener)
public void removeSpiceServiceListener(SpiceServiceListener spiceServiceListener)
public Future<List<Object>> getAllCacheKeys(Class<?> clazz)
public <T> Future<List<T>> getAllDataFromCache(Class<T> clazz) throws CacheLoadingException
CacheLoadingException
public <T> Future<T> getDataFromCache(Class<T> clazz, Object cacheKey) throws CacheLoadingException
getFromCache(Class, Object, long, RequestListener)
.
clazz
- the class of the result to retrieve from cache.cacheKey
- the key used to store and retrieve the result of the request
in the cache
CacheLoadingException
- Exception thrown when a problem occurs while loading data
from cache.public <T> Future<T> putDataInCache(Object cacheKey, T data) throws CacheSavingException, CacheCreationException
putInCache(Class, Object, Object)
.
cacheKey
- the key used to store and retrieve the result of the request
in the cachedata
- the data to be saved in cache.
CacheLoadingException
- Exception thrown when a problem occurs while loading data
from cache.
CacheSavingException
CacheCreationException
public Future<Boolean> isDataInCache(Class<?> clazz, Object cacheKey, long cacheExpiryDuration) throws CacheCreationException
clazz
- the class of the result to retrieve from cache.cacheKey
- the key used to store and retrieve the result of the request
in the cachecacheExpiryDuration
- duration in milliseconds after which the content of the cache
will be considered to be expired.
DurationInMillis.ALWAYS_RETURNED
means data in cache
is always returned if it exists.
DurationInMillis.ALWAYS_EXPIRED
means data in cache is
never returned.(see DurationInMillis
)
CacheCreationException
- Exception thrown when a problem occurs while looking for data
in cache.public Future<Date> getDateOfDataInCache(Class<?> clazz, Object cacheKey) throws CacheCreationException
clazz
- the class of the result to retrieve from cache.cacheKey
- the key used to store and retrieve the result of the request
in the cache
CacheLoadingException
- Exception thrown when a problem occurs while loading data
from cache.
CacheCreationException
public <T> Future<?> removeDataFromCache(Class<T> clazz, Object cacheKey)
clazz
- the Type of data you want to remove from cachecacheKey
- the key of the object in cachepublic <T> Future<?> removeDataFromCache(Class<T> clazz)
clazz
- the type of data you want to remove from cache.public Future<?> removeAllDataFromCache()
CacheManager
of the SpiceService
.
public void setFailOnCacheError(boolean failOnCacheError)
failOnCacheError
- true if an error must fail the processpublic void dumpState()
protected boolean isBound()
protected void waitForServiceToBeBound() throws InterruptedException
SpiceService
.
InterruptedException
- in case the binding is interrupted.protected void waitForServiceToBeUnbound() throws InterruptedException
SpiceService
.
InterruptedException
- in case the binding is interrupted.protected <T> Future<T> executeCommand(SpiceManager.SpiceManagerCommand<T> spiceManagerCommand)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |