SettingsActivity.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. pref.setSummary(etp.getText());
  44. //Set the value of the preference as the summary for the preference
  45. pref = findPreference("pref_location_time");
  46. etp = (EditTextPreference) pref;
  47. defaultPref.edit().putInt("location_time", Integer.valueOf(etp.getText()).intValue()).commit();
  48. pref.setSummary(etp.getText());
  49. //Set the value of the preference as the summary for the preference
  50. pref = findPreference("pref_location_retries");
  51. etp = (EditTextPreference) pref;
  52. defaultPref.edit().putInt("location_retries", Integer.valueOf(etp.getText()).intValue()).commit();
  53. pref.setSummary(etp.getText());
  54. }
  55. protected void onResume() {
  56. // register a listener to catch preference changes
  57. super.onResume();
  58. getPreferenceScreen().getSharedPreferences()
  59. .registerOnSharedPreferenceChangeListener(this);
  60. }
  61. protected void onPause() {
  62. super.onPause();
  63. getPreferenceScreen().getSharedPreferences()
  64. .unregisterOnSharedPreferenceChangeListener(this);
  65. }
  66. public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
  67. String key) {
  68. // Check which preference has been changed
  69. if(key.equals("pref_external_location")){
  70. Preference pref = findPreference(key);
  71. EditTextPreference etp = (EditTextPreference) pref;
  72. String path = etp.getText();
  73. //Check if the Path is valid
  74. if(!path.startsWith("/"))
  75. path = new String("/").concat(path);
  76. if(!path.endsWith("/"))
  77. path = path.concat(new String("/"));
  78. if (!path.matches("/(([a-zA-Z_0-9])+/)*")){
  79. Toast.makeText(this, "Path not valid. Must only contain a-zA-Z_0-9", Toast.LENGTH_SHORT).show();
  80. path = "/";
  81. sharedPreferences.edit().putString(key, path).commit();
  82. }
  83. pref.setSummary(path);
  84. }
  85. else if(key.equals("pref_upload_server")){
  86. Preference pref = findPreference(key);
  87. EditTextPreference etp = (EditTextPreference) pref;
  88. pref.setSummary(etp.getText());
  89. }
  90. else if(key.equals("pref_max_connections")){
  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_max_connections_default);
  97. }
  98. sharedPreferences.edit().putInt("max_connections", Integer.valueOf(value).intValue()).commit();
  99. pref.setSummary(value);
  100. }
  101. else if(key.equals("pref_timeout")){
  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_timeout_default);
  108. }
  109. sharedPreferences.edit().putInt("timeout", Integer.valueOf(value).intValue()).commit();
  110. pref.setSummary(value);
  111. }
  112. else if(key.equals("pref_sleeptime")){
  113. Preference pref = findPreference(key);
  114. EditTextPreference etp = (EditTextPreference) pref;
  115. String value = etp.getText();
  116. if(!value.matches("([0-9])+")){
  117. Toast.makeText(getApplicationContext(), "Enter a valid number.", Toast.LENGTH_SHORT).show();
  118. value = getResources().getString(R.string.pref_sleeptime_default);
  119. }
  120. sharedPreferences.edit().putInt("sleeptime", Integer.valueOf(value).intValue()).commit();
  121. pref.setSummary(value);
  122. }
  123. else if(key.equals("pref_location_time")){
  124. Preference pref = findPreference(key);
  125. EditTextPreference etp = (EditTextPreference) pref;
  126. String value = etp.getText();
  127. if(!value.matches("([0-9])+")){
  128. Toast.makeText(getApplicationContext(), "Enter a valid number.", Toast.LENGTH_SHORT).show();
  129. value = getResources().getString(R.string.pref_location_time_default);
  130. }
  131. sharedPreferences.edit().putInt("location_time", Integer.valueOf(value).intValue()).commit();
  132. pref.setSummary(value);
  133. }
  134. else if(key.equals("pref_location_retries")){
  135. Preference pref = findPreference(key);
  136. EditTextPreference etp = (EditTextPreference) pref;
  137. String value = etp.getText();
  138. if(!value.matches("([0-9])+")){
  139. Toast.makeText(getApplicationContext(), "Enter a valid number.", Toast.LENGTH_SHORT).show();
  140. value = getResources().getString(R.string.pref_location_retries_default);
  141. }
  142. sharedPreferences.edit().putInt("location_retries", Integer.valueOf(value).intValue()).commit();
  143. pref.setSummary(value);
  144. }
  145. }
  146. }