replace hard-coded list of preferences whose summaries are their

values and code to enforce that with custom subclasses that do the
right thing automatically.
This commit is contained in:
Andy2 2011-07-19 18:21:52 -07:00
parent e4ba5fb5fd
commit ea853e2e51
5 changed files with 265 additions and 204 deletions

View file

@ -11,28 +11,32 @@
android:summary="@string/prefs_names_summary"
>
<EditTextPreference android:key="@string/key_player1_name"
android:title="@string/pref_player1_name"
android:defaultValue=""
/>
<EditTextPreference android:key="@string/key_player2_name"
android:title="@string/pref_player2_name"
android:defaultValue="ignored"
android:summary="@string/tell_unused"
android:enabled="false"
/>
<EditTextPreference android:key="@string/key_player3_name"
android:title="@string/pref_player3_name"
android:defaultValue=""
android:summary="@string/tell_unused"
android:enabled="false"
/>
<EditTextPreference android:key="@string/key_player4_name"
android:title="@string/pref_player4_name"
android:defaultValue=""
android:summary="@string/tell_unused"
android:enabled="false"
/>
<org.eehouse.android.xw4.XWEditTextPreference
android:key="@string/key_player1_name"
android:title="@string/pref_player1_name"
android:defaultValue=""
/>
<org.eehouse.android.xw4.XWEditTextPreference
android:key="@string/key_player2_name"
android:title="@string/pref_player2_name"
android:defaultValue="ignored"
android:summary="@string/tell_unused"
android:enabled="false"
/>
<org.eehouse.android.xw4.XWEditTextPreference
android:key="@string/key_player3_name"
android:title="@string/pref_player3_name"
android:defaultValue=""
android:summary="@string/tell_unused"
android:enabled="false"
/>
<org.eehouse.android.xw4.XWEditTextPreference
android:key="@string/key_player4_name"
android:title="@string/pref_player4_name"
android:defaultValue=""
android:summary="@string/tell_unused"
android:enabled="false"
/>
</PreferenceScreen>
<PreferenceScreen android:title="@string/prefs_dicts"
@ -63,30 +67,33 @@
android:defaultValue="false"
/>
<ListPreference android:key="@string/key_default_phonies"
android:title="@string/default_phonies"
android:entries="@array/phony_names"
android:entryValues="@array/phony_names"
android:defaultValue="@string/phonies_warn"
/>
<org.eehouse.android.xw4.XWListPreference
android:key="@string/key_default_phonies"
android:title="@string/default_phonies"
android:entries="@array/phony_names"
android:entryValues="@array/phony_names"
android:defaultValue="@string/phonies_warn"
/>
<CheckBoxPreference android:key="@string/key_default_timerenabled"
android:title="@string/use_timer"
android:defaultValue="false"
/>
<EditTextPreference android:key="@string/key_initial_player_minutes"
android:title="@string/initial_player_minutes"
android:defaultValue="25"
android:numeric="decimal"
/>
<org.eehouse.android.xw4.XWEditTextPreference
android:key="@string/key_initial_player_minutes"
android:title="@string/initial_player_minutes"
android:defaultValue="25"
android:numeric="decimal"
/>
<ListPreference android:key="@string/key_board_size"
android:title="@string/board_size"
android:entries="@array/board_sizes"
android:entryValues="@array/board_sizes"
android:defaultValue="15x15"
/>
<org.eehouse.android.xw4.XWListPreference
android:key="@string/key_board_size"
android:title="@string/board_size"
android:entries="@array/board_sizes"
android:entryValues="@array/board_sizes"
android:defaultValue="15x15"
/>
</PreferenceScreen>
@ -94,97 +101,97 @@
android:summary="@string/prefs_appearance_summary"
>
<!-- <CheckBoxPreference android:key="@string/key_color_tiles" -->
<!-- android:title="@string/color_tiles" -->
<!-- android:summary="@string/color_tiles_summary" -->
<!-- android:defaultValue="true" -->
<!-- /> -->
<CheckBoxPreference android:key="@string/key_hide_intro"
android:title="@string/hide_intro"
android:summary="@string/hide_intro_summary"
android:defaultValue="false"
/>
<CheckBoxPreference android:key="@string/key_hide_title"
android:title="@string/hide_title"
android:summary="@string/hide_title_summary"
android:defaultValue="true"
/>
<CheckBoxPreference android:key="@string/key_show_arrow"
android:title="@string/show_arrow"
android:summary="@string/show_arrow_summary"
android:defaultValue="true"
/>
<CheckBoxPreference android:key="@string/key_keep_screenon"
android:title="@string/keep_screenon"
android:summary="@string/keep_screenon_summary"
android:defaultValue="false"
/>
<!-- android:title="@string/color_tiles" -->
<!-- android:summary="@string/color_tiles_summary" -->
<!-- android:defaultValue="true" -->
<!-- /> -->
<CheckBoxPreference android:key="@string/key_hide_intro"
android:title="@string/hide_intro"
android:summary="@string/hide_intro_summary"
android:defaultValue="false"
/>
<CheckBoxPreference android:key="@string/key_hide_title"
android:title="@string/hide_title"
android:summary="@string/hide_title_summary"
android:defaultValue="true"
/>
<CheckBoxPreference android:key="@string/key_show_arrow"
android:title="@string/show_arrow"
android:summary="@string/show_arrow_summary"
android:defaultValue="true"
/>
<CheckBoxPreference android:key="@string/key_keep_screenon"
android:title="@string/keep_screenon"
android:summary="@string/keep_screenon_summary"
android:defaultValue="false"
/>
<PreferenceScreen android:title="@string/prefs_colors"
android:summary="@string/prefs_colors_summary"
>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_player0"
android:title="@string/player0"
android:defaultValue="0x000000"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_player1"
android:title="@string/player1"
android:defaultValue="0xFF0000"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_player2"
android:title="@string/player2"
android:defaultValue="0x0000FF"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_player3"
android:title="@string/player3"
android:defaultValue="0x008F00"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_bonus_l2x"
android:title="@string/bonus_l2x"
android:defaultValue="0xAFAF00"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_bonus_l3x"
android:title="@string/bonus_l3x"
android:defaultValue="0x00AFAF"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_bonus_w2x"
android:title="@string/bonus_w2x"
android:defaultValue="0xAF00AF"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_bonus_w3x"
android:title="@string/bonus_w3x"
android:defaultValue="0xAFAFAF"
/>
<PreferenceScreen android:title="@string/prefs_colors"
android:summary="@string/prefs_colors_summary"
>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_player0"
android:title="@string/player0"
android:defaultValue="0x000000"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_player1"
android:title="@string/player1"
android:defaultValue="0xFF0000"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_player2"
android:title="@string/player2"
android:defaultValue="0x0000FF"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_player3"
android:title="@string/player3"
android:defaultValue="0x008F00"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_bonus_l2x"
android:title="@string/bonus_l2x"
android:defaultValue="0xAFAF00"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_bonus_l3x"
android:title="@string/bonus_l3x"
android:defaultValue="0x00AFAF"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_bonus_w2x"
android:title="@string/bonus_w2x"
android:defaultValue="0xAF00AF"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_bonus_w3x"
android:title="@string/bonus_w3x"
android:defaultValue="0xAFAFAF"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_clr_crosshairs"
android:title="@string/clr_crosshairs"
android:defaultValue="0x7070FF"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_tile_back"
android:title="@string/tile_back"
android:defaultValue="0xFFFF99"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_empty"
android:title="@string/empty"
android:defaultValue="0xFFFFFF"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_clr_crosshairs"
android:title="@string/clr_crosshairs"
android:defaultValue="0x7070FF"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_tile_back"
android:title="@string/tile_back"
android:defaultValue="0xFFFF99"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_empty"
android:title="@string/empty"
android:defaultValue="0xFFFFFF"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_background"
android:title="@string/background"
android:defaultValue="0xFFFFFF"
/>
</PreferenceScreen>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_background"
android:title="@string/background"
android:defaultValue="0xFFFFFF"
/>
</PreferenceScreen>
</PreferenceScreen>
@ -244,42 +251,42 @@
<PreferenceScreen android:title="@string/advanced"
android:summary="@string/advanced_summary"
>
<EditTextPreference android:key="@string/key_relay_host"
android:title="@string/relay_host"
android:defaultValue="@string/default_host"
/>
<EditTextPreference android:key="@string/key_relay_port"
android:title="@string/relay_port"
android:defaultValue="10997"
android:numeric="decimal"
/>
<EditTextPreference android:key="@string/key_proxy_port"
android:title="@string/proxy_port"
android:defaultValue="10998"
android:numeric="decimal"
/>
<EditTextPreference android:key="@string/key_redir_host"
android:title="@string/redir_host"
android:defaultValue="@string/default_host"
/>
<org.eehouse.android.xw4.XWEditTextPreference
android:key="@string/key_relay_host"
android:title="@string/relay_host"
android:defaultValue="@string/default_host"
/>
<org.eehouse.android.xw4.XWEditTextPreference
android:key="@string/key_relay_port"
android:title="@string/relay_port"
android:defaultValue="10997"
android:numeric="decimal"
/>
<org.eehouse.android.xw4.XWEditTextPreference
android:key="@string/key_proxy_port"
android:title="@string/proxy_port"
android:defaultValue="10998"
android:numeric="decimal"
/>
<org.eehouse.android.xw4.XWEditTextPreference
android:key="@string/key_redir_host"
android:title="@string/redir_host"
android:defaultValue="@string/default_host"
/>
<!-- <EditTextPreference android:key="@string/key_sms_port" -->
<!-- android:title="@string/sms_port" -->
<!-- android:defaultValue="5432" -->
<!-- android:numeric="decimal" -->
<!-- /> -->
<EditTextPreference android:key="@string/key_dict_host"
android:title="@string/dict_host"
android:defaultValue="@string/dict_url"
/>
<org.eehouse.android.xw4.XWEditTextPreference
android:key="@string/key_dict_host"
android:title="@string/dict_host"
android:defaultValue="@string/dict_url"
/>
<CheckBoxPreference android:key="@string/key_logging_on"
android:title="@string/logging_on"
android:defaultValue="false"
/>
<EditTextPreference android:title="@string/git_rev_title"
android:summary="@string/git_rev_gen"
android:enabled="false"
/>
<CheckBoxPreference android:key="@string/key_logging_on"
android:title="@string/logging_on"
android:defaultValue="false"
/>
<EditTextPreference android:title="@string/git_rev_title"
android:summary="@string/git_rev_gen"
android:enabled="false"
/>
</PreferenceScreen>
</PreferenceScreen>

View file

@ -26,7 +26,7 @@ import android.util.AttributeSet;
import junit.framework.Assert;
public class DictListPreference extends ListPreference {
public class DictListPreference extends XWListPreference {
public DictListPreference( Context context, AttributeSet attrs )
{

View file

@ -39,7 +39,6 @@ public class PrefsActivity extends PreferenceActivity
private static final int REVERT_COLORS = 1;
private static final int REVERT_ALL = 2;
private HashSet<String> m_keys;
private String m_keyEmpty;
private String m_keyLogging;
@ -114,31 +113,6 @@ public class PrefsActivity extends PreferenceActivity
// Load the preferences from an XML resource
addPreferencesFromResource( R.xml.xwprefs );
int[] textKeyIds = { R.string.key_relay_host,
R.string.key_redir_host,
R.string.key_relay_port,
R.string.key_proxy_port,
R.string.key_dict_host,
R.string.key_board_size,
R.string.key_initial_player_minutes,
R.string.key_default_dict,
R.string.key_default_robodict,
R.string.key_default_phonies,
R.string.key_player1_name,
R.string.key_player2_name,
R.string.key_player3_name,
R.string.key_player4_name,
};
SharedPreferences sp
= PreferenceManager.getDefaultSharedPreferences( this );
m_keys = new HashSet<String>( textKeyIds.length );
for ( int ii = 0; ii < textKeyIds.length; ++ii ) {
int id = textKeyIds[ii];
String key = getString( id );
setSummary( sp, key );
m_keys.add( key );
}
m_keyEmpty = getString( R.string.key_empty );
m_keyLogging = getString( R.string.key_logging_on );
}
@ -161,9 +135,6 @@ public class PrefsActivity extends PreferenceActivity
public void onSharedPreferenceChanged( SharedPreferences sp, String key )
{
if ( m_keys.contains( key ) ) {
setSummary( sp, key );
}
if ( key.equals( m_keyLogging ) ) {
Utils.logEnable( sp.getBoolean( key, false ) );
}
@ -197,18 +168,6 @@ public class PrefsActivity extends PreferenceActivity
return handled;
}
private void setSummary( SharedPreferences sp, String key )
{
Preference pref = getPreferenceScreen().findPreference( key );
String value = sp.getString( key, "" );
// if ( pref instanceof android.preference.ListPreference ) {
// Utils.logf( "%s: want to do lookup of user string here",
// key );
// }
pref.setSummary( value );
}
private void relaunch()
{
PreferenceManager.setDefaultValues( this, R.xml.xwprefs,

View file

@ -0,0 +1,48 @@
/* -*- compile-command: "cd ../../../../../; ant install"; -*- */
/*
* Copyright 2010 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.preference.EditTextPreference;
import android.content.Context;
import android.util.AttributeSet;
import junit.framework.Assert;
public class XWEditTextPreference extends EditTextPreference {
public XWEditTextPreference( Context context, AttributeSet attrs )
{
super( context, attrs );
}
protected void onAttachedToActivity()
{
super.onAttachedToActivity();
setSummary( getPersistedString( "" ) );
}
protected boolean persistString( String value )
{
setSummary( value );
return super.persistString( value );
}
}

View file

@ -0,0 +1,47 @@
/* -*- compile-command: "cd ../../../../../; ant install"; -*- */
/*
* Copyright 2010 - 2011 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.preference.ListPreference;
import android.content.Context;
import android.util.AttributeSet;
import junit.framework.Assert;
public class XWListPreference extends ListPreference {
public XWListPreference( Context context, AttributeSet attrs )
{
super( context, attrs );
}
protected void onAttachedToActivity()
{
super.onAttachedToActivity();
setSummary( getPersistedString( "" ) );
}
protected boolean persistString( String value )
{
setSummary( value );
return super.persistString( value );
}
}