Bläddra i källkod

Persisting profiles now into the database

Alexander Brakowski 11 år sedan
förälder
incheckning
0f53300b90

+ 3 - 3
res/layout/profile_manager_list_item.xml

@@ -104,13 +104,13 @@
 	        android:layout_marginLeft="20dp"/>
 
 	    <ImageView
-	        android:layout_width="wrap_content"
-	        android:layout_height="wrap_content"
+	        android:layout_width="48dp"
+	        android:layout_height="48dp"
 	        android:id="@+id/profile_manager_item_image"
 	        android:src="@drawable/ic_launcher"
 	        android:layout_above="@+id/profile_manager_item_text"
 	        android:layout_alignParentLeft="true"
-	        android:layout_alignParentStart="true" />
+	        android:layout_alignParentStart="true" android:scaleType="centerInside"/>
 
 	</RelativeLayout>
 

+ 8 - 0
res/xml/profile_preferences.xml

@@ -8,5 +8,13 @@
 		                    android:summary="Change the name of this profile"
 		                    android:defaultValue=""/>
 
+		<EditTextPreference android:key="pref_profile_general_description"
+		                    android:title="Description"
+		                    android:summary="Change the description of this profile here"
+		                    android:defaultValue="" />
+
+		<Preference android:key="pref_profile_general_image"
+		            android:title="Icon"
+		            android:summary="Choose an icon for this profile" />
 	</PreferenceCategory>
 </PreferenceScreen>

+ 90 - 41
src/de/tudarmstadt/informatik/hostage/dao/ProfileManager.java

@@ -7,7 +7,9 @@ import java.util.List;
 import java.util.Map;
 
 import de.tudarmstadt.informatik.hostage.R;
+import de.tudarmstadt.informatik.hostage.logging.UglyDbHelper;
 import de.tudarmstadt.informatik.hostage.model.Profile;
+import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
 import de.tudarmstadt.informatik.hostage.ui2.adapter.ProfileManagerListAdapter;
 import de.tudarmstadt.informatik.hostage.ui2.model.ProfileListItem;
 
@@ -21,9 +23,11 @@ public class ProfileManager {
 	private Map<Integer, Profile> mProfiles;
 	private ProfileManagerListAdapter mProfileListAdapter = null;
 
-	private int mProfileId = 4;
+	private int mProfileId = 0;
 	private Profile mCurrentActivatedProfile = null;
 
+	private UglyDbHelper dbh;
+
 	public static ProfileManager getInstance(){
 		if(INSTANCE == null){
 			INSTANCE = new ProfileManager();
@@ -34,45 +38,24 @@ public class ProfileManager {
 
 	private ProfileManager(){
 		this.mProfiles = new HashMap<Integer, Profile>();
-		this.mProfiles.put(0, new Profile(
-				0,
-				"Windows Vista",
-				"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
-				R.drawable.ic_profile_vista,
-				false
-		));
+		this.dbh = new UglyDbHelper(MainActivity.getContext());
+	}
 
-		this.mProfiles.put(1, new Profile(
-				1,
-				"Windows 7",
-				"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
-				R.drawable.ic_profile_w7,
-				false
-		));
+	public void loadData(){
+		Collection<Profile> profiles = this.dbh.getAllProfiles();
 
-		this.mProfiles.put(2, new Profile(
-				2,
-				"Unix Distro",
-				"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
-				R.drawable.ic_profile_unix,
-				false
-		));
+		if(profiles.size() == 0){
+			this.fillWithSampleData();
+			profiles = mProfiles.values();
+		}
 
-		this.mProfiles.put(3, new Profile(
-				3,
-				"Random",
-				"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
-				R.drawable.ic_service_green,
-				false
-		));
+		for(Profile p: profiles){
+			this.mProfiles.put(p.id, p);
 
-		this.mProfiles.put(4, new Profile(
-				4,
-				"Mix",
-				"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
-				R.drawable.ic_service_green,
-				false
-		));
+			if(p.activated){
+				this.mCurrentActivatedProfile = p;
+			}
+		}
 	}
 
 	public List<Profile> getProfilesList(){
@@ -80,6 +63,10 @@ public class ProfileManager {
 	}
 
 	public Collection<Profile> getProfilesCollection(){
+		if(mProfiles.size() == 0 || mProfiles == null){
+			this.loadData();
+		}
+
 		return mProfiles.values();
 	}
 
@@ -87,44 +74,64 @@ public class ProfileManager {
 		return mProfiles;
 	}
 
+	public long persistProfile(Profile profile){
+		int id = (int) this.dbh.persistProfile(profile);
+
+		if(profile.id != id){
+			profile.id = id;
+		}
+
+		this.mProfiles.put(id, profile);
+
+		return id;
+	}
+
 	public Profile getProfile(int id){
 		if(this.mProfiles.containsKey(id)){
 			return this.mProfiles.get(id);
 		}
 
+		Profile profile = this.dbh.getProfile(id);
+		if(profile != null) return profile;
+
 		return null;
 	}
 
 	public void addProfile(Profile profile){
-		if(profile.id == -1){
-			profile.id = mProfileId++;
-		}
+		int id = (int) this.dbh.persistProfile(profile);
+		profile.id = id;
 
-		this.mProfiles.put(profile.id, profile);
+		this.mProfiles.put(id, profile);
 		if(this.mProfileListAdapter != null){
 			this.mProfileListAdapter.add(profile);
+			this.mProfileListAdapter.notifyDataSetChanged();
 		}
 	}
 
 	public void deleteProfile(Profile profile){
 		if(this.mProfiles.containsKey(profile.id)){
 			this.mProfiles.remove(profile.id);
+			this.dbh.deleteProfile(profile.id);
 
 			if(this.mProfileListAdapter != null){
 				this.mProfileListAdapter.remove(profile);
+				this.mProfileListAdapter.notifyDataSetChanged();
 			}
 		}
 	}
 
-	public void activeProfile(Profile profile){
+	public void activateProfile(Profile profile){
 		if(profile.equals(this.mCurrentActivatedProfile)) return;
 
 		if(this.mCurrentActivatedProfile != null){
 			this.mCurrentActivatedProfile.activated = false;
+			this.persistProfile(this.mCurrentActivatedProfile);
 		}
 
 		profile.activated = true;
 		this.mCurrentActivatedProfile = profile;
+
+		this.dbh.persistProfile(profile);
 	}
 
 	public void setProfileListAdapter(ProfileManagerListAdapter profileListAdapter){
@@ -135,4 +142,46 @@ public class ProfileManager {
 	public ProfileManagerListAdapter getProfileListAdapter(){
 		return this.mProfileListAdapter;
 	}
+
+	public void fillWithSampleData(){
+		this.addProfile(new Profile(
+				0,
+				"Windows Vista",
+				"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
+				R.drawable.ic_profile_vista,
+				false
+		));
+
+		this.addProfile(new Profile(
+				1,
+				"Windows 7",
+				"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
+				R.drawable.ic_profile_w7,
+				false
+		));
+
+		this.addProfile(new Profile(
+				2,
+				"Unix Distro",
+				"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
+				R.drawable.ic_profile_unix,
+				false
+		));
+
+		this.addProfile(new Profile(
+				3,
+				"Random",
+				"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
+				R.drawable.ic_service_green,
+				false
+		));
+
+		this.addProfile(new Profile(
+				4,
+				"Mix",
+				"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
+				R.drawable.ic_service_green,
+				false
+		));
+	}
 }

+ 112 - 1
src/de/tudarmstadt/informatik/hostage/logging/UglyDbHelper.java

@@ -9,8 +9,11 @@ import android.util.Log;
 
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
 
 import de.tudarmstadt.informatik.hostage.logging.Record.TYPE;
+import de.tudarmstadt.informatik.hostage.model.Profile;
 import de.tudarmstadt.informatik.hostage.ui.LogFilter;
 
 /**
@@ -30,7 +33,7 @@ public class UglyDbHelper extends SQLiteOpenHelper {
 
 	// All Static variables
 	// Database Version
-	private static final int DATABASE_VERSION = 3;
+	private static final int DATABASE_VERSION = 5;
 
 	// Database Name
 	private static final String DATABASE_NAME = "recordManager";
@@ -39,6 +42,7 @@ public class UglyDbHelper extends SQLiteOpenHelper {
 	private static final String TABLE_ATTACK_INFO = "attack_info";
     private static final String TABLE_RECORDS = "records";
     private static final String TABLE_BSSIDS = "bssids";
+	private static final String TABLE_PROFILES = "profiles";
 
 	// Contacts Table Columns names
 	public static final String KEY_ID = "_id";
@@ -60,7 +64,24 @@ public class UglyDbHelper extends SQLiteOpenHelper {
 	public static final String KEY_LONGITUDE = "longitude";
 	public static final String KEY_ACCURACY = "accuracy";
 
+	public static final String KEY_PROFILE_ID = "_profile_id";
+	public static final String KEY_PROFILE_NAME = "profile_name";
+	public static final String KEY_PROFILE_DESCRIPTION = "profile_description";
+	public static final String KEY_PROFILE_ICON = "profile_icon";
+	public static final String KEY_PROFILE_EDITABLE = "profile_editable";
+	public static final String KEY_PROFILE_ACTIVE = "profile_active";
+
 	// Database sql create statements
+	private static final String CREATE_PROFILE_TABLE = "CREATE TABLE "
+			+ TABLE_PROFILES + "("
+			+ KEY_PROFILE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+			+ KEY_PROFILE_NAME + " TEXT,"
+			+ KEY_PROFILE_DESCRIPTION + " TEXT,"
+			+ KEY_PROFILE_ICON + " TEXT,"
+			+ KEY_PROFILE_EDITABLE + " INTEGER,"
+			+ KEY_PROFILE_ACTIVE + " INTEGER"
+			+ ")";
+
 	private static final String CREATE_RECORD_TABLE = "CREATE TABLE "
 			+ TABLE_RECORDS + "("
             + KEY_ID + " INTEGER NOT NULL,"
@@ -316,6 +337,7 @@ public class UglyDbHelper extends SQLiteOpenHelper {
 		SQLiteDatabase db = this.getReadableDatabase();
 		db.delete(TABLE_RECORDS, null, null);
 		db.delete(TABLE_ATTACK_INFO, null, null);
+		db.delete(TABLE_PROFILES, null, null);
 		db.close();
 	}
 
@@ -706,6 +728,7 @@ public class UglyDbHelper extends SQLiteOpenHelper {
 		db.execSQL(CREATE_BSSID_TABLE);
 		db.execSQL(CREATE_ATTACK_INFO_TABLE);
 		db.execSQL(CREATE_RECORD_TABLE);
+		db.execSQL(CREATE_PROFILE_TABLE);
 	}
 
 	// Upgrading database
@@ -715,11 +738,99 @@ public class UglyDbHelper extends SQLiteOpenHelper {
 		db.execSQL("DROP TABLE IF EXISTS " + TABLE_RECORDS);
 		db.execSQL("DROP TABLE IF EXISTS " + TABLE_ATTACK_INFO);
 		db.execSQL("DROP TABLE IF EXISTS " + TABLE_BSSIDS);
+		db.execSQL("DROP TABLE IF EXISTS " + TABLE_PROFILES);
 
 		// Create tables again
 		onCreate(db);
 	}
 
+	/**
+	 * Retrieves all the profiles from the database
+	 *
+	 * @return list of profiles
+	 */
+	public List<Profile> getAllProfiles(){
+		List<Profile> profiles = new LinkedList<Profile>();
+
+		// Select All Query
+		String selectQuery = "SELECT  * FROM " + TABLE_PROFILES;
+
+		SQLiteDatabase db = this.getWritableDatabase();
+		Cursor cursor = db.rawQuery(selectQuery, null);
+
+		// looping through all rows and adding to list
+		if (cursor.moveToFirst()) {
+			do {
+				Profile profile = new Profile(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getInt(4) == 1);
+
+				if(cursor.getInt(5) == 1){
+					profile.activated = true;
+				}
+
+				// Adding record to list
+				profiles.add(profile);
+			} while (cursor.moveToNext());
+		}
+		cursor.close();
+		db.close();
+		// return record list
+		return profiles;
+	}
+
+	/**
+	 * Persists the given profile into the database
+	 *
+	 * @param profile the profile which should be persisted
+	 *
+	 * @return
+	 */
+	public long persistProfile(Profile profile){
+		SQLiteDatabase db = this.getReadableDatabase();
+
+		ContentValues values = new ContentValues();
+
+		if(profile.id != -1){
+			values.put(KEY_PROFILE_ID, profile.id);
+		}
+
+		values.put(KEY_PROFILE_NAME, profile.label);
+		values.put(KEY_PROFILE_DESCRIPTION, profile.text);
+		values.put(KEY_PROFILE_ICON, profile.iconPath);
+		values.put(KEY_PROFILE_ACTIVE, profile.activated);
+		values.put(KEY_PROFILE_EDITABLE, profile.editable);
+
+		return db.replace(TABLE_PROFILES, null, values);
+	}
+
+
+	public Profile getProfile(int id) {
+		String selectQuery = "SELECT  * FROM " + TABLE_PROFILES + " WHERE " + TABLE_PROFILES + "." + KEY_PROFILE_ID + " = " + id;
+		SQLiteDatabase db = this.getReadableDatabase();
+
+		Cursor cursor = db.rawQuery(selectQuery, null);
+		Profile profile = null;
+
+		if (cursor.moveToFirst()) {
+			profile = new Profile(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getInt(4) == 1);
+
+			if(cursor.getInt(5) == 1){
+				profile.activated = true;
+			}
+		}
+
+		cursor.close();
+		db.close();
+
+		// return contact
+		return profile;
+	}
+
+	public void deleteProfile(int id){
+		SQLiteDatabase db = this.getReadableDatabase();
+
+		db.delete(TABLE_PROFILES, KEY_PROFILE_ID + "=?", new String[]{String.valueOf(id)});
+	}
+
 	public void updateNetworkInformation(
 			ArrayList<HashMap<String, Object>> networkInformation) {
 		Log.i("DatabaseHandler", "Starte updating");

+ 36 - 6
src/de/tudarmstadt/informatik/hostage/model/Profile.java

@@ -2,6 +2,8 @@ package de.tudarmstadt.informatik.hostage.model;
 
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
+import android.graphics.drawable.BitmapDrawable;
+import android.graphics.drawable.Drawable;
 
 import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
 
@@ -10,28 +12,38 @@ import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
  * @created 14.01.14 18:04
  */
 public class Profile {
-	public String label;
 	public String text;
+	public String label;
 	public int id;
 	public boolean activated;
 	public Bitmap icon;
+	public String iconPath;
 
 	public boolean isBackVisible = false;
 	public boolean editable = false;
 
-	public Profile(int id, String text, String label, Bitmap icon, boolean editable){
+	public Profile(int id, String label, String text, Bitmap icon, boolean editable){
 		this.id = id;
-		this.text = text;
-		this.label = label;
+		this.label = text;
+		this.text = label;
 		this.activated = false;
 		this.icon = icon;
 		this.editable = editable;
 	}
 
-	public Profile(int id, String text, String label, int icon, boolean editable){
+	public Profile(int id, String label, String text, int icon, boolean editable){
 		this(id, text, label, BitmapFactory.decodeResource(MainActivity.context.getResources(), icon), editable);
 	}
 
+	public Profile(int id, String label, String text, String iconPath, boolean editable){
+		this.id = id;
+		this.label = label;
+		this.text = text;
+		this.activated = false;
+		this.iconPath = iconPath;
+		this.editable = editable;
+	}
+
 	public void setIcon(Bitmap bitmap){
 		this.icon = bitmap;
 	}
@@ -40,11 +52,29 @@ public class Profile {
 		this.icon = BitmapFactory.decodeResource(MainActivity.context.getResources(), icon);
 	}
 
+	public Bitmap getIconBitmap(){
+		if(this.icon != null) return icon;
+
+		if(this.iconPath != null){
+			BitmapFactory.Options options = new BitmapFactory.Options();
+			options.inPreferredConfig = Bitmap.Config.ARGB_8888;
+			Bitmap bitmap = BitmapFactory.decodeFile(this.iconPath, options);
+
+			return bitmap;
+		}
+
+		return null;
+	}
+
+	public Drawable getIconDrawable(){
+		return new BitmapDrawable(getIconBitmap());
+	}
+
 	public boolean isEditable(){
 		return this.editable;
 	}
 
 	public Profile cloneProfile(){
-		return new Profile(id, text, label, icon, editable);
+		return new Profile(id, label, text, icon, editable);
 	}
 }

+ 13 - 4
src/de/tudarmstadt/informatik/hostage/ui2/activity/ProfileEditActivity.java

@@ -1,7 +1,15 @@
 package de.tudarmstadt.informatik.hostage.ui2.activity;
 
+import android.content.Intent;
+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.Preference;
 import android.preference.PreferenceActivity;
+import android.provider.MediaStore;
 
 import de.tudarmstadt.informatik.hostage.ui2.fragment.ProfileEditFragment;
 
@@ -10,15 +18,16 @@ import de.tudarmstadt.informatik.hostage.ui2.fragment.ProfileEditFragment;
  * @created 08.02.14 23:36
  */
 public class ProfileEditActivity extends PreferenceActivity {
+	ProfileEditFragment editFragment;
 
+	@Override
 	public void onCreate(Bundle savedInstanceState){
 		super.onCreate(savedInstanceState);
 
+		editFragment = new ProfileEditFragment();
+
 		getFragmentManager().beginTransaction()
-				.replace(android.R.id.content, new ProfileEditFragment())
+				.replace(android.R.id.content, editFragment)
 				.commit();
-
-
 	}
-
 }

+ 11 - 5
src/de/tudarmstadt/informatik/hostage/ui2/adapter/ProfileManagerListAdapter.java

@@ -4,6 +4,8 @@ import android.app.AlertDialog;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -21,8 +23,6 @@ import de.tudarmstadt.informatik.hostage.R;
 import de.tudarmstadt.informatik.hostage.dao.ProfileManager;
 import de.tudarmstadt.informatik.hostage.model.Profile;
 import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
-import de.tudarmstadt.informatik.hostage.ui2.fragment.ProfileManagerFragment;
-import de.tudarmstadt.informatik.hostage.ui2.model.ProfileListItem;
 import de.tudarmstadt.informatik.hostage.ui2.activity.ProfileEditActivity;
 
 /**
@@ -79,9 +79,15 @@ public class ProfileManagerListAdapter extends ArrayAdapter<Profile> {
 
 	    ((SwipeListView)parent).recycle(rowView, position);
 
-	    holder.textView.setText(item.label);
-	    holder.labelView.setText(item.text);
-	    holder.itemIcon.setImageBitmap(item.icon);
+	    holder.textView.setText(item.text);
+	    holder.labelView.setText(item.label);
+
+	    if(item.getIconBitmap() != null){
+	        //Bitmap bitmap = Bitmap.createScaledBitmap(item.getIconBitmap(), 32, 32, true);
+	        holder.itemIcon.setImageBitmap(item.getIconBitmap());
+	    } else {
+		    holder.itemIcon.setImageBitmap(BitmapFactory.decodeResource(MainActivity.context.getResources(), R.drawable.ic_launcher));
+	    }
 
 		holder.buttonEdit.setOnClickListener(new View.OnClickListener() {
 			@Override

+ 1 - 2
src/de/tudarmstadt/informatik/hostage/ui2/fragment/HomeFragment.java

@@ -73,8 +73,7 @@ public class HomeFragment extends Fragment {
 		intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
 		intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);*/
 
-		LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver,
-				new IntentFilter(getString(R.string.broadcast)));
+		LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver, new IntentFilter(getString(R.string.broadcast)));
 		//getActivity().registerReceiver(mReceiver, intentFilter);
 	}
 

+ 74 - 4
src/de/tudarmstadt/informatik/hostage/ui2/fragment/ProfileEditFragment.java

@@ -1,14 +1,21 @@
 package de.tudarmstadt.informatik.hostage.ui2.fragment;
 
 import android.app.ActionBar;
+import android.app.Activity;
 import android.content.Context;
 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.EditTextPreference;
 import android.preference.Preference;
 import android.preference.PreferenceFragment;
 import android.preference.PreferenceManager;
+import android.provider.MediaStore;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.widget.LinearLayout;
@@ -25,6 +32,7 @@ public class ProfileEditFragment extends PreferenceFragment implements
 		SharedPreferences.OnSharedPreferenceChangeListener {
 
 	private LayoutInflater mInflater;
+	private SharedPreferences.Editor prefs;
 
 	@Override
 	public void onCreate(Bundle savedInstanceState){
@@ -54,11 +62,15 @@ public class ProfileEditFragment extends PreferenceFragment implements
 					createNew = true;
 				}
 
-				profile.text = prefs.getString("pref_profile_general_name", profile.text);
+				profile.label = prefs.getString("pref_profile_general_name", profile.label);
+				profile.iconPath = prefs.getString("pref_profile_general_image", profile.iconPath);
+				profile.text = prefs.getString("pref_profile_general_description", profile.text);
 
 				if(createNew){
 					profile.id = -1;
 					pmanager.addProfile(profile);
+				} else {
+					pmanager.persistProfile(profile);
 				}
 
 				getActivity().finish();
@@ -73,17 +85,42 @@ public class ProfileEditFragment extends PreferenceFragment implements
 		});
 
 		Profile profile = getProfile();
-		SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()).edit();
+		prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()).edit();
 
 		if(profile != null){
+			prefs.putString("pref_profile_general_name", profile.label);
+			prefs.putString("pref_profile_general_image", profile.iconPath);
+			prefs.putString("pref_profile_general_description", profile.text);
 
-			prefs.putString("pref_profile_general_name", profile.text);
 			prefs.commit();
 		}
 
 		addPreferencesFromResource(R.xml.profile_preferences);
 
-		findPreference("pref_profile_general_name").setSummary(profile.text);
+		Preference pref = findPreference("pref_profile_general_image");
+
+		assert pref != null;
+
+		if(profile != null){
+			pref.setIcon(profile.getIconDrawable());
+		}
+
+		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, "Select Icon"), PICK_IMAGE);
+						return true;
+					}
+				}
+		);
+
+		findPreference("pref_profile_general_name").setSummary(profile.label);
+		findPreference("pref_profile_general_description").setSummary(profile.text);
 	}
 
 	public Profile getProfile(){
@@ -129,4 +166,37 @@ public class ProfileEditFragment extends PreferenceFragment implements
 			p.setSummary(sharedPreferences.getString(key, ""));
 		}
 	}
+
+	@Override
+	public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
+		super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
+
+		if(resultCode == Activity.RESULT_OK){
+			Uri selectedImage = imageReturnedIntent.getData();
+			String[] filePathColumn = {MediaStore.Images.Media.DATA};
+
+			assert selectedImage != null;
+
+			Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
+
+			assert cursor != null;
+			cursor.moveToFirst();
+
+			int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
+			String filePath = cursor.getString(columnIndex);
+			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);
+
+			assert pref != null;
+			pref.setIcon(new BitmapDrawable(getResources(), bitmap));
+
+			prefs.putString("pref_profile_general_image", filePath);
+			prefs.commit();
+		}
+	}
 }

+ 2 - 1
src/de/tudarmstadt/informatik/hostage/ui2/fragment/ProfileManagerFragment.java

@@ -35,6 +35,7 @@ public class ProfileManagerFragment extends Fragment {
 	    SwipeListView list = (SwipeListView) rootView.findViewById(R.id.profile_manager_listview);
 
 		final ProfileManager pmanager = ProfileManager.getInstance();
+		pmanager.loadData();
 
         List<Profile> strList = pmanager.getProfilesList();
 
@@ -47,7 +48,7 @@ public class ProfileManagerFragment extends Fragment {
 			@Override
 			public void onClickFrontView(int position) {
 				Profile profile = mAdapter.getItem(position);
-				pmanager.activeProfile(profile);
+				pmanager.activateProfile(profile);
 
 				mAdapter.notifyDataSetChanged();
 			}