Sfoglia il codice sorgente

Merge branch 'master' of https://git.tk.informatik.tu-darmstadt.de/scm-ssi-student-hostagev2

Julien Clauter 11 anni fa
parent
commit
72f865677a

+ 6 - 0
res/drawable/services_circle_green.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<shape android:shape="oval" xmlns:android="http://schemas.android.com/apk/res/android">
+    <solid android:color="@color/holo_dark_green" />
+
+</shape>

+ 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>
 

+ 2 - 1
res/layout/services_list_item.xml

@@ -10,7 +10,8 @@
                     android:baselineAligned="false"
                     android:orientation="horizontal"
                     android:weightSum="1">
-        <View android:layout_width="25dp" android:layout_height="25dp"
+        <View android:id="@+id/services_circle"
+              android:layout_width="25dp" android:layout_height="25dp"
               android:layout_marginLeft="12dp"
               android:layout_marginTop="10dp"
               android:background="@drawable/services_circle" />

+ 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>

+ 92 - 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,26 @@ 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 = this.dbh.getAllProfiles();
+		}
 
-		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
-		));
+		this.mProfiles.clear();
 
-		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
-		));
+		for(Profile p: profiles){
+			this.mProfiles.put(p.id, p);
+
+			if(p.activated){
+				this.mCurrentActivatedProfile = p;
+			}
+		}
 	}
 
 	public List<Profile> getProfilesList(){
@@ -80,6 +65,10 @@ public class ProfileManager {
 	}
 
 	public Collection<Profile> getProfilesCollection(){
+		if(mProfiles.size() == 0 || mProfiles == null){
+			this.loadData();
+		}
+
 		return mProfiles.values();
 	}
 
@@ -87,44 +76,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 +144,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 = 7;
 
 	// 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);
 	}
 }

+ 1 - 1
src/de/tudarmstadt/informatik/hostage/net/MyServerSocketFactory.java

@@ -11,7 +11,7 @@ import java.net.SocketImpl;
 import javax.net.ServerSocketFactory;
 
 import de.tudarmstadt.informatik.hostage.system.P;
-import de.tudarmstadt.informatik.hostage.ui.MainActivity;
+import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
 
 /**
  * Server Socket Factory using file descriptors.

+ 4 - 2
src/de/tudarmstadt/informatik/hostage/ui2/activity/MainActivity.java

@@ -257,7 +257,7 @@ public class MainActivity extends Activity {
 		mDrawerToggle.onConfigurationChanged(newConfig);
 	}
 
-	private void displayView(int position) {
+	public void displayView(int position) {
 		if(mSelectedMenuItem != null && position == mSelectedMenuItem.value) {
 			mDrawerLayout.closeDrawer(mDrawerList);
 			return;
@@ -396,7 +396,9 @@ public class MainActivity extends Activity {
             if (value < 0 || value  >= MainMenuItem.values().length) return MainMenuItem.HOME;
             return  MainMenuItem.values()[value];
         }
-
+		public int getValue() {
+			return value;
+		}
     }
 
 	private class DrawerItemClickListener implements ListView.OnItemClickListener {

+ 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

+ 32 - 4
src/de/tudarmstadt/informatik/hostage/ui2/adapter/ServicesListAdapter.java

@@ -1,6 +1,7 @@
 package de.tudarmstadt.informatik.hostage.ui2.adapter;
 
 import android.content.Context;
+import android.os.Build;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -15,6 +16,7 @@ import android.widget.ViewSwitcher;
 import java.util.List;
 
 import de.tudarmstadt.informatik.hostage.R;
+import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
 import de.tudarmstadt.informatik.hostage.ui2.model.ServicesListItem;
 
 /**
@@ -25,10 +27,15 @@ public class ServicesListAdapter extends ArrayAdapter<ServicesListItem> {
 			public TextView protocolName;
 			public TextView recordedAttacks;
 			public Switch activated;
+			public View circle;
 		}
 
 	private final Context context;
 	private final List<ServicesListItem> values;
+	int sdk = Build.VERSION.SDK_INT;
+
+	ViewHolder holder = null;
+	private ServicesListItem item;
 
 	public ServicesListAdapter(Context context, List<ServicesListItem> objects){
 		super(context, R.layout.services_list_item, objects);
@@ -43,9 +50,8 @@ public class ServicesListAdapter extends ArrayAdapter<ServicesListItem> {
 				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 
 		View rowView = convertView;
-		ViewHolder holder = null;
 
-		final ServicesListItem item = values.get(position);
+		item = values.get(position);
 
 		if(rowView == null){
 			rowView = inflater.inflate(R.layout.services_list_item, parent, false);
@@ -54,7 +60,8 @@ public class ServicesListAdapter extends ArrayAdapter<ServicesListItem> {
 			holder.protocolName = (TextView) rowView.findViewById(R.id.services_item_name);
 			holder.recordedAttacks = (TextView) rowView.findViewById(R.id.services_item_rec_attacks);
 			holder.activated = (Switch) rowView.findViewById(R.id.services_item_switch);
-
+			holder.circle = (View) rowView.findViewById(R.id.services_circle);
+			this.notifyDataSetChanged();
 			rowView.setTag(holder);
 		} else {
 			holder = (ViewHolder) rowView.getTag();
@@ -62,8 +69,29 @@ public class ServicesListAdapter extends ArrayAdapter<ServicesListItem> {
 
 		holder.protocolName.setText(item.protocol);
 
-
 		return rowView;
 	}
 
+	@Override
+	public void notifyDataSetChanged(){
+		if(MainActivity.getInstance().getHoneyService().isRunning(item.protocol)){
+			holder.activated.setChecked(true);
+			if(sdk < Build.VERSION_CODES.JELLY_BEAN){
+				holder.circle.setBackgroundDrawable(MainActivity.getInstance().getResources().getDrawable(R.drawable.services_circle_green));
+			}
+			else {
+				holder.circle.setBackground(MainActivity.getInstance().getResources().getDrawable(R.drawable.services_circle_green));
+			}
+		}
+		else {
+			holder.activated.setChecked(false);
+			if(sdk < Build.VERSION_CODES.JELLY_BEAN){
+				holder.circle.setBackgroundDrawable(MainActivity.getInstance().getResources().getDrawable(R.drawable.services_circle));
+			}
+			else {
+				holder.circle.setBackground(MainActivity.getInstance().getResources().getDrawable(R.drawable.services_circle));
+			}
+		}
+	}
+
 }

+ 14 - 3
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);
 	}
 
@@ -154,6 +153,19 @@ public class HomeFragment extends Fragment {
 
 	    updateUI();
 
+		final String[] protocols = getResources().getStringArray(R.array.protocols);
+
+		if(MainActivity.getInstance().getHoneyService() == null){
+			// do nothing
+		}
+		else {
+			for(String protocol: protocols){
+				if (MainActivity.getInstance().getHoneyService().isRunning(protocol)) {
+					setStateActive();
+				}
+			}
+		}
+
 	    mHomeSwitchConnection = (Switch) rootView.findViewById(R.id.home_switch_connection);
 
 	    if(switchChangeListener == null){
@@ -176,7 +188,6 @@ public class HomeFragment extends Fragment {
 					    setStateNotConnected();
 				    } else {
 					    if(isChecked){
-						    String[] protocols = getResources().getStringArray(R.array.protocols);
 						    for(String protocol: protocols){
 							    MainActivity.getInstance().getHoneyService().startListener(protocol);
 						    }

+ 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();
 			}

+ 31 - 18
src/de/tudarmstadt/informatik/hostage/ui2/fragment/RecordOverviewFragment.java

@@ -662,29 +662,42 @@ public class RecordOverviewFragment extends Fragment implements ChecklistDialog.
 		Random random = new Random();
 
 		LatLng tudarmstadtLoc = new LatLng(49.86923, 8.6632768);
-		int numberofRecords = (int) (Math.random() * (50 - 10));
-		for (int i = 0; i < numberofRecords; i++) {
-			Record record = new Record();
-            record.setId(i);
-            record.setAttack_id(i);
-			record.setBssid("BSSID: " + i);
-			record.setSsid("SSID: w" + i);
-			record.setTimestamp(cal.getTimeInMillis()
-					+ ((i * 60 * 60 * 60 * 24) * 1000));
 
-			int index = i % maxProtocolsIndex;
-			String protocolName = this.getResources().getStringArray(
-					R.array.protocols)[index];
+		final int numSSIDs = 30;
+		final double ssidRadius = 0.1;
+		final double bssidRadius = 0.004;
+		int id = 0;
+		for (int ssid = 0; ssid < numSSIDs; ssid++) {
+			LatLng ssidLocation = new LatLng(tudarmstadtLoc.latitude - ssidRadius + 2.0 * ssidRadius * Math.random(), tudarmstadtLoc.longitude - ssidRadius + 2.0 * ssidRadius * Math.random());
 
-			record.setProtocol(protocolName);
+			String ssidName = "WiFi" + ssid;
+			int numBSSIDs = (random.nextInt() % 10) + 10;
+			for (int bssid = 0; bssid < numBSSIDs; bssid++) {
+				Record record = new Record();
+				record.setId(id);
+				record.setAttack_id(id);
+				record.setBssid("BSSID" + id);
+				id++;
 
-			record.setLocalIP("127.0.0.1");
-			record.setType(TYPE.SEND);
+				record.setSsid(ssidName);
+				record.setTimestamp(cal.getTimeInMillis()
+						+ ((id * 60 * 60 * 60 * 24) * 1000));
 
-			record.setLatitude(tudarmstadtLoc.latitude + -0.01 + 0.02 * random.nextDouble());
-			record.setLongitude(tudarmstadtLoc.longitude + -0.01 + 0.02 * random.nextDouble());
+				int index = id % maxProtocolsIndex;
+				String protocolName = this.getResources().getStringArray(
+						R.array.protocols)[index];
 
-			dbh.addRecord(record);
+				record.setProtocol(protocolName);
+
+				record.setLocalIP("127.0.0.1");
+				record.setType(TYPE.SEND);
+
+				record.setLatitude(
+						ssidLocation.latitude - bssidRadius + 2.0 * bssidRadius * Math.random());
+				record.setLongitude(ssidLocation.longitude - bssidRadius + 2.0 * bssidRadius * Math.random());
+
+				dbh.addRecord(record);
+			}
 		}
 
         int countAllLogs = dbh.getAllRecords().size();

+ 37 - 8
src/de/tudarmstadt/informatik/hostage/ui2/fragment/ServicesFragment.java

@@ -15,14 +15,12 @@ import android.widget.TextView;
 
 import java.util.ArrayList;
 
-import de.tudarmstadt.informatik.hostage.HoneyService;
 import de.tudarmstadt.informatik.hostage.R;
 import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
-import de.tudarmstadt.informatik.hostage.protocol.FTP;
+import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
 import de.tudarmstadt.informatik.hostage.ui2.adapter.ServicesListAdapter;
 import de.tudarmstadt.informatik.hostage.ui2.model.ServicesListItem;
 
-import static de.tudarmstadt.informatik.hostage.HoneyService.*;
 
 /**
  * Created by Daniel Lazar on 05.02.14.
@@ -37,6 +35,7 @@ public class ServicesFragment extends Fragment{
 	private CompoundButton.OnCheckedChangeListener switchChangeListener = null;
 
 
+
 	private void assignViews(){
 		mServicesSwitchService = (Switch) rootView.findViewById(R.id.service_switch_connection);
 		mServicesTextName = (TextView) rootView.findViewById(R.id.services_text_name);
@@ -54,7 +53,7 @@ public class ServicesFragment extends Fragment{
 		}
 
 /*		//check if ftp monitoring is running
-		if(!HoneyService.isRunning(protocols[1])){
+		if(!MainActivity.getInstance().getHoneyService().isRunning(protocols[1])){
 			mServicesSwitchFTP.setOnCheckedChangeListener(null);
 		}
 		else {
@@ -74,16 +73,22 @@ public class ServicesFragment extends Fragment{
 		updateUI();
 
 		ListView list = (ListView) rootView.findViewById(R.id.services_list_view);
-		String[] protocols = getResources().getStringArray(R.array.protocols);
+
+		final String[] protocols = getResources().getStringArray(R.array.protocols);
 		ArrayList<ServicesListItem> protocolList= new ArrayList<ServicesListItem>();
 
 		for(String protocol: protocols){
 			protocolList.add(new ServicesListItem(protocol));
+			if(MainActivity.getInstance().getHoneyService().isRunning(protocol)){
+				setStateActive();
+			}
 		}
 
-		ServicesListAdapter adapter = new ServicesListAdapter(getActivity().getBaseContext(), protocolList);
+		final ServicesListAdapter adapter = new ServicesListAdapter(getActivity().getBaseContext(), protocolList);
 		list.setAdapter(adapter);
 
+		mServicesSwitchService = (Switch) rootView.findViewById(R.id.service_switch_connection);
+
 		if(switchChangeListener == null){
 			switchChangeListener = new CompoundButton.OnCheckedChangeListener() {
 				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
@@ -104,20 +109,43 @@ public class ServicesFragment extends Fragment{
 						setStateNotConnected();
 					} else {
 						if(isChecked){
+							for(String protocol: protocols){
+								if(MainActivity.getInstance().getHoneyService().isRunning(protocol)){
+									adapter.notifyDataSetChanged();
+								}
+								else{
+									MainActivity.getInstance().getHoneyService().startListener(protocol);
+									adapter.notifyDataSetChanged();
+								}
+							}
 							setStateActive();
 						} else {
+							for(String protocol: protocols){
+								if(MainActivity.getInstance().getHoneyService().isRunning(protocol)){
+									MainActivity.getInstance().getHoneyService().stopListener(protocol);
+									/*MainActivity.getInstance().stopAndUnbind();*/
+									adapter.notifyDataSetChanged();
+
+								}
+							}
+//							adapter.notifyDataSetChanged();
 							setStateNotActive();
 						}
 					}
 				}
+
 			};
 		}
+		mServicesSwitchService.setOnCheckedChangeListener(switchChangeListener);
+
+
+
 		return rootView;
 
-	}
+	};
 
 	private void setStateActive() {
-
+		mServicesSwitchService.setChecked(true);
 	}
 
 	private void setStateNotConnected() {
@@ -125,6 +153,7 @@ public class ServicesFragment extends Fragment{
 	}
 
 	private void setStateNotActive() {
+		mServicesSwitchService.setChecked(false);
 
 	}
 

+ 62 - 14
src/de/tudarmstadt/informatik/hostage/ui2/fragment/ThreatMapFragment.java

@@ -1,11 +1,8 @@
 package de.tudarmstadt.informatik.hostage.ui2.fragment;
 
-import android.app.AlertDialog;
-import android.app.Dialog;
 import android.app.Fragment;
-import android.content.Context;
-import android.content.DialogInterface;
 import android.graphics.Color;
+import android.location.Location;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.InflateException;
@@ -23,18 +20,21 @@ import com.google.android.gms.maps.model.Marker;
 import com.google.android.gms.maps.model.MarkerOptions;
 
 import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
 
 import de.tudarmstadt.informatik.hostage.R;
 import de.tudarmstadt.informatik.hostage.logging.Record;
 import de.tudarmstadt.informatik.hostage.logging.UglyDbHelper;
+import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
 
 import static com.google.android.gms.common.GooglePlayServicesUtil.*;
 
 /**
- * Created by fabio on 10.02.14.
+ * Created by Fabio Arnold on 10.02.14.
  */
-public class ThreatMapFragment extends Fragment implements GoogleMap.OnMarkerClickListener {
-	private static GoogleMap map = null;
+public class ThreatMapFragment extends Fragment implements GoogleMap.OnInfoWindowClickListener {
+	private GoogleMap map = null;
 	private static View view = null;
 
 	/**
@@ -45,34 +45,80 @@ public class ThreatMapFragment extends Fragment implements GoogleMap.OnMarkerCli
 	private boolean isGooglePlay() {
 		int status = isGooglePlayServicesAvailable(getActivity());
 		boolean result = status == ConnectionResult.SUCCESS;
-		if (result == false) {
+		if (!result) {
 			getErrorDialog(status, getActivity(), 10).show();
 		}
 		return result;
 	}
 
 	@Override
-	public boolean onMarkerClick(Marker marker) {
+	public void onInfoWindowClick(Marker marker) {
+		MainActivity.getInstance().displayView(MainActivity.MainMenuItem.RECORDS.getValue());
 		Log.i("MARKER", ""+marker.getId());
-		return false;
+	}
+
+	private class Point {
+		public double x, y;
+		public Point(double sx, double sy) {
+			x = sx;
+			y = sy;
+		}
 	}
 
 	private void populateMap() {
 		UglyDbHelper dbh = new UglyDbHelper(getActivity());
 		ArrayList<Record> records = dbh.getAllRecords();
 
-		CircleOptions circleOptions = new CircleOptions().radius(200.0).fillColor(Color.argb(127, 240, 80, 60)).strokeWidth(0.0f);
+		HashMap<String, ArrayList<Point>> threatLocations = new HashMap<String, ArrayList<Point>>();
+
 		for (Record record : records) {
 			LatLng location = new LatLng(record.getLatitude(), record.getLongitude());
-			map.addCircle(circleOptions.center(location));
-			map.addMarker(new MarkerOptions().title(record.getSsid()).position(location));
+			ArrayList<Point> points;
+			if (threatLocations.containsKey(record.getSsid())) {
+				points = threatLocations.get(record.getSsid());
+			} else {
+				points = new ArrayList<Point>();
+				threatLocations.put(record.getSsid(), points);
+			}
+			points.add(new Point(location.latitude, location.longitude));
+		}
+
+		final int maxNumAttacks = 20;
+		CircleOptions circleOptions = new CircleOptions().radius(200.0).fillColor(Color.argb(127, 240, 80, 60)).strokeWidth(0.0f);
+		for (Map.Entry<String, ArrayList<Point>> entry : threatLocations.entrySet()) {
+			String ssid = entry.getKey();
+			ArrayList<Point> points = entry.getValue();
+
+			// color
+			int threatLevel = points.size();
+			if (threatLevel > maxNumAttacks) threatLevel = maxNumAttacks;
+			float alpha = 1.0f - (float)(threatLevel-1) / (float)(maxNumAttacks-1);
+			int color = Color.argb(127, (int) (240.0 + 15.0 * alpha), (int) (80.0 + 175.0 * alpha), 60);
+
+			// radius
+			Point minimum = new Point(360.0, 360.0), maximum = new Point(-360.0, -360.0);
+			for (Point point : points) {
+				if (point.x < minimum.x) minimum.x = point.x;
+				if (point.x > maximum.x) maximum.x = point.x;
+				if (point.y < minimum.y) minimum.y = point.y;
+				if (point.y > maximum.y) maximum.y = point.y;
+			}
+			LatLng center = new LatLng(0.5 * (minimum.x + maximum.x), 0.5 * (minimum.y + maximum.y));
+			float[] result = new float[1];
+			Location.distanceBetween(minimum.x, minimum.y, maximum.x, maximum.y, result);
+			float radius = 0.5f * result[0];
+			map.addCircle(circleOptions.center(center).radius(100.0 + radius).fillColor(color));
+			map.addMarker(new MarkerOptions().title(ssid + ": " + points.size() + (points.size() == 1 ? " attack" : " attacks")).position(
+					center));
 		}
 
 		map.setMyLocationEnabled(true);
-		map.setOnMarkerClickListener(this);
+		map.setOnInfoWindowClickListener(this);
 
 		LatLng tudarmstadt = new LatLng(49.86923, 8.6632768);
 		//LatLng mapCenter = new LatLng(41.889, -87.622);
+
+		//Location myLocation = map.getMyLocation();
 		map.moveCamera(CameraUpdateFactory.newLatLngZoom(tudarmstadt, 13));
 	}
 
@@ -86,6 +132,7 @@ public class ThreatMapFragment extends Fragment implements GoogleMap.OnMarkerCli
 			if (parent != null)
 				parent.removeView(view);
 		}
+		
 		try {
 			view = inflater.inflate(R.layout.fragment_threatmap, container, false);
 			if (isGooglePlay()) {
@@ -95,6 +142,7 @@ public class ThreatMapFragment extends Fragment implements GoogleMap.OnMarkerCli
 			}
 		} catch (InflateException e) {
         	// map already exists
+			e.printStackTrace();
 		}
 
 		return view;