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); } }); } }
/** * 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) { return dispatchAsyncOnce(task, 0L); }
private static DispatchQueue createConversationDataQueue() { try { return DispatchQueue.createBackgroundQueue("Apptentive Conversation Data Queue", DispatchQueueType.Serial); } catch (Exception e) { return null; // let unit test handle this } } }
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); } }
@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); } }); }
@Test public void testSchedulingTasksWithException() { DispatchQueue.mainQueue().dispatchAsyncOnce(new DispatchTask() { @Override protected void execute() { addResult("task-1"); } }); DispatchQueue.mainQueue().dispatchAsyncOnce(new DispatchTask() { @Override protected void execute() { throw new RuntimeException(); // throwing an exception should not break the queue } }); DispatchQueue.mainQueue().dispatchAsyncOnce(new DispatchTask() { @Override protected void execute() { addResult("task-2"); } }); dispatchTasks(); assertResult("task-1", "task-2"); } }
/** * Our SDK must connect to our server at least once to download initial configuration for Message * Center. Call this method to see whether or not Message Center can be displayed. This task is * performed asynchronously. * * @param callback Called after we check to see if Message Center can be displayed, but before it * is displayed. Called with true if an Interaction will be displayed, else false. */ public static void canShowMessageCenter(BooleanCallback callback) { dispatchConversationTask(new ConversationDispatchTask(callback, DispatchQueue.mainQueue()) { @Override protected boolean execute(Conversation conversation) { return ApptentiveInternal.canShowMessageCenterInternal(conversation); } }, "check message center availability"); }
/** * Asserts that code executes on the main thread. */ public static void assertBackgroundThread() { if (imp != null && DispatchQueue.isMainQueue()) { imp.assertFailed("Expected background thread but was 'main'"); } }
/** * Add <code>{@link DispatchTask}</code> to the queue */ public void dispatchAsync(DispatchTask task, long delayMillis) { task.setScheduled(true); dispatch(task, delayMillis); }
public static boolean isConversationQueue() { return conversationQueue().isCurrent(); }
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); } }
/** * Add <code>{@link DispatchTask}</code> to the queue */ public void dispatchAsync(DispatchTask task) { dispatchAsync(task, 0L); }
DispatchQueue.mainQueue().dispatchAsyncOnce(toastMessageNotifierTask.setMessage(messageOnToast)); conversationQueue().dispatchAsyncOnce(hostMessageNotifierTask.setMessageCount(getUnreadMessageCount())); } finally { if (listener != null) {
/** * This method can be used to determine if a call to one of the <strong><code>engage()</code></strong> methods such as * {@link #engage(Context, String)} using the same event name will * result in the display of an Interaction. This is useful if you need to know whether an Interaction will be * displayed before you create a UI Button, etc. * * @param event A unique String representing the line this method is called on. For instance, you may want to have * the ability to target interactions to run after the user uploads a file in your app. You may then * call <strong><code>engage(activity, "finished_upload");</code></strong> */ public static synchronized void queryCanShowInteraction(final String event, BooleanCallback callback) { dispatchConversationTask(new ConversationDispatchTask(callback, DispatchQueue.mainQueue()) { @Override protected boolean execute(Conversation conversation) { return canShowLocalAppInteraction(conversation, event); } }, "check if interaction can be shown"); }
/** * Asserts that code executes on the main thread. */ public static void assertMainThread() { if (imp != null && !DispatchQueue.isMainQueue()) { imp.assertFailed(StringUtils.format("Expected 'main' thread but was '%s'", Thread.currentThread().getName())); } }
protected void dispatchOnMainQueue(DispatchTask task) { DispatchQueue.mainQueue().dispatchAsync(task); }
if (!isMainQueue()) { mainQueue().dispatchAsync(new DispatchTask() { @Override protected void execute() {
/** * 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; }