PreferenceHostageFrament.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package de.tudarmstadt.informatik.hostage.ui2.fragment;
  2. import android.content.SharedPreferences;
  3. import android.os.Bundle;
  4. import android.preference.EditTextPreference;
  5. import android.preference.Preference;
  6. import android.preference.PreferenceFragment;
  7. import java.util.HashMap;
  8. import de.tudarmstadt.informatik.hostage.R;
  9. /**
  10. * Manages and creates the application preferences view
  11. *
  12. * @author Alexander Brakowski
  13. * @created 02.03.14 21:03
  14. */
  15. public class PreferenceHostageFrament extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
  16. /**
  17. * Maps an text preference to an suffix string
  18. */
  19. private HashMap<String, String> mSuffixMap;
  20. /**
  21. * {@inheritDoc}
  22. */
  23. @Override
  24. public void onCreate(Bundle savedInstanceState){
  25. super.onCreate(savedInstanceState);
  26. // these preferences are all text preferences
  27. String[] textPreferences = new String[]{
  28. "pref_external_location",
  29. "pref_upload_server",
  30. "pref_max_connections",
  31. "pref_timeout",
  32. "pref_sleeptime",
  33. "pref_location_time",
  34. "pref_location_retries"
  35. };
  36. // map the text preferences to suffixes
  37. this.mSuffixMap = new HashMap<String, String>();
  38. this.mSuffixMap.put("pref_timeout", "s");
  39. this.mSuffixMap.put("pref_sleeptime", "ms");
  40. this.mSuffixMap.put("pref_location_time", "ms");
  41. addPreferencesFromResource(R.xml.settings_preferences);
  42. for(String k: textPreferences){
  43. updatePreferenceSummary(k);
  44. }
  45. }
  46. /**
  47. * Updates the summary text of the given preference
  48. *
  49. * @param key the preference key
  50. */
  51. private void updatePreferenceSummary(String key){
  52. Preference p = findPreference(key);
  53. SharedPreferences sharedPreferences = this.getPreferenceManager().getSharedPreferences();
  54. if(p != null && p instanceof EditTextPreference){
  55. String suffix = "";
  56. if(this.mSuffixMap.containsKey(key)){
  57. suffix = this.mSuffixMap.get(key);
  58. }
  59. p.setSummary(sharedPreferences.getString(key, "") + " " + suffix);
  60. }
  61. }
  62. /**
  63. * {@inheritDoc}
  64. */
  65. @Override
  66. public void onResume() {
  67. super.onResume();
  68. getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
  69. }
  70. /**
  71. * {@inheritDoc}
  72. */
  73. @Override
  74. public void onPause() {
  75. getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
  76. super.onPause();
  77. }
  78. /* @Override
  79. public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
  80. super.onPreferenceTreeClick(preferenceScreen, preference);
  81. if (preference instanceof PreferenceScreen) {
  82. if(MainActivity.getInstance().mDisplayedFragment != null && MainActivity.getInstance().mDisplayedFragment instanceof UpNavigatible){
  83. ((UpNavigatible) MainActivity.getInstance().mDisplayedFragment).setUpNavigatible(true);
  84. ((UpNavigatible) MainActivity.getInstance().mDisplayedFragment).setUpFragment(SettingsFragment.class);
  85. MainActivity.getInstance().setDrawerIndicatorEnabled(false);
  86. return true;
  87. }
  88. }
  89. return false;
  90. }*/
  91. /**
  92. * {@inheritDoc}
  93. */
  94. @Override
  95. public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
  96. updatePreferenceSummary(key);
  97. }
  98. }