MainActivity.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. package de.tudarmstadt.informatik.hostage.ui.activity;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.app.ActionBar;
  5. import android.app.Activity;
  6. import android.app.ActivityManager;
  7. import android.app.AlertDialog;
  8. import android.app.Fragment;
  9. import android.app.FragmentManager;
  10. import android.app.FragmentTransaction;
  11. import android.content.ComponentName;
  12. import android.content.Context;
  13. import android.content.DialogInterface;
  14. import android.content.Intent;
  15. import android.content.ServiceConnection;
  16. import android.content.SharedPreferences;
  17. import android.content.pm.ActivityInfo;
  18. import android.content.res.Configuration;
  19. import android.content.res.TypedArray;
  20. import android.net.Uri;
  21. import android.os.Bundle;
  22. import android.os.IBinder;
  23. import android.support.v4.app.ActionBarDrawerToggle;
  24. import android.support.v4.widget.DrawerLayout;
  25. import android.text.Html;
  26. import android.util.Log;
  27. import android.view.Gravity;
  28. import android.view.KeyEvent;
  29. import android.view.MenuItem;
  30. import android.view.View;
  31. import android.widget.AdapterView;
  32. import android.widget.ListView;
  33. import android.widget.Toast;
  34. import de.tudarmstadt.informatik.hostage.Hostage;
  35. import de.tudarmstadt.informatik.hostage.R;
  36. import de.tudarmstadt.informatik.hostage.model.Profile;
  37. import de.tudarmstadt.informatik.hostage.persistence.ProfileManager;
  38. import de.tudarmstadt.informatik.hostage.system.Device;
  39. import de.tudarmstadt.informatik.hostage.ui.model.LogFilter;
  40. import de.tudarmstadt.informatik.hostage.ui.adapter.DrawerListAdapter;
  41. import de.tudarmstadt.informatik.hostage.ui.fragment.AboutFragment;
  42. import de.tudarmstadt.informatik.hostage.ui.fragment.HomeFragment;
  43. import de.tudarmstadt.informatik.hostage.ui.fragment.ProfileManagerFragment;
  44. import de.tudarmstadt.informatik.hostage.ui.fragment.RecordOverviewFragment;
  45. import de.tudarmstadt.informatik.hostage.ui.fragment.ServicesFragment;
  46. import de.tudarmstadt.informatik.hostage.ui.fragment.SettingsFragment;
  47. import de.tudarmstadt.informatik.hostage.ui.fragment.StatisticsFragment;
  48. import de.tudarmstadt.informatik.hostage.ui.fragment.ThreatMapFragment;
  49. import de.tudarmstadt.informatik.hostage.ui.fragment.UpNavigatibleFragment;
  50. import de.tudarmstadt.informatik.hostage.ui.fragment.opengl.ThreatIndicatorGLRenderer;
  51. import de.tudarmstadt.informatik.hostage.ui.model.DrawerListItem;
  52. /**
  53. * Manages the whole application, and should act like an singleton.
  54. *
  55. * @author Alexander Brakowski
  56. * @created 12.01.14 23:24
  57. */
  58. public class MainActivity extends Activity {
  59. public static volatile Context context;
  60. /** singleton instance of the MainActivity **/
  61. private static MainActivity sInstance = null;
  62. /**
  63. * The currently displayed fragment
  64. */
  65. public Fragment mDisplayedFragment;
  66. /**
  67. * Holds the Hostage Service
  68. */
  69. public Hostage mHoneyService;
  70. /**
  71. * Manages the navigation drawer
  72. */
  73. private DrawerLayout mDrawerLayout;
  74. /**
  75. * Contains the listview to be displayed in the navigation drawer
  76. */
  77. private ListView mDrawerList;
  78. /**
  79. * Holds the toggler for the navigation drawer in the action bar
  80. */
  81. private ActionBarDrawerToggle mDrawerToggle;
  82. /**
  83. * The text that should be displayed in the drawer toggle
  84. */
  85. private CharSequence mDrawerTitle;
  86. /**
  87. * The text that should be displayed in the action bar
  88. */
  89. private CharSequence mTitle;
  90. /**
  91. * Holds the list, that should be displayed in the listview of the navigation drawer
  92. */
  93. private ArrayList<DrawerListItem> mDrawerItems;
  94. /**
  95. * Hold the state of the Hostage service
  96. */
  97. private boolean mServiceBound = false;
  98. /**
  99. * Connection to bind the background service
  100. *
  101. * @see de.tudarmstadt.informatik.hostage.Hostage
  102. */
  103. private ServiceConnection mConnection = new ServiceConnection() {
  104. /**
  105. * After the service is bound, check which has been clicked and start
  106. * it.
  107. *
  108. * @see android.content.ServiceConnection#onServiceConnected(android.content.ComponentName,
  109. * android.os.IBinder)
  110. */
  111. @Override
  112. public void onServiceConnected(ComponentName name, IBinder service) {
  113. mHoneyService = ((Hostage.LocalBinder) service).getService();
  114. mServiceBound = true;
  115. }
  116. /**
  117. * After the service is unbound, delete reference.
  118. *
  119. * @see android.content.ServiceConnection#onServiceDisconnected(android.content.ComponentName)
  120. */
  121. @Override
  122. public void onServiceDisconnected(ComponentName name) {
  123. mHoneyService = null;
  124. mServiceBound = false;
  125. }
  126. };
  127. /**
  128. * Holds an profile manager instance
  129. */
  130. private ProfileManager mProfileManager;
  131. /**
  132. * Holds the root fragment for our hierarchical fragment navigation
  133. */
  134. private Fragment mRootFragment;
  135. /**
  136. * Indicates if the warning, that the application will be closed, when pressing back again
  137. */
  138. private boolean mCloseWarning = false;
  139. /**
  140. * Hold the shared preferences for the app
  141. */
  142. private SharedPreferences mSharedPreferences;
  143. /**
  144. * Retrieve the singleton latest instance of the activity
  145. *
  146. * @return MainActivity - the singleton instance
  147. */
  148. public static MainActivity getInstance() {
  149. return sInstance;
  150. }
  151. /**
  152. * Retrieves the context of the application
  153. *
  154. * @return the context
  155. */
  156. public static Context getContext() {
  157. return MainActivity.context;
  158. }
  159. /**
  160. * {@inheritDoc}
  161. */
  162. @Override
  163. public void onStart() {
  164. super.onStart();
  165. if (isServiceRunning()) {
  166. this.bindService();
  167. }
  168. }
  169. /**
  170. * {@inheritDoc}
  171. */
  172. @Override
  173. public void onStop() {
  174. this.unbindService();
  175. super.onStop();
  176. }
  177. /**
  178. * {@inheritDoc}
  179. */
  180. @Override
  181. protected void onCreate(Bundle savedInstanceState) {
  182. super.onCreate(savedInstanceState);
  183. // make the main activity an singleton
  184. sInstance = this;
  185. // sets the static context reference to the application context
  186. MainActivity.context = getApplicationContext();
  187. setContentView(R.layout.activity_drawer_main);
  188. mProfileManager = ProfileManager.getInstance();
  189. // check for the porthack and iptables
  190. Device.checkCapabilities();
  191. if (Device.isPortRedirectionAvailable()) {
  192. // redirect all the ports!!
  193. Device.executePortRedirectionScript();
  194. }
  195. // init threat indicator animation
  196. ThreatIndicatorGLRenderer.assets = getAssets();
  197. ThreatIndicatorGLRenderer.setThreatLevel(ThreatIndicatorGLRenderer.ThreatLevel.NOT_MONITORING);
  198. // set background color
  199. TypedArray arr = getTheme().obtainStyledAttributes(new int[] { android.R.color.background_light });
  200. ThreatIndicatorGLRenderer.setBackgroundColor(arr.getColor(0, 0xFFFFFF));
  201. arr.recycle();
  202. // configures the action bar
  203. ActionBar actionBar = getActionBar();
  204. actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP);
  205. actionBar.setDisplayHomeAsUpEnabled(true);
  206. actionBar.setHomeButtonEnabled(true);
  207. actionBar.setDisplayShowHomeEnabled(true);
  208. // sets the drawer and action title to the application title
  209. mTitle = mDrawerTitle = getTitle();
  210. mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  211. mDrawerList = (ListView) findViewById(R.id.left_drawer);
  212. // propagates the navigation drawer with items
  213. mDrawerItems = new ArrayList<DrawerListItem>();
  214. mDrawerItems.add(new DrawerListItem(R.string.drawer_overview, R.drawable.ic_menu_home));
  215. mDrawerItems.add(new DrawerListItem(R.string.drawer_threat_map, R.drawable.ic_menu_mapmode));
  216. mDrawerItems.add(new DrawerListItem(R.string.drawer_records, R.drawable.ic_menu_records));
  217. mDrawerItems.add(new DrawerListItem(R.string.drawer_statistics, R.drawable.ic_menu_stats));
  218. mDrawerItems.add(new DrawerListItem(R.string.drawer_services, R.drawable.ic_menu_set_as));
  219. mDrawerItems.add(new DrawerListItem(R.string.drawer_profile_manager, R.drawable.ic_menu_allfriends));
  220. mDrawerItems.add(new DrawerListItem(R.string.drawer_settings, R.drawable.ic_menu_preferences));
  221. mDrawerItems.add(new DrawerListItem(R.string.drawer_help, R.drawable.ic_menu_help));
  222. mDrawerItems.add(new DrawerListItem(R.string.drawer_app_info, R.drawable.ic_menu_info_details));
  223. DrawerListAdapter listAdapter = new DrawerListAdapter(this, mDrawerItems);
  224. mDrawerList.setAdapter(listAdapter);
  225. mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
  226. // configures the navigation drawer
  227. mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
  228. mDrawerLayout, /* DrawerLayout object */
  229. R.drawable.ic_navigation_drawer, /*
  230. * nav drawer image to replace 'Up'
  231. * caret
  232. */
  233. R.string.drawer_open, /* "open drawer" description for accessibility */
  234. R.string.drawer_close /* "close drawer" description for accessibility */
  235. ) {
  236. public void onDrawerClosed(View view) {
  237. getActionBar().setTitle(mTitle);
  238. invalidateOptionsMenu(); // creates call to
  239. // onPrepareOptionsMenu()
  240. }
  241. public void onDrawerOpened(View drawerView) {
  242. getActionBar().setTitle(mDrawerTitle);
  243. invalidateOptionsMenu(); // creates call to
  244. // onPrepareOptionsMenu()
  245. }
  246. };
  247. mDrawerLayout.setDrawerListener(mDrawerToggle);
  248. // start the hostage service
  249. startAndBind();
  250. mSharedPreferences = getSharedPreferences(getString(R.string.shared_preference_path), Hostage.MODE_PRIVATE);
  251. if(mSharedPreferences.getBoolean("isFirstRun", true)){
  252. // opens navigation drawer if first run
  253. mDrawerLayout.postDelayed(new Runnable() {
  254. @Override
  255. public void run() {
  256. mDrawerLayout.openDrawer(Gravity.LEFT);
  257. }
  258. }, 1000);
  259. onFirstRun();
  260. }
  261. if (savedInstanceState == null) {
  262. // on first time display view for first nav item
  263. displayView(0);
  264. }
  265. }
  266. /**
  267. * Displays the disclaimer on first run of the application
  268. */
  269. private void onFirstRun(){
  270. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  271. builder.setMessage(Html.fromHtml(getString(R.string.hostage_disclaimer)))
  272. .setCancelable(false)
  273. .setPositiveButton(getString(R.string.agree), new DialogInterface.OnClickListener() {
  274. public void onClick(DialogInterface dialog, int id) {
  275. // and, if the user accept, you can execute something like this:
  276. // We need an Editor object to make preference changes.
  277. // All objects are from android.context.Context
  278. SharedPreferences.Editor editor = mSharedPreferences.edit();
  279. editor.putBoolean("isFirstRun", false);
  280. // Commit the edits!
  281. editor.commit();
  282. // Enabled shared preferences for 'first' time non-portbinder activation
  283. SharedPreferences.Editor editor1= mSharedPreferences.edit();
  284. editor1.putBoolean("isFirstEmulation", true);
  285. editor1.commit();
  286. }
  287. })
  288. .setNegativeButton(getString(R.string.disagree), new DialogInterface.OnClickListener() {
  289. public void onClick(DialogInterface dialog, int id) {
  290. getHostageService().stopListeners();
  291. stopAndUnbind();
  292. finish();
  293. }
  294. });
  295. AlertDialog alert = builder.create();
  296. alert.show();
  297. }
  298. /**
  299. * Starts the hostage service and binds this activity to the service
  300. */
  301. public void startAndBind() {
  302. if (!isServiceRunning()) {
  303. startService(getServiceIntent());
  304. }
  305. bindService();
  306. }
  307. /**
  308. * Stops the hostage service and unbinds from the service
  309. */
  310. public void stopAndUnbind() {
  311. if (mHoneyService != null) {
  312. unbindService();
  313. }
  314. stopService(getServiceIntent());
  315. }
  316. /**
  317. * Unbindes the activity from the service
  318. */
  319. public void unbindService() {
  320. try {
  321. unbindService(mConnection);
  322. } catch (IllegalArgumentException ex) {
  323. // somehow already unbound.
  324. }
  325. }
  326. /**
  327. * Binds the activity to the service
  328. */
  329. public void bindService() {
  330. bindService(getServiceIntent(), mConnection, BIND_AUTO_CREATE);
  331. // mServiceBound = true;
  332. }
  333. /**
  334. * {@inheritDoc}
  335. */
  336. @Override
  337. protected void onDestroy() {
  338. super.onDestroy();
  339. // Unbind running service
  340. if (!mHoneyService.hasRunningListeners()) {
  341. stopAndUnbind();
  342. }
  343. }
  344. /**
  345. * {@inheritDoc}
  346. */
  347. @Override
  348. public boolean onOptionsItemSelected(MenuItem item) {
  349. // toggle nav drawer on selecting action bar app icon/title
  350. if (mDrawerToggle.onOptionsItemSelected(item)) {
  351. return true;
  352. }
  353. if (item.getItemId() == android.R.id.home) {
  354. if (!mDrawerToggle.isDrawerIndicatorEnabled()) {
  355. navigateBack();
  356. }
  357. }
  358. return super.onOptionsItemSelected(item);
  359. }
  360. /**
  361. * Navigates up to the parent fragment of the current fragment
  362. */
  363. public void navigateBack(){
  364. if (!(this.mDisplayedFragment instanceof UpNavigatibleFragment)) {
  365. mDrawerToggle.setDrawerIndicatorEnabled(true);
  366. return;
  367. }
  368. UpNavigatibleFragment upNav = (UpNavigatibleFragment) this.mDisplayedFragment;
  369. getFragmentManager().popBackStackImmediate(upNav.getUpFragment().getName(), 0);
  370. this.mDisplayedFragment = getFragmentManager().findFragmentById(R.id.content_frame);
  371. configureFragment();
  372. if (!(this.mDisplayedFragment instanceof UpNavigatibleFragment) || !((UpNavigatibleFragment) this.mDisplayedFragment).isUpNavigatible()) {
  373. mDrawerToggle.setDrawerIndicatorEnabled(true);
  374. } else {
  375. mDrawerToggle.setDrawerIndicatorEnabled(false);
  376. }
  377. }
  378. /**
  379. * {@inheritDoc}
  380. */
  381. @Override
  382. public void setTitle(CharSequence title) {
  383. mTitle = title;
  384. getActionBar().setTitle(mTitle);
  385. }
  386. /**
  387. * When using the ActionBarDrawerToggle, you must call it during
  388. * onPostCreate() and onConfigurationChanged()...
  389. */
  390. @Override
  391. protected void onPostCreate(Bundle savedInstanceState) {
  392. super.onPostCreate(savedInstanceState);
  393. // Sync the toggle state after onRestoreInstanceState has occurred.
  394. mDrawerToggle.syncState();
  395. }
  396. /**
  397. * {@inheritDoc}
  398. */
  399. @Override
  400. public void onConfigurationChanged(Configuration newConfig) {
  401. super.onConfigurationChanged(newConfig);
  402. // Pass any configuration change to the drawer toggls
  403. mDrawerToggle.onConfigurationChanged(newConfig);
  404. }
  405. /**
  406. * Displays the view for the given navigation index
  407. *
  408. * @param position the index of the navigation item
  409. */
  410. public void displayView(int position) {
  411. MainMenuItem menuItemPosition = MainMenuItem.create(position);
  412. // close the drawer if the to be displayed fragment is already being displayed
  413. if (this.mDisplayedFragment != null && this.mDisplayedFragment.getClass() == menuItemPosition.getKlass()) {
  414. mDrawerLayout.closeDrawer(mDrawerList);
  415. return;
  416. }
  417. // open help video list when pressing help navigation item
  418. if(menuItemPosition == MainMenuItem.HELP){
  419. Intent intent = new Intent(Intent.ACTION_VIEW);
  420. intent.setData(Uri.parse("https://www.youtube.com/playlist?list=PLJyUmtMldh3s1XtRfE4YFaQ8ME7xjf7Gx"));
  421. startActivity(intent);
  422. return;
  423. }
  424. Fragment fragment = null;
  425. try {
  426. fragment = (Fragment) menuItemPosition.getKlass().newInstance();
  427. } catch (InstantiationException e) {
  428. Log.i(menuItemPosition.getKlass().toString(), "Could not create new instance of fragment");
  429. } catch (IllegalAccessException e) {
  430. Log.i(menuItemPosition.getKlass().toString(), "Could not create new instance of fragment");
  431. }
  432. if (fragment != null) {
  433. if(position == 0 && mRootFragment == null){
  434. mRootFragment = fragment;
  435. }
  436. injectFragment(fragment);
  437. mDrawerList.setItemChecked(position, true);
  438. mDrawerList.setSelection(position);
  439. setTitle(mDrawerItems.get(position).text);
  440. }
  441. mDrawerLayout.closeDrawer(mDrawerList);
  442. }
  443. /**
  444. * Injects an given fragment into the application content view
  445. *
  446. * @param fragment the fragment to inject
  447. */
  448. public void injectFragment(Fragment fragment) {
  449. this.mCloseWarning = false;
  450. // set the action bar up navigation according to the nature of the given fragment
  451. if (fragment instanceof UpNavigatibleFragment) {
  452. UpNavigatibleFragment upFrag = (UpNavigatibleFragment) fragment;
  453. if (upFrag.getUpFragment() == null) {
  454. upFrag.setUpFragment(this.mDisplayedFragment.getClass());
  455. }
  456. if (upFrag.isUpNavigatible()) {
  457. mDrawerToggle.setDrawerIndicatorEnabled(false);
  458. }
  459. }
  460. configureFragment(fragment);
  461. // exchange the existing fragment with the given one
  462. FragmentManager fragmentManager = getFragmentManager();
  463. FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  464. fragmentTransaction.replace(R.id.content_frame, fragment, fragment.getClass().getName());
  465. fragmentTransaction.addToBackStack(fragment.getClass().getName());
  466. fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
  467. fragmentTransaction.commit();
  468. this.mDisplayedFragment = fragment;
  469. }
  470. private void configureFragment() {
  471. configureFragment(this.mDisplayedFragment);
  472. }
  473. /**
  474. * Configures the given fragment, e.g. fixing the screen orientation
  475. *
  476. * @param fragment the fragment to configure
  477. */
  478. private void configureFragment(Fragment fragment) {
  479. if (fragment == null)
  480. return;
  481. if (fragment instanceof HomeFragment || fragment instanceof AboutFragment) {
  482. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT | ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
  483. } else {
  484. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
  485. }
  486. if (fragment instanceof StatisticsFragment || fragment instanceof RecordOverviewFragment) {
  487. Intent intent = this.getIntent();
  488. intent.removeExtra(LogFilter.LOG_FILTER_INTENT_KEY);
  489. }
  490. }
  491. /**
  492. * {@inheritDoc}
  493. */
  494. @Override
  495. public void onBackPressed() {
  496. if (mDisplayedFragment instanceof HomeFragment) {
  497. if (this.mCloseWarning) {
  498. MainActivity.getInstance().getHostageService().stopListeners();
  499. MainActivity.getInstance().stopAndUnbind();
  500. this.mCloseWarning = false;
  501. finish();
  502. } else {
  503. Toast.makeText(this, "Press the back button again to close HosTaGe", Toast.LENGTH_SHORT).show();
  504. this.mCloseWarning = true;
  505. }
  506. //}
  507. } else {
  508. super.onBackPressed();
  509. this.mDisplayedFragment = getFragmentManager().findFragmentById(R.id.content_frame);
  510. configureFragment();
  511. if (!(this.mDisplayedFragment instanceof UpNavigatibleFragment) || !((UpNavigatibleFragment) this.mDisplayedFragment).isUpNavigatible()) {
  512. mDrawerToggle.setDrawerIndicatorEnabled(true);
  513. } else {
  514. mDrawerToggle.setDrawerIndicatorEnabled(false);
  515. }
  516. }
  517. }
  518. /**
  519. * {@inheritDoc}
  520. */
  521. @Override
  522. public boolean onKeyDown(int keycode, KeyEvent e) {
  523. switch (keycode) {
  524. case KeyEvent.KEYCODE_MENU:
  525. if (this.mDrawerToggle.isDrawerIndicatorEnabled()) {
  526. if (this.mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
  527. this.mDrawerLayout.closeDrawer(Gravity.LEFT);
  528. return true;
  529. }
  530. this.mDrawerLayout.openDrawer(Gravity.LEFT);
  531. }
  532. return true;
  533. }
  534. return super.onKeyDown(keycode, e);
  535. }
  536. /**
  537. * Create a new intent intented for binding the hostage service to the activity
  538. *
  539. * @return the new service intent
  540. */
  541. public Intent getServiceIntent() {
  542. return new Intent(this, Hostage.class);
  543. }
  544. /**
  545. * Retrieves the currently displayed fragment
  546. *
  547. * @return the current fragment
  548. */
  549. public Fragment getDisplayedFragment() {
  550. return this.mDisplayedFragment;
  551. }
  552. /**
  553. * Retrieves the Hostage service instance
  554. * @return hostage service
  555. */
  556. public Hostage getHostageService() {
  557. return this.mHoneyService;
  558. }
  559. /**
  560. * Checks if the hostage service is bound to the activity
  561. * @return true, if bound
  562. * false, otherwise
  563. */
  564. public boolean isServiceBound() {
  565. return this.mServiceBound;
  566. }
  567. /**
  568. * Checks whether the hostage service is running
  569. * @return true, if running
  570. * false, otherwise
  571. */
  572. public boolean isServiceRunning() {
  573. ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
  574. for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
  575. if (service.service.getClassName().equals(Hostage.class.getName())) {
  576. return true;
  577. }
  578. }
  579. return false;
  580. }
  581. /**
  582. * Start the monitoring of the given protocols in the hostage service
  583. *
  584. * @param protocols the protocols to start
  585. */
  586. public void startMonitorServices(List<String> protocols){
  587. for(String protocol: protocols){
  588. // if the given protocol is ghost start a listener for every defined port for ghost
  589. if(protocol.equals("GHOST")){
  590. if(mProfileManager.getCurrentActivatedProfile() != null){
  591. Profile profile = mProfileManager.getCurrentActivatedProfile();
  592. if(profile.mGhostActive){
  593. for(int port: profile.getGhostPorts()){
  594. if(!getHostageService().isRunning(protocol, port)) getHostageService().startListener(protocol, port);
  595. }
  596. }
  597. }
  598. } else {
  599. if(!getHostageService().isRunning(protocol)) getHostageService().startListener(protocol);
  600. }
  601. }
  602. }
  603. /**
  604. * Holds the index of the navigation items in an enum and also a reference to an Fragment class for each item
  605. */
  606. public enum MainMenuItem {
  607. HOME(0, HomeFragment.class),
  608. THREAT_MAP(1, ThreatMapFragment.class),
  609. RECORDS(2, RecordOverviewFragment.class),
  610. STATISTICS(3, StatisticsFragment.class),
  611. SERVICES(4, ServicesFragment.class),
  612. PROFILE_MANAGER(5, ProfileManagerFragment.class),
  613. SETTINGS(6, SettingsFragment.class),
  614. HELP(7, Class.class),
  615. APPLICATION_INFO(8, AboutFragment.class);
  616. private int value;
  617. private Class<?> klass;
  618. private MainMenuItem(int value, Class<?> klass) {
  619. this.value = value;
  620. this.klass = klass;
  621. }
  622. static public MainMenuItem create(int value) {
  623. if (value < 0 || value >= MainMenuItem.values().length)
  624. return MainMenuItem.HOME;
  625. return MainMenuItem.values()[value];
  626. }
  627. public static boolean hasClass(Class<?> klass){
  628. for(MainMenuItem m: MainMenuItem.values()){
  629. if(m.getKlass().equals(klass)) return true;
  630. }
  631. return false;
  632. }
  633. public int getValue() {
  634. return value;
  635. }
  636. public Class<?> getKlass() {
  637. return this.klass;
  638. }
  639. }
  640. /**
  641. * The listener for the navigation drawer items.
  642. */
  643. private class DrawerItemClickListener implements ListView.OnItemClickListener {
  644. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  645. displayView(position);
  646. }
  647. }
  648. }