factor out common code

This commit is contained in:
eehouse 2010-05-26 02:27:34 +00:00
parent 28d9d4559d
commit dd2693bab0

View file

@ -131,29 +131,23 @@ public class CommonPrefs {
public static String getDefaultRelayHost( Context context ) public static String getDefaultRelayHost( Context context )
{ {
String key = context.getString( R.string.key_relay_host ); return getString( context, R.string.key_relay_host );
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences( context );
return sp.getString( key, "" );
} }
public static int getDefaultRelayPort( Context context ) public static int getDefaultRelayPort( Context context )
{ {
String key = context.getString( R.string.key_relay_port ); String val = getString( context, R.string.key_relay_port );
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences( context );
String val = sp.getString( key, "" );
int result = 0; int result = 0;
result = Integer.decode( val ); try {
return result; return Integer.parseInt( val );
} catch ( Exception ex ) {
return 0;
}
} }
public static String getDefaultDictURL( Context context ) public static String getDefaultDictURL( Context context )
{ {
String key = context.getString( R.string.key_dict_host ); return getString( context, R.string.key_dict_host );
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences( context );
return sp.getString( key, "" );
} }
public static boolean getVolKeysZoom( Context context ) public static boolean getVolKeysZoom( Context context )
@ -163,28 +157,27 @@ public class CommonPrefs {
public static int getDefaultBoardSize( Context context ) public static int getDefaultBoardSize( Context context )
{ {
String key = context.getString( R.string.key_board_size ); String value = getString( context, R.string.key_board_size );
SharedPreferences sp = PreferenceManager try {
.getDefaultSharedPreferences( context ); return Integer.parseInt( value.substring( 0, 2 ) );
String value = sp.getString( key, "15" ); } catch ( Exception ex ) {
return Integer.parseInt( value.substring( 0, 2 ) ); return 15;
}
} }
public static int getDefaultGameMinutes( Context context ) public static int getDefaultGameMinutes( Context context )
{ {
String key = context.getString( R.string.key_initial_game_minutes ); String value = getString( context, R.string.key_initial_game_minutes );
SharedPreferences sp = PreferenceManager try {
.getDefaultSharedPreferences( context ); return Integer.parseInt( value );
String value = sp.getString( key, "25" ); } catch ( Exception ex ) {
return Integer.parseInt( value ); return 25;
}
} }
public static String getDefaultDict( Context context ) public static String getDefaultDict( Context context )
{ {
String key = context.getString( R.string.key_default_dict ); String value = getString( context, R.string.key_default_dict );
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences( context );
String value = sp.getString( key, "" );
if ( value.equals("") ) { if ( value.equals("") ) {
value = GameUtils.dictList( context )[0]; value = GameUtils.dictList( context )[0];
} }
@ -194,10 +187,7 @@ public class CommonPrefs {
public static CurGameInfo.XWPhoniesChoice public static CurGameInfo.XWPhoniesChoice
getDefaultPhonies( Context context ) getDefaultPhonies( Context context )
{ {
String key = context.getString( R.string.key_default_phonies ); String value = getString( context, R.string.key_default_phonies );
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences( context );
String value = sp.getString( key, "" );
CurGameInfo.XWPhoniesChoice result = CurGameInfo.XWPhoniesChoice result =
CurGameInfo.XWPhoniesChoice.PHONIES_IGNORE; CurGameInfo.XWPhoniesChoice.PHONIES_IGNORE;
@ -227,4 +217,12 @@ public class CommonPrefs {
return sp.getBoolean( key, defaultValue ); return sp.getBoolean( key, defaultValue );
} }
private static String getString( Context context, int keyID )
{
String key = context.getString( keyID );
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences( context );
return sp.getString( key, "" );
}
} }