MainActivity.java 22 KB

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