ProfileEditFragment.java 9.3 KB

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