/** * Gets the current instance of the Toaster. If there is no extant instance, * a new one will be created. * * @return The current Toaster instance */ static synchronized Toaster getInstance() { if (mToaster != null) return mToaster; else { mToaster = new Toaster(); return mToaster; } }
/** * Dismisses every SuperToast and SuperActivityToast that is showing and * cancels everything in the queue. */ public static void cancelAllSuperToasts() { Toaster.getInstance().cancelAllSuperToasts(); }
@Override public void handleMessage(Message message) { final SuperToast superToast = (SuperToast) message.obj; switch (message.what) { case Messages.SHOW_NEXT: showNextSuperToast(); break; case Messages.DISPLAY_SUPERTOAST: displaySuperToast(superToast); break; case Messages.REMOVE_SUPERTOAST: removeSuperToast(superToast); break; default: super.handleMessage(message); break; } }
/** * Send a message at a later time. This is used to dismiss a SuperToast. */ private void sendDelayedMessage(SuperToast superToast, int messageId, long delay) { Message message = obtainMessage(messageId); message.obj = superToast; sendMessageDelayed(message, delay); }
/** * Dismissed the SuperToast if it is showing. */ public void dismiss() { Toaster.getInstance().removeSuperToast(this); }
/** * Saves the state of all SuperToasts that are showing and/or pending. * * This should be called in the {@link android.app.Activity#onSaveInstanceState(android.os.Bundle)} * method of your Activity. * * @param bundle The Bundle provided in onSaveInstanceState() */ @SuppressWarnings("unchecked") public static void onSaveState(Bundle bundle) { final ArrayList<Style> styleList = new ArrayList(); // Create a list of every Style used by a SuperToast in the queue for (SuperToast superToast : Toaster.getInstance().getQueue()) { if (superToast instanceof SuperActivityToast) { superToast.getStyle().isSuperActivityToast = true; } styleList.add(superToast.getStyle()); } bundle.putParcelableArrayList(BUNDLE_KEY, styleList); // Let's avoid any erratic behavior and cancel any showing/pending SuperActivityToasts manually Toaster.getInstance().cancelAllSuperToasts(); }
/** * Returns the number of SuperToasts in the queue. * * @return The queue size */ public static int getQueueSize() { return Toaster.getInstance().getQueue().size(); }
/** * Show the next SuperToast in the current queue. If a SuperToast is currently showing, * do nothing. The currently showing SuperToast will call this method when it dismisses. */ private void showNextSuperToast() { // Do nothing if the queue is empty if (superToastPriorityQueue.isEmpty()) return; // Get next SuperToast in the queue final SuperToast superToast = superToastPriorityQueue.peek(); if (!superToast.isShowing()) { final Message message = obtainMessage(Messages.DISPLAY_SUPERTOAST); message.obj = superToast; sendMessage(message); } }
/** * Shows the SuperToast. If any SuperToast is already showing, this SuperToast * will be enqueued until the others have finished (depending on priority level). */ public void show() { this.onPrepareShow(); Toaster.getInstance().add(this); AccessibilityUtils.sendAccessibilityEvent(this.mView); }
sendDelayedMessage(superToast, Messages.REMOVE_SUPERTOAST, superToast.getDuration() + AnimationUtils.SHOW_DURATION); sendDelayedMessage(superToast, Messages.REMOVE_SUPERTOAST, superToast.getDuration() + AnimationUtils.SHOW_DURATION);
removeMessages(Messages.SHOW_NEXT); removeMessages(Messages.DISPLAY_SUPERTOAST); removeMessages(Messages.REMOVE_SUPERTOAST);
this.sendDelayedMessage(superToast, Messages.SHOW_NEXT, AnimationUtils.HIDE_DURATION);