use a hash map instead of a bunch of str-compares.

This commit is contained in:
Eric House 2015-09-22 20:18:27 -07:00
parent b3a93cc474
commit bfce38c088

View file

@ -34,26 +34,33 @@ import android.preference.PreferenceManager;
import android.preference.PreferenceScreen; import android.preference.PreferenceScreen;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import java.io.File; import java.io.File;
import java.util.HashMap;
import java.util.Map;
import org.eehouse.android.xw4.DlgDelegate.Action; import org.eehouse.android.xw4.DlgDelegate.Action;
import org.eehouse.android.xw4.loc.LocUtils; import org.eehouse.android.xw4.loc.LocUtils;
import junit.framework.Assert;
public class PrefsDelegate extends DelegateBase public class PrefsDelegate extends DelegateBase
implements SharedPreferences.OnSharedPreferenceChangeListener { implements SharedPreferences.OnSharedPreferenceChangeListener {
private PreferenceActivity m_activity; private PreferenceActivity m_activity;
private static int[] s_keys = {
private String m_keyLogging; R.string.key_logging_on,
private String m_smsToasting; R.string.key_show_sms,
private String m_smsEnable; R.string.key_enable_sms,
private String m_downloadPath; R.string.key_download_path,
private String m_thumbSize; R.string.key_thumbsize,
private String m_keyLocale; R.string.key_xlations_locale,
private String m_keyLangs; R.string.key_default_language,
private String m_keyFakeRadio; R.string.key_force_radio,
private String m_keyNagsDisabledNet; R.string.key_disable_nag,
private String m_keyNagsDisabledSolo; R.string.key_disable_nag_solo,
};
private static Map<String, Integer> s_keysHash = null;
public PrefsDelegate( PreferenceActivity activity, Delegator delegator, public PrefsDelegate( PreferenceActivity activity, Delegator delegator,
Bundle savedInstanceState ) Bundle savedInstanceState )
@ -127,20 +134,17 @@ public class PrefsDelegate extends DelegateBase
protected void init( Bundle savedInstanceState ) protected void init( Bundle savedInstanceState )
{ {
if ( null == s_keysHash ) {
s_keysHash = new HashMap<String, Integer>();
for ( int key : s_keys ) {
String str = getString( key );
s_keysHash.put( str, key );
}
}
// Load the preferences from an XML resource // Load the preferences from an XML resource
m_activity.addPreferencesFromResource( R.xml.xwprefs ); m_activity.addPreferencesFromResource( R.xml.xwprefs );
m_keyLogging = getString( R.string.key_logging_on );
m_smsToasting = getString( R.string.key_show_sms );
m_smsEnable = getString( R.string.key_enable_sms );
m_downloadPath = getString( R.string.key_download_path );
m_thumbSize = getString( R.string.key_thumbsize );
m_keyLocale = getString( R.string.key_xlations_locale );
m_keyLangs = getString( R.string.key_default_language );
m_keyFakeRadio = getString( R.string.key_force_radio );
m_keyNagsDisabledNet = getString( R.string.key_disable_nag );
m_keyNagsDisabledSolo = getString( R.string.key_disable_nag_solo );
Button button = (Button)findViewById( R.id.revert_colors ); Button button = (Button)findViewById( R.id.revert_colors );
button.setOnClickListener( new View.OnClickListener() { button.setOnClickListener( new View.OnClickListener() {
public void onClick( View v ) { public void onClick( View v ) {
@ -175,15 +179,20 @@ public class PrefsDelegate extends DelegateBase
@Override @Override
public void onSharedPreferenceChanged( SharedPreferences sp, String key ) public void onSharedPreferenceChanged( SharedPreferences sp, String key )
{ {
if ( key.equals( m_keyLogging ) ) { if ( s_keysHash.containsKey( key ) ) {
switch( s_keysHash.get( key ) ) {
case R.string.key_logging_on:
DbgUtils.logEnable( sp.getBoolean( key, false ) ); DbgUtils.logEnable( sp.getBoolean( key, false ) );
} else if ( key.equals( m_smsToasting ) ) { break;
case R.string.key_show_sms:
SMSService.smsToastEnable( sp.getBoolean( key, false ) ); SMSService.smsToastEnable( sp.getBoolean( key, false ) );
} else if ( key.equals( m_smsEnable ) ) { break;
case R.string.key_enable_sms:
if ( ! sp.getBoolean( key, true ) ) { if ( ! sp.getBoolean( key, true ) ) {
SMSService.stopService( m_activity ); SMSService.stopService( m_activity );
} }
} else if ( key.equals( m_downloadPath ) ) { break;
case R.string.key_download_path:
String value = sp.getString( key, null ); String value = sp.getString( key, null );
if ( null != value ) { if ( null != value ) {
File dir = new File( value ); File dir = new File( value );
@ -200,17 +209,27 @@ public class PrefsDelegate extends DelegateBase
} }
} }
DictUtils.invalDictList(); DictUtils.invalDictList();
} else if ( key.equals( m_thumbSize ) ) { break;
case R.string.key_thumbsize:
DBUtils.clearThumbnails( m_activity ); DBUtils.clearThumbnails( m_activity );
} else if ( key.equals( m_keyLocale ) ) { break;
case R.string.key_xlations_locale:
LocUtils.localeChanged( m_activity, sp.getString( key, null ) ); LocUtils.localeChanged( m_activity, sp.getString( key, null ) );
} else if ( key.equals( m_keyLangs ) ) { break;
case R.string.key_default_language:
forceDictsMatch( sp.getString( key, null ) ); forceDictsMatch( sp.getString( key, null ) );
} else if ( key.equals( m_keyFakeRadio ) ) { break;
case R.string.key_force_radio:
SMSService.resetPhoneInfo(); SMSService.resetPhoneInfo();
} else if ( key.equals( m_keyNagsDisabledNet ) break;
|| key.equals( m_keyNagsDisabledSolo ) ) { case R.string.key_disable_nag:
case R.string.key_disable_nag_solo:
NagTurnReceiver.resetNagsDisabled( m_activity ); NagTurnReceiver.resetNagsDisabled( m_activity );
break;
default:
Assert.fail();
break;
}
} }
} }
@ -247,8 +266,9 @@ public class PrefsDelegate extends DelegateBase
private void setupLangPref() private void setupLangPref()
{ {
String keyLangs = getString( R.string.key_default_language );
ListPreference lp = (ListPreference) ListPreference lp = (ListPreference)
m_activity.findPreference( m_keyLangs ); m_activity.findPreference( keyLangs );
String curLang = lp.getValue().toString(); String curLang = lp.getValue().toString();
boolean haveDictForLang = false; boolean haveDictForLang = false;