3535import android .graphics .drawable .Drawable ;
3636import android .net .Uri ;
3737import android .os .AsyncTask ;
38+ import android .os .Handler ;
39+ import android .os .Looper ;
3840import android .text .Spannable ;
3941import android .text .SpannableStringBuilder ;
4042import android .text .TextUtils ;
@@ -124,6 +126,7 @@ public final class DisplayUtils {
124126 public static final String MONTH_YEAR_PATTERN = "MMMM yyyy" ;
125127 public static final String MONTH_PATTERN = "MMMM" ;
126128 public static final String YEAR_PATTERN = "yyyy" ;
129+ private static final Handler mainLooper = new Handler (Looper .getMainLooper ());
127130 public static final int SVG_SIZE = 512 ;
128131
129132 private static Map <String , String > mimeType2HumanReadable ;
@@ -574,83 +577,69 @@ public static String getData(InputStream inputStream) {
574577 return text .toString ();
575578 }
576579
577- public static Snackbar showSnackMessage (Fragment fragment , @ StringRes int messageResource ) {
580+ // region snackbar
581+ public static void showSnackMessage (Fragment fragment , @ StringRes int messageResource ) {
578582 if (fragment == null ) {
579- return null ;
583+ return ;
580584 }
581585
582586 final var activity = fragment .getActivity ();
583587 if (activity == null ) {
584- return null ;
588+ return ;
585589 }
586590
587- return showSnackMessage (activity , messageResource );
591+ showSnackMessage (activity , messageResource );
588592 }
589593
590- /**
591- * Show a temporary message in a {@link Snackbar} bound to the content view.
592- *
593- * @param activity The {@link Activity} to which's content view the {@link Snackbar} is bound.
594- * @param messageResource The resource id of the string resource to use. Can be formatted text.
595- * @return The created {@link Snackbar}
596- */
597- public static Snackbar showSnackMessage (Activity activity , @ StringRes int messageResource ) {
598- return showSnackMessage (activity .findViewById (android .R .id .content ), messageResource );
594+ public static void showSnackMessage (Activity activity , @ StringRes int messageResource ) {
595+ showSnackMessage (activity .findViewById (android .R .id .content ), messageResource );
599596 }
600597
601- /**
602- * Show a temporary message in a {@link Snackbar} bound to the content view.
603- *
604- * @param activity The {@link Activity} to which's content view the {@link Snackbar} is bound.
605- * @param message Message to show.
606- * @return The created {@link Snackbar}
607- */
608- public static Snackbar showSnackMessage (Activity activity , String message ) {
609- final Snackbar snackbar = Snackbar .make (activity .findViewById (android .R .id .content ), message , Snackbar .LENGTH_LONG );
610- var fab = findFABView (activity );
611- if (fab != null && fab .getVisibility () == View .VISIBLE ) {
612- snackbar .setAnchorView (fab );
613- }
614- snackbar .show ();
615- return snackbar ;
598+ public static void showSnackMessage (Activity activity , @ StringRes int messageResource , Object ... formatArgs ) {
599+ showSnackMessage (activity , activity .findViewById (android .R .id .content ), messageResource , formatArgs );
616600 }
617601
618- private static View findFABView (Activity activity ) {
619- return activity .findViewById (R .id .fab_main );
602+ public static void showSnackMessage (Context context , View view , @ StringRes int messageResource , Object ... formatArgs ) {
603+ final Snackbar snackbar = Snackbar .make (view , String .format (context .getString (messageResource , formatArgs )), Snackbar .LENGTH_LONG );
604+ snackbar .show ();
620605 }
621606
622- private static View findFABView (View view ) {
623- return view .findViewById (R .id .fab_main );
607+ public static void showSnackMessage (Activity activity , String message ) {
608+ activity .runOnUiThread (() -> {
609+ final Snackbar snackbar = Snackbar .make (activity .findViewById (android .R .id .content ), message , Snackbar .LENGTH_LONG );
610+ var fab = findFABView (activity );
611+ if (fab != null && fab .getVisibility () == View .VISIBLE ) {
612+ snackbar .setAnchorView (fab );
613+ }
614+ snackbar .show ();
615+ });
624616 }
625617
626- /**
627- * Show a temporary message in a {@link Snackbar} bound to the given view.
628- *
629- * @param view The view the {@link Snackbar} is bound to.
630- * @param messageResource The resource id of the string resource to use. Can be formatted text.
631- * @return The created {@link Snackbar}
632- */
633- public static Snackbar showSnackMessage (View view , @ StringRes int messageResource ) {
618+ public static void showSnackMessage (View view , @ StringRes int messageResource ) {
634619 final Snackbar snackbar = Snackbar .make (view , messageResource , Snackbar .LENGTH_LONG );
635- var fab = findFABView (view .getRootView ());
636- if (fab != null && fab .getVisibility () == View .VISIBLE ) {
637- snackbar .setAnchorView (fab );
638- }
639- snackbar .show ();
640- return snackbar ;
620+ mainLooper .post (() -> {
621+ var fab = findFABView (view .getRootView ());
622+ if (fab != null && fab .getVisibility () == View .VISIBLE ) {
623+ snackbar .setAnchorView (fab );
624+ }
625+ snackbar .show ();
626+ });
641627 }
642628
643- /**
644- * Show a temporary message in a {@link Snackbar} bound to the given view.
645- *
646- * @param view The view the {@link Snackbar} is bound to.
647- * @param message The message.
648- * @return The created {@link Snackbar}
649- */
650- public static Snackbar showSnackMessage (View view , String message ) {
651- final Snackbar snackbar = Snackbar .make (view , message , Snackbar .LENGTH_LONG );
652- snackbar .show ();
653- return snackbar ;
629+ public static void showSnackMessage (View view , String message ) {
630+ mainLooper .post (() -> {
631+ final Snackbar snackbar = Snackbar .make (view , message , Snackbar .LENGTH_LONG );
632+ snackbar .show ();
633+ });
634+ }
635+ // endregion
636+
637+ private static View findFABView (Activity activity ) {
638+ return activity .findViewById (R .id .fab_main );
639+ }
640+
641+ private static View findFABView (View view ) {
642+ return view .findViewById (R .id .fab_main );
654643 }
655644
656645 /**
@@ -664,36 +653,6 @@ public static Snackbar createSnackbar(View view, @StringRes int messageResource,
664653 return Snackbar .make (view , messageResource , length );
665654 }
666655
667- /**
668- * Show a temporary message in a {@link Snackbar} bound to the content view.
669- *
670- * @param activity The {@link Activity} to which's content view the {@link Snackbar} is bound.
671- * @param messageResource The resource id of the string resource to use. Can be formatted text.
672- * @param formatArgs The format arguments that will be used for substitution.
673- * @return The created {@link Snackbar}
674- */
675- public static Snackbar showSnackMessage (Activity activity , @ StringRes int messageResource , Object ... formatArgs ) {
676- return showSnackMessage (activity , activity .findViewById (android .R .id .content ), messageResource , formatArgs );
677- }
678-
679- /**
680- * Show a temporary message in a {@link Snackbar} bound to the content view.
681- *
682- * @param context to load resources.
683- * @param view The content view the {@link Snackbar} is bound to.
684- * @param messageResource The resource id of the string resource to use. Can be formatted text.
685- * @param formatArgs The format arguments that will be used for substitution.
686- * @return The created {@link Snackbar}
687- */
688- public static Snackbar showSnackMessage (Context context , View view , @ StringRes int messageResource , Object ... formatArgs ) {
689- final Snackbar snackbar = Snackbar .make (
690- view ,
691- String .format (context .getString (messageResource , formatArgs )),
692- Snackbar .LENGTH_LONG );
693- snackbar
694- .show ();
695- return snackbar ;
696- }
697656
698657 // Solution inspired by https://stackoverflow.com/questions/34936590/why-isnt-my-vector-drawable-scaling-as-expected
699658 // Copied from https://raw.githubusercontent.com/nextcloud/talk-android/8ec8606bc61878e87e3ac8ad32c8b72d4680013c/app/src/main/java/com/nextcloud/talk/utils/DisplayUtils.java
0 commit comments