123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- package de.tudarmstadt.informatik.hostage.ui;
- 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.widget.Toast;
- import de.tudarmstadt.informatik.hostage.R;
- /**
- * 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());
- // Set the value of the preference as the summary for the preference
- pref = findPreference("pref_sleeptime");
- etp = (EditTextPreference) pref;
- defaultPref.edit()
- .putInt("sleeptime", 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_location_time");
- etp = (EditTextPreference) pref;
- defaultPref
- .edit()
- .putInt("location_time",
- 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_location_retries");
- etp = (EditTextPreference) pref;
- defaultPref
- .edit()
- .putInt("location_retries",
- Integer.valueOf(etp.getText()).intValue()).commit();
- pref.setSummary(etp.getText());
- }
- @Override
- 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);
- } else if (key.equals("pref_sleeptime")) {
- 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_sleeptime_default);
- }
- sharedPreferences.edit()
- .putInt("sleeptime", Integer.valueOf(value).intValue())
- .commit();
- pref.setSummary(value);
- } else if (key.equals("pref_location_time")) {
- 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_location_time_default);
- }
- sharedPreferences.edit()
- .putInt("location_time", Integer.valueOf(value).intValue())
- .commit();
- pref.setSummary(value);
- } else if (key.equals("pref_location_retries")) {
- 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_location_retries_default);
- }
- sharedPreferences
- .edit()
- .putInt("location_retries",
- Integer.valueOf(value).intValue()).commit();
- pref.setSummary(value);
- }
- }
- @Override
- protected void onPause() {
- super.onPause();
- getPreferenceScreen().getSharedPreferences()
- .unregisterOnSharedPreferenceChangeListener(this);
- }
- @Override
- protected void onResume() {
- // register a listener to catch preference changes
- super.onResume();
- getPreferenceScreen().getSharedPreferences()
- .registerOnSharedPreferenceChangeListener(this);
- }
- }
|