/** * 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); }
@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()); } }
@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); } }