From 23533c6b0e45507fb46edbf25b03bda92b9cf0ef Mon Sep 17 00:00:00 2001 From: Eric House Date: Sat, 13 Jan 2024 09:04:57 -0800 Subject: [PATCH] combine rematch-name and rematch-order alerts --- .../java/org/eehouse/android/xw4/DBUtils.java | 24 ++-- .../java/org/eehouse/android/xw4/DlgID.java | 1 - .../org/eehouse/android/xw4/GameUtils.java | 25 ++-- .../android/xw4/GamesListDelegate.java | 107 +++--------------- .../android/xw4/RematchConfigView.java | 26 ++++- .../eehouse/android/xw4/jni/GameSummary.java | 1 + .../src/main/res/layout/rematch_config.xml | 41 +++++-- xwords4/android/jni/xwjni.c | 1 + xwords4/common/game.c | 2 +- xwords4/common/game.h | 1 + 10 files changed, 101 insertions(+), 128 deletions(-) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DBUtils.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DBUtils.java index 6ef91463c..badb91b4b 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DBUtils.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DBUtils.java @@ -183,12 +183,12 @@ public class DBUtils { summary.modtime = cursor.getLong(cursor. getColumnIndex(DBHelper.LASTPLAY_TIME)); - int tmp = cursor.getInt(cursor. + int tmpInt = cursor.getInt(cursor. getColumnIndex(DBHelper.GAME_OVER)); - summary.gameOver = tmp != 0; - tmp = cursor.getInt(cursor. + summary.gameOver = tmpInt != 0; + tmpInt = cursor.getInt(cursor. getColumnIndex(DBHelper.QUASHED)); - summary.quashed = tmp != 0; + summary.quashed = tmpInt != 0; summary.lastMoveTime = cursor.getInt(cursor.getColumnIndex(DBHelper.LASTMOVE)); @@ -197,8 +197,9 @@ public class DBUtils { summary.created = cursor .getLong(cursor.getColumnIndex(DBHelper.CREATE_TIME)); - tmp = cursor.getInt( cursor.getColumnIndex( DBHelper.CAN_REMATCH )); - summary.canRematch = 0 != tmp; + tmpInt = cursor.getInt( cursor.getColumnIndex( DBHelper.CAN_REMATCH )); + summary.canRematch = 0 != (1 & tmpInt); + summary.canOfferRO = 0 != (2 & tmpInt); String str = cursor .getString(cursor.getColumnIndex(DBHelper.EXTRAS)); @@ -223,8 +224,8 @@ public class DBUtils { int col = cursor.getColumnIndex( DBHelper.CONTYPE ); if ( 0 <= col ) { - tmp = cursor.getInt( col ); - summary.conTypes = new CommsConnTypeSet( tmp ); + tmpInt = cursor.getInt( col ); + summary.conTypes = new CommsConnTypeSet( tmpInt ); col = cursor.getColumnIndex( DBHelper.SEED ); if ( 0 < col ) { summary.seed = cursor.getInt( col ); @@ -264,8 +265,8 @@ public class DBUtils { } col = cursor.getColumnIndex( DBHelper.SERVERROLE ); - tmp = cursor.getInt( col ); - summary.serverRole = DeviceRole.values()[tmp]; + tmpInt = cursor.getInt( col ); + summary.serverRole = DeviceRole.values()[tmpInt]; } cursor.close(); } @@ -310,7 +311,8 @@ public class DBUtils { values.put( DBHelper.QUASHED, summary.quashed? 1 : 0 ); values.put( DBHelper.LASTMOVE, summary.lastMoveTime ); values.put( DBHelper.NEXTDUPTIMER, summary.dupTimerExpires ); - values.put( DBHelper.CAN_REMATCH, summary.canRematch?1:0 ); + int tmpInt = (summary.canRematch? 1 : 0) | (summary.canOfferRO? 2 : 0); + values.put( DBHelper.CAN_REMATCH, tmpInt ); // Don't overwrite extras! Sometimes this method is called from // JNIThread which has created the summary from common code that 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 5087fe729..9e46cf204 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 @@ -65,7 +65,6 @@ public enum DlgID { GAMES_LIST_NEWGAME, CHANGE_CONN, GAMES_LIST_NAME_REMATCH, - GAMES_LIST_GET_RO, ASK_DUP_PAUSE, CHOOSE_TILES, SHOW_TILES, diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GameUtils.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GameUtils.java index ba4f0b113..c44c5b97d 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GameUtils.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GameUtils.java @@ -584,7 +584,7 @@ public class GameUtils { public static long makeRematch( Context context, long srcRowid, long groupID, String gameName, - NeedRematchOrder nro ) + RematchOrder ro ) { long rowid = DBUtils.ROWID_NOTFOUND; try ( GameLock lock = GameLock.tryLockRO( srcRowid ) ) { @@ -592,22 +592,13 @@ public class GameUtils { CurGameInfo gi = new CurGameInfo( context ); try ( GamePtr gamePtr = loadMakeGame( context, gi, lock ) ) { if ( null != gamePtr ) { - RematchOrder ro = RematchOrder.RO_SAME; - if ( XwJNI.server_canOfferRematch( gamePtr ) ) { - ro = XWPrefs.getDefaultRematchOrder( context ); - if ( null == ro ) { - ro = nro.getRematchOrder(); - } - } - if ( null != ro ) { - UtilCtxt util = new UtilCtxtImpl( context ); - CommonPrefs cp = CommonPrefs.get( context ); - try ( GamePtr gamePtrNew = XwJNI - .game_makeRematch( gamePtr, util, cp, gameName, ro ) ) { - if ( null != gamePtrNew ) { - rowid = saveNewGame1( context, gamePtrNew, - groupID, gameName ); - } + UtilCtxt util = new UtilCtxtImpl( context ); + CommonPrefs cp = CommonPrefs.get( context ); + try ( GamePtr gamePtrNew = XwJNI + .game_makeRematch( gamePtr, util, cp, gameName, ro ) ) { + if ( null != gamePtrNew ) { + rowid = saveNewGame1( context, gamePtrNew, + groupID, gameName ); } } } diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GamesListDelegate.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GamesListDelegate.java index 6417c0ca8..02cfa6c73 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GamesListDelegate.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GamesListDelegate.java @@ -875,24 +875,22 @@ public class GamesListDelegate extends ListDelegateBase } break; - case GAMES_LIST_GET_RO: { - NRO nro = (NRO)params[0]; - dialog = mkRematchConfigDlg( nro ); - } - break; - case GAMES_LIST_NAME_REMATCH: { - final LinearLayout view = (LinearLayout) - LocUtils.inflate( m_activity, R.layout.msg_label_and_edit ); + final RematchConfigView view = (RematchConfigView) + LocUtils.inflate( m_activity, R.layout.rematch_config ); + int iconResID = R.drawable.ic_sologame; if ( null != m_rematchExtras ) { - EditWClear edit = (EditWClear)view.findViewById( R.id.edit ); - edit.setText( m_rematchExtras.getString( REMATCH_NEWNAME_EXTRA )); + long rowid = m_rematchExtras.getLong( REMATCH_ROWID_EXTRA, + ROWID_NOTFOUND ); + GameSummary summary = GameUtils.getSummary( m_activity, rowid ); + view.setName( m_rematchExtras.getString( REMATCH_NEWNAME_EXTRA ) ) + .setCanOfferRO( summary.canOfferRO ); + solo = m_rematchExtras.getBoolean( REMATCH_IS_SOLO, true ); if ( !solo ) { iconResID = R.drawable.ic_multigame; } - view.findViewById( R.id.msg ).setVisibility( View.GONE ); } dialog = makeAlertBuilder() @@ -902,12 +900,10 @@ public class GamesListDelegate extends ListDelegateBase .setPositiveButton( android.R.string.ok, new OnClickListener() { @Override public void onClick( DialogInterface dlg, int item ) { - EditWClear edit = (EditWClear)((Dialog)dlg) - .findViewById( R.id.edit ); - String gameName = edit.getText().toString(); - startRematchWithName( gameName, true ); + startRematchWithName( view.getName(), view.getRO(), true ); } } ) + .setNegativeButton( android.R.string.cancel, null ) .create(); } break; @@ -1584,30 +1580,6 @@ public class GamesListDelegate extends ListDelegateBase return handled || super.onDismissed( action, params ); } - private Dialog mkRematchConfigDlg( NRO nro ) - { - final RematchConfigView view = (RematchConfigView) - LocUtils.inflate( m_activity, R.layout.rematch_config ); - - int iconResID = nro.isSolo() - ? R.drawable.ic_sologame : R.drawable.ic_multigame; - AlertDialog.Builder ab = makeAlertBuilder() - .setView( view ) - .setIcon( iconResID ) - .setTitle( R.string.button_rematch ) - .setNegativeButton( android.R.string.cancel, null ) - .setPositiveButton( android.R.string.ok, - new OnClickListener() { - @Override - public void onClick( DialogInterface dlg, int ii ) { - RematchOrder ro = view.onOkClicked(); - nro.rerun( ro ); - } - } ) - ; - return ab.create(); - } - private Dialog mkLoadStoreDlg( final Uri uri ) { final BackupConfigView view = (BackupConfigView) @@ -2712,6 +2684,7 @@ public class GamesListDelegate extends ListDelegateBase } private void startRematchWithName( final String gameName, + final RematchOrder ro, boolean showRationale ) { if ( null != gameName && 0 < gameName.length() ) { @@ -2721,7 +2694,7 @@ public class GamesListDelegate extends ListDelegateBase final CommsConnTypeSet addrs = new CommsConnTypeSet( bits ); boolean hasSMS = addrs.contains( CommsConnType.COMMS_CONN_SMS ); if ( !hasSMS || null != SMSPhoneInfo.get( m_activity ) ) { - rematchWithNameAndPerm( gameName, addrs ); + rematchWithNameAndPerm( gameName, ro, addrs ); } else { int id = (1 == addrs.size()) ? R.string.phone_lookup_rationale_drop @@ -2729,7 +2702,7 @@ public class GamesListDelegate extends ListDelegateBase String msg = getString( R.string.phone_lookup_rationale ) + "\n\n" + getString( id ); Perms23.tryGetPerms( this, Perms23.NBS_PERMS, msg, - Action.ASKED_PHONE_STATE, gameName, addrs ); + Action.ASKED_PHONE_STATE, gameName, ro, addrs ); } } } @@ -2741,56 +2714,12 @@ public class GamesListDelegate extends ListDelegateBase addrs.remove( CommsConnType.COMMS_CONN_SMS ); } if ( 0 < addrs.size() ) { - rematchWithNameAndPerm( (String)params[0], addrs ); + rematchWithNameAndPerm( (String)params[0], (RematchOrder)params[1], addrs ); } } - private class NRO implements Serializable, GameUtils.NeedRematchOrder { - private RematchOrder mChosenOrder = null; - private Bundle mExtras; - private String mGameName; - private CommsConnTypeSet mAddrs; - - NRO( Bundle extras, String gameName, CommsConnTypeSet addrs ) - { - mExtras = extras; - mGameName = gameName; - mAddrs = addrs; - } - - @Override - public RematchOrder getRematchOrder() - { - RematchOrder result = mChosenOrder; - if ( null == result ) { - showDialogFragment( DlgID.GAMES_LIST_GET_RO, this ); - } - return result; - } - - boolean isSolo() { return mExtras.getBoolean( REMATCH_IS_SOLO, true ); } - - void rerun( RematchOrder ro ) - { - mChosenOrder = ro; - m_rematchExtras = mExtras; - runOnUiThread( new Runnable() { - @Override - public void run() { - rematchWithNameAndPerm( mGameName, mAddrs, NRO.this ); - } - } ); - } - } // class NRO - - private void rematchWithNameAndPerm( String gameName, CommsConnTypeSet addrs ) - { - NRO nro = new NRO( m_rematchExtras, gameName, addrs ); - rematchWithNameAndPerm( gameName, addrs, nro ); - } - - private void rematchWithNameAndPerm( String gameName, CommsConnTypeSet addrs, - NRO nro ) + private void rematchWithNameAndPerm( String gameName, RematchOrder ro, + CommsConnTypeSet addrs ) { if ( null != gameName && 0 < gameName.length() ) { Bundle extras = m_rematchExtras; @@ -2800,7 +2729,7 @@ public class GamesListDelegate extends ListDelegateBase DBUtils.GROUPID_UNSPEC ); long newid = GameUtils.makeRematch( m_activity, srcRowID, - groupID, gameName, nro ); + groupID, gameName, ro ); if ( DBUtils.ROWID_NOTFOUND != newid ) { if ( extras.getBoolean( REMATCH_DELAFTER_EXTRA, false ) ) { diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/RematchConfigView.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/RematchConfigView.java index 3c82c2066..4aba1ece5 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/RematchConfigView.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/RematchConfigView.java @@ -1,6 +1,7 @@ /* -*- compile-command: "find-and-gradle.sh inXw4dDeb"; -*- */ /* - * Copyright 2023 by Eric House (xwords@eehouse.org). All rights reserved. + * Copyright 2023 - 2024 by Eric House (xwords@eehouse.org). All rights + * reserved. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -50,6 +51,27 @@ public class RematchConfigView extends LinearLayout mContext = cx; } + public RematchConfigView setName( String name ) + { + EditWClear ewc = (EditWClear)findViewById( R.id.name ); + ewc.setText( name ); + return this; + } + + public String getName() + { + EditWClear ewc = (EditWClear)findViewById( R.id.name ); + return ewc.getText().toString(); + } + + public RematchConfigView setCanOfferRO( boolean canOfferRO ) + { + if ( !canOfferRO ) { + findViewById( R.id.ro_stuff ).setVisibility( View.GONE ); + } + return this; + } + @Override protected void onFinishInflate() { @@ -69,7 +91,7 @@ public class RematchConfigView extends LinearLayout } } - public RematchOrder onOkClicked() + public RematchOrder getRO() { int id = mGroup.getCheckedRadioButtonId(); RematchOrder ro = mRos.get(id); diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/GameSummary.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/GameSummary.java index 53794091f..a214976fb 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/GameSummary.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/GameSummary.java @@ -85,6 +85,7 @@ public class GameSummary implements Serializable { public DeviceRole serverRole; public int nPacketsPending; public boolean canRematch; + public boolean canOfferRO; private Integer m_giFlags; private String m_playersSummary; diff --git a/xwords4/android/app/src/main/res/layout/rematch_config.xml b/xwords4/android/app/src/main/res/layout/rematch_config.xml index 39a7edc87..1c5bc1c3e 100644 --- a/xwords4/android/app/src/main/res/layout/rematch_config.xml +++ b/xwords4/android/app/src/main/res/layout/rematch_config.xml @@ -8,17 +8,44 @@ android:padding="8dp" > - + - + + + + + + + diff --git a/xwords4/android/jni/xwjni.c b/xwords4/android/jni/xwjni.c index bba4aa9c9..9f27af545 100644 --- a/xwords4/android/jni/xwjni.c +++ b/xwords4/android/jni/xwjni.c @@ -2319,6 +2319,7 @@ Java_org_eehouse_android_xw4_jni_XwJNI_game_1summarize setInt( env, jsummary, "turn", summary.turn ); setBool( env, jsummary, "turnIsLocal", summary.turnIsLocal ); setBool( env, jsummary, "canRematch", summary.canRematch ); + setBool( env, jsummary, "canOfferRO", summary.canOfferRO ); setInt( env, jsummary, "lastMoveTime", summary.lastMoveTime ); setInt( env, jsummary, "dupTimerExpires", summary.dupTimerExpires ); diff --git a/xwords4/common/game.c b/xwords4/common/game.c index e5df0f415..28b0f57e5 100644 --- a/xwords4/common/game.c +++ b/xwords4/common/game.c @@ -534,7 +534,7 @@ game_summarize( const XWGame* game, const CurGameInfo* gi, GameSummary* summary summary->gameOver = server_getGameIsOver( server ); summary->nMoves = model_getNMoves( game->model ); summary->dupTimerExpires = server_getDupTimerExpires( server ); - summary->canRematch = server_canRematch( server, NULL ); + summary->canRematch = server_canRematch( server, &summary->canOfferRO ); for ( int ii = 0; ii < gi->nPlayers; ++ii ) { const LocalPlayer* lp = &gi->players[ii]; diff --git a/xwords4/common/game.h b/xwords4/common/game.h index cf85c0b6f..a31c61b13 100644 --- a/xwords4/common/game.h +++ b/xwords4/common/game.h @@ -56,6 +56,7 @@ typedef struct _GameSummary { XP_Bool gameOver; XP_Bool quashed; XP_Bool canRematch; + XP_Bool canOfferRO; XP_S8 turn; XP_U32 lastMoveTime; XP_S32 dupTimerExpires;