@Override protected boolean execute(Conversation conversation) { PendingIntent pendingIntent = ApptentiveInternal.prepareMessageCenterPendingIntent(foreground.getApplicationContext(), conversation); if (pendingIntent != null) { final ApptentiveToastNotificationManager manager = ApptentiveToastNotificationManager.getInstance(foreground, true); final ApptentiveToastNotification.Builder builder = new ApptentiveToastNotification.Builder(foreground); builder.setContentTitle(foreground.getResources().getString(R.string.apptentive_message_center_title)) .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS) .setSmallIcon(R.drawable.avatar).setContentText(apptentiveMsg.getBody()) .setContentIntent(pendingIntent) .setFullScreenIntent(pendingIntent, false); DispatchQueue.mainQueue().dispatchAsync(new DispatchTask() { @Override protected void execute() { ApptentiveToastNotification notification = builder.buildApptentiveToastNotification(); notification.setAvatarUrl(apptentiveMsg.getSenderProfilePhoto()); manager.notify(TOAST_TYPE_UNREAD_MESSAGE, notification);} }); } return true; } });
@Override public void run() { try { setScheduled(false); if (!isCancelled()) { execute(); } } catch (Exception e) { ApptentiveLog.e(e, "Exception while executing task"); logException(e); } finally { setCancelled(false); } }
/** * Add <code>{@link DispatchTask}</code> to the queue if it's not already on the queue (this * way you can ensure only one instance of the task is scheduled at a time). After the task is * executed you can schedule it again. * * @return true if task was scheduled */ public boolean dispatchAsyncOnce(DispatchTask task, long delayMillis) { if (!task.isScheduled()) { dispatchAsync(task, delayMillis); return true; } return false; }
@Override protected void dispatch(DispatchTask task, long delayMillis) { if (runImmediately) { task.run(); } else { tasks.add(task); } }
/** * Add <code>{@link DispatchTask}</code> to the queue */ public void dispatchAsync(DispatchTask task, long delayMillis) { task.setScheduled(true); dispatch(task, delayMillis); }
void stopPolling() { if (isPolling()) { ApptentiveLog.v(MESSAGES, "Stop polling messages (%s)", getLocalConversationIdentifier()); messagePollingTask.cancel(); messagePollingTask = null; notifyStopPolling(); } }
public void dispatchTasks() { while (tasks.size() > 0) { tasks.poll().run(); } }
@Override protected void execute() { if (successful) { final Activity activity = getActivity(); dispatchOnConversationQueue(new DispatchTask() { @Override protected void execute() { ApptentiveInternal.getInstance().showMessageCenterInternal(activity, null); dispatchOnMainQueue(new DispatchTask() { @Override protected void execute() { transit(); } }); } }); } else { updateStatus(); } } });
private boolean retryRequest(DispatchQueue networkQueue, int responseCode) { assertFalse(retryDispatchTask.isScheduled()); ++retryAttempt; if (!retryPolicy.shouldRetryRequest(responseCode, retryAttempt)) { ApptentiveLog.v(NETWORK, "Retry policy declined request retry: %s", this); return false; } retrying = true; long retryTimeout = retryPolicy.getRetryTimeoutMillis(retryAttempt); ApptentiveLog.v(NETWORK, "Retry request in %d ms: %s", retryTimeout, this); networkQueue.dispatchAsyncOnce(retryDispatchTask, retryTimeout); return true; }
@Override protected void execute() { Conversation conversation = notNull(ApptentiveInternal.getInstance().getConversation()); if (conversation == null) { dispatchOnMainQueue(new DispatchTask() { @Override protected void execute() { finish(); } }); } } });
private void invokeCallback(final boolean result) { if (callback != null) { try { if (callbackQueue != null) { callbackQueue.dispatchAsync(new DispatchTask() { @Override protected void execute() { callback.onFinish(result); } }); } else { callback.onFinish(result); } } catch (Exception e) { ApptentiveLog.e(CONVERSATION, "Exception while invoking callback"); logException(e); } } }