mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-08 05:24:39 +01:00
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.
This commit is contained in:
parent
fc0480376b
commit
f28c21206c
3 changed files with 44 additions and 10 deletions
|
@ -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<DelegateBase>(this) );
|
||||
}
|
||||
s_instances.put( clazz, new WeakReference<DelegateBase>(this) );
|
||||
}
|
||||
|
||||
protected void onResume()
|
||||
|
@ -165,7 +167,10 @@ public class DelegateBase implements DlgClickNotify,
|
|||
protected DelegateBase curThis()
|
||||
{
|
||||
DelegateBase result = null;
|
||||
WeakReference<DelegateBase> ref = s_instances.get( getClass() );
|
||||
WeakReference<DelegateBase> 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<DelegateBase> ref : s_instances.values() ) {
|
||||
DelegateBase base = ref.get();
|
||||
if ( null != base ) {
|
||||
result = base.getActivity();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Log.d( TAG, "getHasLooper() => %s", result );
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 );
|
||||
}
|
||||
}
|
||||
} );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -58,6 +58,10 @@ public class FBMService extends FirebaseMessagingService {
|
|||
} else {
|
||||
RelayService.fcmConfirmed( this, true );
|
||||
|
||||
if ( BuildConfig.DEBUG ) {
|
||||
Utils.showToast( this, TAG + ".onMessageReceived()" );
|
||||
}
|
||||
|
||||
Map<String, String> data = message.getData();
|
||||
Log.d( TAG, "onMessageReceived(data=%s)", data );
|
||||
|
||||
|
|
Loading…
Reference in a new issue