MainActivity.java 21 KB

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