package de.tudarmstadt.informatik.hostage.ui; import de.tudarmstadt.informatik.hostage.R; import android.content.SharedPreferences; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.os.Bundle; import android.preference.EditTextPreference; import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.PreferenceManager; import android.util.Log; import android.widget.Toast; /** * SettingsActivity creates the settings defined in /res/xml/preferences.xml. * @author Lars Pandikow. */ public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); //Set the value of the preference as the summary for the preference Preference pref = findPreference("pref_external_location"); EditTextPreference etp = (EditTextPreference) pref; pref.setSummary(etp.getText()); //Set the value of the preference as the summary for the preference pref = findPreference("pref_upload_server"); etp = (EditTextPreference) pref; pref.setSummary(etp.getText()); SharedPreferences defaultPref = PreferenceManager.getDefaultSharedPreferences(this); //Set the value of the preference as the summary for the preference pref = findPreference("pref_max_connections"); etp = (EditTextPreference) pref; defaultPref.edit().putInt("max_connections", Integer.valueOf(etp.getText()).intValue()).commit(); pref.setSummary(etp.getText()); //Set the value of the preference as the summary for the preference pref = findPreference("pref_timeout"); etp = (EditTextPreference) pref; defaultPref.edit().putInt("timeout", Integer.valueOf(etp.getText()).intValue()).commit(); pref.setSummary(etp.getText()); } protected void onResume() { // register a listener to catch preference changes super.onResume(); getPreferenceScreen().getSharedPreferences() .registerOnSharedPreferenceChangeListener(this); } protected void onPause() { super.onPause(); getPreferenceScreen().getSharedPreferences() .unregisterOnSharedPreferenceChangeListener(this); } public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { // Check which preference has been changed if(key.equals("pref_external_location")){ Preference pref = findPreference(key); EditTextPreference etp = (EditTextPreference) pref; String path = etp.getText(); //Check if the Path is valid if(!path.startsWith("/")) path = new String("/").concat(path); if(!path.endsWith("/")) path = path.concat(new String("/")); if (!path.matches("/(([a-zA-Z_0-9])+/)*")){ Toast.makeText(this, "Path not valid. Must only contain a-zA-Z_0-9", Toast.LENGTH_SHORT).show(); path = "/"; sharedPreferences.edit().putString(key, path).commit(); } pref.setSummary(path); } else if(key.equals("pref_upload_server")){ Preference pref = findPreference(key); EditTextPreference etp = (EditTextPreference) pref; pref.setSummary(etp.getText()); } else if(key.equals("pref_max_connections")){ Preference pref = findPreference(key); EditTextPreference etp = (EditTextPreference) pref; String value = etp.getText(); if(!value.matches("([0-9])+")){ Toast.makeText(getApplicationContext(), "Enter a valid number.", Toast.LENGTH_SHORT).show(); value = getResources().getString(R.string.pref_max_connections_default); } sharedPreferences.edit().putInt("max_connections", Integer.valueOf(value).intValue()).commit(); pref.setSummary(value); } else if(key.equals("pref_timeout")){ Preference pref = findPreference(key); EditTextPreference etp = (EditTextPreference) pref; String value = etp.getText(); if(!value.matches("([0-9])+")){ Toast.makeText(getApplicationContext(), "Enter a valid number.", Toast.LENGTH_SHORT).show(); value = getResources().getString(R.string.pref_timeout_default); } sharedPreferences.edit().putInt("timeout", Integer.valueOf(value).intValue()).commit(); pref.setSummary(value); } } }