|
@@ -2,6 +2,7 @@ package de.tudarmstadt.informatik.hostage.ui2.activity;
|
|
|
|
|
|
import android.app.ActionBar;
|
|
import android.app.ActionBar;
|
|
import android.app.Activity;
|
|
import android.app.Activity;
|
|
|
|
+import android.app.ActivityManager;
|
|
import android.app.Fragment;
|
|
import android.app.Fragment;
|
|
import android.app.FragmentManager;
|
|
import android.app.FragmentManager;
|
|
import android.app.FragmentTransaction;
|
|
import android.app.FragmentTransaction;
|
|
@@ -42,53 +43,95 @@ import de.tudarmstadt.informatik.hostage.ui2.model.DrawerListItem;
|
|
* @created 12.01.14 23:24
|
|
* @created 12.01.14 23:24
|
|
*/
|
|
*/
|
|
public class MainActivity extends Activity {
|
|
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 DrawerLayout mDrawerLayout;
|
|
|
|
+
|
|
private ListView mDrawerList;
|
|
private ListView mDrawerList;
|
|
|
|
+
|
|
private ActionBarDrawerToggle mDrawerToggle;
|
|
private ActionBarDrawerToggle mDrawerToggle;
|
|
|
|
|
|
private CharSequence mDrawerTitle;
|
|
private CharSequence mDrawerTitle;
|
|
|
|
+
|
|
private CharSequence mTitle;
|
|
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
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
super.onCreate(savedInstanceState);
|
|
|
|
+
|
|
|
|
+ sInstance = this;
|
|
|
|
+
|
|
MainActivity.context = getApplicationContext();
|
|
MainActivity.context = getApplicationContext();
|
|
- this.selectedMenuItem = MainMenuItem.HOME;
|
|
|
|
|
|
|
|
setContentView(R.layout.activity_drawer_main);
|
|
setContentView(R.layout.activity_drawer_main);
|
|
|
|
|
|
@@ -108,17 +151,20 @@ public class MainActivity extends Activity {
|
|
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
|
|
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
|
|
mDrawerList = (ListView) findViewById(R.id.left_drawer);
|
|
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.setAdapter(listAdapter);
|
|
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
|
|
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
|
|
@@ -145,20 +191,33 @@ public class MainActivity extends Activity {
|
|
};
|
|
};
|
|
mDrawerLayout.setDrawerListener(mDrawerToggle);
|
|
mDrawerLayout.setDrawerListener(mDrawerToggle);
|
|
|
|
|
|
|
|
+ checkRootAndPorthack();
|
|
|
|
+ startAndBind();
|
|
|
|
+
|
|
if (savedInstanceState == null) {
|
|
if (savedInstanceState == null) {
|
|
// on first time display view for first nav item
|
|
// on first time display view for first nav item
|
|
displayView(0);
|
|
displayView(0);
|
|
}
|
|
}
|
|
- checkRootAndPorthack();
|
|
|
|
- startAndBind();
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- private void startAndBind() {
|
|
|
|
- startService(getServiceIntent());
|
|
|
|
|
|
+ public void startAndBind() {
|
|
|
|
+ if(!isServiceRunning()){
|
|
|
|
+ startService(getServiceIntent());
|
|
|
|
+ }
|
|
|
|
+
|
|
bindService();
|
|
bindService();
|
|
}
|
|
}
|
|
|
|
|
|
- private void bindService() {
|
|
|
|
|
|
+ public void stopAndUnbind() {
|
|
|
|
+ unbindService();
|
|
|
|
+ stopService(getServiceIntent());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void unbindService() {
|
|
|
|
+ unbindService(mConnection);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void bindService() {
|
|
bindService(getServiceIntent(), mConnection, BIND_AUTO_CREATE);
|
|
bindService(getServiceIntent(), mConnection, BIND_AUTO_CREATE);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -198,16 +257,16 @@ public class MainActivity extends Activity {
|
|
}
|
|
}
|
|
|
|
|
|
private void displayView(int position) {
|
|
private void displayView(int position) {
|
|
|
|
+ if(mSelectedMenuItem != null && position == mSelectedMenuItem.value) {
|
|
|
|
+ mDrawerLayout.closeDrawer(mDrawerList);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
// DONT OPEN SAME VIEW AGAIN
|
|
// DONT OPEN SAME VIEW AGAIN
|
|
- MainMenuItem menuItemPosition = MainMenuItem.create(position);
|
|
|
|
|
|
+ MainMenuItem menuItemPosition = mSelectedMenuItem = MainMenuItem.create(position);
|
|
|
|
|
|
Fragment fragment = null;
|
|
Fragment fragment = null;
|
|
|
|
|
|
- if (this.selectedMenuItem == menuItemPosition && this.displayedFragment != null) {
|
|
|
|
- menuItemPosition = MainMenuItem.UNUSED;
|
|
|
|
- fragment = this.displayedFragment;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
// update the main content by replacing fragments
|
|
// update the main content by replacing fragments
|
|
switch (menuItemPosition) {
|
|
switch (menuItemPosition) {
|
|
case HOME:
|
|
case HOME:
|
|
@@ -233,80 +292,32 @@ public class MainActivity extends Activity {
|
|
|
|
|
|
if (fragment != null) {
|
|
if (fragment != null) {
|
|
// update selected item and title, then close the drawer if needed
|
|
// 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() {
|
|
public Intent getServiceIntent() {
|
|
return new Intent(this, HoneyService.class);
|
|
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() {
|
|
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
|
|
* 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() {
|
|
private void checkRootAndPorthack() {
|
|
isRooted = false;
|
|
isRooted = false;
|
|
@@ -337,4 +348,51 @@ public class MainActivity extends Activity {
|
|
Log.i("MainAc", "Rooted: " + isRooted + " Porthack: "
|
|
Log.i("MainAc", "Rooted: " + isRooted + " Porthack: "
|
|
+ porthackInstalled);
|
|
+ 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);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|