/** * Add <code>{@link DispatchTask}</code> to the queue */ public void dispatchAsync(DispatchTask task) { dispatchAsync(task, 0L); }
@Test public void testSchedulingTasksOnce() { DispatchTask task = new DispatchTask() { @Override protected void execute() { addResult("executed"); } }; assertTrue(DispatchQueue.mainQueue().dispatchAsyncOnce(task)); assertFalse(DispatchQueue.mainQueue().dispatchAsyncOnce(task)); assertFalse(DispatchQueue.mainQueue().dispatchAsyncOnce(task)); dispatchTasks(); assertResult("executed"); assertTrue(DispatchQueue.mainQueue().dispatchAsyncOnce(task)); assertFalse(DispatchQueue.mainQueue().dispatchAsyncOnce(task)); assertFalse(DispatchQueue.mainQueue().dispatchAsyncOnce(task)); dispatchTasks(); assertResult("executed"); DispatchQueue.mainQueue().dispatchAsync(task); DispatchQueue.mainQueue().dispatchAsync(task); dispatchTasks(); assertResult("executed", "executed"); }
/** * Handles request synchronously */ void dispatchRequest(final HttpRequest request) { networkQueue.dispatchAsync(new DispatchTask() { @Override protected void execute() { request.dispatchSync(networkQueue); } }); }
private static void notifyFailure(final LoginCallback callback, final String errorMessage) { if (callback != null) { DispatchQueue.mainQueue().dispatchAsync(new DispatchTask() { @Override protected void execute() { callback.onLoginFail(errorMessage); } }); } }
public static void dispatchOnConversationQueue(DispatchTask task) { conversationQueue().dispatchAsync(task); }
protected void dispatchOnMainQueue(DispatchTask task) { DispatchQueue.mainQueue().dispatchAsync(task); }
protected void dispatchOnMainQueue(DispatchTask task) { DispatchQueue.mainQueue().dispatchAsync(task); }
/** * 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; }
public void notifyHostUnreadMessagesListeners(final int unreadMessages) { checkConversationQueue(); // we dispatch listeners on the main queue for (WeakReference<UnreadMessagesListener> listenerRef : hostUnreadMessagesListeners) { final UnreadMessagesListener listener = listenerRef.get(); if (listener != null) { DispatchQueue.mainQueue().dispatchAsync(new DispatchTask() { @Override protected void execute() { listener.onUnreadMessageCountChanged(unreadMessages); } }); } } }
AsyncLogWriter(File destDir, DispatchQueue writeQueue, int logHistorySize) { if (destDir == null) { throw new IllegalArgumentException("Dest dir is null"); } if (writeQueue == null) { throw new IllegalArgumentException("Write queue is null"); } if (logHistorySize < 1) { throw new IllegalArgumentException("Illegal log history size: " + logHistorySize); } this.destDir = destDir; this.logHistorySize = logHistorySize; this.writeQueue = writeQueue; pendingMessages = new ArrayList<>(MESSAGE_QUEUE_SIZE); File logFile = new File(destDir, createLogFilename()); ApptentiveLog.v(UTIL, "Log file: " + logFile); writeQueueTask = new LogFileWriteTask(logFile); // run initialization as the first task on the write queue writeQueue.dispatchAsync(createInitializationTask()); }
public static void launchMessageCenterErrorActivity(final Context context) { if (!isMainQueue()) { mainQueue().dispatchAsync(new DispatchTask() { @Override protected void execute() { launchMessageCenterErrorActivity(context); } }); return; } if (context != null) { Intent intent = MessageCenterInteraction.generateMessageCenterErrorIntent(context); if (!(context instanceof Activity)) { intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); } context.startActivity(intent); } }
private void retrySending(long delayMillis) { ApptentiveLog.d(PAYLOADS, "Retry sending payloads in %d ms", delayMillis); conversationQueue().dispatchAsync(new DispatchTask() { @Override protected void execute() { singleThreadExecutor.execute(new Runnable() { @Override public void run() { try { ApptentiveLog.d(PAYLOADS, "Retrying sending payloads"); sendNextPayloadSync(); } catch (Exception e) { ApptentiveLog.e(PAYLOADS, e, "Exception while trying to retry sending payloads"); logException(e); } } }); } }, delayMillis); }
@Override protected boolean execute(Conversation conversation) { String apptentivePushData = ApptentiveInternal.getApptentivePushNotificationData(data); final PendingIntent intent = ApptentiveInternal.generatePendingIntentFromApptentivePushData(conversation, apptentivePushData); DispatchQueue.mainQueue().dispatchAsync(new DispatchTask() { @Override protected void execute() { callback.onPendingIntent(intent); } }); return true; } }, "build pending intent");
public static void showToast(final Context context, final String message, final int duration) { if (!DispatchQueue.isMainQueue()) { DispatchQueue.mainQueue().dispatchAsync(new DispatchTask() { @Override protected void execute() { showToast(context, message, duration); } }); return; } try { Toast.makeText(context, message, duration).show(); } catch (Exception e) { ApptentiveLog.e(e, "Exception while trying to display toast message"); logException(e); } }
@Override protected boolean execute(Conversation conversation) { String apptentivePushData = ApptentiveInternal.getApptentivePushNotificationData(intent); final PendingIntent intent = ApptentiveInternal.generatePendingIntentFromApptentivePushData(conversation, apptentivePushData); DispatchQueue.mainQueue().dispatchAsync(new DispatchTask() { @Override protected void execute() { callback.onPendingIntent(intent); } }); return true; } }, "build pending intent");
@Override protected boolean execute(Conversation conversation) { String apptentivePushData = ApptentiveInternal.getApptentivePushNotificationData(bundle); final PendingIntent intent = ApptentiveInternal.generatePendingIntentFromApptentivePushData(conversation, apptentivePushData); DispatchQueue.mainQueue().dispatchAsync(new DispatchTask() { @Override protected void execute() { callback.onPendingIntent(intent); } }); return true; } }, "build pending intent");
private void showDebugNotification(final Context context) { final String subject = getSubject(context); final File[] attachments = listAttachments(context); DispatchQueue.mainQueue().dispatchAsync(new DispatchTask() { @Override protected void execute() { NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = TroubleshootingNotificationBuilder.buildNotification(context, subject, getSystemInfo(context), attachments, emailRecipients); Assert.assertNotNull(notification, "Failed to create troubleshooting notification"); if (notificationManager != null) { notificationManager.notify(NOTIFICATION_ID, notification); } } }); }
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); } } }
public void notifyAuthenticationFailedListener(final Apptentive.AuthenticationFailedReason reason, String conversationIdOfFailedRequest) { checkConversationQueue(); if (isConversationActive()) { String activeConversationId = getConversation().getConversationId(); if (StringUtils.equal(activeConversationId, conversationIdOfFailedRequest)) { final Apptentive.AuthenticationFailedListener listener = authenticationFailedListenerRef != null ? authenticationFailedListenerRef.get() : null; if (listener != null) { // we need to dispatch listener on the main queue DispatchQueue.mainQueue().dispatchAsync(new DispatchTask() { @Override protected void execute() { listener.onAuthenticationFailed(reason); } }); } } } }
@Test public void testSchedulingTasks() { DispatchTask task = new DispatchTask() { @Override protected void execute() { addResult("executed"); } }; DispatchQueue.mainQueue().dispatchAsync(task); DispatchQueue.mainQueue().dispatchAsync(task); dispatchTasks(); assertResult("executed", "executed"); }