@Override public final void onDestroy() { spiceManager.shouldStop(); super.onDestroy(); }
@Override protected void onStart() { spiceManager.start(this); super.onStart(); }
public class MyApplication extends Application { private static MyApplication instance; private SpiceManager spiceManager = new SpiceManager(RequestService.class); @Override public void onCreate() { super.onCreate(); } public SpiceManager getManager() { return spiceManager; } }
private boolean tryToStartService() { boolean success = false; // start the service it is not started yet. Context context = getContextReference(); if (context != null) { checkServiceIsProperlyDeclaredInAndroidManifest(context); final Intent intent = new Intent(context, spiceServiceClass); context.startService(intent); success = true; } return success; }
/** * Execute a request, without using cache. No result from cache will be * returned. The method {@link SpiceRequest#loadDataFromNetwork()} will * always be invoked. The result will not be stored in cache. * @param request * the request to execute. * @param requestListener * the listener to notify when the request will finish. */ public <T> void execute(final SpiceRequest<T> request, final RequestListener<T> requestListener) { final CachedSpiceRequest<T> cachedSpiceRequest = new CachedSpiceRequest<T>(request, null, DurationInMillis.ALWAYS_RETURNED); execute(cachedSpiceRequest, requestListener); }
@SuppressWarnings({ "unchecked", "rawtypes", "deprecation" }) @Override public final void onStart(final Intent intent, final int startId) { super.onStart(intent, startId); notificationId = intent.getIntExtra(BUNDLE_KEY_NOTIFICATION_ID, DEFAULT_ROBOSPICE_NOTIFICATION_ID); requestClass = (Class<?>) intent.getSerializableExtra(BUNDLE_KEY_REQUEST_CLASS); requestCacheKey = intent.getStringExtra(BUNDLE_KEY_REQUEST_CACHE_KEY); spiceServiceClass = (Class<? extends SpiceService>) intent.getSerializableExtra(BUNDLE_KEY_SERVICE_CLASS); if (spiceServiceClass == null) { throw new RuntimeException("Please specify a service class to monitor. Use #createIntent as helper."); } foreground = intent.getBooleanExtra(BUNDLE_KEY_FOREGROUND, true); spiceManager = new SpiceManager(spiceServiceClass); notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); spiceManager.start(this); spiceManager.addListenerIfPending(requestClass, requestCacheKey, new NotificationRequestListener()); if (foreground) { startForeground(notificationId, onCreateForegroundNotification()); } }
@SuppressWarnings({ "unchecked", "deprecation" }) @Override public final void onStart(final Intent intent, final int startId) { super.onStart(intent, startId); if (intent == null) { return; } notificationId = intent.getIntExtra(BUNDLE_KEY_NOTIFICATION_ID, DEFAULT_ROBOSPICE_NOTIFICATION_ID); spiceServiceClass = (Class<? extends SpiceService>) intent.getSerializableExtra(BUNDLE_KEY_SERVICE_CLASS); if (spiceServiceClass == null) { throw new RuntimeException("Please specify a service class to monitor. Use #createIntent as helper."); } foreground = intent.getBooleanExtra(BUNDLE_KEY_FOREGROUND, true); spiceManager = new SpiceManager(spiceServiceClass); notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); spiceManager.start(this); spiceManager.addSpiceServiceListener(new NotificationSpiceServiceListener()); if (foreground) { startForeground(notificationId, onCreateForegroundNotification()); } Ln.d(getClass().getSimpleName() + " started."); }
@Override public void onStart() { super.onStart(); if(!hasStartedRequest) { getSpiceManager().execute(getRequest(), getCacheKey(), DurationInMillis.ALWAYS_EXPIRED, this); hasStartedRequest = true; } else { getSpiceManager().addListenerIfPending(getRequest().getResultType(), getCacheKey(), this); } }
@Override public void run() { if (!tryToStartService()) { Ln.d("Service was not started as Activity died prematurely"); isStopped = true; return; } bindToService(); try { waitForServiceToBeBound(); if (spiceService == null) { Ln.d("No spice service bound."); return; } while (!requestQueue.isEmpty() || !isStopped && !Thread.interrupted()) { try { sendRequestToService(requestQueue.take()); } catch (final InterruptedException ex) { Ln.d("Interrupted while waiting for new request."); // we receive an interrupted exception while waiting // see java spec : http://stackoverflow.com/a/6699006/693752 break; } } Ln.d("SpiceManager request runner terminated. Requests count: %d, stopped %b, interrupted %b", requestQueue.size(), isStopped, Thread.interrupted()); } catch (final InterruptedException e) { Ln.d(e, "Interrupted while waiting for acquiring service."); } }
/** * Add listener to a pending request if it exists. If no such request * exists, this method calls onRequestNotFound on the listener. If a request * identified by clazz and requestCacheKey, it will receive an additional * listener. * @param clazz * the class of the result of the pending request to look for. * @param requestCacheKey * the key used to store and retrieve the result of the request * in the cache * @param requestListener * the listener to notify when the request will finish. */ public <T> void addListenerIfPending(final Class<T> clazz, final Object requestCacheKey, final PendingRequestListener<T> requestListener) { addListenerIfPending(clazz, requestCacheKey, (RequestListener<T>) requestListener); }
/** * Execute a request, put the result in cache and register listeners to * notify when request is finished. * @param cachedSpiceRequest * the request to execute. {@link CachedSpiceRequest} is a * wrapper of {@link SpiceRequest} that contains cache key and * cache duration * @param requestListener * the listener to notify when the request will finish */ public <T> void execute(final CachedSpiceRequest<T> cachedSpiceRequest, final RequestListener<T> requestListener) { addRequestListenerToListOfRequestListeners(cachedSpiceRequest, requestListener); Ln.d("adding request to request queue"); this.requestQueue.add(cachedSpiceRequest); }
/** * Execute a request. Before invoking the method * {@link SpiceRequest#loadDataFromNetwork()}, the cache will be checked : * if a result has been cached with the cache key <i>requestCacheKey</i>, * RoboSpice will consider the parameter <i>cacheExpiryDuration</i> 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 {@link SpiceRequest#loadDataFromNetwork()} will be invoked and the * result will be stored in cache using the cache key * <i>requestCacheKey</i>. * @param request * the request to execute * @param requestCacheKey * the key used to store and retrieve the result of the request * in the cache * @param cacheExpiryDuration * duration in milliseconds after which the content of the cache * will be considered to be expired. * {@link DurationInMillis#ALWAYS_RETURNED} means data in cache * is always returned if it exists. * {@link DurationInMillis#ALWAYS_EXPIRED} means data in cache is * never returned.(see {@link DurationInMillis}) * @param requestListener * the listener to notify when the request will finish */ public <T> void execute(final SpiceRequest<T> request, final Object requestCacheKey, final long cacheExpiryDuration, final RequestListener<T> requestListener) { final CachedSpiceRequest<T> cachedSpiceRequest = new CachedSpiceRequest<T>(request, requestCacheKey, cacheExpiryDuration); execute(cachedSpiceRequest, requestListener); }
/** * @See #addListenerIfPending(Class, Object, PendingRequestListener) */ @Deprecated public <T> void addListenerIfPending(final Class<T> clazz, final Object requestCacheKey, final RequestListener<T> requestListener) { final SpiceRequest<T> request = new SpiceRequest<T>(clazz) { @Override public T loadDataFromNetwork() throws Exception { return null; } }; final CachedSpiceRequest<T> cachedSpiceRequest = new CachedSpiceRequest<T>(request, requestCacheKey, DurationInMillis.ALWAYS_EXPIRED); cachedSpiceRequest.setProcessable(false); execute(cachedSpiceRequest, requestListener); }
@Override protected void onStop() { spiceManager.shouldStop(); super.onStop(); }
private SpiceManager spiceManager = new SpiceManager(CustomSpiceService.class);
@Override protected void onStart() { super.onStart(); getJsonContentManager().execute(loremRequest, "lorem.txt", DurationInMillis.ONE_DAY, new LoremRequestListener()); getJsonContentManager().execute(weatherRequest, "75000.json", DurationInMillis.ONE_DAY, new WeatherRequestJsonListener()); getJsonContentManager().execute(weatherRequestXml, "75000.xml", DurationInMillis.ONE_DAY, new WeatherRequestXmlListener()); getJsonContentManager().execute(imageRequest, "logo", DurationInMillis.ONE_DAY, new ImageRequestListener()); }
@Override public final void onDestroy() { spiceManager.shouldStop(); super.onDestroy(); }
@Override public void onStart() { super.onStart(); spiceManager.start(getActivity()); }
public static SpiceManager spiceManager = new SpiceManager(SpiceService.class);