From 38898776bd079f32e506b6954994d1f552fa170e Mon Sep 17 00:00:00 2001 From: Eric House Date: Mon, 18 Jan 2016 07:41:47 -0800 Subject: [PATCH] when re-inviting, pre-populate BT and SMS invite dialogs from the most recent invitation of that "means" recorded for that game --- .../android/XWords4/res/values/strings.xml | 2 +- .../XWords4/res_src/values-ba_CK/strings.xml | 2 +- .../XWords4/res_src/values-ca_PS/strings.xml | 2 +- .../eehouse/android/xw4/BTInviteDelegate.java | 12 +++++- .../eehouse/android/xw4/BoardDelegate.java | 15 +++---- .../src/org/eehouse/android/xw4/DBUtils.java | 41 +++++++++++++------ .../org/eehouse/android/xw4/DelegateBase.java | 5 ++- .../org/eehouse/android/xw4/DlgDelegate.java | 36 +++++++++++----- .../src/org/eehouse/android/xw4/DlgState.java | 5 +++ .../eehouse/android/xw4/InviteDelegate.java | 3 ++ .../android/xw4/SMSInviteDelegate.java | 13 ++++-- 11 files changed, 97 insertions(+), 39 deletions(-) diff --git a/xwords4/android/XWords4/res/values/strings.xml b/xwords4/android/XWords4/res/values/strings.xml index 4841f7ae5..c76ee3622 100644 --- a/xwords4/android/XWords4/res/values/strings.xml +++ b/xwords4/android/XWords4/res/values/strings.xml @@ -586,7 +586,7 @@ Invite sent via SMS to phone number %1$s on %2$s Invite sent via Bluetooth to - paired device %1$s on %2$s + paired device \"%1$s\" on %2$s Invite forwarded by the relay to another device on %1$s Invite sent via %1$s on diff --git a/xwords4/android/XWords4/res_src/values-ba_CK/strings.xml b/xwords4/android/XWords4/res_src/values-ba_CK/strings.xml index a5e871cb2..b9277fd92 100644 --- a/xwords4/android/XWords4/res_src/values-ba_CK/strings.xml +++ b/xwords4/android/XWords4/res_src/values-ba_CK/strings.xml @@ -500,7 +500,7 @@ Etivni tnes aiv SMS ot enohp rebmun %1$s no %2$s Etivni tnes aiv Htooteulb ot - deriap ecived %1$s no %2$s + deriap ecived \"%1$s\" no %2$s Etivni dedrawrof yb eht yaler ot rehtona ecived no %1$s Etivni tnes aiv %1$s no diff --git a/xwords4/android/XWords4/res_src/values-ca_PS/strings.xml b/xwords4/android/XWords4/res_src/values-ca_PS/strings.xml index b7200d8e2..914682cbf 100644 --- a/xwords4/android/XWords4/res_src/values-ca_PS/strings.xml +++ b/xwords4/android/XWords4/res_src/values-ca_PS/strings.xml @@ -500,7 +500,7 @@ INVITE SENT VIA SMS TO PHONE NUMBER %1$s ON %2$s INVITE SENT VIA BLUETOOTH TO - PAIRED DEVICE %1$s ON %2$s + PAIRED DEVICE \"%1$s\" ON %2$s INVITE FORWARDED BY THE RELAY TO ANOTHER DEVICE ON %1$s INVITE SENT VIA %1$s ON diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTInviteDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTInviteDelegate.java index 16337eb9a..26869a062 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTInviteDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTInviteDelegate.java @@ -44,6 +44,8 @@ import java.util.Iterator; import junit.framework.Assert; import org.eehouse.android.xw4.DlgDelegate.Action; +import org.eehouse.android.xw4.DBUtils.SentInvitesInfo; +import org.eehouse.android.xw4.DlgDelegate.DlgClickNotify.InviteMeans; public class BTInviteDelegate extends InviteDelegate { @@ -53,12 +55,17 @@ public class BTInviteDelegate extends InviteDelegate { private boolean m_setChecked; private BTDevsAdapter m_adapter; - public static void launchForResult( Activity activity, int nMissing, + public static void launchForResult( Activity activity, int nMissing, + SentInvitesInfo info, RequestCode requestCode ) { Assert.assertTrue( 0 < nMissing ); // don't call if nMissing == 0 Intent intent = new Intent( activity, BTInviteActivity.class ); intent.putExtra( INTENT_KEY_NMISSING, nMissing ); + if ( null != info ) { + String lastDev = info.getLastDev( InviteMeans.BLUETOOTH ); + intent.putExtra( INTENT_KEY_LASTDEV, lastDev ); + } activity.startActivityForResult( intent, requestCode.ordinal() ); } @@ -239,6 +246,9 @@ public class BTInviteDelegate extends InviteDelegate { if ( m_setChecked || m_checked.contains( btAddr ) ) { box.setChecked( true ); + } else if ( null != m_lastDev && m_lastDev.equals( btAddr ) ) { + m_lastDev = null; + box.setChecked( true ); } return layout; } 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 065609c12..4dada4fa6 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardDelegate.java @@ -438,7 +438,8 @@ public class BoardDelegate extends DelegateBase if ( m_summary.hasRematchInfo() ) { tryRematchInvites( true ); } else { - showInviteChoicesThen( Action.LAUNCH_INVITE_ACTION ); + showInviteChoicesThen( Action.LAUNCH_INVITE_ACTION, + m_sentInfo ); } } else { askDropRelay(); @@ -1083,6 +1084,8 @@ public class BoardDelegate extends DelegateBase Object[] params ) { if ( action == Action.LAUNCH_INVITE_ACTION ) { + SentInvitesInfo info = params[0] instanceof SentInvitesInfo + ? (SentInvitesInfo)params[0] : null; switch( means ) { case NFC: if ( XWPrefs.getNFCToSelfEnabled( m_activity ) ) { @@ -1094,11 +1097,11 @@ public class BoardDelegate extends DelegateBase } break; case BLUETOOTH: - BTInviteDelegate.launchForResult( m_activity, m_nMissing, + BTInviteDelegate.launchForResult( m_activity, m_nMissing, info, RequestCode.BT_INVITE_RESULT ); break; case SMS: - SMSInviteDelegate.launchForResult( m_activity, m_nMissing, + SMSInviteDelegate.launchForResult( m_activity, m_nMissing, info, RequestCode.SMS_INVITE_RESULT ); break; case RELAY: @@ -2422,7 +2425,6 @@ public class BoardDelegate extends DelegateBase m_invitesPending = m_missingDevs.length; for ( int ii = 0; ii < m_missingDevs.length; ++ii ) { String dev = m_missingDevs[ii]; - String devName = dev; int nPlayers = m_missingCounts[ii]; Assert.assertTrue( 0 <= m_nGuestDevs ); int forceChannel = ii + m_nGuestDevs + 1; @@ -2436,8 +2438,7 @@ public class BoardDelegate extends DelegateBase case BLUETOOTH: if ( ! m_progressShown ) { m_progressShown = true; - devName = BTService.nameForAddr( dev ); - String progMsg = devName; + String progMsg = BTService.nameForAddr( dev ); progMsg = getString( R.string.invite_progress_fmt, progMsg ); startProgress( R.string.invite_progress_title, progMsg, new OnCancelListener() { @@ -2463,7 +2464,7 @@ public class BoardDelegate extends DelegateBase break; } - recordInviteSent( m_missingMeans, devName ); + recordInviteSent( m_missingMeans, dev ); } m_missingDevs = null; m_missingCounts = null; diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java index 0937134a7..9260eeaee 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java @@ -423,25 +423,41 @@ public class DBUtils { public static class SentInvitesInfo { public long m_rowid; private ArrayList m_means; - private ArrayList m_target; - private ArrayList m_timestamp; + private ArrayList m_targets; + private ArrayList m_timestamps; private int m_cachedCount = 0; private SentInvitesInfo( long rowID ) { m_rowid = rowID; m_means = new ArrayList(); - m_target = new ArrayList(); - m_timestamp = new ArrayList(); + m_targets = new ArrayList(); + m_timestamps = new ArrayList(); } private void addEntry( InviteMeans means, String target, Timestamp ts ) { m_means.add( means ); - m_target.add( target ); - m_timestamp.add( ts ); + m_targets.add( target ); + m_timestamps.add( ts ); m_cachedCount = -1; } + public InviteMeans getLastMeans() + { + return 0 < m_means.size() ? m_means.get(0) : null; + } + + public String getLastDev( InviteMeans means ) + { + String result = null; + for ( int ii = 0; null == result && ii < m_means.size(); ++ii ) { + if ( means == m_means.get( ii ) ) { + result = m_targets.get( ii ); + } + } + return result; + } + // There will be lots of duplicates, but we can't detect them all. BUT // if means and target are the same it's definitely a dup. So count // them all and return the largest number we have. 99% of the time we @@ -449,7 +465,7 @@ public class DBUtils { public int getMinPlayerCount() { if ( -1 == m_cachedCount ) { DbgUtils.logf( "getMinPlayerCount(%H)", this ); - int count = m_timestamp.size(); + int count = m_timestamps.size(); Map> hashes = new HashMap>(); int fakeCount = 0; // make all null-targets count for one @@ -462,7 +478,7 @@ public class DBUtils { hashes.put( means, devs ); } devs = hashes.get( means ); - String target = m_target.get( ii ); + String target = m_targets.get( ii ); if ( null == target ) { target = String.format( "%d", ++fakeCount ); } @@ -487,12 +503,12 @@ public class DBUtils { public String getAsText( Context context ) { - int count = m_timestamp.size(); + int count = m_timestamps.size(); String[] strs = new String[count]; for ( int ii = 0; ii < count; ++ii ) { InviteMeans means = m_means.get(ii); - String target = m_target.get(ii); - String timestamp = m_timestamp.get(ii).toString(); + String target = m_targets.get(ii); + String timestamp = m_timestamps.get(ii).toString(); String msg; switch ( means ) { @@ -501,8 +517,9 @@ public class DBUtils { target, timestamp ); break; case BLUETOOTH: + String devName = BTService.nameForAddr( target ); msg = LocUtils.getString( context, R.string.invit_expl_bt_fmt, - target, timestamp ); + devName, timestamp ); break; case RELAY: msg = LocUtils.getString( context, R.string.invit_expl_relay_fmt, 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 04957cc80..f1deb5acf 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DelegateBase.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DelegateBase.java @@ -465,9 +465,10 @@ public class DelegateBase implements DlgClickNotify, m_dlgDelegate.launchLookup( words, lang, !studyOn ); } - protected void showInviteChoicesThen( Action action ) + protected void showInviteChoicesThen( Action action, + DBUtils.SentInvitesInfo info ) { - m_dlgDelegate.showInviteChoicesThen( action ); + m_dlgDelegate.showInviteChoicesThen( action, info ); } protected void showOKOnlyDialogThen( String msg, Action action ) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgDelegate.java index b65da7b35..847264bce 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgDelegate.java @@ -47,6 +47,7 @@ import java.util.Iterator; import junit.framework.Assert; import org.eehouse.android.xw4.loc.LocUtils; +import org.eehouse.android.xw4.DBUtils.SentInvitesInfo; public class DlgDelegate { @@ -391,13 +392,14 @@ public class DlgDelegate { showDialog( DlgID.CONFIRM_THEN ); } - public void showInviteChoicesThen( final Action action ) + public void showInviteChoicesThen( final Action action, + SentInvitesInfo info ) { if ( (XWApp.SMS_INVITE_ENABLED && Utils.deviceSupportsSMS( m_activity )) || XWPrefs.getNFCToSelfEnabled( m_activity ) || NFCUtils.nfcAvail( m_activity )[0] || BTService.BTAvailable() ) { - DlgState state = new DlgState( DlgID.INVITE_CHOICES_THEN, action ); + DlgState state = new DlgState( DlgID.INVITE_CHOICES_THEN, action, info ); addState( state ); showDialog( DlgID.INVITE_CHOICES_THEN ); } else { @@ -616,6 +618,12 @@ public class DlgDelegate { final ArrayList means = new ArrayList(); ArrayList items = new ArrayList(); + DlgClickNotify.InviteMeans lastMeans = null; + if ( null != state.m_params + && state.m_params[0] instanceof SentInvitesInfo ) { + lastMeans =((SentInvitesInfo)state.m_params[0]).getLastMeans(); + } + if ( XWApp.SMS_INVITE_ENABLED && Utils.deviceSupportsSMS(m_activity) ) { items.add( getString( R.string.invite_choice_sms ) ); means.add( DlgClickNotify.InviteMeans.SMS ); @@ -640,22 +648,27 @@ public class DlgDelegate { means.add( DlgClickNotify.InviteMeans.CLIPBOARD ); final int[] sel = { -1 }; + if ( null != lastMeans ) { + for ( int ii = 0; ii < means.size(); ++ii ) { + if ( lastMeans == means.get(ii) ) { + sel[0] = ii; + break; + } + } + } + OnClickListener selChanged = new OnClickListener() { public void onClick( DialogInterface dlg, int view ) { - // First time through, enable the button - if ( -1 == sel[0] ) { - ((AlertDialog)dlg) - .getButton( AlertDialog.BUTTON_POSITIVE ) - .setEnabled( true ); - } sel[0] = view; - if ( view == clipPos ) { String msg = getString( R.string.not_again_clip_expl_fmt, getString(R.string.slmenu_copy_sel) ); showNotAgainDlgThen( msg, R.string.key_na_clip_expl ); } + Button button = ((AlertDialog)dlg) + .getButton( AlertDialog.BUTTON_POSITIVE ); + button.setEnabled( true ); } }; OnClickListener okClicked = new OnClickListener() { @@ -672,7 +685,7 @@ public class DlgDelegate { AlertDialog.Builder builder = LocUtils.makeAlertBuilder( m_activity ) .setTitle( R.string.invite_choice_title ) - .setSingleChoiceItems( items.toArray( new String[items.size()] ), + .setSingleChoiceItems( items.toArray( new String[items.size()] ), sel[0], selChanged ) .setPositiveButton( android.R.string.ok, okClicked ) .setNegativeButton( android.R.string.cancel, null ); @@ -685,7 +698,8 @@ public class DlgDelegate { AlertDialog ad = (AlertDialog)dialog; Button button = ad.getButton( AlertDialog.BUTTON_POSITIVE ); if ( null != button ) { - button.setEnabled( false ); + long[] ids = ad.getListView().getCheckedItemIds(); + button.setEnabled( 1 == ids.length ); } } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgState.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgState.java index 7f7ca2e06..96e3e28c7 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgState.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgState.java @@ -91,6 +91,11 @@ public class DlgState implements Parcelable { this( dlgID, null, 0, null, 0, params ); } + public DlgState( DlgID dlgID, Action action, Object... params ) + { + this( dlgID, null, 0, action, 0, params ); + } + public int describeContents() { return 0; } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/InviteDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/InviteDelegate.java index 284df01c7..f14125772 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/InviteDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/InviteDelegate.java @@ -40,8 +40,10 @@ abstract class InviteDelegate extends ListDelegateBase public static final String DEVS = "DEVS"; public static final String COUNTS = "COUNTS"; protected static final String INTENT_KEY_NMISSING = "NMISSING"; + protected static final String INTENT_KEY_LASTDEV = "LDEV"; protected int m_nMissing; + protected String m_lastDev; protected Button m_okButton; protected Button m_rescanButton; protected Button m_clearButton; @@ -56,6 +58,7 @@ abstract class InviteDelegate extends ListDelegateBase m_activity = delegator.getActivity(); Intent intent = getIntent(); m_nMissing = intent.getIntExtra( INTENT_KEY_NMISSING, -1 ); + m_lastDev = intent.getStringExtra( INTENT_KEY_LASTDEV ); } protected void init( int button_invite, int button_rescan, diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/SMSInviteDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/SMSInviteDelegate.java index 3d7589ad2..53614f4db 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/SMSInviteDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/SMSInviteDelegate.java @@ -49,6 +49,7 @@ import java.util.Map; import junit.framework.Assert; import org.eehouse.android.xw4.DlgDelegate.Action; +import org.eehouse.android.xw4.DBUtils.SentInvitesInfo; public class SMSInviteDelegate extends InviteDelegate { @@ -63,11 +64,16 @@ public class SMSInviteDelegate extends InviteDelegate { private boolean m_immobileConfirmed; private Activity m_activity; - public static void launchForResult( Activity activity, int nMissing, + public static void launchForResult( Activity activity, int nMissing, + SentInvitesInfo info, RequestCode requestCode ) { Intent intent = new Intent( activity, SMSInviteActivity.class ); intent.putExtra( INTENT_KEY_NMISSING, nMissing ); + if ( null != info ) { + String lastDev = info.getLastDev( InviteMeans.SMS ); + intent.putExtra( INTENT_KEY_LASTDEV, lastDev ); + } activity.startActivityForResult( intent, requestCode.ordinal() ); } @@ -327,7 +333,8 @@ public class SMSInviteDelegate extends InviteDelegate { m_phoneRecs = new ArrayList(phones.length); for ( String phone : phones ) { - PhoneRec rec = new PhoneRec( phone ); + boolean matches = phone.equals( m_lastDev ); + PhoneRec rec = new PhoneRec( null, phone, matches ); m_phoneRecs.add( rec ); } } @@ -383,7 +390,7 @@ public class SMSInviteDelegate extends InviteDelegate { this( null, phone, false ); } - public PhoneRec( String name, String phone, boolean checked ) + private PhoneRec( String name, String phone, boolean checked ) { m_phone = phone; m_isChecked = checked;