@Override void cancel() { super.cancel(); if (callback != null) { callback = null; } }
private void deliverAction(@Nullable RequestHandler.Result result, Action action, @Nullable Exception e) { if (action.cancelled) { return; } if (!action.willReplay) { targetToAction.remove(action.getTarget()); } if (result != null) { action.complete(result); if (loggingEnabled) { log(OWNER_MAIN, VERB_COMPLETED, action.request.logId(), "from " + result.getLoadedFrom()); } } else { Exception exception = checkNotNull(e, "e == null"); action.error(exception); if (loggingEnabled) { log(OWNER_MAIN, VERB_ERRORED, action.request.logId(), exception.getMessage()); } } }
/** * Cancel any existing requests with given tag. You can set a tag * on new requests with {@link RequestCreator#tag(Object)}. * * @see RequestCreator#tag(Object) */ public void cancelTag(@NonNull Object tag) { checkMain(); checkNotNull(tag, "tag == null"); List<Action> actions = new ArrayList<>(targetToAction.values()); //noinspection ForLoopReplaceableByForEach for (int i = 0, n = actions.size(); i < n; i++) { Action action = actions.get(i); if (tag.equals(action.getTag())) { cancelExistingRequest(action.getTarget()); } } List<DeferredRequestCreator> deferredRequestCreators = new ArrayList<>(targetToDeferredRequestCreator.values()); //noinspection ForLoopReplaceableByForEach for (int i = 0, n = deferredRequestCreators.size(); i < n; i++) { DeferredRequestCreator deferredRequestCreator = deferredRequestCreators.get(i); if (tag.equals(deferredRequestCreator.getTag())) { deferredRequestCreator.cancel(); } } }
void performResumeTag(Object tag) { // Trying to resume a tag that is not paused. if (!pausedTags.remove(tag)) { return; } List<Action> batch = null; for (Iterator<Action> i = pausedActions.values().iterator(); i.hasNext();) { Action action = i.next(); if (action.getTag().equals(tag)) { if (batch == null) { batch = new ArrayList<>(); } batch.add(action); i.remove(); } } if (batch != null) { mainThreadHandler.sendMessage(mainThreadHandler.obtainMessage(REQUEST_BATCH_RESUME, batch)); } }
private void markForReplay(Action action) { Object target = action.getTarget(); action.willReplay = true; failedActions.put(target, action); }
private void verifyActionComplete(Action action) { ArgumentCaptor<RequestHandler.Result> captor = ArgumentCaptor.forClass(RequestHandler.Result.class); verify(action).complete(captor.capture()); RequestHandler.Result result = captor.getValue(); assertThat(result.getBitmap()).isEqualTo(bitmap); assertThat(result.getLoadedFrom()).isEqualTo(MEMORY); } }
@Test public void completeInvokesErrorOnAllFailedRequests() { Action action1 = mockAction(URI_KEY_1, URI_1, mockImageViewTarget()); Action action2 = mockCanceledAction(); Exception exception = mock(Exception.class); BitmapHunter hunter = mockHunter(URI_KEY_1, null); when(hunter.getException()).thenReturn(exception); when(hunter.getActions()).thenReturn(Arrays.asList(action1, action2)); picasso.complete(hunter); verify(action1).error(exception); verify(action2, never()).error(exception); verify(listener).onImageLoadFailed(picasso, URI_1, exception); }
@OnLifecycleEvent(Lifecycle.Event.ON_STOP) void pauseAll() { checkMain(); List<Action> actions = new ArrayList<>(targetToAction.values()); //noinspection ForLoopReplaceableByForEach for (int i = 0, n = actions.size(); i < n; i++) { Action action = actions.get(i); dispatcher.dispatchPauseTag(action.getTag()); } List<DeferredRequestCreator> deferredRequestCreators = new ArrayList<>(targetToDeferredRequestCreator.values()); //noinspection ForLoopReplaceableByForEach for (int i = 0, n = deferredRequestCreators.size(); i < n; i++) { DeferredRequestCreator deferredRequestCreator = deferredRequestCreators.get(i); Object tag = deferredRequestCreator.getTag(); if (tag != null) { dispatcher.dispatchPauseTag(tag); } } }
void enqueueAndSubmit(Action action) { Object target = action.getTarget(); if (targetToAction.get(target) != action) { // This will also check we are on the main thread. cancelExistingRequest(target); targetToAction.put(target, action); } submit(action); }
@Test public void completeInvokesSuccessOnAllSuccessfulRequests() { Action action1 = mockAction(URI_KEY_1, URI_1, mockImageViewTarget()); Action action2 = mockCanceledAction(); BitmapHunter hunter = mockHunter(URI_KEY_1, new RequestHandler.Result(bitmap, MEMORY)); when(hunter.getActions()).thenReturn(Arrays.asList(action1, action2)); picasso.complete(hunter); verifyActionComplete(action1); verify(action2, never()).complete(any(RequestHandler.Result.class)); }
void performSubmit(Action action, boolean dismissFailed) { if (pausedTags.contains(action.getTag())) { pausedActions.put(action.getTarget(), action); if (action.picasso.loggingEnabled) { log(OWNER_DISPATCHER, VERB_PAUSED, action.request.logId(), "because tag '" + action.getTag() + "' is paused"); } return; } BitmapHunter hunter = hunterMap.get(action.request.key); if (hunter != null) { hunter.attach(action); return; } if (service.isShutdown()) { if (action.picasso.loggingEnabled) { log(OWNER_DISPATCHER, VERB_IGNORED, action.request.logId(), "because shut down"); } return; } hunter = forRequest(action.picasso, this, cache, stats, action); hunter.future = service.submit(hunter); hunterMap.put(action.request.key, hunter); if (dismissFailed) { failedActions.remove(action.getTarget()); } if (action.picasso.loggingEnabled) { log(OWNER_DISPATCHER, VERB_ENQUEUED, action.request.logId()); } }
@OnLifecycleEvent(Lifecycle.Event.ON_START) void resumeAll() { checkMain(); List<Action> actions = new ArrayList<>(targetToAction.values()); //noinspection ForLoopReplaceableByForEach for (int i = 0, n = actions.size(); i < n; i++) { Action action = actions.get(i); dispatcher.dispatchResumeTag(action.getTag()); } List<DeferredRequestCreator> deferredRequestCreators = new ArrayList<>(targetToDeferredRequestCreator.values()); //noinspection ForLoopReplaceableByForEach for (int i = 0, n = deferredRequestCreators.size(); i < n; i++) { DeferredRequestCreator deferredRequestCreator = deferredRequestCreators.get(i); Object tag = deferredRequestCreator.getTag(); if (tag != null) { dispatcher.dispatchResumeTag(tag); } } }
@Override void cancel() { super.cancel(); callback = null; } }
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) void cancelAll() { checkMain(); List<Action> actions = new ArrayList<>(targetToAction.values()); //noinspection ForLoopReplaceableByForEach for (int i = 0, n = actions.size(); i < n; i++) { Action action = actions.get(i); cancelExistingRequest(action.getTarget()); } List<DeferredRequestCreator> deferredRequestCreators = new ArrayList<>(targetToDeferredRequestCreator.values()); //noinspection ForLoopReplaceableByForEach for (int i = 0, n = deferredRequestCreators.size(); i < n; i++) { DeferredRequestCreator deferredRequestCreator = deferredRequestCreators.get(i); deferredRequestCreator.cancel(); } }
void performCancel(Action action) { String key = action.request.key; BitmapHunter hunter = hunterMap.get(key); if (hunter != null) { hunter.detach(action); if (hunter.cancel()) { hunterMap.remove(key); if (action.picasso.loggingEnabled) { log(OWNER_DISPATCHER, VERB_CANCELED, action.request.logId()); } } } if (pausedTags.contains(action.getTag())) { pausedActions.remove(action.getTarget()); if (action.picasso.loggingEnabled) { log(OWNER_DISPATCHER, VERB_CANCELED, action.request.logId(), "because paused request got canceled"); } } Action remove = failedActions.remove(action.getTarget()); if (remove != null && remove.picasso.loggingEnabled) { log(OWNER_DISPATCHER, VERB_CANCELED, remove.request.logId(), "from replaying"); } }
static Action mockAction(Request request, final Object target, String tag) { Action action = spy(new Action(mockPicasso(), request) { @Override void complete(RequestHandler.Result result) { } @Override void error(Exception e) { } @Override Object getTarget() { return target; } }); when(action.getTag()).thenReturn(tag != null ? tag : action); return action; }
@Override void cancel() { super.cancel(); if (callback != null) { callback = null; } } }
@Test public void performCancelUnqueuesAndDetachesPausedRequest() { Action action = mockAction(URI_KEY_1, URI_1, mockTarget(), "tag"); BitmapHunter hunter = mockHunter(URI_KEY_1 + KEY_SEPARATOR, new RequestHandler.Result(bitmap1, MEMORY), action); dispatcher.hunterMap.put(URI_KEY_1 + KEY_SEPARATOR, hunter); dispatcher.pausedTags.add("tag"); dispatcher.pausedActions.put(action.getTarget(), action); dispatcher.performCancel(action); assertThat(dispatcher.pausedTags).containsExactly("tag"); assertThat(dispatcher.pausedActions).isEmpty(); verify(hunter).detach(action); }
if (single != null && single.getTag().equals(tag)) { hunter.detach(single); pausedActions.put(single.getTarget(), single); if (loggingEnabled) { log(OWNER_DISPATCHER, VERB_PAUSED, single.request.logId(), for (int i = joined.size() - 1; i >= 0; i--) { Action action = joined.get(i); if (!action.getTag().equals(tag)) { continue; pausedActions.put(action.getTarget(), action); if (loggingEnabled) { log(OWNER_DISPATCHER, VERB_PAUSED, action.request.logId(),
@Test public void targetActionWithDefaultTag() { new RequestCreator(picasso, URI_1, 0).into(mockTarget()); verify(picasso).enqueueAndSubmit(actionCaptor.capture()); assertThat(actionCaptor.getValue().getTag()).isEqualTo(actionCaptor.getValue()); }