From 6120365ee1e425acbc270b17c5cad621710777a9 Mon Sep 17 00:00:00 2001 From: Manoj Khanna Date: Thu, 19 Mar 2015 19:58:24 +0530 Subject: Fix for Issue #1114 Displays the snackbar above the keyboard on all activities. --- .../keychain/ui/util/Notify.java | 35 ++++++++++++++++++---- 1 file changed, 30 insertions(+), 5 deletions(-) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/Notify.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/Notify.java index 3bc29edb6..9736b5765 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/Notify.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/Notify.java @@ -19,6 +19,11 @@ package org.sufficientlysecure.keychain.ui.util; import android.app.Activity; import android.content.res.Resources; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentManager; +import android.view.View; +import android.view.ViewGroup; import com.nispok.snackbar.Snackbar; import com.nispok.snackbar.Snackbar.SnackbarDuration; @@ -61,11 +66,11 @@ public class Notify { break; } - SnackbarManager.show(bar); + showSnackbar(activity, bar); } - public static Showable createNotify (Activity activity, int resId, int duration, Style style) { + public static Showable createNotify (final Activity activity, int resId, int duration, Style style) { final Snackbar bar = getSnackbar(activity) .text(resId); @@ -90,7 +95,7 @@ public class Notify { return new Showable () { @Override public void show() { - SnackbarManager.show(bar); + showSnackbar(activity, bar); } }; } @@ -104,7 +109,7 @@ public class Notify { return createNotify(activity, msg, duration, style, null, 0); } - public static Showable createNotify(Activity activity, String msg, int duration, Style style, + public static Showable createNotify(final Activity activity, String msg, int duration, Style style, final ActionListener listener, int resIdAction) { final Snackbar bar = getSnackbar(activity) @@ -141,7 +146,7 @@ public class Notify { return new Showable () { @Override public void show() { - SnackbarManager.show(bar); + showSnackbar(activity, bar); } }; @@ -178,6 +183,26 @@ public class Notify { return bar; } + private static void showSnackbar(Activity activity, Snackbar snackbar) { + if (activity instanceof FragmentActivity) { + FragmentManager fragmentManager = ((FragmentActivity) activity).getSupportFragmentManager(); + + int count = fragmentManager.getBackStackEntryCount(); + Fragment fragment = fragmentManager.getFragments().get(count > 0 ? count - 1 : 0); + + if (fragment != null) { + View view = fragment.getView(); + + if (view != null) { + SnackbarManager.show(snackbar, (ViewGroup) view); + return; + } + } + } + + SnackbarManager.show(snackbar); + } + public interface Showable { public void show(); -- cgit v1.2.3 From 4e4b8efd6ea275d5fe2d07c188ab8a2581617ec6 Mon Sep 17 00:00:00 2001 From: Manoj Khanna Date: Fri, 20 Mar 2015 18:23:23 +0530 Subject: Reworked Notify class --- .../keychain/ui/util/Notify.java | 196 ++++++++------------- 1 file changed, 72 insertions(+), 124 deletions(-) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/Notify.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/Notify.java index 9736b5765..4224fb9a2 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/Notify.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/Notify.java @@ -18,10 +18,7 @@ package org.sufficientlysecure.keychain.ui.util; import android.app.Activity; -import android.content.res.Resources; import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentActivity; -import android.support.v4.app.FragmentManager; import android.view.View; import android.view.ViewGroup; @@ -40,135 +37,49 @@ import org.sufficientlysecure.keychain.util.FabContainer; */ public class Notify { - public static enum Style {OK, WARN, INFO, ERROR} + public static enum Style {OK, WARN, ERROR} public static final int LENGTH_INDEFINITE = 0; public static final int LENGTH_LONG = 3500; - /** - * Shows a simple in-layout notification with the CharSequence given as parameter - * @param text Text to show - * @param style Notification styling - */ - public static void showNotify(final Activity activity, CharSequence text, Style style) { - - Snackbar bar = getSnackbar(activity) + public static Showable create(final Activity activity, String text, int duration, Style style, + final ActionListener actionListener, int actionResId) { + final Snackbar snackbar = Snackbar.with(activity) + .type(SnackbarType.MULTI_LINE) .text(text); - switch (style) { - case OK: - break; - case WARN: - bar.textColor(activity.getResources().getColor(R.color.android_orange_light)); - break; - case ERROR: - bar.textColor(activity.getResources().getColor(R.color.android_red_light)); - break; - } - - showSnackbar(activity, bar); - - } - - public static Showable createNotify (final Activity activity, int resId, int duration, Style style) { - final Snackbar bar = getSnackbar(activity) - .text(resId); - if (duration == LENGTH_INDEFINITE) { - bar.duration(SnackbarDuration.LENGTH_INDEFINITE); + snackbar.duration(SnackbarDuration.LENGTH_INDEFINITE); } else { - bar.duration(duration); + snackbar.duration(duration); } switch (style) { case OK: - bar.actionColor(activity.getResources().getColor(R.color.android_green_light)); + snackbar.actionColorResource(R.color.android_green_light); break; - case WARN: - bar.textColor(activity.getResources().getColor(R.color.android_orange_light)); - break; - case ERROR: - bar.textColor(activity.getResources().getColor(R.color.android_red_light)); - break; - } - - return new Showable () { - @Override - public void show() { - showSnackbar(activity, bar); - } - }; - } - - public static Showable createNotify(Activity activity, int resId, int duration, Style style, - final ActionListener listener, int resIdAction) { - return createNotify(activity, activity.getString(resId), duration, style, listener, resIdAction); - } - - public static Showable createNotify(Activity activity, String msg, int duration, Style style) { - return createNotify(activity, msg, duration, style, null, 0); - } - - public static Showable createNotify(final Activity activity, String msg, int duration, Style style, - final ActionListener listener, int resIdAction) { - - final Snackbar bar = getSnackbar(activity) - .text(msg); - - if (listener != null) { - bar.actionLabel(resIdAction); - bar.actionListener(new ActionClickListener() { - @Override - public void onActionClicked(Snackbar snackbar) { - listener.onAction(); - } - }); - } - - if (duration == LENGTH_INDEFINITE) { - bar.duration(SnackbarDuration.LENGTH_INDEFINITE); - } else { - bar.duration(duration); - } - switch (style) { - case OK: - bar.actionColor(activity.getResources().getColor(R.color.android_green_light)); - break; case WARN: - bar.textColor(activity.getResources().getColor(R.color.android_orange_light)); + snackbar.textColorResource(R.color.android_orange_light); break; + case ERROR: - bar.textColor(activity.getResources().getColor(R.color.android_red_light)); + snackbar.textColorResource(R.color.android_red_light); break; } - return new Showable () { - @Override - public void show() { - showSnackbar(activity, bar); - } - }; - - } - - /** - * Shows a simple in-layout notification with the resource text from given id - * @param resId ResourceId of notification text - * @param style Notification styling - * @throws Resources.NotFoundException - */ - public static void showNotify(Activity activity, int resId, Style style) throws Resources.NotFoundException { - showNotify(activity, activity.getResources().getText(resId), style); - } - - private static Snackbar getSnackbar(final Activity activity) { - Snackbar bar = Snackbar.with(activity) - .type(SnackbarType.MULTI_LINE) - .duration(SnackbarDuration.LENGTH_LONG); + if (actionListener != null) { + snackbar.actionLabel(actionResId) + .actionListener(new ActionClickListener() { + @Override + public void onActionClicked(Snackbar snackbar) { + actionListener.onAction(); + } + }); + } if (activity instanceof FabContainer) { - bar.eventListener(new EventListenerAdapter() { + snackbar.eventListener(new EventListenerAdapter() { @Override public void onShow(Snackbar snackbar) { ((FabContainer) activity).fabMoveUp(snackbar.getHeight()); @@ -180,37 +91,74 @@ public class Notify { } }); } - return bar; - } - private static void showSnackbar(Activity activity, Snackbar snackbar) { - if (activity instanceof FragmentActivity) { - FragmentManager fragmentManager = ((FragmentActivity) activity).getSupportFragmentManager(); + return new Showable() { + @Override + public void show() { + SnackbarManager.show(snackbar, activity); + } - int count = fragmentManager.getBackStackEntryCount(); - Fragment fragment = fragmentManager.getFragments().get(count > 0 ? count - 1 : 0); + @Override + public void show(Fragment fragment) { + if (fragment != null) { + View view = fragment.getView(); + + if (view != null && view instanceof ViewGroup) { + SnackbarManager.show(snackbar, (ViewGroup) view); + return; + } + } - if (fragment != null) { - View view = fragment.getView(); + show(); + } - if (view != null) { - SnackbarManager.show(snackbar, (ViewGroup) view); + @Override + public void show(ViewGroup viewGroup) { + if (viewGroup != null) { + SnackbarManager.show(snackbar, viewGroup); return; } + + show(); } - } + }; + } + + public static Showable create(Activity activity, String text, int duration, Style style) { + return create(activity, text, duration, style, null, -1); + } - SnackbarManager.show(snackbar); + public static Showable create(Activity activity, String text, Style style) { + return create(activity, text, LENGTH_LONG, style); + } + + public static Showable create(Activity activity, int textResId, int duration, Style style, + ActionListener actionListener, int actionResId) { + return create(activity, activity.getString(textResId), duration, style, actionListener, actionResId); + } + + public static Showable create(Activity activity, int textResId, int duration, Style style) { + return create(activity, activity.getString(textResId), duration, style); + } + + public static Showable create(Activity activity, int textResId, Style style) { + return create(activity, activity.getString(textResId), style); } public interface Showable { + public void show(); + public void show(Fragment fragment); + + public void show(ViewGroup viewGroup); + } public interface ActionListener { + public void onAction(); } -} \ No newline at end of file +} -- cgit v1.2.3 From 850a3747886064d648e7fe2436eff9531eb6011c Mon Sep 17 00:00:00 2001 From: Manoj Khanna Date: Fri, 20 Mar 2015 19:19:35 +0530 Subject: Added docs for Notify class --- .../java/org/sufficientlysecure/keychain/ui/util/Notify.java | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/Notify.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/Notify.java index 4224fb9a2..3121e02ef 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/Notify.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/Notify.java @@ -147,10 +147,20 @@ public class Notify { public interface Showable { + /** + * Shows the notification on the bottom of the Activity. + */ public void show(); + /** + * Shows the notification on the bottom of the Fragment. + */ public void show(Fragment fragment); + /** + * Shows the notification on the given ViewGroup. + * The viewGroup should be either a RelativeLayout or FrameLayout. + */ public void show(ViewGroup viewGroup); } -- cgit v1.2.3 From 8b4388e1a2d5cb2b7f0524c18ad3383e3375ba7b Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Sun, 22 Mar 2015 16:55:46 +0100 Subject: use top line in snackbar for status indication --- .../keychain/ui/util/Notify.java | 37 ++++++++++++++-------- 1 file changed, 23 insertions(+), 14 deletions(-) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/Notify.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/Notify.java index 3121e02ef..7e07ed818 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/Notify.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/util/Notify.java @@ -37,7 +37,28 @@ import org.sufficientlysecure.keychain.util.FabContainer; */ public class Notify { - public static enum Style {OK, WARN, ERROR} + public static enum Style { + OK, WARN, ERROR; + + public void applyToBar(Snackbar bar) { + + switch (this) { + case OK: + // bar.actionColorResource(R.color.android_green_light); + bar.lineColorResource(R.color.android_green_light); + break; + case WARN: + // bar.textColorResource(R.color.android_orange_light); + bar.lineColorResource(R.color.android_orange_light); + break; + case ERROR: + // bar.textColorResource(R.color.android_red_light); + bar.lineColorResource(R.color.android_red_light); + break; + } + + } + } public static final int LENGTH_INDEFINITE = 0; public static final int LENGTH_LONG = 3500; @@ -54,19 +75,7 @@ public class Notify { snackbar.duration(duration); } - switch (style) { - case OK: - snackbar.actionColorResource(R.color.android_green_light); - break; - - case WARN: - snackbar.textColorResource(R.color.android_orange_light); - break; - - case ERROR: - snackbar.textColorResource(R.color.android_red_light); - break; - } + style.applyToBar(snackbar); if (actionListener != null) { snackbar.actionLabel(actionResId) -- cgit v1.2.3