From 4893141b7f53e7083f9d22933c5ba03e8e0378c8 Mon Sep 17 00:00:00 2001 From: Samuel Tardieu Date: Fri, 25 Jul 2014 12:26:12 +0200 Subject: Allow toasts to be shown from any thread --- .../cgeo/geocaching/activity/ActivityMixin.java | 43 ++++++++++++++++------ 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/main/src/cgeo/geocaching/activity/ActivityMixin.java b/main/src/cgeo/geocaching/activity/ActivityMixin.java index 769082d..b58d3ae 100644 --- a/main/src/cgeo/geocaching/activity/ActivityMixin.java +++ b/main/src/cgeo/geocaching/activity/ActivityMixin.java @@ -60,26 +60,47 @@ public final class ActivityMixin { return R.style.popup_dark; } + /** + * Show a long toast message to the user. This can be called from any thread. + * + * @param activity the activity the user is facing + * @param resId the message + */ public static void showToast(final Activity activity, final int resId) { ActivityMixin.showToast(activity, activity.getString(resId)); } - public static void showToast(final Activity activity, final String text) { + private static void postShowToast(final Activity activity, final String text, final int toastDuration) { if (StringUtils.isNotBlank(text)) { - Toast toast = Toast.makeText(activity, text, Toast.LENGTH_LONG); - - toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 100); - toast.show(); + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + final Toast toast = Toast.makeText(activity, text, toastDuration); + toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 100); + toast.show(); + } + }); } } - public static void showShortToast(final Activity activity, final String text) { - if (StringUtils.isNotBlank(text)) { - Toast toast = Toast.makeText(activity, text, Toast.LENGTH_SHORT); + /** + * Show a long toast message to the user. This can be called from any thread. + * + * @param activity the activity the user is facing + * @param text the message + */ + public static void showToast(final Activity activity, final String text) { + postShowToast(activity, text, Toast.LENGTH_LONG); + } - toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 100); - toast.show(); - } + /** + * Show a short toast message to the user. This can be called from any thread. + * + * @param activity the activity the user is facing + * @param text the message + */ + public static void showShortToast(final Activity activity, final String text) { + postShowToast(activity, text, Toast.LENGTH_SHORT); } public static void keepScreenOn(final Activity abstractActivity, boolean keepScreenOn) { -- cgit v1.1