SettingsActivity.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.preference.PreferenceManager;
  10. import android.util.Log;
  11. import android.widget.Toast;
  12. /**
  13. * SettingsActivity creates the settings defined in /res/xml/preferences.xml.
  14. * @author Lars Pandikow.
  15. */
  16. public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
  17. @Override
  18. public void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. addPreferencesFromResource(R.xml.preferences);
  21. //Set the value of the preference as the summary for the preference
  22. Preference pref = findPreference("pref_external_location");
  23. EditTextPreference etp = (EditTextPreference) pref;
  24. pref.setSummary(etp.getText());
  25. //Set the value of the preference as the summary for the preference
  26. pref = findPreference("pref_upload_server");
  27. etp = (EditTextPreference) pref;
  28. pref.setSummary(etp.getText());
  29. SharedPreferences defaultPref = PreferenceManager.getDefaultSharedPreferences(this);
  30. //Set the value of the preference as the summary for the preference
  31. pref = findPreference("pref_max_connections");
  32. etp = (EditTextPreference) pref;
  33. defaultPref.edit().putInt("max_connections", Integer.valueOf(etp.getText()).intValue()).commit();
  34. pref.setSummary(etp.getText());
  35. //Set the value of the preference as the summary for the preference
  36. pref = findPreference("pref_timeout");
  37. etp = (EditTextPreference) pref;
  38. defaultPref.edit().putInt("timeout", Integer.valueOf(etp.getText()).intValue()).commit();
  39. pref.setSummary(etp.getText());
  40. }
  41. protected void onResume() {
  42. // register a listener to catch preference changes
  43. super.onResume();
  44. getPreferenceScreen().getSharedPreferences()
  45. .registerOnSharedPreferenceChangeListener(this);
  46. }
  47. protected void onPause() {
  48. super.onPause();
  49. getPreferenceScreen().getSharedPreferences()
  50. .unregisterOnSharedPreferenceChangeListener(this);
  51. }
  52. public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
  53. String key) {
  54. // Check which preference has been changed
  55. if(key.equals("pref_external_location")){
  56. Preference pref = findPreference(key);
  57. EditTextPreference etp = (EditTextPreference) pref;
  58. String path = etp.getText();
  59. //Check if the Path is valid
  60. if(!path.startsWith("/"))
  61. path = new String("/").concat(path);
  62. if(!path.endsWith("/"))
  63. path = path.concat(new String("/"));
  64. if (!path.matches("/(([a-zA-Z_0-9])+/)*")){
  65. Toast.makeText(this, "Path not valid. Must only contain a-zA-Z_0-9", Toast.LENGTH_SHORT).show();
  66. path = "/";
  67. sharedPreferences.edit().putString(key, path).commit();
  68. }
  69. pref.setSummary(path);
  70. }
  71. else if(key.equals("pref_upload_server")){
  72. Preference pref = findPreference(key);
  73. EditTextPreference etp = (EditTextPreference) pref;
  74. pref.setSummary(etp.getText());
  75. }
  76. else if(key.equals("pref_max_connections")){
  77. Preference pref = findPreference(key);
  78. EditTextPreference etp = (EditTextPreference) pref;
  79. String value = etp.getText();
  80. if(!value.matches("([0-9])+")){
  81. Toast.makeText(getApplicationContext(), "Enter a valid number.", Toast.LENGTH_SHORT).show();
  82. value = getResources().getString(R.string.pref_max_connections_default);
  83. }
  84. sharedPreferences.edit().putInt("max_connections", Integer.valueOf(value).intValue()).commit();
  85. pref.setSummary(value);
  86. }
  87. else if(key.equals("pref_timeout")){
  88. Preference pref = findPreference(key);
  89. EditTextPreference etp = (EditTextPreference) pref;
  90. String value = etp.getText();
  91. if(!value.matches("([0-9])+")){
  92. Toast.makeText(getApplicationContext(), "Enter a valid number.", Toast.LENGTH_SHORT).show();
  93. value = getResources().getString(R.string.pref_timeout_default);
  94. }
  95. sharedPreferences.edit().putInt("timeout", Integer.valueOf(value).intValue()).commit();
  96. pref.setSummary(value);
  97. }
  98. }
  99. }