From 236be21a78d977eca8e4f135baf5924018ece1c4 Mon Sep 17 00:00:00 2001 From: Eric House Date: Fri, 1 Feb 2019 16:49:12 -0800 Subject: [PATCH] put up toast when FCM message received It's too useful to live without. So: tap into the static list of live Delegates, and if any of them has an Activity available use it to run Toast on UI thread. Otherwise there will be no display. --- .../org/eehouse/android/xw4/DelegateBase.java | 31 ++++++++++++++++--- .../java/org/eehouse/android/xw4/Utils.java | 19 +++++++++--- .../org/eehouse/android/xw4/FBMService.java | 4 +++ 3 files changed, 44 insertions(+), 10 deletions(-) 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 );