Skip to content

Commit

Permalink
Merge pull request #23 from eveliotc/parent-and-priority
Browse files Browse the repository at this point in the history
Parent and priority
  • Loading branch information
johnkil committed Feb 17, 2014
2 parents 662b33c + 972ab7e commit 2124473
Show file tree
Hide file tree
Showing 7 changed files with 292 additions and 59 deletions.
88 changes: 88 additions & 0 deletions library/src/com/devspark/appmsg/AppMsg.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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
Expand Down Expand Up @@ -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;
}

/**
* <p>Set priority for this message</p>
* <p><b>Note</b>: This only affects the order in which the messages get shown,
* not the stacking order of the views.</p>
*
* <p>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.</p>
*
* @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}.
*
Expand Down
29 changes: 22 additions & 7 deletions library/src/com/devspark/appmsg/MsgManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -43,7 +45,7 @@
/**
* @author Evgeny Shishkin
*/
class MsgManager extends Handler {
class MsgManager extends Handler implements Comparator<AppMsg> {

private static final int MESSAGE_DISPLAY = 0xc2007;
private static final int MESSAGE_ADD_VIEW = 0xc20074dd;
Expand All @@ -53,10 +55,10 @@ class MsgManager extends Handler {
private static ReleaseCallbacks sReleaseCallbacks;

private final Queue<AppMsg> msgQueue;
private Queue<AppMsg> stickyQueue;
private final Queue<AppMsg> stickyQueue;

private MsgManager() {
msgQueue = new LinkedList<AppMsg>();
msgQueue = new PriorityQueue<AppMsg>(1, this);
stickyQueue = new LinkedList<AppMsg>();
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion sample/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Sherlock.Light.DarkActionBar">
<activity android:name=".MainActivity">
<activity android:name=".MainActivity" android:windowSoftInputMode="stateUnchanged">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

Expand Down
76 changes: 52 additions & 24 deletions sample/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,86 @@
android:fillViewport="true">

<LinearLayout
android:id="@+id/animated_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:animateLayoutChanges="true"
android:padding="48dp">

<Button
android:id="@+id/alert"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:textAllCaps="true"
android:text="@string/style"
/>
<Spinner
android:id="@+id/style_spnr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:spinnerMode="dropdown"
android:entries="@array/styles"
android:prompt="@string/style"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:textAllCaps="true"
android:text="@string/priority"
/>
<Spinner
android:id="@+id/priority_spnr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showAppMsg"
android:text="@string/alert" />
android:minHeight="48dp"
android:spinnerMode="dropdown"
android:entries="@array/priorities"
android:prompt="@string/priority"
/>

<Button
android:id="@+id/confirm"
<CheckBox
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showAppMsg"
android:text="@string/confirm" />
android:text="@string/bottom" />

<Button
android:id="@+id/info"
<LinearLayout
android:id="@+id/alt_parent"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showAppMsg"
android:text="@string/info" />
android:minHeight="48dp"
android:orientation="vertical" />

<Button
android:id="@+id/custom"
<CheckBox
android:id="@+id/parent_chk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showAppMsg"
android:text="@string/custom" />
android:text="@string/custom_parent" />

<Button
android:id="@+id/sticky"
<EditText
android:id="@+id/provided_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showAppMsg"
android:text="@string/sticky" />
android:hint="@string/your_message_here"
/>

<CheckBox
android:id="@+id/bottom"
<Button
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/bottom" />
android:onClick="buttonClick"
android:text="@string/show_appmsg" />

<Button
android:id="@+id/cancel_all"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showAppMsg"
android:onClick="buttonClick"
android:text="@string/cancel_all" />

</LinearLayout>
Expand Down
16 changes: 16 additions & 0 deletions sample/res/values/arrays.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="styles">
<item>@string/alert</item>
<item>@string/confirm</item>
<item>@string/info</item>
<item>@string/custom</item>
<item>@string/sticky</item>
</string-array>

<string-array name="priorities">
<item>@string/high</item>
<item>@string/normal</item>
<item>@string/low</item>
</string-array>
</resources>
8 changes: 8 additions & 0 deletions sample/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,13 @@
<string name="bottom">Show at bottom of screen</string>
<string name="remove">Remove</string>
<string name="cancel_all">Cancel All</string>
<string name="high">High</string>
<string name="normal">Normal</string>
<string name="low">Low</string>
<string name="show_appmsg">Show AppMsg!</string>
<string name="style">Style</string>
<string name="priority">Priority</string>
<string name="your_message_here">Your message here</string>
<string name="custom_parent">Custom parent</string>

</resources>
Loading

0 comments on commit 2124473

Please sign in to comment.