ProfileEditFragment.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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.MultiSelectListPreference;
  16. import android.preference.Preference;
  17. import android.preference.PreferenceCategory;
  18. import android.preference.PreferenceFragment;
  19. import android.preference.PreferenceManager;
  20. import android.preference.PreferenceScreen;
  21. import android.provider.MediaStore;
  22. import android.view.LayoutInflater;
  23. import android.view.View;
  24. import android.widget.LinearLayout;
  25. import java.util.HashMap;
  26. import de.tudarmstadt.informatik.hostage.R;
  27. import de.tudarmstadt.informatik.hostage.dao.ProfileManager;
  28. import de.tudarmstadt.informatik.hostage.model.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. private LayoutInflater mInflater;
  36. private SharedPreferences.Editor prefs;
  37. private HashMap<String, Boolean> profileProtocols;
  38. @Override
  39. public void onCreate(Bundle savedInstanceState){
  40. getActivity().getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
  41. super.onCreate(savedInstanceState);
  42. mInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  43. View actionBarButtons = mInflater.inflate(R.layout.actionbar_donebar, new LinearLayout(getActivity()), false);
  44. getActivity().getActionBar().setCustomView(actionBarButtons);
  45. View doneButton = actionBarButtons.findViewById(R.id.action_done);
  46. View cancelButton = actionBarButtons.findViewById(R.id.action_cancel);
  47. doneButton.setOnClickListener(new View.OnClickListener() {
  48. @Override
  49. public void onClick(View v) {
  50. SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
  51. ProfileManager pmanager = ProfileManager.getInstance();
  52. Profile profile = getProfile();
  53. boolean createNew = false;
  54. if(profile == null){
  55. profile = new Profile();
  56. createNew = true;
  57. } else {
  58. if(!profile.isEditable()){
  59. profile = profile.cloneProfile();
  60. profile.mEditable = true;
  61. createNew = true;
  62. }
  63. }
  64. profile.mLabel = prefs.getString("pref_profile_general_name", profile.mLabel);
  65. profile.mIconPath = prefs.getString("pref_profile_general_image", profile.mIconPath);
  66. profile.mText = prefs.getString("pref_profile_general_description", profile.mText);
  67. profile.mActiveProtocols = new HashMap<String, Boolean>(profileProtocols);
  68. if(createNew){
  69. profile.mId = -1;
  70. profile.mIconId = 0;
  71. profile.mIconName = "";
  72. profile.mIsRandom = false;
  73. profile.mIcon = null;
  74. pmanager.addProfile(profile);
  75. } else {
  76. pmanager.persistProfile(profile);
  77. }
  78. getActivity().finish();
  79. }
  80. });
  81. cancelButton.setOnClickListener(new View.OnClickListener() {
  82. @Override
  83. public void onClick(View v) {
  84. getActivity().finish();
  85. }
  86. });
  87. Profile profile = getProfile();
  88. prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()).edit();
  89. String pname = "",
  90. pimage = null,
  91. pdesc = "";
  92. if(profile != null){
  93. pname = profile.mLabel;
  94. pimage = profile.mIconPath;
  95. pdesc = profile.mText;
  96. }
  97. prefs.putString("pref_profile_general_name", pname);
  98. prefs.putString("pref_profile_general_image", pimage);
  99. prefs.putString("pref_profile_general_description", pdesc);
  100. prefs.commit();
  101. addPreferencesFromResource(R.xml.profile_preferences);
  102. Preference pref = findPreference("pref_profile_general_image");
  103. assert pref != null;
  104. if(profile != null){
  105. pref.setIcon(profile.getIconDrawable());
  106. profileProtocols = new HashMap<String, Boolean>(profile.mActiveProtocols);
  107. } else {
  108. profileProtocols = new HashMap<String, Boolean>();
  109. }
  110. pref.setOnPreferenceClickListener(
  111. new Preference.OnPreferenceClickListener() {
  112. @Override
  113. public boolean onPreferenceClick(Preference preference) {
  114. Intent intent = new Intent();
  115. intent.setType("image/*");
  116. intent.setAction(Intent.ACTION_GET_CONTENT);
  117. int PICK_IMAGE = 1;
  118. startActivityForResult(Intent.createChooser(intent, getString(R.string.select_icon)), PICK_IMAGE);
  119. return true;
  120. }
  121. }
  122. );
  123. if(profile != null){
  124. findPreference("pref_profile_general_name").setSummary(profile.mLabel);
  125. findPreference("pref_profile_general_description").setSummary(profile.mText);
  126. }
  127. if(profile == null || profile.isEditable()){
  128. getPreferenceScreen().removePreference(findPreference("pref_profile_warning"));
  129. }
  130. PreferenceCategory protocolsCategory = (PreferenceCategory) findPreference("pref_profile_protocols_settings");
  131. String[] protocols = getResources().getStringArray(R.array.protocols);
  132. String[] protocols_summary = getResources().getStringArray(R.array.protocols_description);
  133. for(int i = 0; i<protocols.length; i++){
  134. prefs.putBoolean("pref_profile_protocol_" + protocols[i], profile != null && profile.isProtocolActive(protocols[i]));
  135. prefs.commit();
  136. CheckBoxPreference check = new CheckBoxPreference(getActivity());
  137. check.setTitle(protocols[i]);
  138. check.setKey("pref_profile_protocol_" + protocols[i]);
  139. check.setSummary(protocols_summary[i]);
  140. //check.setChecked(profile != null && profile.isProtocolActive(protocols[i]));
  141. //System.out.println("-----------------_> " + profile.isProtocolActive(protocols[i]) + " :: " + protocols[i]);
  142. protocolsCategory.addPreference(check);
  143. }
  144. }
  145. public Profile getProfile(){
  146. ProfileManager pmanager = ProfileManager.getInstance();
  147. Intent intent = getActivity().getIntent();
  148. //Profile profile = (Profile) intent.getSerializableExtra("profile");
  149. int profile_id = intent.getIntExtra("profile_id", -1);
  150. if(profile_id != -1){
  151. return pmanager.getProfile(profile_id);
  152. }
  153. return null;
  154. }
  155. @Override
  156. public void onResume() {
  157. super.onResume();
  158. getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
  159. }
  160. @Override
  161. public void onPause() {
  162. getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
  163. super.onPause();
  164. }
  165. /**
  166. * Called when a shared preference is changed, added, or removed. This
  167. * may be called even if a preference is set to its existing value.
  168. *
  169. * <p>This callback will be run on your main thread.
  170. *
  171. * @param sharedPreferences The {@link android.content.SharedPreferences} that received
  172. * the change.
  173. * @param key The key of the preference that was changed, added, or
  174. */
  175. @Override
  176. public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
  177. Preference p = findPreference(key);
  178. if(p instanceof EditTextPreference){
  179. p.setSummary(sharedPreferences.getString(key, ""));
  180. } else if(p instanceof CheckBoxPreference){
  181. profileProtocols.put(p.getTitle().toString(), ((CheckBoxPreference) p).isChecked());
  182. //System.out.println("------------------------------- P: " + ((CheckBoxPreference) p).isChecked());
  183. }
  184. }
  185. @Override
  186. public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
  187. super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
  188. if(resultCode == Activity.RESULT_OK){
  189. Uri selectedImage = imageReturnedIntent.getData();
  190. String[] filePathColumn = {MediaStore.Images.Media.DATA};
  191. assert selectedImage != null;
  192. Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
  193. assert cursor != null;
  194. cursor.moveToFirst();
  195. int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
  196. String filePath = cursor.getString(columnIndex);
  197. cursor.close();
  198. Preference pref = findPreference("pref_profile_general_image");
  199. BitmapFactory.Options options = new BitmapFactory.Options();
  200. options.inPreferredConfig = Bitmap.Config.ARGB_8888;
  201. Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
  202. assert pref != null;
  203. pref.setIcon(new BitmapDrawable(getResources(), bitmap));
  204. prefs.putString("pref_profile_general_image", filePath);
  205. prefs.commit();
  206. }
  207. }
  208. }