diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardDelegate.java index ec2504a87..1f78b1b42 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardDelegate.java @@ -83,7 +83,6 @@ public class BoardDelegate extends DelegateBase private static final String GETDICT = "GETDICT"; private Activity m_activity; - private Delegator m_delegator; private BoardView m_view; private GamePtr m_jniGamePtr; private GameLock m_gameLock; @@ -581,7 +580,6 @@ public class BoardDelegate extends DelegateBase { super( delegator, savedInstanceState, R.layout.board, R.menu.board_menu ); m_activity = delegator.getActivity(); - m_delegator = delegator; } protected void init( Bundle savedInstanceState ) @@ -931,7 +929,7 @@ public class BoardDelegate extends DelegateBase cmd = JNICmd.CMD_TOGGLE_TRAY; break; case R.id.games_menu_study: - StudyListDelegate.launchOrAlert( m_activity, m_gi.dictLang, this ); + StudyListDelegate.launchOrAlert( getDelegator(), m_gi.dictLang, this ); break; case R.id.board_menu_game_netstats: m_jniThread.handle( JNICmd.CMD_NETSTATS, R.string.netstats_title ); @@ -950,7 +948,7 @@ public class BoardDelegate extends DelegateBase // small devices only case R.id.board_menu_dict: String dictName = m_gi.dictName( m_view.getCurPlayer() ); - DictBrowseDelegate.launch( m_activity, dictName ); + DictBrowseDelegate.launch( getDelegator(), dictName ); break; case R.id.board_menu_game_counts: @@ -1029,11 +1027,11 @@ public class BoardDelegate extends DelegateBase String curDict = m_gi.dictName( m_view.getCurPlayer() ); View button = m_toolbar.getViewFor( Toolbar.BUTTON_BROWSE_DICT ); if ( Action.BUTTON_BROWSEALL_ACTION == action && - DictsDelegate.handleDictsPopup( m_activity, button, + DictsDelegate.handleDictsPopup( getDelegator(), button, curDict, m_gi.dictLang ) ){ break; } - DictBrowseDelegate.launch( m_activity, curDict ); + DictBrowseDelegate.launch( getDelegator(), curDict ); break; case PREV_HINT_ACTION: cmd = JNICmd.CMD_PREV_HINT; @@ -1439,7 +1437,7 @@ public class BoardDelegate extends DelegateBase String msg = getString( R.string.reload_new_dict_fmt, getDict ); showToast( msg ); - m_delegator.finish(); + finish(); GameUtils.launchGame( m_activity, m_rowid, false ); } @@ -2174,7 +2172,7 @@ public class BoardDelegate extends DelegateBase } } catch ( GameUtils.NoSuchGameException nsge ) { DbgUtils.loge( nsge ); - m_delegator.finish(); + finish(); } } } // loadGame @@ -2375,7 +2373,7 @@ public class BoardDelegate extends DelegateBase int curPlayer = XwJNI.board_getSelPlayer( m_jniGamePtr ); String[] names = m_gi.playerNames(); boolean[] locs = m_gi.playersLocal(); // to convert old histories - ChatDelegate.startForResult( m_activity, RequestCode.CHAT_REQUEST, + ChatDelegate.startForResult( getDelegator(), RequestCode.CHAT_REQUEST, m_rowid, curPlayer, names, locs ); } } @@ -2656,10 +2654,6 @@ public class BoardDelegate extends DelegateBase private void doRematchIf() { - // Intent intent = GamesListDelegate.makeRematchIntent( m_activity, m_gi, m_rowid ); - // if ( null != intent ) { - // startActivity( intent ); - // m_delegator.finish(); if ( doRematchIf( m_activity, this, m_rowid, m_summary, m_gi, m_jniGamePtr ) ) { finish(); diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/ChatDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/ChatDelegate.java index a9bb61242..b288c1394 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/ChatDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/ChatDelegate.java @@ -204,7 +204,8 @@ public class ChatDelegate extends DelegateBase { } - public static void startForResult( Activity parent, RequestCode requestCode, + public static void startForResult( Delegator delegator, + RequestCode requestCode, long rowID, int curPlayer, String[] names, boolean[] locs ) { @@ -215,12 +216,13 @@ public class ChatDelegate extends DelegateBase { bundle.putStringArray( INTENT_KEY_NAMES, names ); bundle.putBooleanArray( INTENT_KEY_LOCS, locs ); - if ( parent instanceof FragActivity ) { - FragActivity.addFragment( new ChatFrag(), bundle ); + Activity activity = delegator.getActivity(); + if ( activity instanceof FragActivity ) { + FragActivity.addFragment( new ChatFrag(), bundle, delegator ); } else { - Intent intent = new Intent( parent, ChatActivity.class ); + Intent intent = new Intent( activity, ChatActivity.class ); intent.putExtras( bundle ); - parent.startActivityForResult( intent, requestCode.ordinal() ); + activity.startActivityForResult( intent, requestCode.ordinal() ); } } } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DelegateBase.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DelegateBase.java index e14f858d2..14336bb10 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DelegateBase.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DelegateBase.java @@ -147,6 +147,8 @@ public class DelegateBase implements DlgClickNotify, return m_activity.getIntent(); } + protected Delegator getDelegator() { return m_delegator; } + protected int getLayoutID() { return m_layoutID; diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictBrowseDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictBrowseDelegate.java index cc3eb620b..6565bc9c8 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictBrowseDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictBrowseDelegate.java @@ -366,7 +366,7 @@ public class DictBrowseDelegate extends ListDelegateBase finish(); // pop fragment stack before adding new (only it doesn't work) - launch( m_activity, getArguments() ); + launch( getDelegator(), getArguments() ); } } @@ -422,29 +422,31 @@ public class DictBrowseDelegate extends ListDelegateBase m_maxSpinner.setOnItemSelectedListener( this ); } - private static void launch( Context context, Bundle bundle ) + private static void launch( Delegator delegator, Bundle bundle ) { - if ( context instanceof FragActivity ) { - FragActivity.addFragment( new DictBrowseFrag(), bundle ); + Activity activity = delegator.getActivity(); + if ( activity instanceof FragActivity ) { + FragActivity.addFragment( new DictBrowseFrag(), bundle, delegator ); } else { - Intent intent = new Intent( context, DictBrowseActivity.class ); + Intent intent = new Intent( activity, DictBrowseActivity.class ); intent.putExtras( bundle ); - context.startActivity( intent ); + activity.startActivity( intent ); } } - public static void launch( Context caller, String name, + public static void launch( Delegator delegator, String name, DictUtils.DictLoc loc ) { Bundle bundle = new Bundle(); bundle.putString( DICT_NAME, name ); bundle.putInt( DICT_LOC, loc.ordinal() ); - launch( caller, bundle ); + launch( delegator, bundle ); } - public static void launch( Context caller, String name ) + public static void launch( Delegator delegator, String name ) { - DictUtils.DictLoc loc = DictUtils.getDictLoc( caller, name ); - launch( caller, name, loc ); + DictUtils.DictLoc loc + = DictUtils.getDictLoc( delegator.getActivity(), name ); + launch( delegator, name, loc ); } } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsDelegate.java index fb1707a38..c441a3f19 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsDelegate.java @@ -109,13 +109,13 @@ public class DictsDelegate extends ListDelegateBase private String m_noteNone; private static interface SafePopup { - public void doPopup( Context context, View button, + public void doPopup( Delegator dlgtor, View button, String curDict, int lang ); } private static SafePopup s_safePopup = null; private static class SafePopupImpl implements SafePopup { - public void doPopup( final Context context, View button, + public void doPopup( final Delegator dlgtor, View button, String curDict, int lang ) { final HashMap itemData @@ -127,18 +127,19 @@ public class DictsDelegate extends ListDelegateBase { DictAndLoc dal = itemData.get( item ); - DictBrowseDelegate.launch( context, dal.name, + DictBrowseDelegate.launch( dlgtor, dal.name, dal.loc ); return true; } }; + Context context = dlgtor.getActivity(); PopupMenu popup = new PopupMenu( context, button ); Menu menu = popup.getMenu(); // Add at top but save until have dal info MenuItem curItem = - menu.add( LocUtils.getString( context, + menu.add( LocUtils.getString( context, R.string.cur_menu_marker_fmt, curDict ) ); @@ -565,7 +566,7 @@ public class DictsDelegate extends ListDelegateBase switchShowingRemote( m_checkbox.isChecked() ); } else { XWListItem item = (XWListItem)view; - DictBrowseDelegate.launch( m_activity, item.getText(), + DictBrowseDelegate.launch( getDelegator(), item.getText(), (DictLoc)item.getCached() ); } } @@ -1064,9 +1065,10 @@ public class DictsDelegate extends ListDelegateBase new GetDefaultDictTask( context, lc, lstnr ).execute(); } - public static boolean handleDictsPopup( Context context, View button, + public static boolean handleDictsPopup( Delegator delegator, View button, String curDict, int lang ) { + Context context = delegator.getActivity(); int nDicts = DictLangCache.getLangCount( context, lang ); if ( null == s_safePopup && 1 < nDicts ) { int sdkVersion = Integer.valueOf( android.os.Build.VERSION.SDK ); @@ -1077,7 +1079,7 @@ public class DictsDelegate extends ListDelegateBase boolean canHandle = null != s_safePopup && 1 < nDicts; if ( canHandle ) { - s_safePopup.doPopup( context, button, curDict, lang ); + s_safePopup.doPopup( delegator, button, curDict, lang ); } return canHandle; } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/FragActivity.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/FragActivity.java index 9f4028e9c..cd93c22c6 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/FragActivity.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/FragActivity.java @@ -26,6 +26,7 @@ import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentManager.BackStackEntry; import android.support.v4.app.FragmentTransaction; import android.view.View; import android.widget.FrameLayout; @@ -64,7 +65,7 @@ public class FragActivity extends FragmentActivity if ( savedInstanceState == null ) { // In case this activity was started with special instructions from an Intent, // pass the Intent's extras to the fragment as arguments - addFragmentImpl( new GamesListFrag(), getIntent().getExtras() ); + addFragmentImpl( new GamesListFrag(), getIntent().getExtras(), null ); } } @@ -134,27 +135,40 @@ public class FragActivity extends FragmentActivity getSupportFragmentManager().popBackStack(); } - private void addFragmentImpl( Fragment fragment, Bundle bundle ) + private void addFragmentImpl( Fragment fragment, Bundle bundle, + Delegator parent ) { fragment.setArguments( bundle ); - addFragmentImpl( fragment ); + addFragmentImpl( fragment, parent ); } - private void addFragmentImpl( Fragment fragment ) + private void addFragmentImpl( Fragment fragment, Delegator delegator ) { String newName = fragment.getClass().getName(); boolean replace = false; FragmentManager fm = getSupportFragmentManager(); int fragCount = fm.getBackStackEntryCount(); - int contCount = m_root.getChildCount(); - DbgUtils.logf( "fragCount: %d; contCount: %d", fragCount, contCount ); - Assert.assertTrue( fragCount == contCount ); + int containerCount = m_root.getChildCount(); + DbgUtils.logf( "fragCount: %d; containerCount: %d", fragCount, containerCount ); + // Assert.assertTrue( fragCount == containerCount ); - if ( 0 < contCount ) { + // Replace IF we're adding something of the same class at right OR if + // we're adding something with the existing left pane as its parent + // (delegator) + if ( 0 < fragCount ) { FragmentManager.BackStackEntry entry = fm.getBackStackEntryAt( fragCount - 1 ); String curName = entry.getName(); DbgUtils.logf( "name of last entry: %s", curName ); replace = curName.equals( newName ); + + if ( !replace && 1 < fragCount ) { + entry = fm.getBackStackEntryAt( fragCount - 2 ); + curName = entry.getName(); + String delName = delegator.getClass().getName(); + DbgUtils.logf( "comparing %s, %s", curName, delName ); + replace = curName.equals( delName ); + } + if ( replace ) { fm.popBackStack(); } @@ -167,10 +181,10 @@ public class FragActivity extends FragmentActivity cont.setLayoutParams( new LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f) ); int id = --m_nextID; cont.setId( id ); - m_root.addView( cont, replace ? contCount - 1 : contCount ); + m_root.addView( cont, replace ? containerCount - 1 : containerCount ); - if ( !replace && contCount >= m_maxPanes ) { - int indx = contCount - m_maxPanes; + if ( !replace && containerCount >= m_maxPanes ) { + int indx = containerCount - m_maxPanes; View child = m_root.getChildAt( indx ); child.setVisibility( View.GONE ); @@ -205,7 +219,9 @@ public class FragActivity extends FragmentActivity FragmentManager fm = getSupportFragmentManager(); int hidingId = layout.getId(); Fragment frag = fm.findFragmentById( hidingId ); - frag.setMenuVisibility( visible ); + if ( null != frag ) { // hasn't been popped? + frag.setMenuVisibility( visible ); + } } // Walk all Fragment children and if they care notify of change. @@ -250,8 +266,14 @@ public class FragActivity extends FragmentActivity addFragment( new BoardFrag(), args ); } - public static void addFragment( Fragment fragment, Bundle bundle ) + public static void addFragment( Fragment fragment, Bundle bundle ) { - getThis().addFragmentImpl( fragment, bundle ); + addFragment( fragment, bundle, null ); + } + + public static void addFragment( Fragment fragment, Bundle bundle, + Delegator parent ) + { + getThis().addFragmentImpl( fragment, bundle, parent ); } } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesListDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesListDelegate.java index 885edd7f7..ea88b5f4c 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesListDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesListDelegate.java @@ -566,7 +566,6 @@ public class GamesListDelegate extends ListDelegateBase private Activity m_activity; private static GamesListDelegate s_self; - private GamesListDelegator m_delegator; private GameListAdapter m_adapter; private Handler m_handler; private String m_missingDict; @@ -596,7 +595,6 @@ public class GamesListDelegate extends ListDelegateBase public GamesListDelegate( GamesListDelegator delegator, Bundle sis ) { super( delegator, sis, R.layout.game_list, R.menu.games_list_menu ); - m_delegator = delegator; m_activity = delegator.getActivity(); m_launchedGames = new HashSet(); s_self = this; @@ -1490,7 +1488,7 @@ public class GamesListDelegate extends ListDelegateBase break; case R.id.games_menu_study: - StudyListDelegate.launchOrAlert( m_activity, StudyListDelegate.NO_LANG, this ); + StudyListDelegate.launchOrAlert( getDelegator(), StudyListDelegate.NO_LANG, this ); break; case R.id.games_menu_about: diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/StudyListDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/StudyListDelegate.java index ac3b1d3c3..8d594e65c 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/StudyListDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/StudyListDelegate.java @@ -345,10 +345,11 @@ public class StudyListDelegate extends ListDelegateBase setTitleBar(); } - public static void launchOrAlert( Activity activity, int lang, + public static void launchOrAlert( Delegator delegator, int lang, DlgDelegate.HasDlgDelegate dlg ) { String msg = null; + Activity activity = delegator.getActivity(); if ( 0 == DBUtils.studyListLangs( activity ).length ) { msg = LocUtils.getString( activity, R.string.study_no_lists ); } else if ( NO_LANG != lang && @@ -364,7 +365,7 @@ public class StudyListDelegate extends ListDelegateBase if ( activity instanceof FragActivity ) { StudyListFrag frag = new StudyListFrag(); - ((FragActivity)activity).addFragment( frag, bundle ); + ((FragActivity)activity).addFragment( frag, bundle, delegator ); } else { Intent intent = new Intent( activity, StudyListActivity.class ); intent.putExtras( bundle );