Browse Source

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

Julien Clauter 10 years ago
parent
commit
04954ffa86

+ 0 - 10
HostageV2/src/main/AndroidManifest.xml

@@ -1,10 +0,0 @@
-<manifest xmlns:android="http://schemas.android.com/apk/res/android" >
-
-    <application>
-        <activity android:name=".........src.de.tudarmstadt.informatik.hostage.ui2.preference.ProfileEditActivity"
-            android:label="@string/title_activity_profile_edit"
-            >
-        </activity>
-    </application>
-
-</manifest>

+ 0 - 59
HostageV2/src/main/res/values/strings_activity_profile_edit.xml

@@ -1,59 +0,0 @@
-<resources>
-        <string name="title_activity_profile_edit">Profile Edit</string>
-
-    <!-- Strings related to Settings -->
-
-    <!-- Example General settings -->
-    <string name="pref_header_general">General</string>
-
-    <string name="pref_title_social_recommendations">Enable social recommendations</string>
-    <string name="pref_description_social_recommendations">Recommendations for people to contact based on your message history</string>
-
-    <string name="pref_title_display_name">Display name</string>
-    <string name="pref_default_display_name">John Smith</string>
-
-    <string name="pref_title_add_friends_to_messages">Add friends to messages</string>
-    <string-array name="pref_example_list_titles">
-        <item>Always</item>
-        <item>When possible</item>
-        <item>Never</item>
-    </string-array>
-    <string-array name="pref_example_list_values">
-        <item>1</item>
-        <item>0</item>
-        <item>-1</item>
-    </string-array>
-
-    <!-- Example settings for Data & Sync -->
-    <string name="pref_header_data_sync">Data &amp; sync</string>
-
-    <string name="pref_title_sync_frequency">Sync frequency</string>
-    <string-array name="pref_sync_frequency_titles">
-        <item>15 minutes</item>
-        <item>30 minutes</item>
-        <item>1 hour</item>
-        <item>3 hours</item>
-        <item>6 hours</item>
-        <item>Never</item>
-    </string-array>
-    <string-array name="pref_sync_frequency_values">
-        <item>15</item>
-        <item>30</item>
-        <item>60</item>
-        <item>180</item>
-        <item>360</item>
-        <item>-1</item>
-    </string-array>
-
-    <string name="pref_title_system_sync_settings">System sync settings</string>
-
-    <!-- Example settings for Notifications -->
-    <string name="pref_header_notifications">Notifications</string>
-
-    <string name="pref_title_new_message_notifications">New message notifications</string>
-
-    <string name="pref_title_ringtone">Ringtone</string>
-    <string name="pref_ringtone_silent">Silent</string>
-
-    <string name="pref_title_vibrate">Vibrate</string>
-</resources>

+ 0 - 21
HostageV2/src/main/res/xml/pref_data_sync.xml

@@ -1,21 +0,0 @@
-<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
-
-    <!-- NOTE: Hide buttons to simplify the UI. Users can touch outside the dialog to
-         dismiss it. -->
-    <!-- NOTE: ListPreference's summary should be set to its value by the activity code. -->
-    <ListPreference
-        android:key="sync_frequency"
-        android:title="@string/pref_title_sync_frequency"
-        android:entries="@array/pref_sync_frequency_titles"
-        android:entryValues="@array/pref_sync_frequency_values"
-        android:defaultValue="180"
-        android:negativeButtonText="@null"
-        android:positiveButtonText="@null" />
-
-    <!-- This preference simply launches an intent when selected. Use this UI sparingly, per
-         design guidelines. -->
-    <Preference android:title="@string/pref_title_system_sync_settings">
-        <intent android:action="android.settings.SYNC_SETTINGS" />
-    </Preference>
-
-</PreferenceScreen>

+ 0 - 33
HostageV2/src/main/res/xml/pref_general.xml

@@ -1,33 +0,0 @@
-<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
-
-    <CheckBoxPreference
-        android:key="example_checkbox"
-        android:title="@string/pref_title_social_recommendations"
-        android:summary="@string/pref_description_social_recommendations"
-        android:defaultValue="true" />
-
-    <!-- NOTE: EditTextPreference accepts EditText attributes. -->
-    <!-- NOTE: EditTextPreference's summary should be set to its value by the activity code. -->
-    <EditTextPreference
-        android:key="example_text"
-        android:title="@string/pref_title_display_name"
-        android:defaultValue="@string/pref_default_display_name"
-        android:selectAllOnFocus="true"
-        android:inputType="textCapWords"
-        android:capitalize="words"
-        android:singleLine="true"
-        android:maxLines="1" />
-
-    <!-- NOTE: Hide buttons to simplify the UI. Users can touch outside the dialog to
-         dismiss it. -->
-    <!-- NOTE: ListPreference's summary should be set to its value by the activity code. -->
-    <ListPreference
-        android:key="example_list"
-        android:title="@string/pref_title_add_friends_to_messages"
-        android:defaultValue="-1"
-        android:entries="@array/pref_example_list_titles"
-        android:entryValues="@array/pref_example_list_values"
-        android:negativeButtonText="@null"
-        android:positiveButtonText="@null" />
-
-</PreferenceScreen>

+ 0 - 17
HostageV2/src/main/res/xml/pref_headers.xml

@@ -1,17 +0,0 @@
-<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
-
-    <!-- These settings headers are only used on tablets. -->
-
-    <header
-        android:fragment=".........src.de.tudarmstadt.informatik.hostage.ui2.preference.ProfileEditActivity$GeneralPreferenceFragment"
-        android:title="@string/pref_header_general" />
-
-    <header
-        android:fragment=".........src.de.tudarmstadt.informatik.hostage.ui2.preference.ProfileEditActivity$NotificationPreferenceFragment"
-        android:title="@string/pref_header_notifications" />
-
-    <header
-        android:fragment=".........src.de.tudarmstadt.informatik.hostage.ui2.preference.ProfileEditActivity$DataSyncPreferenceFragment"
-        android:title="@string/pref_header_data_sync" />
-
-</preference-headers>

+ 0 - 27
HostageV2/src/main/res/xml/pref_notification.xml

@@ -1,27 +0,0 @@
-<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
-
-    <!-- A 'parent' preference, which enables/disables child preferences (below)
-         when checked/unchecked. -->
-    <CheckBoxPreference
-        android:key="notifications_new_message"
-        android:title="@string/pref_title_new_message_notifications"
-        android:defaultValue="true" />
-
-    <!-- Allows the user to choose a ringtone in the 'notification' category. -->
-    <!-- NOTE: This preference will be enabled only when the checkbox above is checked. -->
-    <!-- NOTE: RingtonePreference's summary should be set to its value by the activity code. -->
-    <RingtonePreference
-        android:dependency="notifications_new_message"
-        android:key="notifications_new_message_ringtone"
-        android:title="@string/pref_title_ringtone"
-        android:ringtoneType="notification"
-        android:defaultValue="content://settings/system/notification_sound" />
-
-    <!-- NOTE: This preference will be enabled only when the checkbox above is checked. -->
-    <CheckBoxPreference
-        android:dependency="notifications_new_message"
-        android:key="notifications_new_message_vibrate"
-        android:title="@string/pref_title_vibrate"
-        android:defaultValue="true" />
-
-</PreferenceScreen>

+ 167 - 109
src/de/tudarmstadt/informatik/hostage/ui2/activity/MainActivity.java

@@ -2,6 +2,7 @@ package de.tudarmstadt.informatik.hostage.ui2.activity;
 
 import android.app.ActionBar;
 import android.app.Activity;
+import android.app.ActivityManager;
 import android.app.Fragment;
 import android.app.FragmentManager;
 import android.app.FragmentTransaction;
@@ -42,53 +43,95 @@ import de.tudarmstadt.informatik.hostage.ui2.model.DrawerListItem;
  * @created 12.01.14 23:24
  */
 public class MainActivity extends Activity {
+	public static volatile Context context;
 
-    public enum MainMenuItem {
-        UNUSED(-1),
-        HOME(0),
-        THREAT_MAP(1),
-        RECORDS(2),
-        SERVICES(3),
-        PROFILE_MANAGER(4),
-        SETTINGS(5),
-        APPLICATION_INFO(6);
-        private int value;
+	public static boolean isRooted = false;
 
-        private MainMenuItem(int value) {
-            this.value = value;
-        }
-        static public MainMenuItem create(int value){
-            if (value < -1 || value+1  >= MainMenuItem.values().length) return MainMenuItem.HOME;
-            return  MainMenuItem.values()[value+1];
-        }
+	public static boolean porthackInstalled = false;
 
-    }
+	/** singleton instance of the MainActivity **/
+	private static MainActivity sInstance = null;
+
+	public Fragment mDisplayedFragment;
+
+	public HoneyService mHoneyService;
 
 	private DrawerLayout mDrawerLayout;
+
 	private ListView mDrawerList;
+
 	private ActionBarDrawerToggle mDrawerToggle;
 
 	private CharSequence mDrawerTitle;
+
 	private CharSequence mTitle;
 
-	public Fragment displayedFragment;
-    private MainMenuItem selectedMenuItem;
+    private MainMenuItem mSelectedMenuItem = null;
 
-	private ArrayList<DrawerListItem> drawerItems;
-	private HoneyService mHoneyService;
+	private ArrayList<DrawerListItem> mDrawerItems;
 
-	public static volatile Context context;
+	private boolean mServiceBound;
 
-	public static boolean isRooted = false;
-	public static boolean porthackInstalled = false;
+	/**
+	 * Connection to bind the background service
+	 *
+	 * @see HoneyService
+	 */
+	private ServiceConnection mConnection = new ServiceConnection() {
+		/**
+		 * After the service is bound, check which has been clicked and start
+		 * it.
+		 *
+		 * @see android.content.ServiceConnection#onServiceConnected(android.content.ComponentName, android.os.IBinder)
+		 */
+		@Override
+		public void onServiceConnected(ComponentName name, IBinder service) {
+			mHoneyService = ((HoneyService.LocalBinder) service).getService();
+			mServiceBound = true;
+		}
+
+		/**
+		 * After the service is unbound, delete reference.
+		 *
+		 * @see android.content.ServiceConnection#onServiceDisconnected(android.content.ComponentName)
+		 */
+		@Override
+		public void onServiceDisconnected(ComponentName name) {
+			mHoneyService = null;
+			mServiceBound = false;
+		}
+
+	};
+
+	/**
+	 * Retrieve the singleton latest instance of the activity
+	 * @return MainActivity - the singleton instance
+	 */
+	public static MainActivity getInstance(){
+		assert(sInstance != null);
+		return sInstance;
+	}
 
-	private boolean serviceBound;
+	public static Context getContext(){
+		return MainActivity.context;
+	}
+
+	@Override
+	public void onStart(){
+		super.onStart();
+
+		if(isServiceRunning()){
+			this.bindService();
+		}
+	}
 
 	@Override
 	protected void onCreate(Bundle savedInstanceState) {
 		super.onCreate(savedInstanceState);
+
+		sInstance = this;
+
 		MainActivity.context = getApplicationContext();
-        this.selectedMenuItem = MainMenuItem.HOME;
 
 		setContentView(R.layout.activity_drawer_main);
 
@@ -108,17 +151,20 @@ public class MainActivity extends Activity {
 		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
 		mDrawerList = (ListView) findViewById(R.id.left_drawer);
 
-		drawerItems = new ArrayList<DrawerListItem>();
-		drawerItems.add(new DrawerListItem(R.string.drawer_overview, R.drawable.ic_menu_home));
-		drawerItems.add(new DrawerListItem(R.string.drawer_threat_map, R.drawable.ic_menu_mapmode));
-		drawerItems.add(new DrawerListItem(R.string.drawer_records, R.drawable.ic_action_stats));
-		drawerItems.add(new DrawerListItem(R.string.drawer_services, R.drawable.ic_menu_set_as));
-		drawerItems.add(new DrawerListItem(R.string.drawer_profile_manager, R.drawable.ic_menu_allfriends));
-		drawerItems.add(new DrawerListItem(R.string.drawer_settings, R.drawable.ic_menu_preferences));
-		drawerItems.add(new DrawerListItem(R.string.drawer_app_info, R.drawable.ic_menu_info_details));
+		mDrawerItems = new ArrayList<DrawerListItem>();
+		mDrawerItems.add(new DrawerListItem(R.string.drawer_overview, R.drawable.ic_menu_home));
+		mDrawerItems.add(new DrawerListItem(R.string.drawer_threat_map, R.drawable.ic_menu_mapmode));
+		mDrawerItems.add(new DrawerListItem(R.string.drawer_records, R.drawable.ic_action_stats));
+		mDrawerItems.add(new DrawerListItem(R.string.drawer_services, R.drawable.ic_menu_set_as));
+		mDrawerItems.add(
+				new DrawerListItem(R.string.drawer_profile_manager, R.drawable.ic_menu_allfriends));
+		mDrawerItems.add(
+				new DrawerListItem(R.string.drawer_settings, R.drawable.ic_menu_preferences));
+		mDrawerItems.add(
+				new DrawerListItem(R.string.drawer_app_info, R.drawable.ic_menu_info_details));
 
 
-		DrawerListAdapter listAdapter = new DrawerListAdapter(this, drawerItems);
+		DrawerListAdapter listAdapter = new DrawerListAdapter(this, mDrawerItems);
 
 		mDrawerList.setAdapter(listAdapter);
 		mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
@@ -145,20 +191,33 @@ public class MainActivity extends Activity {
 		};
 		mDrawerLayout.setDrawerListener(mDrawerToggle);
 
+		checkRootAndPorthack();
+		startAndBind();
+
 		if (savedInstanceState == null) {
 			// on first time display view for first nav item
 			displayView(0);
 		}
-		checkRootAndPorthack();
-		startAndBind();
 	}
 
-	private void startAndBind() {
-		startService(getServiceIntent());
+	public void startAndBind() {
+		if(!isServiceRunning()){
+			startService(getServiceIntent());
+		}
+
 		bindService();
 	}
 
-	private void bindService() {
+	public void stopAndUnbind() {
+		unbindService();
+		stopService(getServiceIntent());
+	}
+
+	public void unbindService() {
+		unbindService(mConnection);
+	}
+
+	public void bindService() {
 		bindService(getServiceIntent(), mConnection, BIND_AUTO_CREATE);
 	}
 
@@ -198,16 +257,16 @@ public class MainActivity extends Activity {
 	}
 
 	private void displayView(int position) {
+		if(mSelectedMenuItem != null && position == mSelectedMenuItem.value) {
+			mDrawerLayout.closeDrawer(mDrawerList);
+			return;
+		}
+
         // DONT OPEN SAME VIEW AGAIN
-        MainMenuItem menuItemPosition = MainMenuItem.create(position);
+		MainMenuItem menuItemPosition = mSelectedMenuItem = MainMenuItem.create(position);
 
         Fragment fragment = null;
 
-        if (this.selectedMenuItem == menuItemPosition &&  this.displayedFragment != null) {
-            menuItemPosition = MainMenuItem.UNUSED;
-            fragment = this.displayedFragment;
-        }
-
         // update the main content by replacing fragments
 		switch (menuItemPosition) {
 			case HOME:
@@ -233,80 +292,32 @@ public class MainActivity extends Activity {
 
 		if (fragment != null) {
 			// update selected item and title, then close the drawer if needed
-            if (!fragment.equals(this.displayedFragment)){
-                FragmentManager fragmentManager = getFragmentManager();
-                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
-                fragmentTransaction.replace(R.id.content_frame, fragment, fragment.getTag());
-                fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
-                fragmentTransaction.commit();
-			    mDrawerList.setItemChecked(position, true);
-			    mDrawerList.setSelection(position);
-			    setTitle(drawerItems.get(position).text);
-                this.selectedMenuItem = MainMenuItem.create(position);
-            }
-
-            mDrawerLayout.closeDrawer(mDrawerList);
-            this.displayedFragment = fragment;
+            FragmentManager fragmentManager = getFragmentManager();
+            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
+            fragmentTransaction.replace(R.id.content_frame, fragment, fragment.getTag());
+            fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
+            fragmentTransaction.commit();
+		    mDrawerList.setItemChecked(position, true);
+		    mDrawerList.setSelection(position);
+		    setTitle(mDrawerItems.get(position).text);
+
+            this.mDisplayedFragment = fragment;
         }
+
+		mDrawerLayout.closeDrawer(mDrawerList);
 	}
 
 	public Intent getServiceIntent() {
 		return new Intent(this, HoneyService.class);
 	}
 
-	private class DrawerItemClickListener implements ListView.OnItemClickListener {
-		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
-			displayView(position);
-		}
-	}
-
 	public Fragment getDisplayedFragment() {
-		return this.displayedFragment;
-	}
-
-	public static Context getContext(){
-		return MainActivity.context;
-	}
-
-	/**
-	 * Connection to bind the background service
-	 *
-	 * @see HoneyService
-	 */
-	private ServiceConnection mConnection = new ServiceConnection() {
-		/**
-		 * After the service is bound, check which has been clicked and start
-		 * it.
-		 *
-		 * @see android.content.ServiceConnection#onServiceConnected(android.content.ComponentName)
-		 */
-		@Override
-		public void onServiceConnected(ComponentName name, IBinder service) {
-			mHoneyService = ((HoneyService.LocalBinder) service).getService();
-			serviceBound = true;
-			updateUI();
-		}
-
-		/**
-		 * After the service is unbound, delete reference.
-		 *
-		 * @see android.content.ServiceConnection#onServiceDisconnected(android.content.ComponentName)
-		 */
-		@Override
-		public void onServiceDisconnected(ComponentName name) {
-			mHoneyService = null;
-			serviceBound = false;
-		}
-
-	};
-
-	private void updateUI() {
-
+		return this.mDisplayedFragment;
 	}
 
 	/**
 	 * Checks if the phone ist rooted and if porthack is installed. Sets flags
-	 * {@link isRooted} and {@link porthackInstalled}
+	 * {@link this.isRooted} and {@link this.porthackInstalled}
 	 */
 	private void checkRootAndPorthack() {
 		isRooted = false;
@@ -337,4 +348,51 @@ public class MainActivity extends Activity {
 		Log.i("MainAc", "Rooted: " + isRooted + " Porthack: "
 				+ porthackInstalled);
 	}
+
+	public HoneyService getHoneyService(){
+		return this.mHoneyService;
+	}
+
+	public boolean isServiceBound(){
+		return this.mServiceBound;
+	}
+
+	public boolean isServiceRunning() {
+		ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
+		for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(
+				Integer.MAX_VALUE)) {
+			if (service.service.getClassName().equals(
+					HoneyService.class.getName())) {
+				return true;
+			}
+		}
+		return false;
+	}
+
+	public enum MainMenuItem {
+        HOME(0),
+        THREAT_MAP(1),
+        RECORDS(2),
+        SERVICES(3),
+        PROFILE_MANAGER(4),
+        SETTINGS(5),
+        APPLICATION_INFO(6);
+
+        private int value;
+
+        private MainMenuItem(int value) {
+            this.value = value;
+        }
+        static public MainMenuItem create(int value){
+            if (value < 0 || value  >= MainMenuItem.values().length) return MainMenuItem.HOME;
+            return  MainMenuItem.values()[value];
+        }
+
+    }
+
+	private class DrawerItemClickListener implements ListView.OnItemClickListener {
+		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
+			displayView(position);
+		}
+	}
 }

+ 18 - 7
src/de/tudarmstadt/informatik/hostage/ui2/fragment/HomeFragment.java

@@ -8,6 +8,7 @@ import android.content.Intent;
 import android.content.IntentFilter;
 import android.net.wifi.WifiManager;
 import android.os.Bundle;
+import android.support.v4.content.LocalBroadcastManager;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -17,6 +18,7 @@ import android.widget.TextView;
 
 import de.tudarmstadt.informatik.hostage.R;
 import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
+import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
 import de.tudarmstadt.informatik.hostage.ui2.fragment.opengl.ThreatIndicatorGLRenderer;
 
 /**
@@ -34,7 +36,7 @@ public class HomeFragment extends Fragment {
 	private TextView mHomeTextProfileHeader;
 
 	private View rootView;
-	private BroadcastReceiver wifiReceiver;
+	private BroadcastReceiver mReceiver;
 
 	private CompoundButton.OnCheckedChangeListener switchChangeListener = null;
 
@@ -50,10 +52,10 @@ public class HomeFragment extends Fragment {
 	}
 
 	private void registerBroadcastReceiver(){
-		wifiReceiver = new BroadcastReceiver() {
+		mReceiver = new BroadcastReceiver() {
 			@Override
 			public void onReceive(Context context, Intent intent) {
-				final String action = intent.getAction();
+				//final String action = intent.getAction();
 				//if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION) || action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {
 					updateUI();
 					/*if (intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)){
@@ -65,13 +67,15 @@ public class HomeFragment extends Fragment {
 			}
 		};
 
-		IntentFilter intentFilter = new IntentFilter();
+		/*IntentFilter intentFilter = new IntentFilter();
 		intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
 		intentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
 		intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
-		intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
+		intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);*/
 
-		getActivity().registerReceiver(wifiReceiver, intentFilter);
+		LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver,
+				new IntentFilter(getString(R.string.broadcast)));
+		//getActivity().registerReceiver(mReceiver, intentFilter);
 	}
 
 	public HomeFragment(){}
@@ -172,8 +176,15 @@ 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);
+						    }
+
 						    setStateActive();
 					    } else {
+						    MainActivity.getInstance().getHoneyService().stopListeners();
+						    MainActivity.getInstance().stopAndUnbind();
 						    setStateNotActive();
 					    }
 				    }
@@ -188,6 +199,6 @@ public class HomeFragment extends Fragment {
 	@Override
 	public void onDestroy(){
 		super.onDestroy();
-		getActivity().unregisterReceiver(wifiReceiver);
+		LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mReceiver);
 	}
 }