diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DelegateBase.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DelegateBase.java index 517bcc91b..869a59a5e 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DelegateBase.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DelegateBase.java @@ -141,11 +141,13 @@ public class DelegateBase implements DlgClickNotify, protected void onStart() { - Class clazz = getClass(); - if ( s_instances.containsKey( clazz ) ) { - Log.d( TAG, "onStart(): replacing curThis" ); + synchronized (s_instances) { + Class clazz = getClass(); + if ( s_instances.containsKey( clazz ) ) { + Log.d( TAG, "onStart(): replacing curThis" ); + } + s_instances.put( clazz, new WeakReference(this) ); } - s_instances.put( clazz, new WeakReference(this) ); } protected void onResume() @@ -165,7 +167,10 @@ public class DelegateBase implements DlgClickNotify, protected DelegateBase curThis() { DelegateBase result = null; - WeakReference ref = s_instances.get( getClass() ); + WeakReference ref; + synchronized (s_instances) { + ref = s_instances.get( getClass() ); + } if ( null != ref ) { result = ref.get(); } @@ -777,4 +782,20 @@ public class DelegateBase implements DlgClickNotify, { // Assert.fail(); } + + public static Activity getHasLooper() + { + Activity result = null; + synchronized (s_instances) { + for ( WeakReference ref : s_instances.values() ) { + DelegateBase base = ref.get(); + if ( null != base ) { + result = base.getActivity(); + break; + } + } + } + Log.d( TAG, "getHasLooper() => %s", result ); + return result; + } } diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/Utils.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/Utils.java index 8252bb8f4..72c354dee 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/Utils.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/Utils.java @@ -159,13 +159,22 @@ public class Utils { showToast( context, text ); } - public static void showToast( Context context, String msg ) + public static void showToast( final Context context, + final String msg ) { // Make this safe to call from non-looper threads - try { - Toast.makeText( context, msg, Toast.LENGTH_SHORT).show(); - } catch ( java.lang.RuntimeException re ) { - Log.ex( TAG, re ); + Activity activity = DelegateBase.getHasLooper(); + if ( null != activity ) { + activity.runOnUiThread( new Runnable() { + @Override + public void run() { + try { + Toast.makeText( context, msg, Toast.LENGTH_SHORT).show(); + } catch ( java.lang.RuntimeException re ) { + Log.ex( TAG, re ); + } + } + } ); } } diff --git a/xwords4/android/app/src/xw4/java/org/eehouse/android/xw4/FBMService.java b/xwords4/android/app/src/xw4/java/org/eehouse/android/xw4/FBMService.java index 031352e87..f9fe8f2e8 100644 --- a/xwords4/android/app/src/xw4/java/org/eehouse/android/xw4/FBMService.java +++ b/xwords4/android/app/src/xw4/java/org/eehouse/android/xw4/FBMService.java @@ -58,6 +58,10 @@ public class FBMService extends FirebaseMessagingService { } else { RelayService.fcmConfirmed( this, true ); + if ( BuildConfig.DEBUG ) { + Utils.showToast( this, TAG + ".onMessageReceived()" ); + } + Map data = message.getData(); Log.d( TAG, "onMessageReceived(data=%s)", data );