Browse Source

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

Alexander Brakowski 10 years ago
parent
commit
e166c3710a
1 changed files with 99 additions and 0 deletions
  1. 99 0
      src/de/tudarmstadt/informatik/hostage/ui2/activity/MainActivity.java

+ 99 - 0
src/de/tudarmstadt/informatik/hostage/ui2/activity/MainActivity.java

@@ -1,5 +1,7 @@
 package de.tudarmstadt.informatik.hostage.ui2.activity;
 
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
 import java.util.ArrayList;
 
 import android.app.ActionBar;
@@ -7,18 +9,23 @@ import android.app.Activity;
 import android.app.Fragment;
 import android.app.FragmentManager;
 import android.app.FragmentTransaction;
+import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
+import android.content.ServiceConnection;
 import android.content.res.Configuration;
 import android.content.res.TypedArray;
 import android.os.Bundle;
+import android.os.IBinder;
 import android.support.v4.app.ActionBarDrawerToggle;
 import android.support.v4.widget.DrawerLayout;
+import android.util.Log;
 import android.view.MenuItem;
 import android.view.View;
 import android.widget.AdapterView;
 import android.widget.ListView;
 
+import de.tudarmstadt.informatik.hostage.HoneyService;
 import de.tudarmstadt.informatik.hostage.R;
 import de.tudarmstadt.informatik.hostage.ui.LogFilter;
 import de.tudarmstadt.informatik.hostage.ui.ViewLogTable;
@@ -46,14 +53,21 @@ public class MainActivity extends Activity {
 	public Fragment displayedFragment;
 
 	private ArrayList<DrawerListItem> drawerItems;
+	private HoneyService mHoneyService;
 
 	public static volatile Context context;
 
+	public static boolean isRooted = false;
+	public static boolean porthackInstalled = false;
+
+	private boolean serviceBound;
+
 	@Override
 	protected void onCreate(Bundle savedInstanceState) {
 		super.onCreate(savedInstanceState);
 		MainActivity.context = getApplicationContext();
 
+
 		setContentView(R.layout.activity_drawer_main);
 
 		ThreatIndicatorGLRenderer.assets = getAssets();
@@ -113,6 +127,17 @@ public class MainActivity extends Activity {
 			// on first time display view for first nav item
 			displayView(0);
 		}
+		checkRootAndPorthack();
+		startAndBind();
+	}
+
+	private void startAndBind() {
+		startService(getServiceIntent());
+		bindService();
+	}
+
+	private void bindService() {
+		bindService(getServiceIntent(), mConnection, BIND_AUTO_CREATE);
 	}
 
 	@Override
@@ -192,6 +217,10 @@ public class MainActivity extends Activity {
 		}
 	}
 
+	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);
@@ -205,4 +234,74 @@ public class MainActivity extends Activity {
 	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() {
+
+	}
+
+	/**
+	 * Checks if the phone ist rooted and if porthack is installed. Sets flags
+	 * {@link isRooted} and {@link porthackInstalled}
+	 */
+	private void checkRootAndPorthack() {
+		isRooted = false;
+		porthackInstalled = false;
+		Process p;
+		try {
+			String found = "Found";
+			String notFound = "Not found";
+			String command = "[ -f /data/local/p ] && echo " + found
+					+ " || echo " + notFound;
+			p = Runtime.getRuntime().exec(new String[] { "su", "-c", command });
+			BufferedReader in = new BufferedReader(new InputStreamReader(
+					p.getInputStream()));
+			/*
+			 * int av = byte[] b = new byte[av]; if (av != 0) { in.read(b); }
+			 */
+			String echoResponse = in.readLine();
+			Log.i("MainAc", echoResponse);
+			if (echoResponse.equals(found)) {
+				isRooted = true;
+				porthackInstalled = true;
+			} else if (echoResponse.equals(notFound)) {
+				isRooted = true;
+			}
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+		Log.i("MainAc", "Rooted: " + isRooted + " Porthack: "
+				+ porthackInstalled);
+	}
 }