ProfileEditFragment.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package de.tudarmstadt.informatik.hostage.ui2.fragment;
  2. import android.app.ActionBar;
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.SharedPreferences;
  7. import android.database.Cursor;
  8. import android.graphics.Bitmap;
  9. import android.graphics.BitmapFactory;
  10. import android.graphics.drawable.BitmapDrawable;
  11. import android.net.Uri;
  12. import android.os.Bundle;
  13. import android.preference.CheckBoxPreference;
  14. import android.preference.EditTextPreference;
  15. import android.preference.Preference;
  16. import android.preference.PreferenceCategory;
  17. import android.preference.PreferenceFragment;
  18. import android.preference.PreferenceManager;
  19. import android.provider.MediaStore;
  20. import android.view.LayoutInflater;
  21. import android.view.View;
  22. import android.widget.LinearLayout;
  23. import java.util.HashMap;
  24. import de.tudarmstadt.informatik.hostage.R;
  25. import de.tudarmstadt.informatik.hostage.persistence.ProfileManager;
  26. import de.tudarmstadt.informatik.hostage.model.Profile;
  27. /**
  28. * Creates an preference screen to edit an profile
  29. *
  30. * @author Alexander Brakowski
  31. * @created 08.02.14 23:39
  32. */
  33. public class ProfileEditFragment extends PreferenceFragment implements
  34. SharedPreferences.OnSharedPreferenceChangeListener {
  35. /**
  36. * Holds the shared preference editor for out preference screen
  37. */
  38. private SharedPreferences.Editor mPrefs;
  39. /**
  40. * A map which mirrors the state protocols in the preferencescreen
  41. */
  42. private HashMap<String, Boolean> mProfileProtocols;
  43. /**
  44. * {@inheritDoc}
  45. */
  46. @Override
  47. public void onCreate(Bundle savedInstanceState){
  48. getActivity().getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
  49. super.onCreate(savedInstanceState);
  50. LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  51. // remove default action bar and replace it with an "done"/"discard" action bar
  52. View actionBarButtons = inflater.inflate(R.layout.actionbar_donebar, new LinearLayout(getActivity()), false);
  53. getActivity().getActionBar().setCustomView(actionBarButtons);
  54. View doneButton = actionBarButtons.findViewById(R.id.action_done);
  55. View cancelButton = actionBarButtons.findViewById(R.id.action_cancel);
  56. // add click listener for the save button
  57. doneButton.setOnClickListener(new View.OnClickListener() {
  58. @Override
  59. public void onClick(View v) {
  60. SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
  61. ProfileManager pmanager = ProfileManager.getInstance();
  62. Profile profile = getProfile();
  63. boolean createNew = false;
  64. // no profile was given to the fragment, which means this is a new profile
  65. if(profile == null){
  66. profile = new Profile();
  67. createNew = true;
  68. } else {
  69. // profile was given, if profile is not editable, clone the profile and make it editable
  70. if(!profile.isEditable()){
  71. profile = profile.cloneProfile();
  72. profile.mEditable = true;
  73. createNew = true;
  74. }
  75. }
  76. // update the profile object data with data from the preferences
  77. profile.mLabel = prefs.getString("pref_profile_general_name", profile.mLabel);
  78. profile.mIconPath = prefs.getString("pref_profile_general_image", profile.mIconPath);
  79. profile.mText = prefs.getString("pref_profile_general_description", profile.mText);
  80. profile.mGhostActive = prefs.getBoolean("pref_profile_protocols_ghost_active", profile.mGhostActive);
  81. profile.mGhostPorts = prefs.getString("pref_profile_protocols_ghost_text", "");
  82. if(profile.mGhostPorts.isEmpty()){
  83. profile.mGhostActive = false;
  84. }
  85. profile.mActiveProtocols = new HashMap<String, Boolean>(mProfileProtocols);
  86. // persist the changes of the profile
  87. if(createNew){
  88. profile.mId = -1;
  89. profile.mIconId = 0;
  90. profile.mIconName = "";
  91. profile.mIsRandom = false;
  92. profile.mIcon = null;
  93. pmanager.addProfile(profile);
  94. } else {
  95. pmanager.persistProfile(profile);
  96. }
  97. getActivity().finish();
  98. }
  99. });
  100. cancelButton.setOnClickListener(new View.OnClickListener() {
  101. @Override
  102. public void onClick(View v) {
  103. getActivity().finish();
  104. }
  105. });
  106. Profile profile = getProfile();
  107. mPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity()).edit();
  108. String pname = "",
  109. pimage = null,
  110. pdesc = "",
  111. pghost = "";
  112. boolean pbghost = false;
  113. if(profile != null){
  114. pname = profile.mLabel;
  115. pimage = profile.mIconPath;
  116. pdesc = profile.mText;
  117. pghost = profile.mGhostPorts;
  118. pbghost = profile.mGhostActive;
  119. }
  120. // fill the preferences of the preference screen with data from the profile object
  121. mPrefs.putString("pref_profile_general_name", pname);
  122. mPrefs.putString("pref_profile_general_image", pimage);
  123. mPrefs.putString("pref_profile_general_description", pdesc);
  124. mPrefs.putString("pref_profile_protocols_ghost_text", pghost);
  125. mPrefs.putBoolean("pref_profile_protocols_ghost_active", pbghost);
  126. mPrefs.commit();
  127. // create the preference view
  128. addPreferencesFromResource(R.xml.profile_preferences);
  129. Preference pref = findPreference("pref_profile_general_image");
  130. assert pref != null;
  131. if(profile != null){
  132. pref.setIcon(profile.getIconDrawable());
  133. mProfileProtocols = new HashMap<String, Boolean>(profile.mActiveProtocols);
  134. } else {
  135. mProfileProtocols = new HashMap<String, Boolean>();
  136. }
  137. // show an image chooser dialog when pressing the image preference
  138. pref.setOnPreferenceClickListener(
  139. new Preference.OnPreferenceClickListener() {
  140. @Override
  141. public boolean onPreferenceClick(Preference preference) {
  142. Intent intent = new Intent();
  143. intent.setType("image/*");
  144. intent.setAction(Intent.ACTION_GET_CONTENT);
  145. int PICK_IMAGE = 1;
  146. startActivityForResult(Intent.createChooser(intent, getString(R.string.select_icon)), PICK_IMAGE);
  147. return true;
  148. }
  149. }
  150. );
  151. if(profile != null){
  152. findPreference("pref_profile_general_name").setSummary(profile.mLabel);
  153. findPreference("pref_profile_general_description").setSummary(profile.mText);
  154. if(!profile.mGhostPorts.isEmpty()) findPreference("pref_profile_protocols_ghost_text").setSummary(profile.mGhostPorts);
  155. }
  156. if(profile == null || profile.isEditable()){
  157. getPreferenceScreen().removePreference(findPreference("pref_profile_warning"));
  158. }
  159. PreferenceCategory protocolsCategory = (PreferenceCategory) findPreference("pref_profile_protocols_settings");
  160. String[] protocols = getResources().getStringArray(R.array.protocols);
  161. String[] protocols_summary = getResources().getStringArray(R.array.protocols_description);
  162. // add all available protocols to the preference screen with an checkbox
  163. for(int i = 0; i<protocols.length; i++){
  164. if(protocols[i].equals("GHOST")) continue;
  165. mPrefs.putBoolean("pref_profile_protocol_" + protocols[i], profile != null && profile.isProtocolActive(protocols[i]));
  166. mPrefs.commit();
  167. CheckBoxPreference check = new CheckBoxPreference(getActivity());
  168. check.setTitle(protocols[i]);
  169. check.setKey("pref_profile_protocol_" + protocols[i]);
  170. check.setSummary(protocols_summary[i]);
  171. protocolsCategory.addPreference(check);
  172. }
  173. }
  174. /**
  175. * Retrieve the given profile from the intent
  176. * @return the profile
  177. */
  178. public Profile getProfile(){
  179. ProfileManager pmanager = ProfileManager.getInstance();
  180. Intent intent = getActivity().getIntent();
  181. int profile_id = intent.getIntExtra("profile_id", -1);
  182. if(profile_id != -1){
  183. return pmanager.getProfile(profile_id);
  184. }
  185. return null;
  186. }
  187. /**
  188. * {@inheritDoc}
  189. */
  190. @Override
  191. public void onResume() {
  192. super.onResume();
  193. getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
  194. }
  195. /**
  196. * {@inheritDoc}
  197. */
  198. @Override
  199. public void onPause() {
  200. getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
  201. super.onPause();
  202. }
  203. /**
  204. * Called when a shared preference is changed, added, or removed. This
  205. * may be called even if a preference is set to its existing value.
  206. *
  207. * <p>This callback will be run on your main thread.
  208. *
  209. * @param sharedPreferences The {@link android.content.SharedPreferences} that received
  210. * the change.
  211. * @param key The key of the preference that was changed, added, or
  212. */
  213. @Override
  214. public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
  215. Preference p = findPreference(key);
  216. if(p instanceof EditTextPreference){
  217. p.setSummary(sharedPreferences.getString(key, ""));
  218. } else if(p instanceof CheckBoxPreference && !p.getKey().equals("pref_profile_protocols_ghost_active")){
  219. mProfileProtocols.put(p.getTitle().toString(), ((CheckBoxPreference) p).isChecked());
  220. //System.out.println("------------------------------- P: " + ((CheckBoxPreference) p).isChecked());
  221. }
  222. }
  223. /**
  224. * {@inheritDoc}
  225. */
  226. @Override
  227. public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
  228. super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
  229. if(resultCode == Activity.RESULT_OK){
  230. Cursor cursor = getActivity().getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
  231. new String[]{
  232. MediaStore.Images.Media.DATA,
  233. MediaStore.Images.Media.DATE_ADDED,
  234. MediaStore.Images.ImageColumns.ORIENTATION
  235. },
  236. MediaStore.Images.Media.DATE_ADDED, null, "date_added ASC");
  237. String filePath = "";
  238. if(cursor != null && cursor.moveToFirst())
  239. {
  240. do {
  241. filePath = Uri.parse(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA))).toString();
  242. } while(cursor.moveToNext());
  243. cursor.close();
  244. }
  245. Preference pref = findPreference("pref_profile_general_image");
  246. BitmapFactory.Options options = new BitmapFactory.Options();
  247. options.inPreferredConfig = Bitmap.Config.ARGB_8888;
  248. Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
  249. pref.setIcon(new BitmapDrawable(getResources(), bitmap));
  250. mPrefs.putString("pref_profile_general_image", filePath);
  251. mPrefs.commit();
  252. }
  253. }
  254. }