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 ;
@@ -116,7 +118,7 @@ public final class DisplayUtils {
116118 private static final int BYTE_SIZE_DIVIDER = 1024 ;
117119 private static final double BYTE_SIZE_DIVIDER_DOUBLE = 1024.0 ;
118120 private static final int DATE_TIME_PARTS_SIZE = 2 ;
119-
121+ private static final Handler mainLooper = new Handler ( Looper . getMainLooper ());
120122 public static final String MONTH_YEAR_PATTERN = "MMMM yyyy" ;
121123 public static final String MONTH_PATTERN = "MMMM" ;
122124 public static final String YEAR_PATTERN = "yyyy" ;
@@ -519,85 +521,104 @@ public static String getData(InputStream inputStream) {
519521 return text .toString ();
520522 }
521523
522- public static Snackbar showSnackMessage (Fragment fragment , @ StringRes int messageResource ) {
524+ // region snackbar
525+ public static void showSnackMessage (Fragment fragment , @ StringRes int messageResource ) {
523526 if (fragment == null ) {
524- return null ;
527+ Log_OC .e (TAG , "snackbar cannot be shown fragment is null" );
528+ return ;
525529 }
526530
527531 final var activity = fragment .getActivity ();
528532 if (activity == null ) {
529- return null ;
533+ Log_OC .e (TAG , "snackbar cannot be shown activity is null" );
534+ return ;
530535 }
531536
532- return showSnackMessage (activity , messageResource );
537+ showSnackMessage (activity , messageResource );
533538 }
534539
535- /**
536- * Show a temporary message in a {@link Snackbar} bound to the content view.
537- *
538- * @param activity The {@link Activity} to which's content view the {@link Snackbar} is bound.
539- * @param messageResource The resource id of the string resource to use. Can be formatted text.
540- * @return The created {@link Snackbar}
541- */
542- public static Snackbar showSnackMessage (Activity activity , @ StringRes int messageResource ) {
543- return showSnackMessage (activity .findViewById (android .R .id .content ), messageResource );
540+ public static void showSnackMessage (Activity activity , @ StringRes int messageResource ) {
541+ if (activity == null ) {
542+ Log_OC .e (TAG , "snackbar cannot be shown activity is null" );
543+ return ;
544+ }
545+
546+ showSnackMessage (activity .findViewById (android .R .id .content ), messageResource );
544547 }
545548
546- /**
547- * Show a temporary message in a {@link Snackbar} bound to the content view.
548- *
549- * @param activity The {@link Activity} to which's content view the {@link Snackbar} is bound.
550- * @param message Message to show.
551- * @return The created {@link Snackbar}
552- */
553- public static Snackbar showSnackMessage (Activity activity , String message ) {
554- final Snackbar snackbar = Snackbar .make (activity .findViewById (android .R .id .content ), message , Snackbar .LENGTH_LONG );
555- var fab = findFABView (activity );
556- if (fab != null && fab .getVisibility () == View .VISIBLE ) {
557- snackbar .setAnchorView (fab );
549+ public static void showSnackMessage (Activity activity , @ StringRes int messageResource , Object ... formatArgs ) {
550+ if (activity == null ) {
551+ Log_OC .e (TAG , "snackbar cannot be shown activity is null" );
552+ return ;
553+ }
554+
555+ showSnackMessage (activity , activity .findViewById (android .R .id .content ), messageResource , formatArgs );
556+ }
557+
558+ public static void showSnackMessage (Context context , View view , @ StringRes int messageResource , Object ... formatArgs ) {
559+ if (context == null || view == null ) {
560+ Log_OC .e (TAG , "snackbar cannot be shown view is null" );
561+ return ;
558562 }
563+
564+ final var snackbar = Snackbar .make (view , String .format (context .getString (messageResource , formatArgs )), Snackbar .LENGTH_LONG );
559565 snackbar .show ();
560- return snackbar ;
561566 }
562567
563- private static View findFABView (Activity activity ) {
564- return activity .findViewById (R .id .fab_main );
568+ public static void showSnackMessage (Activity activity , String message ) {
569+ if (activity == null ) {
570+ Log_OC .e (TAG , "snackbar cannot be shown activity is null" );
571+ return ;
572+ }
573+
574+ activity .runOnUiThread (() -> {
575+ final var snackbar = Snackbar .make (activity .findViewById (android .R .id .content ), message , Snackbar .LENGTH_LONG );
576+ var fab = findFABView (activity );
577+ if (fab != null && fab .getVisibility () == View .VISIBLE ) {
578+ snackbar .setAnchorView (fab );
579+ }
580+ snackbar .show ();
581+ });
565582 }
566583
567- private static View findFABView (View view ) {
568- return view .findViewById (R .id .fab_main );
584+ public static void showSnackMessage (View view , @ StringRes int messageResource ) {
585+ if (view == null ) {
586+ Log_OC .e (TAG , "snackbar cannot be shown view is null" );
587+ return ;
588+ }
589+
590+ mainLooper .post (() -> {
591+ final var snackbar = Snackbar .make (view , messageResource , Snackbar .LENGTH_LONG );
592+ var fab = findFABView (view .getRootView ());
593+ if (fab != null && fab .getVisibility () == View .VISIBLE ) {
594+ snackbar .setAnchorView (fab );
595+ }
596+ snackbar .show ();
597+ });
569598 }
570599
571- /**
572- * Show a temporary message in a {@link Snackbar} bound to the given view.
573- *
574- * @param view The view the {@link Snackbar} is bound to.
575- * @param messageResource The resource id of the string resource to use. Can be formatted text.
576- * @return The created {@link Snackbar}
577- */
578- public static Snackbar showSnackMessage (View view , @ StringRes int messageResource ) {
579- final Snackbar snackbar = Snackbar .make (view , messageResource , Snackbar .LENGTH_LONG );
580- var fab = findFABView (view .getRootView ());
581- if (fab != null && fab .getVisibility () == View .VISIBLE ) {
582- snackbar .setAnchorView (fab );
600+ public static void showSnackMessage (View view , String message ) {
601+ if (view == null ) {
602+ Log_OC .e (TAG , "snackbar cannot be shown view is null" );
603+ return ;
583604 }
584- snackbar .show ();
585- return snackbar ;
605+
606+ mainLooper .post (() -> {
607+ final Snackbar snackbar = Snackbar .make (view , message , Snackbar .LENGTH_LONG );
608+ snackbar .show ();
609+ });
586610 }
611+ // endregion
587612
588- /**
589- * Show a temporary message in a {@link Snackbar} bound to the given view.
590- *
591- * @param view The view the {@link Snackbar} is bound to.
592- * @param message The message.
593- * @return The created {@link Snackbar}
594- */
595- public static Snackbar showSnackMessage (View view , String message ) {
596- final Snackbar snackbar = Snackbar .make (view , message , Snackbar .LENGTH_LONG );
597- snackbar .show ();
598- return snackbar ;
613+ private static View findFABView (Activity activity ) {
614+ return activity .findViewById (R .id .fab_main );
599615 }
600616
617+ private static View findFABView (View view ) {
618+ return view .findViewById (R .id .fab_main );
619+ }
620+
621+
601622 /**
602623 * create a temporary message in a {@link Snackbar} bound to the given view.
603624 *
@@ -609,37 +630,6 @@ public static Snackbar createSnackbar(View view, @StringRes int messageResource,
609630 return Snackbar .make (view , messageResource , length );
610631 }
611632
612- /**
613- * Show a temporary message in a {@link Snackbar} bound to the content view.
614- *
615- * @param activity The {@link Activity} to which's content view the {@link Snackbar} is bound.
616- * @param messageResource The resource id of the string resource to use. Can be formatted text.
617- * @param formatArgs The format arguments that will be used for substitution.
618- * @return The created {@link Snackbar}
619- */
620- public static Snackbar showSnackMessage (Activity activity , @ StringRes int messageResource , Object ... formatArgs ) {
621- return showSnackMessage (activity , activity .findViewById (android .R .id .content ), messageResource , formatArgs );
622- }
623-
624- /**
625- * Show a temporary message in a {@link Snackbar} bound to the content view.
626- *
627- * @param context to load resources.
628- * @param view The content view the {@link Snackbar} is bound to.
629- * @param messageResource The resource id of the string resource to use. Can be formatted text.
630- * @param formatArgs The format arguments that will be used for substitution.
631- * @return The created {@link Snackbar}
632- */
633- public static Snackbar showSnackMessage (Context context , View view , @ StringRes int messageResource , Object ... formatArgs ) {
634- final Snackbar snackbar = Snackbar .make (
635- view ,
636- String .format (context .getString (messageResource , formatArgs )),
637- Snackbar .LENGTH_LONG );
638- snackbar
639- .show ();
640- return snackbar ;
641- }
642-
643633 // Solution inspired by https://stackoverflow.com/questions/34936590/why-isnt-my-vector-drawable-scaling-as-expected
644634 // Copied from https://raw.githubusercontent.com/nextcloud/talk-android/8ec8606bc61878e87e3ac8ad32c8b72d4680013c/app/src/main/java/com/nextcloud/talk/utils/DisplayUtils.java
645635 // under GPL3
0 commit comments