diff --git a/library/src/com/devspark/appmsg/AppMsg.java b/library/src/com/devspark/appmsg/AppMsg.java index 5a8ceff..e862737 100644 --- a/library/src/com/devspark/appmsg/AppMsg.java +++ b/library/src/com/devspark/appmsg/AppMsg.java @@ -21,6 +21,7 @@ import android.content.res.Resources; import android.view.LayoutInflater; import android.view.View; +import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.view.animation.Animation; import android.view.animation.AnimationUtils; @@ -64,6 +65,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. */ @@ -82,9 +104,11 @@ public class AppMsg { private final Activity mActivity; private int mDuration = LENGTH_SHORT; private View mView; + private ViewGroup mParent; 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 +472,70 @@ 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; + } + + /** + * @return + * Provided parent to add {@link #getView()} to using {@link #getLayoutParams()}. + */ + public ViewGroup getParent() { + return mParent; + } + + /** + * Provide a different parent than Activity decor view + * @param parent + * Provided parent to add {@link #getView()} to using {@link #getLayoutParams()}. + * + */ + public void setParent(ViewGroup parent) { + mParent = parent; + } + + /** + * Provide a different parent than Activity decor view + * + * @param parentId + * Provided parent id to add {@link #getView()} to using {@link #getLayoutParams()}. + * + */ + public void setParent(int parentId) { + setParent((ViewGroup) mActivity.findViewById(parentId)); + } + /** * 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..9626485 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(); } @@ -216,10 +218,14 @@ private void removeMsg(final AppMsg appMsg) { private void addMsgToView(AppMsg appMsg) { View view = appMsg.getView(); - if (view.getParent() == null) { - appMsg.getActivity().addContentView( - view, - appMsg.getLayoutParams()); + if (view.getParent() == null) { // Not added yet + final ViewGroup targetParent = appMsg.getParent(); + final ViewGroup.LayoutParams params = appMsg.getLayoutParams(); + if (targetParent != null) { + targetParent.addView(view, params); + } else { + appMsg.getActivity().addContentView(view, params); + } } view.clearAnimation(); view.startAnimation(appMsg.mInAnimation); @@ -258,6 +264,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; 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..92c0a16 100644 --- a/sample/res/layout/activity_main.xml +++ b/sample/res/layout/activity_main.xml @@ -5,58 +5,86 @@ android:fillViewport="true"> -