From 5bbfd443338fd07a31e94f9e7f1803356f884281 Mon Sep 17 00:00:00 2001 From: Eric House Date: Wed, 12 Apr 2017 07:37:53 -0700 Subject: [PATCH] try to dismiss duplicate alerts Each alert gets a unique name. Add to backstack, and before doing so look for another with the same name and dismiss it. This works most of the time, especially to prevent them from piling up with orientation changes. But in a robot-vs-robot game, and occasionally in a game with three robots and one not, SCORE alerts stack up. It's the removal that's failing, not the test for a duplicate, so perhaps I need to somehow wait for it to succeed before adding the new alert. --- .../org/eehouse/android/xw4/AboutAlert.java | 4 +++ .../eehouse/android/xw4/BoardDelegate.java | 5 ++- .../java/org/eehouse/android/xw4/DBAlert.java | 24 +++++++++++--- .../org/eehouse/android/xw4/DelegateBase.java | 5 ++- .../eehouse/android/xw4/DlgDelegateAlert.java | 20 ++++++++---- .../java/org/eehouse/android/xw4/DlgID.java | 2 -- .../org/eehouse/android/xw4/LookupAlert.java | 4 +++ .../org/eehouse/android/xw4/MainActivity.java | 31 +++++++++++++------ .../eehouse/android/xw4/TilePickAlert.java | 5 ++- .../org/eehouse/android/xw4/XWActivity.java | 21 +++++++++++-- .../eehouse/android/xw4/XWDialogFragment.java | 4 ++- 11 files changed, 93 insertions(+), 32 deletions(-) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/AboutAlert.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/AboutAlert.java index 279466427..c19ad0198 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/AboutAlert.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/AboutAlert.java @@ -37,6 +37,7 @@ import junit.framework.Assert; import org.eehouse.android.xw4.loc.LocUtils; public class AboutAlert extends XWDialogFragment { + private static final String TAG = AboutAlert.class.getSimpleName(); public static AboutAlert newInstance() { @@ -82,4 +83,7 @@ public class AboutAlert extends XWDialogFragment { .setPositiveButton( android.R.string.ok, null ) .create(); } + + @Override + protected String getFragTag() { return TAG; } } diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BoardDelegate.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BoardDelegate.java index a02beae0b..97dd89415 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BoardDelegate.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BoardDelegate.java @@ -309,9 +309,8 @@ public class BoardDelegate extends DelegateBase int title = (Integer)params[0]; String msg = (String)params[1]; ab.setMessage( msg ); - if ( 0 != title ) { - ab.setTitle( title ); - } + Assert.assertTrue( 0 != title ); + ab.setTitle( title ); ab.setPositiveButton( android.R.string.ok, null ); if ( DlgID.DLG_SCORES == dlgID ) { if ( null != m_mySIS.words && m_mySIS.words.length > 0 ) { diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DBAlert.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DBAlert.java index 4da52eac2..dadc9f79d 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DBAlert.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DBAlert.java @@ -26,6 +26,9 @@ import android.os.Bundle; import android.os.Bundle; import android.os.Handler; import android.support.v4.app.DialogFragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; import org.eehouse.android.xw4.loc.LocUtils; @@ -62,12 +65,23 @@ public class DBAlert extends XWDialogFragment { public DBAlert() {} - public DlgID getDlgID() { return mDlgID; } + public DlgID getDlgID() { + if ( null == mDlgID ) { + mDlgID = DlgID.values()[getArguments().getInt(DLG_ID_KEY, -1)]; + } + return mDlgID; + } + + @Override + public String getFragTag() + { + return getDlgID().toString(); + } @Override public void onSaveInstanceState( Bundle bundle ) { - bundle.putInt( DLG_ID_KEY, mDlgID.ordinal() ); + bundle.putInt( DLG_ID_KEY, getDlgID().ordinal() ); bundle.putSerializable( PARMS_KEY, mParams ); super.onSaveInstanceState( bundle ); } @@ -78,7 +92,6 @@ public class DBAlert extends XWDialogFragment { if ( null == sis ) { sis = getArguments(); } - mDlgID = DlgID.values()[sis.getInt(DLG_ID_KEY, -1)]; mParams = (Object[])sis.getSerializable(PARMS_KEY); XWActivity activity = (XWActivity)getActivity(); @@ -87,7 +100,8 @@ public class DBAlert extends XWDialogFragment { if ( null == dialog ) { dialog = LocUtils.makeAlertBuilder( getActivity() ) .setTitle( "Stub Alert" ) - .setMessage( String.format( "Unable to create for %s", mDlgID.toString() ) ) + .setMessage( String.format( "Unable to create for %s", + getDlgID().toString() ) ) .setPositiveButton( "Bummer", null ) // .setNegativeButton( "Try now", new OnClickListener() { // @Override @@ -103,7 +117,7 @@ public class DBAlert extends XWDialogFragment { public void run() { MainActivity activity = (MainActivity)getActivity(); if ( null != activity ) { - DBAlert newMe = newInstance( mDlgID, mParams ); + DBAlert newMe = newInstance( getDlgID(), mParams ); activity.show( newMe ); dismiss(); // kill myself... } else { 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 a2d18ab8a..cf141743a 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 @@ -438,8 +438,7 @@ public class DelegateBase implements DlgClickNotify, protected void showDialogFragment( DlgID dlgID, Object... params ) { - DialogFragment fragment = DBAlert.newInstance( dlgID, params ); - show( fragment ); + show( DBAlert.newInstance( dlgID, params ) ); } protected void show( DlgState state ) @@ -468,7 +467,7 @@ public class DelegateBase implements DlgClickNotify, show( df ); } - protected void show( DialogFragment df ) + protected void show( XWDialogFragment df ) { if ( m_activity instanceof XWActivity ) { ((XWActivity)m_activity).show( df ); diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DlgDelegateAlert.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DlgDelegateAlert.java index 4414ca569..0c7401047 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DlgDelegateAlert.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DlgDelegateAlert.java @@ -47,12 +47,14 @@ abstract class DlgDelegateAlert extends XWDialogFragment { protected final DlgState getState( Bundle sis ) { - if ( null != sis ) { - m_state = (DlgState)sis.getParcelable( STATE_KEY ); - } else { - Bundle args = getArguments(); - Assert.assertNotNull( args ); - m_state = DlgState.fromBundle( args ); + if ( m_state == null ) { + if ( null != sis ) { + m_state = (DlgState)sis.getParcelable( STATE_KEY ); + } else { + Bundle args = getArguments(); + Assert.assertNotNull( args ); + m_state = DlgState.fromBundle( args ); + } } return m_state; } @@ -80,6 +82,12 @@ abstract class DlgDelegateAlert extends XWDialogFragment { super.onDismiss( dif ); } + @Override + protected String getFragTag() + { + return getState(null).m_id.toString(); + } + protected void checkNotAgainCheck( DlgState state, NotAgainView naView ) { if ( null != naView && naView.getChecked() ) { diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DlgID.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DlgID.java index f1b4d33ae..6c758a3d3 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DlgID.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DlgID.java @@ -22,7 +22,6 @@ package org.eehouse.android.xw4; public enum DlgID { NONE - , LOOKUP , CHANGE_GROUP , CONFIRM_CHANGE , CONFIRM_CHANGE_PLAY @@ -64,7 +63,6 @@ public enum DlgID { , ASK_PASSWORD , DLG_RETRY , DLG_SCORES - , PICK_TILE_REQUESTTRAY , DLG_USEDICT , DLG_GETDICT , GAMES_LIST_NEWGAME diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/LookupAlert.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/LookupAlert.java index abc0a5efc..45df88f55 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/LookupAlert.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/LookupAlert.java @@ -31,6 +31,7 @@ import junit.framework.Assert; import org.eehouse.android.xw4.loc.LocUtils; public class LookupAlert extends XWDialogFragment { + private static final String TAG = LookupAlert.class.getSimpleName(); private LookupAlertView m_view; public static LookupAlert newInstance( String[] words, int lang, boolean noStudy ) @@ -73,4 +74,7 @@ public class LookupAlert extends XWDialogFragment { result.setOnKeyListener( m_view ); return result; } + + @Override + protected String getFragTag() { return TAG; } } diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/MainActivity.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/MainActivity.java index 76e2d0f85..9953b76ec 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/MainActivity.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/MainActivity.java @@ -357,16 +357,29 @@ public class MainActivity extends XWActivity private void logPaneFragments() { - if ( BuildConfig.DEBUG && null != m_root ) { - List pairs = new ArrayList<>(); - int childCount = m_root.getChildCount(); - for ( int ii = 0; ii < childCount; ++ii ) { - View child = m_root.getChildAt( ii ); - String name = findFragment( child ).getClass().getSimpleName(); - String pair = String.format("%d:%s", ii, name ); - pairs.add( pair ); + if ( BuildConfig.DEBUG ) { + List panePairs = new ArrayList<>(); + if ( null != m_root ) { + int childCount = m_root.getChildCount(); + for ( int ii = 0; ii < childCount; ++ii ) { + View child = m_root.getChildAt( ii ); + String name = findFragment( child ).getClass().getSimpleName(); + String pair = String.format("%d:%s", ii, name ); + panePairs.add( pair ); + } } - Log.d( TAG, "logPaneFragments(): %s", TextUtils.join(", ", pairs) ); + + FragmentManager fm = getSupportFragmentManager(); + List fragPairs = new ArrayList<>(); + int fragCount = fm.getBackStackEntryCount(); + for ( int ii = 0; ii < fragCount; ++ii ) { + FragmentManager.BackStackEntry entry = fm.getBackStackEntryAt( ii ); + String name = entry.getName(); + String pair = String.format("%d:%s", ii, name ); + fragPairs.add( pair ); + } + Log.d( TAG, "panes: [%s]; frags: [%s]", TextUtils.join(",", panePairs), + TextUtils.join(",", fragPairs) ); } } diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/TilePickAlert.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/TilePickAlert.java index 272e96845..94fbad4d6 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/TilePickAlert.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/TilePickAlert.java @@ -1,4 +1,4 @@ -/* -*- compile-command: "cd ../../../../../../../../ && ./gradlew installXw4Debug"; -*- */ +/* -*- compile-command: "find-and-gradle.sh insXw4Deb"; -*- */ /* * Copyright 2017 by Eric House (xwords@eehouse.org). All rights reserved. * @@ -39,6 +39,7 @@ import org.eehouse.android.xw4.loc.LocUtils; public class TilePickAlert extends XWDialogFragment implements TilePickView.TilePickListener { + private static final String TAG = TilePickAlert.class.getSimpleName(); private static final String TPS = "TPS"; private static final String ACTION = "ACTION"; private TilePickView m_view; @@ -127,6 +128,8 @@ public class TilePickAlert extends XWDialogFragment return m_dialog; } + protected String getFragTag() { return TAG; } + // TilePickView.TilePickListener interface @Override public void onTilesChanged( int nToPick, int[] newTiles ) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/XWActivity.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/XWActivity.java index d3d9188cc..e388416ff 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/XWActivity.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/XWActivity.java @@ -26,7 +26,10 @@ import android.content.Intent; import android.content.res.Configuration; import android.os.Bundle; import android.support.v4.app.DialogFragment; +import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentTransaction; import android.view.ContextMenu.ContextMenuInfo; import android.view.ContextMenu; import android.view.Menu; @@ -278,9 +281,23 @@ public class XWActivity extends FragmentActivity Assert.fail(); } - protected void show( DialogFragment df ) + protected void show( XWDialogFragment df ) { - df.show( getSupportFragmentManager(), "dialog" ); + FragmentManager fm = getSupportFragmentManager(); + String tag = df.getFragTag(); + Log.d( TAG, "%s.show(%s) called; tag: %s", getClass().getSimpleName(), + df.getClass().getSimpleName(), tag ); + FragmentTransaction trans = fm.beginTransaction(); + Fragment prev = fm.findFragmentByTag( tag ); + if ( null != prev && prev instanceof DialogFragment ) { + Log.d( TAG, "removing %s (tag %s)", + prev.getClass().getSimpleName(), tag ); + ((DialogFragment)prev).dismiss(); + } + trans.addToBackStack( tag ); + + // Create and show the dialog. show() commits the transaction + df.show( trans, tag ); } protected Dialog makeDialog( DBAlert alert, Object[] params ) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/XWDialogFragment.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/XWDialogFragment.java index 50e410fcb..ab5be9fe2 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/XWDialogFragment.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/XWDialogFragment.java @@ -32,7 +32,7 @@ import java.util.Map; import junit.framework.Assert; -public class XWDialogFragment extends DialogFragment { +abstract class XWDialogFragment extends DialogFragment { private static final String TAG = XWDialogFragment.class.getSimpleName(); private OnDismissListener m_onDismiss; @@ -87,6 +87,8 @@ public class XWDialogFragment extends DialogFragment { super.onDismiss( dif ); } + abstract String getFragTag(); + protected void setOnDismissListener( OnDismissListener lstnr ) { Assert.assertTrue( null == lstnr || null == m_onDismiss || !BuildConfig.DEBUG );