SettingsActivity.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package de.tudarmstadt.informatik.hostage.ui;
  2. import android.content.SharedPreferences;
  3. import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
  4. import android.os.Bundle;
  5. import android.preference.EditTextPreference;
  6. import android.preference.Preference;
  7. import android.preference.PreferenceActivity;
  8. import android.preference.PreferenceManager;
  9. import android.widget.Toast;
  10. import de.tudarmstadt.informatik.hostage.R;
  11. /**
  12. * SettingsActivity creates the settings defined in /res/xml/preferences.xml.
  13. *
  14. * @author Lars Pandikow.
  15. */
  16. public class SettingsActivity extends PreferenceActivity implements
  17. OnSharedPreferenceChangeListener {
  18. @Override
  19. public void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. addPreferencesFromResource(R.xml.preferences);
  22. // Set the value of the preference as the summary for the preference
  23. Preference pref = findPreference("pref_external_location");
  24. EditTextPreference etp = (EditTextPreference) pref;
  25. pref.setSummary(etp.getText());
  26. // Set the value of the preference as the summary for the preference
  27. pref = findPreference("pref_upload_server");
  28. etp = (EditTextPreference) pref;
  29. pref.setSummary(etp.getText());
  30. SharedPreferences defaultPref = PreferenceManager
  31. .getDefaultSharedPreferences(this);
  32. // Set the value of the preference as the summary for the preference
  33. pref = findPreference("pref_max_connections");
  34. etp = (EditTextPreference) pref;
  35. defaultPref
  36. .edit()
  37. .putInt("max_connections",
  38. 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_timeout");
  42. etp = (EditTextPreference) pref;
  43. defaultPref.edit()
  44. .putInt("timeout", Integer.valueOf(etp.getText()).intValue())
  45. .commit();
  46. pref.setSummary(etp.getText());
  47. // Set the value of the preference as the summary for the preference
  48. pref = findPreference("pref_sleeptime");
  49. etp = (EditTextPreference) pref;
  50. defaultPref.edit()
  51. .putInt("sleeptime", Integer.valueOf(etp.getText()).intValue())
  52. .commit();
  53. pref.setSummary(etp.getText());
  54. // Set the value of the preference as the summary for the preference
  55. pref = findPreference("pref_location_time");
  56. etp = (EditTextPreference) pref;
  57. defaultPref
  58. .edit()
  59. .putInt("location_time",
  60. Integer.valueOf(etp.getText()).intValue()).commit();
  61. pref.setSummary(etp.getText());
  62. // Set the value of the preference as the summary for the preference
  63. pref = findPreference("pref_location_retries");
  64. etp = (EditTextPreference) pref;
  65. defaultPref
  66. .edit()
  67. .putInt("location_retries",
  68. Integer.valueOf(etp.getText()).intValue()).commit();
  69. pref.setSummary(etp.getText());
  70. }
  71. @Override
  72. public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
  73. String key) {
  74. // Check which preference has been changed
  75. if (key.equals("pref_external_location")) {
  76. Preference pref = findPreference(key);
  77. EditTextPreference etp = (EditTextPreference) pref;
  78. String path = etp.getText();
  79. // Check if the Path is valid
  80. if (!path.startsWith("/"))
  81. path = new String("/").concat(path);
  82. if (!path.endsWith("/"))
  83. path = path.concat(new String("/"));
  84. if (!path.matches("/(([a-zA-Z_0-9])+/)*")) {
  85. Toast.makeText(this,
  86. "Path not valid. Must only contain a-zA-Z_0-9",
  87. Toast.LENGTH_SHORT).show();
  88. path = "/";
  89. sharedPreferences.edit().putString(key, path).commit();
  90. }
  91. pref.setSummary(path);
  92. } else if (key.equals("pref_upload_server")) {
  93. Preference pref = findPreference(key);
  94. EditTextPreference etp = (EditTextPreference) pref;
  95. pref.setSummary(etp.getText());
  96. } else if (key.equals("pref_max_connections")) {
  97. Preference pref = findPreference(key);
  98. EditTextPreference etp = (EditTextPreference) pref;
  99. String value = etp.getText();
  100. if (!value.matches("([0-9])+")) {
  101. Toast.makeText(getApplicationContext(),
  102. "Enter a valid number.", Toast.LENGTH_SHORT).show();
  103. value = getResources().getString(
  104. R.string.pref_max_connections_default);
  105. }
  106. sharedPreferences
  107. .edit()
  108. .putInt("max_connections",
  109. Integer.valueOf(value).intValue()).commit();
  110. pref.setSummary(value);
  111. } else if (key.equals("pref_timeout")) {
  112. Preference pref = findPreference(key);
  113. EditTextPreference etp = (EditTextPreference) pref;
  114. String value = etp.getText();
  115. if (!value.matches("([0-9])+")) {
  116. Toast.makeText(getApplicationContext(),
  117. "Enter a valid number.", Toast.LENGTH_SHORT).show();
  118. value = getResources().getString(R.string.pref_timeout_default);
  119. }
  120. sharedPreferences.edit()
  121. .putInt("timeout", Integer.valueOf(value).intValue())
  122. .commit();
  123. pref.setSummary(value);
  124. } else if (key.equals("pref_sleeptime")) {
  125. Preference pref = findPreference(key);
  126. EditTextPreference etp = (EditTextPreference) pref;
  127. String value = etp.getText();
  128. if (!value.matches("([0-9])+")) {
  129. Toast.makeText(getApplicationContext(),
  130. "Enter a valid number.", Toast.LENGTH_SHORT).show();
  131. value = getResources().getString(
  132. R.string.pref_sleeptime_default);
  133. }
  134. sharedPreferences.edit()
  135. .putInt("sleeptime", Integer.valueOf(value).intValue())
  136. .commit();
  137. pref.setSummary(value);
  138. } else if (key.equals("pref_location_time")) {
  139. Preference pref = findPreference(key);
  140. EditTextPreference etp = (EditTextPreference) pref;
  141. String value = etp.getText();
  142. if (!value.matches("([0-9])+")) {
  143. Toast.makeText(getApplicationContext(),
  144. "Enter a valid number.", Toast.LENGTH_SHORT).show();
  145. value = getResources().getString(
  146. R.string.pref_location_time_default);
  147. }
  148. sharedPreferences.edit()
  149. .putInt("location_time", Integer.valueOf(value).intValue())
  150. .commit();
  151. pref.setSummary(value);
  152. } else if (key.equals("pref_location_retries")) {
  153. Preference pref = findPreference(key);
  154. EditTextPreference etp = (EditTextPreference) pref;
  155. String value = etp.getText();
  156. if (!value.matches("([0-9])+")) {
  157. Toast.makeText(getApplicationContext(),
  158. "Enter a valid number.", Toast.LENGTH_SHORT).show();
  159. value = getResources().getString(
  160. R.string.pref_location_retries_default);
  161. }
  162. sharedPreferences
  163. .edit()
  164. .putInt("location_retries",
  165. Integer.valueOf(value).intValue()).commit();
  166. pref.setSummary(value);
  167. }
  168. }
  169. @Override
  170. protected void onPause() {
  171. super.onPause();
  172. getPreferenceScreen().getSharedPreferences()
  173. .unregisterOnSharedPreferenceChangeListener(this);
  174. }
  175. @Override
  176. protected void onResume() {
  177. // register a listener to catch preference changes
  178. super.onResume();
  179. getPreferenceScreen().getSharedPreferences()
  180. .registerOnSharedPreferenceChangeListener(this);
  181. }
  182. }