when user tries to turn on the preference disabling relay play and has

relay games in play, warn, and only disable if confirmed.
This commit is contained in:
Eric House 2016-06-24 21:19:30 -07:00
parent aab779f258
commit 372b0c2f1e
14 changed files with 495 additions and 306 deletions

File diff suppressed because it is too large Load diff

View file

@ -2108,9 +2108,18 @@
relay.\n\nYou can enable relay play now, or later. relay.\n\nYou can enable relay play now, or later.
</string> </string>
<string name="warn_relay_havegames">Are you sure you want to
disable play using the relay?</string>
<plurals name="warn_relay_games_fmt">
<item quantity="one">\n\n(You have one game using it.)</item>
<item quantity="other">\n\n(You have %1$d games using it.)</item>
</plurals>
<string name="button_enable_sms">Enable SMS</string> <string name="button_enable_sms">Enable SMS</string>
<string name="button_enable_bt">Enable Bluetooth</string> <string name="button_enable_bt">Enable Bluetooth</string>
<string name="button_enable_relay">Enable Relay play</string> <string name="button_enable_relay">Enable Relay play</string>
<string name="button_disable_relay">Disable Relay play</string>
<string name="button_later">Later</string> <string name="button_later">Later</string>
<!-- --> <!-- -->

View file

@ -305,11 +305,13 @@
android:entryValues="@array/connect_frequencies_values" android:entryValues="@array/connect_frequencies_values"
android:defaultValue="900" android:defaultValue="900"
/> />
<CheckBoxPreference android:key="@string/key_disable_relay"
android:title="@string/disable_relay" <org.eehouse.android.xw4.RelayCheckBoxPreference
android:summary="@string/disable_relay_summary" android:key="@string/key_disable_relay"
android:defaultValue="false" android:title="@string/disable_relay"
/> android:summary="@string/disable_relay_summary"
android:defaultValue="false"
/>
</PreferenceScreen> </PreferenceScreen>
<CheckBoxPreference android:key="@string/key_notify_sound" <CheckBoxPreference android:key="@string/key_notify_sound"

View file

@ -1826,9 +1826,16 @@
siht ecived. On sevom lliw eb tnes ro deviecer aiv eht siht ecived. On sevom lliw eb tnes ro deviecer aiv eht
yaler.\n\nUoy nac elbane yaler yalp ,won ro retal. yaler.\n\nUoy nac elbane yaler yalp ,won ro retal.
</string> </string>
<string name="warn_relay_havegames">Era uoy erus uoy tnaw ot
elbasid yalp gnisu eht ?yaler</string>
<plurals name="warn_relay_games_fmt">
<item quantity="one">\n\nuOy( evah eno emag gnisu ti.)</item>
<item quantity="other">\n\nuOy( evah %1$d semag gnisu ti.)</item>
</plurals>
<string name="button_enable_sms">Elbane SMS</string> <string name="button_enable_sms">Elbane SMS</string>
<string name="button_enable_bt">Elbane Htooteulb</string> <string name="button_enable_bt">Elbane Htooteulb</string>
<string name="button_enable_relay">Elbane Yaler yalp</string> <string name="button_enable_relay">Elbane Yaler yalp</string>
<string name="button_disable_relay">Elbasid Yaler yalp</string>
<string name="button_later">Retal</string> <string name="button_later">Retal</string>
<!-- --> <!-- -->
<string name="gamel_menu_checkupdates">Kcehc rof setadpu</string> <string name="gamel_menu_checkupdates">Kcehc rof setadpu</string>

View file

@ -1826,9 +1826,16 @@
THIS DEVICE. NO MOVES WILL BE SENT OR RECEIVED VIA THE THIS DEVICE. NO MOVES WILL BE SENT OR RECEIVED VIA THE
RELAY.\n\nYOU CAN ENABLE RELAY PLAY NOW, OR LATER. RELAY.\n\nYOU CAN ENABLE RELAY PLAY NOW, OR LATER.
</string> </string>
<string name="warn_relay_havegames">ARE YOU SURE YOU WANT TO
DISABLE PLAY USING THE RELAY?</string>
<plurals name="warn_relay_games_fmt">
<item quantity="one">\n\n(YOU HAVE ONE GAME USING IT.)</item>
<item quantity="other">\n\n(YOU HAVE %1$d GAMES USING IT.)</item>
</plurals>
<string name="button_enable_sms">ENABLE SMS</string> <string name="button_enable_sms">ENABLE SMS</string>
<string name="button_enable_bt">ENABLE BLUETOOTH</string> <string name="button_enable_bt">ENABLE BLUETOOTH</string>
<string name="button_enable_relay">ENABLE RELAY PLAY</string> <string name="button_enable_relay">ENABLE RELAY PLAY</string>
<string name="button_disable_relay">DISABLE RELAY PLAY</string>
<string name="button_later">LATER</string> <string name="button_later">LATER</string>
<!-- --> <!-- -->
<string name="gamel_menu_checkupdates">CHECK FOR UPDATES</string> <string name="gamel_menu_checkupdates">CHECK FOR UPDATES</string>

View file

@ -0,0 +1,62 @@
/* -*- compile-command: "find-and-ant.sh debug install"; -*- */
/*
* Copyright 2009 - 2016 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
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.eehouse.android.xw4;
import android.app.Activity;
import android.content.Context;
import android.preference.CheckBoxPreference;
import android.util.AttributeSet;
import android.view.View;
import org.eehouse.android.xw4.DlgDelegate.Action;
public abstract class ConfirmingCheckBoxPreference extends CheckBoxPreference {
private boolean m_attached = false;
public ConfirmingCheckBoxPreference( Context context, AttributeSet attrs )
{
super( context, attrs );
}
@Override
protected void onAttachedToActivity() {
super.onAttachedToActivity();
m_attached = true;
}
abstract void checkIfConfirmed();
@Override
public void setChecked( boolean checked )
{
if ( checked && m_attached && getContext() instanceof PrefsActivity ) {
checkIfConfirmed();
} else {
super.setChecked( checked );
}
}
// Because s_this.super.setChecked() isn't allowed...
protected void super_setChecked( boolean checked )
{
super.setChecked( checked );
}
}

View file

@ -752,6 +752,28 @@ public class DBUtils {
return result; return result;
} }
public static int getRelayGameCount( Context context ) {
int result = 0;
String[] columns = { DBHelper.CONTYPE };
String selection = String.format( "%s = 0", DBHelper.GAME_OVER );
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase();
Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns,
selection, null, null, null, null );
int indx = cursor.getColumnIndex( DBHelper.CONTYPE );
while ( cursor.moveToNext() ) {
CommsConnTypeSet typs = new CommsConnTypeSet( cursor.getInt(indx) );
if ( typs.contains(CommsConnType.COMMS_CONN_RELAY) ) {
++result;
}
}
cursor.close();
db.close();
}
return result;
}
public static long[] getRowIDsFor( Context context, String relayID ) public static long[] getRowIDsFor( Context context, String relayID )
{ {
long[] result = null; long[] result = null;

View file

@ -480,6 +480,11 @@ public class DelegateBase implements DlgClickNotify,
m_dlgDelegate.showConfirmThen( msg, posButton, negButton, action ); m_dlgDelegate.showConfirmThen( msg, posButton, negButton, action );
} }
protected void showConfirmThen( String msg, int posButton, int negButton, Action action )
{
m_dlgDelegate.showConfirmThen( msg, posButton, negButton, action );
}
protected void showConfirmThen( int msg, int posButton, int negButton, protected void showConfirmThen( int msg, int posButton, int negButton,
Action action, Object... params ) Action action, Object... params )
{ {
@ -639,6 +644,7 @@ public class DelegateBase implements DlgClickNotify,
break; break;
case ENABLE_RELAY_DO: case ENABLE_RELAY_DO:
RelayService.setEnabled( m_activity, true ); RelayService.setEnabled( m_activity, true );
handled = true;
break; break;
default: default:
Assert.fail(); Assert.fail();

View file

@ -122,6 +122,7 @@ public class DlgDelegate {
ENABLE_SMS_DO, ENABLE_SMS_DO,
ENABLE_BT_DO, ENABLE_BT_DO,
ENABLE_RELAY_DO, ENABLE_RELAY_DO,
DISABLE_RELAY_DO,
} }
public static class ActionPair { public static class ActionPair {
@ -377,6 +378,11 @@ public class DlgDelegate {
showConfirmThen( null, getString(msg), posButton, negButton, action, null ); showConfirmThen( null, getString(msg), posButton, negButton, action, null );
} }
public void showConfirmThen( String msg, int posButton, int negButton, Action action )
{
showConfirmThen( null, msg, posButton, negButton, action, null );
}
public void showConfirmThen( int msg, int posButton, int negButton, Action action, public void showConfirmThen( int msg, int posButton, int negButton, Action action,
Object... params ) Object... params )
{ {

View file

@ -106,13 +106,19 @@ public class PrefsActivity extends PreferenceActivity
m_dlgt.showOKOnlyDialog( msg ); m_dlgt.showOKOnlyDialog( msg );
} }
public void showNotAgainDlgThen( int msgID, int prefsKey, public void showNotAgainDlgThen( int msgID, int prefsKey,
DlgDelegate.Action action ) DlgDelegate.Action action )
{ {
m_dlgt.showNotAgainDlgThen( msgID, prefsKey, action ); m_dlgt.showNotAgainDlgThen( msgID, prefsKey, action );
} }
protected void showConfirmThen( int msg, int posButton, int negButton, protected void showConfirmThen( int msg, int posButton, int negButton,
Action action )
{
m_dlgt.showConfirmThen( msg, posButton, negButton, action );
}
protected void showConfirmThen( String msg, int posButton, int negButton,
Action action ) Action action )
{ {
m_dlgt.showConfirmThen( msg, posButton, negButton, action ); m_dlgt.showConfirmThen( msg, posButton, negButton, action );

View file

@ -240,11 +240,24 @@ public class PrefsDelegate extends DelegateBase
@Override @Override
public void dlgButtonClicked( Action action, int button, Object[] params ) public void dlgButtonClicked( Action action, int button, Object[] params )
{ {
if ( AlertDialog.BUTTON_POSITIVE == button DbgUtils.logf( "PrefsDelegate.dlgButtonClicked(%s)", action.toString() );
&& action == Action.ENABLE_SMS_DO ) { boolean handled = AlertDialog.BUTTON_POSITIVE == button;
XWPrefs.setSMSEnabled( m_activity, true ); if ( handled ) {
SMSCheckBoxPreference.setChecked(); switch ( action ) {
} else { case ENABLE_SMS_DO:
XWPrefs.setSMSEnabled( m_activity, true );
SMSCheckBoxPreference.setChecked();
break;
case DISABLE_RELAY_DO:
RelayService.setEnabled( m_activity, false );
RelayCheckBoxPreference.setChecked();
break;
default:
handled = false;
}
}
if ( !handled ) {
super.dlgButtonClicked( action, button, params ); super.dlgButtonClicked( action, button, params );
} }
} }

View file

@ -0,0 +1,62 @@
/* -*- compile-command: "find-and-ant.sh debug install"; -*- */
/*
* Copyright 2009 - 2012 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
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.eehouse.android.xw4;
import android.app.Activity;
import android.content.Context;
import android.preference.CheckBoxPreference;
import android.util.AttributeSet;
import android.view.View;
import org.eehouse.android.xw4.DlgDelegate.Action;
import org.eehouse.android.xw4.loc.LocUtils;
public class RelayCheckBoxPreference extends ConfirmingCheckBoxPreference {
private static ConfirmingCheckBoxPreference s_this = null;
public RelayCheckBoxPreference( Context context, AttributeSet attrs )
{
super( context, attrs );
s_this = this;
}
@Override
protected void checkIfConfirmed() {
PrefsActivity activity = (PrefsActivity)getContext();
int count = DBUtils.getRelayGameCount( activity );
if ( 0 < count ) {
String msg = LocUtils.getString( activity,
R.string.warn_relay_havegames );
msg += LocUtils.getQuantityString( activity, R.plurals.warn_relay_games_fmt,
count, count );
activity.showConfirmThen( msg, R.string.button_disable_relay,
android.R.string.cancel,
Action.DISABLE_RELAY_DO );
}
}
protected static void setChecked()
{
if ( null != s_this ) {
s_this.super_setChecked( true );
}
}
}

View file

@ -1360,7 +1360,7 @@ public class RelayService extends XWService
private boolean shouldMaintainConnection() private boolean shouldMaintainConnection()
{ {
boolean result = !relayEnabled( this ) boolean result = relayEnabled( this )
&& (XWApp.GCM_IGNORED || !s_gcmWorking); && (XWApp.GCM_IGNORED || !s_gcmWorking);
if ( result ) { if ( result ) {
long interval = Utils.getCurSeconds() - m_lastGamePacketReceived; long interval = Utils.getCurSeconds() - m_lastGamePacketReceived;

View file

@ -28,16 +28,12 @@ import android.view.View;
import org.eehouse.android.xw4.DlgDelegate.Action; import org.eehouse.android.xw4.DlgDelegate.Action;
public class SMSCheckBoxPreference extends CheckBoxPreference { public class SMSCheckBoxPreference extends ConfirmingCheckBoxPreference {
private static ConfirmingCheckBoxPreference s_this = null;
private Context m_context;
private boolean m_attached = false;
private static SMSCheckBoxPreference s_this = null;
public SMSCheckBoxPreference( Context context, AttributeSet attrs ) public SMSCheckBoxPreference( Context context, AttributeSet attrs )
{ {
super( context, attrs ); super( context, attrs );
m_context = context;
s_this = this; s_this = this;
} }
@ -45,21 +41,15 @@ public class SMSCheckBoxPreference extends CheckBoxPreference {
protected void onAttachedToActivity() protected void onAttachedToActivity()
{ {
super.onAttachedToActivity(); super.onAttachedToActivity();
if ( !XWApp.SMSSUPPORTED || !Utils.deviceSupportsSMS( m_context ) ) { if ( !XWApp.SMSSUPPORTED || !Utils.deviceSupportsSMS( getContext() ) ) {
setEnabled( false ); setEnabled( false );
} }
m_attached = true;
} }
@Override @Override
public void setChecked( boolean checked ) protected void checkIfConfirmed() {
{ PrefsActivity activity = (PrefsActivity)getContext();
if ( checked && m_attached && m_context instanceof PrefsActivity ) { activity.showSMSEnableDialog( Action.ENABLE_SMS_DO );
PrefsActivity activity = (PrefsActivity)m_context;
activity.showSMSEnableDialog( Action.ENABLE_SMS_DO );
} else {
super.setChecked( checked );
}
} }
protected static void setChecked() protected static void setChecked()
@ -68,10 +58,4 @@ public class SMSCheckBoxPreference extends CheckBoxPreference {
s_this.super_setChecked( true ); s_this.super_setChecked( true );
} }
} }
// Because s_this.super.setChecked() isn't allowed...
private void super_setChecked( boolean checked )
{
super.setChecked( checked );
}
} }