SettingsActivity.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package de.tudarmstadt.informatik.hostage.ui;
  2. import de.tudarmstadt.informatik.hostage.R;
  3. import android.content.SharedPreferences;
  4. import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
  5. import android.os.Bundle;
  6. import android.preference.EditTextPreference;
  7. import android.preference.Preference;
  8. import android.preference.PreferenceActivity;
  9. import android.widget.Toast;
  10. /**
  11. * SettingsActivity creates the settings defined in /xml/preferences.xml.
  12. * @author Lars Pandikow.
  13. */
  14. public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
  15. @Override
  16. public void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. addPreferencesFromResource(R.xml.preferences);
  19. //Set the value of the preference as the summary for the preference
  20. Preference pref = findPreference("pref_external_location");
  21. EditTextPreference etp = (EditTextPreference) pref;
  22. pref.setSummary(etp.getText());
  23. //Set the value of the preference as the summary for the preference
  24. pref = findPreference("pref_upload_server");
  25. etp = (EditTextPreference) pref;
  26. pref.setSummary(etp.getText());
  27. }
  28. protected void onResume() {
  29. // register a listener to catch preference changes
  30. super.onResume();
  31. getPreferenceScreen().getSharedPreferences()
  32. .registerOnSharedPreferenceChangeListener(this);
  33. }
  34. protected void onPause() {
  35. super.onPause();
  36. getPreferenceScreen().getSharedPreferences()
  37. .unregisterOnSharedPreferenceChangeListener(this);
  38. }
  39. public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
  40. String key) {
  41. // Check which preference has been changed
  42. if(key.equals("pref_external_location")){
  43. Preference pref = findPreference(key);
  44. EditTextPreference etp = (EditTextPreference) pref;
  45. String path = etp.getText();
  46. //Check if the Path is valid
  47. if(!path.startsWith("/"))
  48. path = new String("/").concat(path);
  49. if(!path.endsWith("/"))
  50. path = path.concat(new String("/"));
  51. if (!path.matches("/(([a-zA-Z_0-9])+/)*")){
  52. Toast.makeText(getApplicationContext(), "Path not valid. Must only contain a-zA-Z_0-9", Toast.LENGTH_SHORT).show();
  53. path = "/";
  54. }
  55. pref.setSummary(path);
  56. }
  57. else if(key.equals("pref_upload_server")){
  58. Preference pref = findPreference(key);
  59. EditTextPreference etp = (EditTextPreference) pref;
  60. pref.setSummary(etp.getText());
  61. }
  62. }
  63. }