From e1b851365b1c0815cd49ac58bb9608e0e71f5838 Mon Sep 17 00:00:00 2001 From: Evelio Tarazona Date: Sun, 9 Feb 2014 18:38:56 -0800 Subject: [PATCH 1/3] Priority Allow to provide a priority to be taken into account by the queue --- library/src/com/devspark/appmsg/AppMsg.java | 57 +++++++++++++++++++ .../src/com/devspark/appmsg/MsgManager.java | 17 +++++- 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/library/src/com/devspark/appmsg/AppMsg.java b/library/src/com/devspark/appmsg/AppMsg.java index 5a8ceff..1adf000 100644 --- a/library/src/com/devspark/appmsg/AppMsg.java +++ b/library/src/com/devspark/appmsg/AppMsg.java @@ -64,6 +64,27 @@ public class AppMsg { */ public static final int LENGTH_STICKY = -1; + /** + * Lowest priority, messages with this priority will be showed after all messages with priority + * {@link #PRIORITY_HIGH} and {@link #PRIORITY_NORMAL} have been shown. + * + * @see #setPriority(int) + */ + public static final int PRIORITY_LOW = Integer.MIN_VALUE; + /** + * Normal priority, messages with this priority will be showed after all messages with priority + * {@link #PRIORITY_HIGH} but before {@link #PRIORITY_LOW} have been shown. + * + * @see #setPriority(int) + */ + public static final int PRIORITY_NORMAL = 0; + /** + * Highest priority, messages with this priority will be showed before any other message. + * + * @see #setPriority(int) + */ + public static final int PRIORITY_HIGH = Integer.MAX_VALUE; + /** * Show the text notification for a long period of time with a negative style. */ @@ -85,6 +106,7 @@ public class AppMsg { private LayoutParams mLayoutParams; private boolean mFloating; Animation mInAnimation, mOutAnimation; + int mPriority = PRIORITY_NORMAL; /** * Construct an empty AppMsg object. You must call {@link #setView} before @@ -448,6 +470,41 @@ public AppMsg setAnimation(Animation inAnimation, Animation outAnimation) { return this; } + /** + * @return + * Current priority + * + * @see #PRIORITY_HIGH + * @see #PRIORITY_NORMAL + * @see #PRIORITY_LOW + */ + public int getPriority() { + return mPriority; + } + + /** + *

Set priority for this message

+ *

Note: This only affects the order in which the messages get shown, + * not the stacking order of the views.

+ * + *

Example: In the queue there are 3 messages [A, B, C], + * all of them with priority {@link #PRIORITY_NORMAL}, currently message A is being shown + * so we add a new message D with priority {@link #PRIORITY_HIGH}, after A goes away, given that + * D has a higher priority than B an the reset, D will be shown, then once that D is gone, + * B will be shown, and then finally C.

+ * + * @param priority + * A value indicating priority, although you can use any integer value, usage of already + * defined is highly encouraged. + * + * @see #PRIORITY_HIGH + * @see #PRIORITY_NORMAL + * @see #PRIORITY_LOW + */ + public void setPriority(int priority) { + mPriority = priority; + } + /** * The style for a {@link AppMsg}. * diff --git a/library/src/com/devspark/appmsg/MsgManager.java b/library/src/com/devspark/appmsg/MsgManager.java index 1a12916..ca01d08 100644 --- a/library/src/com/devspark/appmsg/MsgManager.java +++ b/library/src/com/devspark/appmsg/MsgManager.java @@ -29,9 +29,11 @@ import java.lang.ref.WeakReference; import java.util.Collection; +import java.util.Comparator; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; +import java.util.PriorityQueue; import java.util.Queue; import java.util.WeakHashMap; @@ -43,7 +45,7 @@ /** * @author Evgeny Shishkin */ -class MsgManager extends Handler { +class MsgManager extends Handler implements Comparator { private static final int MESSAGE_DISPLAY = 0xc2007; private static final int MESSAGE_ADD_VIEW = 0xc20074dd; @@ -53,10 +55,10 @@ class MsgManager extends Handler { private static ReleaseCallbacks sReleaseCallbacks; private final Queue msgQueue; - private Queue stickyQueue; + private final Queue stickyQueue; private MsgManager() { - msgQueue = new LinkedList(); + msgQueue = new PriorityQueue(1, this); stickyQueue = new LinkedList(); } @@ -258,6 +260,15 @@ public void handleMessage(Message msg) { } } + @Override + public int compare(AppMsg lhs, AppMsg rhs) { + return inverseCompareInt(lhs.mPriority, rhs.mPriority); + } + + static int inverseCompareInt(int lhs, int rhs) { + return lhs < rhs ? 1 : (lhs == rhs ? 0 : -1); + } + private static class OutAnimationListener implements Animation.AnimationListener { private final AppMsg appMsg; From 465dadf92a978e0fa5f8f360fcc1c3c06ac80c80 Mon Sep 17 00:00:00 2001 From: Evelio Tarazona Date: Sun, 9 Feb 2014 19:53:36 -0800 Subject: [PATCH 2/3] Refactor sample a bit to make room for Priority and custom text --- sample/AndroidManifest.xml | 2 +- sample/res/layout/activity_main.xml | 66 +++++++------ sample/res/values/arrays.xml | 16 ++++ sample/res/values/strings.xml | 7 ++ .../devspark/appmsg/sample/MainActivity.java | 93 ++++++++++++++----- 5 files changed, 131 insertions(+), 53 deletions(-) create mode 100644 sample/res/values/arrays.xml diff --git a/sample/AndroidManifest.xml b/sample/AndroidManifest.xml index 9233577..e14c182 100644 --- a/sample/AndroidManifest.xml +++ b/sample/AndroidManifest.xml @@ -11,7 +11,7 @@ android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/Theme.Sherlock.Light.DarkActionBar"> - + diff --git a/sample/res/layout/activity_main.xml b/sample/res/layout/activity_main.xml index 39a3730..1b6964c 100644 --- a/sample/res/layout/activity_main.xml +++ b/sample/res/layout/activity_main.xml @@ -11,52 +11,64 @@ android:orientation="vertical" android:padding="48dp"> -