123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- package de.tudarmstadt.informatik.hostage.ui.fragment;
- import android.app.ActionBar;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.database.Cursor;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.drawable.BitmapDrawable;
- import android.net.Uri;
- import android.os.Bundle;
- import android.preference.CheckBoxPreference;
- import android.preference.EditTextPreference;
- import android.preference.Preference;
- import android.preference.PreferenceCategory;
- import android.preference.PreferenceFragment;
- import android.preference.PreferenceManager;
- import android.provider.MediaStore;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.LinearLayout;
- import java.util.HashMap;
- import de.tudarmstadt.informatik.hostage.R;
- import de.tudarmstadt.informatik.hostage.persistence.ProfileManager;
- import de.tudarmstadt.informatik.hostage.model.Profile;
- /**
- * Creates an preference screen to edit an profile
- *
- * @author Alexander Brakowski
- * @created 08.02.14 23:39
- */
- public class ProfileEditFragment extends PreferenceFragment implements
- SharedPreferences.OnSharedPreferenceChangeListener {
- /**
- * Holds the shared preference editor for out preference screen
- */
- private SharedPreferences.Editor mPrefs;
- /**
- * A map which mirrors the state protocols in the preferencescreen
- */
- private HashMap<String, Boolean> mProfileProtocols;
- /**
- * {@inheritDoc}
- */
- @Override
- public void onCreate(Bundle savedInstanceState){
- getActivity().getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
- super.onCreate(savedInstanceState);
- LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- // remove default action bar and replace it with an "done"/"discard" action bar
- View actionBarButtons = inflater.inflate(R.layout.actionbar_donebar, new LinearLayout(getActivity()), false);
- getActivity().getActionBar().setCustomView(actionBarButtons);
- View doneButton = actionBarButtons.findViewById(R.id.action_done);
- View cancelButton = actionBarButtons.findViewById(R.id.action_cancel);
- // add click listener for the save button
- doneButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
- ProfileManager pmanager = ProfileManager.getInstance();
- Profile profile = getProfile();
- boolean createNew = false;
- // no profile was given to the fragment, which means this is a new profile
- if(profile == null){
- profile = new Profile();
- createNew = true;
- } else {
- // profile was given, if profile is not editable, clone the profile and make it editable
- if(!profile.isEditable()){
- profile = profile.cloneProfile();
- profile.mEditable = true;
- createNew = true;
- }
- }
- // update the profile object data with data from the preferences
- profile.mLabel = prefs.getString("pref_profile_general_name", profile.mLabel);
- profile.mIconPath = prefs.getString("pref_profile_general_image", profile.mIconPath);
- profile.mText = prefs.getString("pref_profile_general_description", profile.mText);
- profile.mGhostActive = prefs.getBoolean("pref_profile_protocols_ghost_active", profile.mGhostActive);
- profile.mGhostPorts = prefs.getString("pref_profile_protocols_ghost_text", "");
- if(profile.mLabel == null || profile.mLabel.isEmpty()){
- new AlertDialog.Builder(getActivity()).setTitle(R.string.information).setMessage(R.string.profile_needs_name)
- .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- }
- }).setIcon(android.R.drawable.ic_dialog_info).show();
- return;
- }
- if(profile.mGhostPorts.isEmpty()){
- profile.mGhostActive = false;
- }
- profile.mActiveProtocols = new HashMap<String, Boolean>(mProfileProtocols);
- // persist the changes of the profile
- if(createNew){
- profile.mId = -1;
- profile.mIconId = 0;
- profile.mIconName = "";
- profile.mIsRandom = false;
- profile.mIcon = null;
- pmanager.addProfile(profile);
- } else {
- pmanager.persistProfile(profile);
- }
- getActivity().finish();
- }
- });
- cancelButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- getActivity().finish();
- }
- });
- Profile profile = getProfile();
- mPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity()).edit();
- String pname = "",
- pimage = null,
- pdesc = "",
- pghost = "";
- boolean pbghost = false;
- if(profile != null){
- pname = profile.mLabel;
- pimage = profile.mIconPath;
- pdesc = profile.mText;
- pghost = profile.mGhostPorts;
- pbghost = profile.mGhostActive;
- }
- // fill the preferences of the preference screen with data from the profile object
- mPrefs.putString("pref_profile_general_name", pname);
- mPrefs.putString("pref_profile_general_image", pimage);
- mPrefs.putString("pref_profile_general_description", pdesc);
- mPrefs.putString("pref_profile_protocols_ghost_text", pghost);
- mPrefs.putBoolean("pref_profile_protocols_ghost_active", pbghost);
- mPrefs.commit();
- // create the preference view
- addPreferencesFromResource(R.xml.profile_preferences);
- Preference pref = findPreference("pref_profile_general_image");
- assert pref != null;
- if(profile != null){
- pref.setIcon(profile.getIconDrawable());
- mProfileProtocols = new HashMap<String, Boolean>(profile.mActiveProtocols);
- } else {
- mProfileProtocols = new HashMap<String, Boolean>();
- }
- // show an image chooser dialog when pressing the image preference
- pref.setOnPreferenceClickListener(
- new Preference.OnPreferenceClickListener() {
- @Override
- public boolean onPreferenceClick(Preference preference) {
- Intent intent = new Intent();
- intent.setType("image/*");
- intent.setAction(Intent.ACTION_GET_CONTENT);
- int PICK_IMAGE = 1;
- startActivityForResult(Intent.createChooser(intent, getString(R.string.select_icon)), PICK_IMAGE);
- return true;
- }
- }
- );
- if(profile != null){
- findPreference("pref_profile_general_name").setSummary(profile.mLabel);
- findPreference("pref_profile_general_description").setSummary(profile.mText);
- if(!profile.mGhostPorts.isEmpty()) findPreference("pref_profile_protocols_ghost_text").setSummary(profile.mGhostPorts);
- }
- if(profile == null || profile.isEditable()){
- getPreferenceScreen().removePreference(findPreference("pref_profile_warning"));
- }
- PreferenceCategory protocolsCategory = (PreferenceCategory) findPreference("pref_profile_protocols_settings");
- String[] protocols = getResources().getStringArray(R.array.protocols);
- String[] protocols_summary = getResources().getStringArray(R.array.protocols_description);
- // add all available protocols to the preference screen with an checkbox
- for(int i = 0; i<protocols.length; i++){
- if(protocols[i].equals("GHOST")) continue;
- mPrefs.putBoolean("pref_profile_protocol_" + protocols[i], profile != null && profile.isProtocolActive(protocols[i]));
- mPrefs.commit();
- CheckBoxPreference check = new CheckBoxPreference(getActivity());
- check.setTitle(protocols[i]);
- check.setKey("pref_profile_protocol_" + protocols[i]);
- check.setSummary(protocols_summary[i]);
- protocolsCategory.addPreference(check);
- }
- }
- /**
- * Retrieve the given profile from the intent
- * @return the profile
- */
- public Profile getProfile(){
- ProfileManager pmanager = ProfileManager.getInstance();
- Intent intent = getActivity().getIntent();
- int profile_id = intent.getIntExtra("profile_id", -1);
- if(profile_id != -1){
- return pmanager.getProfile(profile_id);
- }
- return null;
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void onResume() {
- super.onResume();
- getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void onPause() {
- getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
- super.onPause();
- }
- /**
- * Called when a shared preference is changed, added, or removed. This
- * may be called even if a preference is set to its existing value.
- *
- * <p>This callback will be run on your main thread.
- *
- * @param sharedPreferences The {@link android.content.SharedPreferences} that received
- * the change.
- * @param key The key of the preference that was changed, added, or
- */
- @Override
- public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
- Preference p = findPreference(key);
- if(p instanceof EditTextPreference){
- p.setSummary(sharedPreferences.getString(key, ""));
- } else if(p instanceof CheckBoxPreference && !p.getKey().equals("pref_profile_protocols_ghost_active")){
- mProfileProtocols.put(p.getTitle().toString(), ((CheckBoxPreference) p).isChecked());
- //System.out.println("------------------------------- P: " + ((CheckBoxPreference) p).isChecked());
- }
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
- super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
- if(resultCode == Activity.RESULT_OK){
- Cursor cursor = getActivity().getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
- new String[]{
- MediaStore.Images.Media.DATA,
- MediaStore.Images.Media.DATE_ADDED,
- MediaStore.Images.ImageColumns.ORIENTATION
- },
- MediaStore.Images.Media.DATE_ADDED, null, "date_added ASC");
- String filePath = "";
- if(cursor != null && cursor.moveToFirst())
- {
- do {
- filePath = Uri.parse(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA))).toString();
- } while(cursor.moveToNext());
- cursor.close();
- }
- Preference pref = findPreference("pref_profile_general_image");
- BitmapFactory.Options options = new BitmapFactory.Options();
- options.inPreferredConfig = Bitmap.Config.ARGB_8888;
- Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
- pref.setIcon(new BitmapDrawable(getResources(), bitmap));
- mPrefs.putString("pref_profile_general_image", filePath);
- mPrefs.commit();
- }
- }
- }
|