PreferenceHostageFragment.java 3.5 KB

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