PreferenceHostageFrament.java 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package de.tudarmstadt.informatik.hostage.ui.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. * @author Alexander Brakowski
  11. * @created 02.03.14 21:03
  12. */
  13. public class PreferenceHostageFrament extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
  14. private HashMap<String, String> mSuffixMap;
  15. private String[] mTextPreferences;
  16. @Override
  17. public void onCreate(Bundle savedInstanceState){
  18. super.onCreate(savedInstanceState);
  19. this.mTextPreferences = new String[]{
  20. "pref_external_location",
  21. "pref_upload_server",
  22. "pref_max_connections",
  23. "pref_timeout",
  24. "pref_sleeptime",
  25. "pref_location_time",
  26. "pref_location_retries"
  27. };
  28. this.mSuffixMap = new HashMap<String, String>();
  29. this.mSuffixMap.put("pref_timeout", "s");
  30. this.mSuffixMap.put("pref_sleeptime", "ms");
  31. this.mSuffixMap.put("pref_location_time", "ms");
  32. addPreferencesFromResource(R.xml.settings_preferences);
  33. for(String k: this.mTextPreferences){
  34. updatePreferenceSummary(k);
  35. }
  36. }
  37. private void updatePreferenceSummary(String key){
  38. Preference p = findPreference(key);
  39. SharedPreferences sharedPreferences = this.getPreferenceManager().getSharedPreferences();
  40. if(p != null && p instanceof EditTextPreference){
  41. String suffix = "";
  42. if(this.mSuffixMap.containsKey(key)){
  43. suffix = this.mSuffixMap.get(key);
  44. }
  45. p.setSummary(sharedPreferences.getString(key, "") + " " + suffix);
  46. }
  47. }
  48. @Override
  49. public void onResume() {
  50. super.onResume();
  51. getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
  52. }
  53. @Override
  54. public void onPause() {
  55. getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
  56. super.onPause();
  57. }
  58. /* @Override
  59. public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
  60. super.onPreferenceTreeClick(preferenceScreen, preference);
  61. if (preference instanceof PreferenceScreen) {
  62. if(MainActivity.getInstance().mDisplayedFragment != null && MainActivity.getInstance().mDisplayedFragment instanceof UpNavigatible){
  63. ((UpNavigatible) MainActivity.getInstance().mDisplayedFragment).setUpNavigatible(true);
  64. ((UpNavigatible) MainActivity.getInstance().mDisplayedFragment).setUpFragment(SettingsFragment.class);
  65. MainActivity.getInstance().setDrawerIndicatorEnabled(false);
  66. return true;
  67. }
  68. }
  69. return false;
  70. }*/
  71. @Override
  72. public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
  73. updatePreferenceSummary(key);
  74. }
  75. }