ProfileEditFragment.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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.mGhostActive = prefs.getBoolean("pref_profile_protocols_ghost_active", profile.mGhostActive);
  68. profile.mGhostPorts = prefs.getString("pref_profile_protocols_ghost_text", "");
  69. if(profile.mGhostPorts.isEmpty()){
  70. profile.mGhostActive = false;
  71. }
  72. profile.mActiveProtocols = new HashMap<String, Boolean>(profileProtocols);
  73. if(createNew){
  74. profile.mId = -1;
  75. profile.mIconId = 0;
  76. profile.mIconName = "";
  77. profile.mIsRandom = false;
  78. profile.mIcon = null;
  79. pmanager.addProfile(profile);
  80. } else {
  81. pmanager.persistProfile(profile);
  82. }
  83. getActivity().finish();
  84. }
  85. });
  86. cancelButton.setOnClickListener(new View.OnClickListener() {
  87. @Override
  88. public void onClick(View v) {
  89. getActivity().finish();
  90. }
  91. });
  92. Profile profile = getProfile();
  93. prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()).edit();
  94. String pname = "",
  95. pimage = null,
  96. pdesc = "",
  97. pghost = "";
  98. boolean pbghost = false;
  99. if(profile != null){
  100. pname = profile.mLabel;
  101. pimage = profile.mIconPath;
  102. pdesc = profile.mText;
  103. pghost = profile.mGhostPorts;
  104. pbghost = profile.mGhostActive;
  105. }
  106. prefs.putString("pref_profile_general_name", pname);
  107. prefs.putString("pref_profile_general_image", pimage);
  108. prefs.putString("pref_profile_general_description", pdesc);
  109. prefs.putString("pref_profile_protocols_ghost_text", pghost);
  110. prefs.putBoolean("pref_profile_protocols_ghost_active", pbghost);
  111. prefs.commit();
  112. addPreferencesFromResource(R.xml.profile_preferences);
  113. Preference pref = findPreference("pref_profile_general_image");
  114. assert pref != null;
  115. if(profile != null){
  116. pref.setIcon(profile.getIconDrawable());
  117. profileProtocols = new HashMap<String, Boolean>(profile.mActiveProtocols);
  118. } else {
  119. profileProtocols = new HashMap<String, Boolean>();
  120. }
  121. pref.setOnPreferenceClickListener(
  122. new Preference.OnPreferenceClickListener() {
  123. @Override
  124. public boolean onPreferenceClick(Preference preference) {
  125. Intent intent = new Intent();
  126. intent.setType("image/*");
  127. intent.setAction(Intent.ACTION_GET_CONTENT);
  128. int PICK_IMAGE = 1;
  129. startActivityForResult(Intent.createChooser(intent, getString(R.string.select_icon)), PICK_IMAGE);
  130. return true;
  131. }
  132. }
  133. );
  134. if(profile != null){
  135. findPreference("pref_profile_general_name").setSummary(profile.mLabel);
  136. findPreference("pref_profile_general_description").setSummary(profile.mText);
  137. if(!profile.mGhostPorts.isEmpty()) findPreference("pref_profile_protocols_ghost_text").setSummary(profile.mGhostPorts);
  138. }
  139. if(profile == null || profile.isEditable()){
  140. getPreferenceScreen().removePreference(findPreference("pref_profile_warning"));
  141. }
  142. PreferenceCategory protocolsCategory = (PreferenceCategory) findPreference("pref_profile_protocols_settings");
  143. String[] protocols = getResources().getStringArray(R.array.protocols);
  144. String[] protocols_summary = getResources().getStringArray(R.array.protocols_description);
  145. for(int i = 0; i<protocols.length; i++){
  146. if(protocols[i].equals("GHOST")) continue;
  147. prefs.putBoolean("pref_profile_protocol_" + protocols[i], profile != null && profile.isProtocolActive(protocols[i]));
  148. prefs.commit();
  149. CheckBoxPreference check = new CheckBoxPreference(getActivity());
  150. check.setTitle(protocols[i]);
  151. check.setKey("pref_profile_protocol_" + protocols[i]);
  152. check.setSummary(protocols_summary[i]);
  153. //check.setChecked(profile != null && profile.isProtocolActive(protocols[i]));
  154. //System.out.println("-----------------_> " + profile.isProtocolActive(protocols[i]) + " :: " + protocols[i]);
  155. protocolsCategory.addPreference(check);
  156. }
  157. }
  158. public Profile getProfile(){
  159. ProfileManager pmanager = ProfileManager.getInstance();
  160. Intent intent = getActivity().getIntent();
  161. //Profile profile = (Profile) intent.getSerializableExtra("profile");
  162. int profile_id = intent.getIntExtra("profile_id", -1);
  163. if(profile_id != -1){
  164. return pmanager.getProfile(profile_id);
  165. }
  166. return null;
  167. }
  168. @Override
  169. public void onResume() {
  170. super.onResume();
  171. getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
  172. }
  173. @Override
  174. public void onPause() {
  175. getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
  176. super.onPause();
  177. }
  178. /**
  179. * Called when a shared preference is changed, added, or removed. This
  180. * may be called even if a preference is set to its existing value.
  181. *
  182. * <p>This callback will be run on your main thread.
  183. *
  184. * @param sharedPreferences The {@link android.content.SharedPreferences} that received
  185. * the change.
  186. * @param key The key of the preference that was changed, added, or
  187. */
  188. @Override
  189. public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
  190. Preference p = findPreference(key);
  191. if(p instanceof EditTextPreference){
  192. p.setSummary(sharedPreferences.getString(key, ""));
  193. } else if(p instanceof CheckBoxPreference && !p.getKey().equals("pref_profile_protocols_ghost_active")){
  194. profileProtocols.put(p.getTitle().toString(), ((CheckBoxPreference) p).isChecked());
  195. //System.out.println("------------------------------- P: " + ((CheckBoxPreference) p).isChecked());
  196. }
  197. }
  198. @Override
  199. public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
  200. super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
  201. if(resultCode == Activity.RESULT_OK){
  202. Uri selectedImage = imageReturnedIntent.getData();
  203. String[] filePathColumn = {MediaStore.Images.Media.DATA};
  204. assert selectedImage != null;
  205. Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
  206. assert cursor != null;
  207. cursor.moveToFirst();
  208. int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
  209. String filePath = cursor.getString(columnIndex);
  210. cursor.close();
  211. Preference pref = findPreference("pref_profile_general_image");
  212. BitmapFactory.Options options = new BitmapFactory.Options();
  213. options.inPreferredConfig = Bitmap.Config.ARGB_8888;
  214. Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
  215. assert pref != null;
  216. pref.setIcon(new BitmapDrawable(getResources(), bitmap));
  217. prefs.putString("pref_profile_general_image", filePath);
  218. prefs.commit();
  219. }
  220. }
  221. }