ProfileEditFragment.java 10 KB

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