MainActivity.java 23 KB

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