MainActivity.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. package de.tudarmstadt.informatik.hostage.ui;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import android.app.ActivityManager;
  5. import android.app.ActivityManager.RunningServiceInfo;
  6. import android.content.BroadcastReceiver;
  7. import android.content.ComponentName;
  8. import android.content.Context;
  9. import android.content.Intent;
  10. import android.content.IntentFilter;
  11. import android.content.ServiceConnection;
  12. import android.content.SharedPreferences;
  13. import android.os.Bundle;
  14. import android.os.IBinder;
  15. import android.support.v4.app.FragmentActivity;
  16. import android.support.v4.content.LocalBroadcastManager;
  17. import android.view.GestureDetector;
  18. import android.view.GestureDetector.SimpleOnGestureListener;
  19. import android.view.Menu;
  20. import android.view.MenuItem;
  21. import android.view.MotionEvent;
  22. import android.view.View;
  23. import android.view.View.OnTouchListener;
  24. import android.view.animation.Animation;
  25. import android.view.animation.AnimationUtils;
  26. import android.widget.AdapterView;
  27. import android.widget.AdapterView.OnItemClickListener;
  28. import android.widget.CheckBox;
  29. import android.widget.ImageView;
  30. import android.widget.ListView;
  31. import android.widget.TextView;
  32. import android.widget.ToggleButton;
  33. import android.widget.ViewAnimator;
  34. import de.tudarmstadt.informatik.hostage.Hostage;
  35. import de.tudarmstadt.informatik.hostage.Hostage.LocalBinder;
  36. import de.tudarmstadt.informatik.hostage.R;
  37. import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
  38. /**
  39. * MainActivity is the central activity for the GUI of the application.
  40. * MainActivity is launched when the application is first started. It shows the
  41. * user: <br>
  42. * - information about the network<br>
  43. * - light indicators for recorded attacks on each protocol<br>
  44. * - amount of attacks on each protocols<br>
  45. * The user can start and stop services.
  46. *
  47. * @author Mihai Plasoianu
  48. * @author Lars Pandikow
  49. * @author Wulf Pfeiffer
  50. */
  51. public class MainActivity extends FragmentActivity {
  52. /**
  53. * Integer representing a grey light.
  54. */
  55. public static final int LIGHT_GREY = 0x01;
  56. /**
  57. * Integer representing a green light.
  58. */
  59. public static final int LIGHT_GREEN = 0x02;
  60. /**
  61. * Integer representing a red light.
  62. */
  63. public static final int LIGHT_RED = 0x03;
  64. /**
  65. * Integer representing a yellow light.
  66. */
  67. public static final int LIGHT_YELLOW = 0x04;
  68. private SharedPreferences connectionInfo;
  69. private Hostage mService;
  70. private boolean serviceBound;
  71. // variables for the swipe animation
  72. private ViewAnimator viewAnimator;
  73. private GestureDetector gestureDetector;
  74. private Animation animFlipInLR;
  75. private Animation animFlipOutLR;
  76. private Animation animFlipInRL;
  77. private Animation animFlipOutRL;
  78. private ListView listView;
  79. private ListViewAdapter adapter;
  80. /**
  81. * Connection to bind the background service
  82. *
  83. * @see Hostage
  84. */
  85. private ServiceConnection mConnection = new ServiceConnection() {
  86. /**
  87. * After the service is bound, check which has been clicked and start
  88. * it.
  89. *
  90. * @see android.content.ServiceConnection#onServiceConnected(android.content.ComponentName)
  91. */
  92. @Override
  93. public void onServiceConnected(ComponentName name, IBinder service) {
  94. mService = ((LocalBinder) service).getService();
  95. serviceBound = true;
  96. updateUI();
  97. }
  98. /**
  99. * After the service is unbound, delete reference.
  100. *
  101. * @see android.content.ServiceConnection#onServiceDisconnected(android.content.ComponentName)
  102. */
  103. @Override
  104. public void onServiceDisconnected(ComponentName name) {
  105. mService = null;
  106. serviceBound = false;
  107. }
  108. };
  109. /**
  110. * Receiver for custom broadcast.
  111. *
  112. * @see #BROADCAST
  113. */
  114. private BroadcastReceiver mReceiver = new BroadcastReceiver() {
  115. @Override
  116. public void onReceive(Context context, Intent intent) {
  117. // Update user interface.
  118. updateUI();
  119. updateConnectionInfText();
  120. }
  121. };
  122. SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener() {
  123. @Override
  124. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
  125. float sensitvity = 50;
  126. if ((e1.getX() - e2.getX()) > sensitvity) {
  127. swipeRightToLeft();
  128. } else if ((e2.getX() - e1.getX()) > sensitvity) {
  129. swipeLeftToRight();
  130. }
  131. return true;
  132. }
  133. };
  134. /**
  135. * Called when User presses on/off button.
  136. *
  137. * @param view
  138. */
  139. public void buttonOnOffClick(View view) {
  140. if (((ToggleButton) view).isChecked()) {
  141. if (isParanoid()) {
  142. String[] protocols = getResources().getStringArray(R.array.protocols);
  143. for (String protocol : protocols) {
  144. mService.startListener(protocol);
  145. }
  146. } else {
  147. if (mService.isRunning("SMB")) {
  148. mService.stopListener("SMB");
  149. } else {
  150. mService.startListener("SMB");
  151. }
  152. }
  153. } else {
  154. mService.stopListeners();
  155. stopAndUnbind();
  156. }
  157. }
  158. @Override
  159. public boolean onCreateOptionsMenu(Menu menu) {
  160. getMenuInflater().inflate(R.menu.main, menu);
  161. return true;
  162. }
  163. @Override
  164. public boolean onOptionsItemSelected(MenuItem item) {
  165. // Handle item selection
  166. switch (item.getItemId()) {
  167. case R.id.action_settings:
  168. startActivity(new Intent(this, SettingsActivity.class));
  169. break;
  170. case R.id.action_about:
  171. startActivity(new Intent(this, AboutActivity.class));
  172. break;
  173. default:
  174. }
  175. return super.onOptionsItemSelected(item);
  176. }
  177. @Override
  178. public boolean onTouchEvent(MotionEvent event) {
  179. return gestureDetector.onTouchEvent(event);
  180. }
  181. /**
  182. * Starts the ViewLog activity, when the Button is pressed.
  183. *
  184. * @see ViewLog
  185. * @param view
  186. * View elements which triggers the method call.
  187. */
  188. public void showLog(View view) {
  189. // startActivity(new Intent(this, ViewLog.class));
  190. }
  191. public void startPlayGround(View view) {
  192. startActivity(new Intent(this, PlayGroundActivity.class));
  193. }
  194. /**
  195. * Binds service to Activity
  196. *
  197. * @see Hostage
  198. */
  199. private void bindService() {
  200. bindService(getServiceIntent(), mConnection, BIND_AUTO_CREATE);
  201. }
  202. /**
  203. * Returns an intent to start HoneyService.
  204. *
  205. * @return An Intent to start HoneyService
  206. */
  207. private Intent getServiceIntent() {
  208. return new Intent(this, Hostage.class);
  209. }
  210. /**
  211. * Initializes the ListView. Creating its contents dynamic from protocol
  212. * res/values/protocols.xml
  213. */
  214. private void initListView() {
  215. ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
  216. for (String protocol : getResources().getStringArray(R.array.protocols)) {
  217. HashMap<String, String> d = new HashMap<String, String>();
  218. d.put("light", String.valueOf(R.drawable.light_grey));
  219. d.put("protocol", protocol);
  220. d.put("connections", "-");
  221. data.add(d);
  222. }
  223. listView = (ListView) findViewById(R.id.listViewProtocols);
  224. adapter = new ListViewAdapter(getLayoutInflater(), data);
  225. listView.setAdapter(adapter);
  226. listView.setOnTouchListener(new OnTouchListener() {
  227. @Override
  228. public boolean onTouch(View v, MotionEvent event) {
  229. return gestureDetector.onTouchEvent(event);
  230. }
  231. });
  232. listView.setOnItemClickListener(new OnItemClickListener() {
  233. @Override
  234. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  235. String protocolName = (String) ((HashMap<?, ?>) adapter.getItem(position)).get("protocol");
  236. if (mService.isRunning(protocolName)) {
  237. mService.stopListener(protocolName);
  238. } else {
  239. mService.startListener(protocolName);
  240. }
  241. }
  242. });
  243. }
  244. /**
  245. * Initializes variables for screen animation
  246. */
  247. private void initViewAnimator() {
  248. viewAnimator = (ViewAnimator) findViewById(R.id.viewAnimator);
  249. gestureDetector = new GestureDetector(this, simpleOnGestureListener);
  250. animFlipInLR = AnimationUtils.loadAnimation(this, R.anim.in_left_to_right);
  251. animFlipOutLR = AnimationUtils.loadAnimation(this, R.anim.out_left_to_right);
  252. animFlipInRL = AnimationUtils.loadAnimation(this, R.anim.in_right_to_left);
  253. animFlipOutRL = AnimationUtils.loadAnimation(this, R.anim.out_right_to_left);
  254. }
  255. /**
  256. * Checks if user selected paranoid mode.
  257. *
  258. * @return True when paranoid mode is selected, else returns false.
  259. */
  260. private boolean isParanoid() {
  261. return ((CheckBox) findViewById(R.id.checkBoxParanoid)).isChecked();
  262. }
  263. /**
  264. * Checks if a {@link Hostage} instance is running.
  265. *
  266. * @return True if {@link Hostage} is running, else false.
  267. */
  268. private boolean isServiceBound() {
  269. return serviceBound;
  270. }
  271. /**
  272. * Checks if a {@link Hostage} instance is running.
  273. *
  274. * @return True if {@link Hostage} is running, else false.
  275. */
  276. private boolean isServiceRunning() {
  277. ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
  278. for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
  279. if (service.service.getClassName().equals(Hostage.class.getName())) {
  280. return true;
  281. }
  282. }
  283. return false;
  284. }
  285. /**
  286. * Register broadcast receiver for custom broadcast.
  287. *
  288. * @see #BROADCAST
  289. */
  290. private void registerReceiver() {
  291. LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, new IntentFilter(getString(R.string.broadcast)));
  292. }
  293. /**
  294. * If mobile phone is connected to a wireless network starts the background
  295. * service ands binds itself to it. Else notifies the user that service
  296. * could not be started.
  297. */
  298. private void startAndBind() {
  299. startService(getServiceIntent());
  300. bindService();
  301. }
  302. /**
  303. * Stops service and unbinds it.
  304. *
  305. * @see Hostage
  306. */
  307. private void stopAndUnbind() {
  308. unbindService();
  309. stopService(getServiceIntent());
  310. }
  311. /**
  312. * Called when a swipe to the Right is registered.
  313. */
  314. private void swipeLeftToRight() {
  315. if (viewAnimator.getDisplayedChild() == 1) {
  316. viewAnimator.setInAnimation(animFlipInLR);
  317. viewAnimator.setOutAnimation(animFlipOutLR);
  318. viewAnimator.setDisplayedChild(0);
  319. }
  320. }
  321. /**
  322. * Called when a swipe to the Left is registered.
  323. */
  324. private void swipeRightToLeft() {
  325. if (viewAnimator.getDisplayedChild() == 0) {
  326. viewAnimator.setInAnimation(animFlipInRL);
  327. viewAnimator.setOutAnimation(animFlipOutRL);
  328. viewAnimator.setDisplayedChild(1);
  329. }
  330. }
  331. /**
  332. * Unbinds service.
  333. *
  334. * @see Hostage
  335. */
  336. private void unbindService() {
  337. unbindService(mConnection);
  338. }
  339. /**
  340. * Unregisters broadcast receiver for custom broadcast.
  341. *
  342. * @see #BROADCAST
  343. */
  344. private void unregisterReceiver() {
  345. LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
  346. }
  347. /**
  348. * Gets Information about connection state and updates the GUI.
  349. */
  350. private void updateConnectionInfText() {
  351. TextView ssidView = (TextView) findViewById(R.id.textViewSSIDValue);
  352. TextView bssidView = (TextView) findViewById(R.id.textViewBSSIDValue);
  353. TextView internalIPView = (TextView) findViewById(R.id.textViewInternalIPValue);
  354. TextView externalIPView = (TextView) findViewById(R.id.textViewExternalIPValue);
  355. // externalIPView.setText("Loading...");
  356. // Get connection information
  357. String ssid = connectionInfo.getString(getString(R.string.connection_info_ssid), null);
  358. String bssid = connectionInfo.getString(getString(R.string.connection_info_bssid), null);
  359. String internalIP = connectionInfo.getString(getString(R.string.connection_info_internal_ip), null);
  360. String externalIP = connectionInfo.getString(getString(R.string.connection_info_external_ip), null);
  361. // Set text fields
  362. if (ssid != null)
  363. ssidView.setText(ssid);
  364. else
  365. ssidView.setText("-");
  366. if (bssid != null)
  367. bssidView.setText(bssid);
  368. else
  369. bssidView.setText("-");
  370. if (internalIP != null)
  371. internalIPView.setText(internalIP);
  372. else
  373. internalIPView.setText("-");
  374. if (externalIP != null)
  375. externalIPView.setText(externalIP);
  376. else
  377. externalIPView.setText("-");
  378. }
  379. /**
  380. * Sets the connections count for a given protocol.
  381. *
  382. * @param connections
  383. * New value for recorded connections.
  384. * @param protocolName
  385. * Name of the protocol which should be updated.
  386. */
  387. private void updateProtocolConnections(int connections, String protocolName) {
  388. for (int i = 0; i < adapter.getCount(); ++i) {
  389. HashMap<String, String> d = (HashMap<String, String>) adapter.getItem(i);
  390. if (d.get("protocol").equals(protocolName)) {
  391. d.put("connections", String.valueOf(connections));
  392. }
  393. }
  394. adapter.notifyDataSetChanged();
  395. }
  396. /**
  397. * Sets the light indicator for a given protocol.
  398. *
  399. * @param light
  400. * Integer code to set the light color.
  401. * @param protocolName
  402. * Name of the protocol which should be updated.
  403. */
  404. private void updateProtocolLight(int light, String protocolName) {
  405. for (int i = 0; i < adapter.getCount(); ++i) {
  406. HashMap<String, String> d = (HashMap<String, String>) adapter.getItem(i);
  407. if (d.get("protocol").equals(protocolName)) {
  408. switch (light) {
  409. case LIGHT_GREY:
  410. d.put("light", String.valueOf(R.drawable.light_grey));
  411. d.put("connections", "-");
  412. break;
  413. case LIGHT_GREEN:
  414. d.put("light", String.valueOf(R.drawable.light_green));
  415. break;
  416. case LIGHT_RED:
  417. d.put("light", String.valueOf(R.drawable.light_red));
  418. break;
  419. case LIGHT_YELLOW:
  420. d.put("light", String.valueOf(R.drawable.light_yellow));
  421. break;
  422. }
  423. }
  424. }
  425. adapter.notifyDataSetChanged();
  426. }
  427. /**
  428. * Sets the big light indicator.
  429. *
  430. * @param light
  431. * Integer code to set the light color.
  432. * @see #LIGHT_GREY
  433. * @see #LIGHT_GREEN
  434. * @see #LIGHT_RED
  435. * @see #LIGHT_YELLOW
  436. */
  437. private void updateStatusLight(int light) {
  438. switch (light) {
  439. case LIGHT_GREY:
  440. ((ImageView) findViewById(R.id.imageViewLight)).setImageResource(R.drawable.light_grey_large);
  441. break;
  442. case LIGHT_GREEN:
  443. ((ImageView) findViewById(R.id.imageViewLight)).setImageResource(R.drawable.light_green_large);
  444. break;
  445. case LIGHT_RED:
  446. ((ImageView) findViewById(R.id.imageViewLight)).setImageResource(R.drawable.light_red_large);
  447. break;
  448. case LIGHT_YELLOW:
  449. ((ImageView) findViewById(R.id.imageViewLight)).setImageResource(R.drawable.light_yellow_large);
  450. break;
  451. }
  452. }
  453. /* ############# Help functions for animation ################## */
  454. /**
  455. * Updates Information shown by the GUI.
  456. */
  457. private void updateUI() {
  458. boolean activeListeners = false;
  459. boolean activeHandlers = false;
  460. boolean yellowLight = false;
  461. // Check for all protocols if listeners are active and attacks have been
  462. // recorded
  463. // Update protocol lights and connection information.
  464. for (String protocol : getResources().getStringArray(R.array.protocols)) {
  465. if (isServiceBound()) {
  466. // Check if protocol is active
  467. if (mService.isRunning(protocol)) {
  468. activeListeners = true;
  469. int handlerCount = mService.getNumberOfActiveConnections(protocol);
  470. // Check if attacks have been recorded in this session.
  471. if (handlerCount > 0) {
  472. activeHandlers = true;
  473. updateProtocolLight(LIGHT_RED, protocol);
  474. updateProtocolConnections(handlerCount, protocol);
  475. } else {
  476. // Check if the bssid of the wireless network has
  477. // already
  478. // been recorded as infected.
  479. HostageDBOpenHelper dbh = new HostageDBOpenHelper(this);
  480. if (dbh.bssidSeen(protocol, connectionInfo.getString(getString(R.string.connection_info_bssid), null))) {
  481. updateProtocolLight(LIGHT_YELLOW, protocol);
  482. yellowLight = true;
  483. } else {
  484. updateProtocolLight(LIGHT_GREEN, protocol);
  485. }
  486. updateProtocolConnections(0, protocol);
  487. }
  488. } else {
  489. updateProtocolLight(LIGHT_GREY, protocol);
  490. }
  491. } else {
  492. updateProtocolLight(LIGHT_GREY, protocol);
  493. }
  494. }
  495. // Update the big attack indicator.
  496. if (activeListeners) {
  497. if (activeHandlers) {
  498. updateStatusLight(LIGHT_RED);
  499. } else {
  500. if (yellowLight) {
  501. updateStatusLight(LIGHT_YELLOW);
  502. } else {
  503. updateStatusLight(LIGHT_GREEN);
  504. }
  505. }
  506. ((ToggleButton) findViewById(R.id.toggleButtonOnOff)).setChecked(true);
  507. findViewById(R.id.checkBoxParanoid).setEnabled(false);
  508. } else {
  509. updateStatusLight(LIGHT_GREY);
  510. ((ToggleButton) findViewById(R.id.toggleButtonOnOff)).setChecked(false);
  511. findViewById(R.id.checkBoxParanoid).setEnabled(true);
  512. }
  513. }
  514. @Override
  515. protected void onCreate(Bundle savedInstanceState) {
  516. super.onCreate(savedInstanceState);
  517. setContentView(R.layout.activity_main);
  518. connectionInfo = getSharedPreferences(getString(R.string.connection_info), Context.MODE_PRIVATE);
  519. // Create dynamic view elements
  520. initViewAnimator();
  521. initListView();
  522. // Initialize Class variables
  523. startAndBind();
  524. }
  525. @Override
  526. protected void onDestroy() {
  527. // Unbind running service
  528. if (!mService.hasRunningListeners()) {
  529. stopAndUnbind();
  530. }
  531. super.onDestroy();
  532. }
  533. @Override
  534. protected void onStart() {
  535. super.onStart();
  536. // Register Broadcast Receiver
  537. registerReceiver();
  538. // Bind service if running, else check for connection change and delete
  539. // sessionData
  540. if (isServiceRunning()) {
  541. bindService(getServiceIntent(), mConnection, BIND_AUTO_CREATE);
  542. }
  543. // Update UI
  544. updateConnectionInfText();
  545. }
  546. @Override
  547. protected void onStop() {
  548. // Unregister Broadcast Receiver
  549. unregisterReceiver();
  550. super.onStop();
  551. }
  552. }