SettingsActivity.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. //Set the value of the preference as the summary for the preference
  41. pref = findPreference("pref_sleeptime");
  42. etp = (EditTextPreference) pref;
  43. defaultPref.edit().putInt("sleeptime", Integer.valueOf(etp.getText()).intValue()).commit();
  44. }
  45. protected void onResume() {
  46. // register a listener to catch preference changes
  47. super.onResume();
  48. getPreferenceScreen().getSharedPreferences()
  49. .registerOnSharedPreferenceChangeListener(this);
  50. }
  51. protected void onPause() {
  52. super.onPause();
  53. getPreferenceScreen().getSharedPreferences()
  54. .unregisterOnSharedPreferenceChangeListener(this);
  55. }
  56. public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
  57. String key) {
  58. // Check which preference has been changed
  59. if(key.equals("pref_external_location")){
  60. Preference pref = findPreference(key);
  61. EditTextPreference etp = (EditTextPreference) pref;
  62. String path = etp.getText();
  63. //Check if the Path is valid
  64. if(!path.startsWith("/"))
  65. path = new String("/").concat(path);
  66. if(!path.endsWith("/"))
  67. path = path.concat(new String("/"));
  68. if (!path.matches("/(([a-zA-Z_0-9])+/)*")){
  69. Toast.makeText(this, "Path not valid. Must only contain a-zA-Z_0-9", Toast.LENGTH_SHORT).show();
  70. path = "/";
  71. sharedPreferences.edit().putString(key, path).commit();
  72. }
  73. pref.setSummary(path);
  74. }
  75. else if(key.equals("pref_upload_server")){
  76. Preference pref = findPreference(key);
  77. EditTextPreference etp = (EditTextPreference) pref;
  78. pref.setSummary(etp.getText());
  79. }
  80. else if(key.equals("pref_max_connections")){
  81. Preference pref = findPreference(key);
  82. EditTextPreference etp = (EditTextPreference) pref;
  83. String value = etp.getText();
  84. if(!value.matches("([0-9])+")){
  85. Toast.makeText(getApplicationContext(), "Enter a valid number.", Toast.LENGTH_SHORT).show();
  86. value = getResources().getString(R.string.pref_max_connections_default);
  87. }
  88. sharedPreferences.edit().putInt("max_connections", Integer.valueOf(value).intValue()).commit();
  89. pref.setSummary(value);
  90. }
  91. else if(key.equals("pref_timeout")){
  92. Preference pref = findPreference(key);
  93. EditTextPreference etp = (EditTextPreference) pref;
  94. String value = etp.getText();
  95. if(!value.matches("([0-9])+")){
  96. Toast.makeText(getApplicationContext(), "Enter a valid number.", Toast.LENGTH_SHORT).show();
  97. value = getResources().getString(R.string.pref_timeout_default);
  98. }
  99. sharedPreferences.edit().putInt("timeout", Integer.valueOf(value).intValue()).commit();
  100. pref.setSummary(value);
  101. }
  102. else if(key.equals("pref_sleeptime")){
  103. Preference pref = findPreference(key);
  104. EditTextPreference etp = (EditTextPreference) pref;
  105. String value = etp.getText();
  106. if(!value.matches("([0-9])+")){
  107. Toast.makeText(getApplicationContext(), "Enter a valid number.", Toast.LENGTH_SHORT).show();
  108. value = getResources().getString(R.string.pref_sleeptime_default);
  109. }
  110. sharedPreferences.edit().putInt("sleeptime", Integer.valueOf(value).intValue()).commit();
  111. pref.setSummary(value);
  112. }
  113. }
  114. }