MainActivity.java 21 KB

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