SettingsActivity.java 5.5 KB

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