diff --git a/example/AndroidManifest.xml b/example/AndroidManifest.xml
index 4d2d36c..fbb6c7c 100644
--- a/example/AndroidManifest.xml
+++ b/example/AndroidManifest.xml
@@ -23,6 +23,10 @@
android:name="com.cocosw.undobar.example.UndoStyle"
android:label="@string/title_activity_undo_style" >
+
+
diff --git a/example/res/layout/activity_main.xml b/example/res/layout/activity_main.xml
index 9d09cd2..dc25981 100644
--- a/example/res/layout/activity_main.xml
+++ b/example/res/layout/activity_main.xml
@@ -33,4 +33,12 @@
android:layout_below="@+id/button3"
android:layout_centerHorizontal="true"
android:text="Customize style" />
+
+
\ No newline at end of file
diff --git a/example/res/values/strings.xml b/example/res/values/strings.xml
index 6acea65..86398d1 100644
--- a/example/res/values/strings.xml
+++ b/example/res/values/strings.xml
@@ -2,7 +2,16 @@
UndoBarExample
UndoStyle
+ UndoCountdownStyle
Customize Style
Settings
Hello world!
+
+
+ - Dismissing in 1...
+ - Dismissing in %d...
+
+
+ Dismissing
+
\ No newline at end of file
diff --git a/example/res/values/styles.xml b/example/res/values/styles.xml
index 4b8e21b..d937a18 100644
--- a/example/res/values/styles.xml
+++ b/example/res/values/styles.xml
@@ -21,7 +21,10 @@
- 16dp
-
-
-
+
+
+
+
+
+
\ No newline at end of file
diff --git a/example/src/com/cocosw/undobar/example/MainActivity.java b/example/src/com/cocosw/undobar/example/MainActivity.java
index 96940a1..dbbe5bf 100644
--- a/example/src/com/cocosw/undobar/example/MainActivity.java
+++ b/example/src/com/cocosw/undobar/example/MainActivity.java
@@ -19,6 +19,7 @@ protected void onCreate(final Bundle savedInstanceState) {
findViewById(id.button2).setOnClickListener(this);
findViewById(id.button3).setOnClickListener(this);
findViewById(id.button4).setOnClickListener(this);
+ findViewById(id.button5).setOnClickListener(this);
}
@Override
@@ -36,6 +37,9 @@ public void onClick(final View v) {
case id.button4:
startActivity(new Intent(this, Customize.class));
break;
+ case id.button5:
+ startActivity(new Intent(this, UndoCountdownStyle.class));
+ break;
default:
break;
}
diff --git a/example/src/com/cocosw/undobar/example/UndoCountdownStyle.java b/example/src/com/cocosw/undobar/example/UndoCountdownStyle.java
new file mode 100644
index 0000000..99f9b5d
--- /dev/null
+++ b/example/src/com/cocosw/undobar/example/UndoCountdownStyle.java
@@ -0,0 +1,65 @@
+package com.cocosw.undobar.example;
+
+import java.util.Arrays;
+
+import android.app.ListActivity;
+import android.os.Bundle;
+import android.os.Parcelable;
+import android.view.View;
+import android.widget.ArrayAdapter;
+import android.widget.ListView;
+import android.widget.Toast;
+
+import com.cocosw.undobar.UndoBarController;
+import com.cocosw.undobar.UndoBarController.CountDownFormatter;
+import com.cocosw.undobar.UndoBarController.UndoListener;
+import com.cocosw.undobar.UndoBarStyle;
+
+public class UndoCountdownStyle extends ListActivity implements UndoListener
+{
+
+ @Override
+ protected void onCreate(final Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setListAdapter(new ArrayAdapter(this,
+ android.R.layout.simple_list_item_1, android.R.id.text1,
+ Arrays.asList(new String[] { "Item 1", "Item 2", "Item 3",
+ "Item 4", "Item 5", "Item 6", "Item 7", "Item 8",
+ "Item 9", "Item 10", "Item 11", "Item 12", "Item 13",
+ "Item 14", "Item 15", })));
+ }
+
+ @Override
+ protected void onListItemClick(final ListView l, final View v,
+ final int position, final long id) {
+ final Bundle b = new Bundle();
+ b.putInt("index", position);
+ UndoBarStyle undoBarStyle = new UndoBarStyle(com.cocosw.undobar.R.drawable.ic_undobar_undo,
+ R.string.undo, 5000).setCountDownFormatter(new MyFormatCountDownCallback());
+ UndoBarController.show(this, getListAdapter().getItem(position) + " was selected", this, b,
+ false, undoBarStyle);
+ }
+
+ @Override
+ public void onUndo(final Parcelable token) {
+ if (token != null) {
+ final int position = ((Bundle) token).getInt("index");
+ Toast.makeText(this, "undo clicked, index " + position,
+ Toast.LENGTH_SHORT).show();
+ }
+ }
+
+ private class MyFormatCountDownCallback implements CountDownFormatter {
+
+ @Override
+ public String getCountDownString(final long millisUntilFinished) {
+ int seconds = (int) Math.ceil(millisUntilFinished / 1000.0);
+
+ if (seconds > 0) {
+ return getResources().getQuantityString(R.plurals.countdown_seconds, seconds,
+ seconds);
+ }
+ return getString(R.string.countdown_dismissing);
+ }
+ }
+}
diff --git a/library/res/layout/undobar.xml b/library/res/layout/undobar.xml
index 9160d6a..c9b971f 100644
--- a/library/res/layout/undobar.xml
+++ b/library/res/layout/undobar.xml
@@ -1,11 +1,21 @@
+ style="@style/UndoBar" >
-
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/library/res/values/styles.xml b/library/res/values/styles.xml
index 1fec39a..3e457a2 100644
--- a/library/res/values/styles.xml
+++ b/library/res/values/styles.xml
@@ -15,11 +15,14 @@
-->
-
+
+
+
+
-
+
+
+
+
-
+
+
+
-
\ No newline at end of file
diff --git a/library/src/com/cocosw/undobar/UndoBarController.java b/library/src/com/cocosw/undobar/UndoBarController.java
index 3e0375d..0926380 100644
--- a/library/src/com/cocosw/undobar/UndoBarController.java
+++ b/library/src/com/cocosw/undobar/UndoBarController.java
@@ -58,14 +58,31 @@ public class UndoBarController extends LinearLayout {
private static Animation inAnimation = inFromBottomAnimation(null);
private static Animation outAnimation = outToBottomAnimation(null);
private final TextView mMessageView;
+ private final TextView mMessageCountdownView;
private final TextView mButton;
private final Handler mHideHandler = new Handler();
- private final Runnable mHideRunnable = new Runnable() {
+
+ private long mDismissStartMillis;
+ private final CountDownRunnable mCountDownRunnable;
+
+ private class CountDownRunnable implements Runnable {
+
@Override
public void run() {
- hideUndoBar(false);
+ long millisRemaining = style.duration
+ - (System.currentTimeMillis() - mDismissStartMillis);
+ if (style.countDownFormatter != null) {
+ mMessageCountdownView.setText(style.countDownFormatter
+ .getCountDownString(millisRemaining));
+ }
+
+ if (millisRemaining <= 0) {
+ hideUndoBar(false);
+ } else {
+ mHideHandler.postDelayed(this, Math.min(millisRemaining, 1000));
+ }
}
- };
+ }
private UndoListener mUndoListener;
// State objects
private Parcelable mUndoToken;
@@ -84,7 +101,9 @@ public void run() {
private UndoBarController(final Context context, final AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.undobar, this, true);
+ mCountDownRunnable = new CountDownRunnable();
mMessageView = (TextView) findViewById(R.id.undobar_message);
+ mMessageCountdownView = (TextView) findViewById(R.id.undobar_message_countdown);
mButton = (TextView) findViewById(id.undobar_button);
mButton.setOnClickListener(
new View.OnClickListener() {
@@ -357,7 +376,7 @@ private void setUndoListener(final UndoListener mUndoListener) {
}
private void hideUndoBar(final boolean immediate) {
- mHideHandler.removeCallbacks(mHideRunnable);
+ mHideHandler.removeCallbacks(mCountDownRunnable);
mUndoToken = null;
if (immediate) {
setVisibility(View.GONE);
@@ -418,9 +437,17 @@ private void showUndoBar(final boolean immediate,
if (style.bgRes > 0)
findViewById(id._undobar).setBackgroundResource(style.bgRes);
- mHideHandler.removeCallbacks(mHideRunnable);
+ mHideHandler.removeCallbacks(mCountDownRunnable);
+
+ if (style.countDownFormatter != null) {
+ mMessageCountdownView.setVisibility(View.VISIBLE);
+ mMessageCountdownView.setText(style.countDownFormatter
+ .getCountDownString(style.duration));
+ }
+
+ mDismissStartMillis = System.currentTimeMillis();
if (style.duration > 0) {
- mHideHandler.postDelayed(mHideRunnable, style.duration);
+ mHideHandler.postDelayed(mCountDownRunnable, Math.min(1000, style.duration));
}
if (!immediate) {
clearAnimation();
@@ -452,6 +479,20 @@ public interface UndoListener {
// ensureView(act);
// }
+ /**
+ * A callback interface which is used to provide the text to display when
+ * counting down.
+ */
+ public interface CountDownFormatter {
+ /**
+ * Called each tick of the CountDownTimer
+ *
+ * @param millisLeft time in milliseconds remaining before the item is
+ * automatically removed
+ */
+ public String getCountDownString(final long millisLeft);
+ }
+
/**
* UndoBar Builder
*/
diff --git a/library/src/com/cocosw/undobar/UndoBarStyle.java b/library/src/com/cocosw/undobar/UndoBarStyle.java
index 1dcfa88..87fbb21 100644
--- a/library/src/com/cocosw/undobar/UndoBarStyle.java
+++ b/library/src/com/cocosw/undobar/UndoBarStyle.java
@@ -3,6 +3,9 @@
import android.os.Parcel;
import android.os.Parcelable;
import android.view.animation.Animation;
+import android.widget.TextView;
+
+import com.cocosw.undobar.UndoBarController.CountDownFormatter;
public class UndoBarStyle implements Parcelable {
@@ -14,6 +17,7 @@ public class UndoBarStyle implements Parcelable {
long duration = DEFAULT_DURATION;
Animation inAnimation;
Animation outAnimation;
+ CountDownFormatter countDownFormatter;
/**
@@ -58,7 +62,7 @@ public UndoBarStyle(final int icon, final int title, final int bg,
*
* @param inAnimation animation for fade in
* @param outAnimation animation for fade out
- * @return UndoBar
+ * @return UndoBarStyle
*/
public UndoBarStyle setAnim(Animation inAnimation, Animation outAnimation) {
this.inAnimation = inAnimation;
@@ -66,6 +70,17 @@ public UndoBarStyle setAnim(Animation inAnimation, Animation outAnimation) {
return this;
}
+ /**
+ * Set countdown formatter for current style
+ *
+ * @param countDownFormatter The {@link CountDownFormatter} which provides
+ * text to be shown in the countdown {@link TextView}
+ * @return UndoBarStyle
+ */
+ public UndoBarStyle setCountDownFormatter(CountDownFormatter countDownFormatter) {
+ this.countDownFormatter = countDownFormatter;
+ return this;
+ }
@Override
public String toString() {
@@ -102,6 +117,7 @@ public UndoBarStyle(Parcel source) {
titleRes = source.readInt();
bgRes = source.readInt();
duration = source.readLong();
+ countDownFormatter = source.readParcelable(getClass().getClassLoader());
}
@Override
@@ -110,6 +126,11 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(titleRes);
dest.writeInt(bgRes);
dest.writeLong(duration);
+ if (countDownFormatter != null && countDownFormatter instanceof Parcelable) {
+ dest.writeParcelable((Parcelable) countDownFormatter, flags);
+ } else {
+ dest.writeParcelable(null, flags);
+ }
}
@Override
@@ -118,10 +139,12 @@ public int describeContents() {
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
+ @Override
public UndoBarStyle createFromParcel(Parcel source) {
return new UndoBarStyle(source);
}
+ @Override
public UndoBarStyle[] newArray(int size) {
return new UndoBarStyle[size];
}