MainActivity.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. package de.tudarmstadt.informatik.hostage.ui;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import android.app.Activity;
  5. import android.app.ActivityManager;
  6. import android.app.ActivityManager.RunningServiceInfo;
  7. import android.content.BroadcastReceiver;
  8. import android.content.ComponentName;
  9. import android.content.Context;
  10. import android.content.Intent;
  11. import android.content.IntentFilter;
  12. import android.content.ServiceConnection;
  13. import android.os.Bundle;
  14. import android.os.IBinder;
  15. import android.support.v4.content.LocalBroadcastManager;
  16. import android.view.GestureDetector;
  17. import android.view.GestureDetector.SimpleOnGestureListener;
  18. import android.view.Menu;
  19. import android.view.MotionEvent;
  20. import android.view.View;
  21. import android.view.View.OnTouchListener;
  22. import android.view.animation.Animation;
  23. import android.view.animation.AnimationUtils;
  24. import android.widget.AdapterView;
  25. import android.widget.AdapterView.OnItemClickListener;
  26. import android.widget.CheckBox;
  27. import android.widget.ImageView;
  28. import android.widget.ListView;
  29. import android.widget.ToggleButton;
  30. import android.widget.ViewAnimator;
  31. import de.tudarmstadt.informatik.hostage.HoneyListener;
  32. import de.tudarmstadt.informatik.hostage.HoneyService;
  33. import de.tudarmstadt.informatik.hostage.HoneyService.LocalBinder;
  34. import de.tudarmstadt.informatik.hostage.R;
  35. public class MainActivity extends Activity {
  36. public static final String BROADCAST = "de.tudarmstadt.informatik.hostage.BROADCAST";
  37. public static final int LIGHT_GREY = 0x01;
  38. public static final int LIGHT_GREEN = 0x02;
  39. public static final int LIGHT_RED = 0x03;
  40. public static final int LIGHT_YELLOW = 0x04;
  41. private HoneyService mService;
  42. private ViewAnimator viewAnimator;
  43. private GestureDetector gestureDetector;
  44. private Animation animFlipInLR;
  45. private Animation animFlipOutLR;
  46. private Animation animFlipInRL;
  47. private Animation animFlipOutRL;
  48. private ListView listView;
  49. private ListViewAdapter adapter;
  50. private boolean running;
  51. @Override
  52. protected void onCreate(Bundle savedInstanceState) {
  53. super.onCreate(savedInstanceState);
  54. setContentView(R.layout.activity_main);
  55. initViewAnimator();
  56. initListView();
  57. }
  58. @Override
  59. public boolean onCreateOptionsMenu(Menu menu) {
  60. getMenuInflater().inflate(R.menu.main, menu);
  61. return true;
  62. }
  63. @Override
  64. protected void onStart() {
  65. super.onStart();
  66. registerReceiver();
  67. if (isServiceRunning()) {
  68. bindService(getServiceIntent(), mConnection, BIND_ABOVE_CLIENT);
  69. }
  70. }
  71. @Override
  72. protected void onStop() {
  73. super.onStop();
  74. if (isServiceRunning()) {
  75. unbindService(mConnection);
  76. }
  77. unregisterReceiver();
  78. }
  79. @Override
  80. public boolean onTouchEvent(MotionEvent event) {
  81. return gestureDetector.onTouchEvent(event);
  82. }
  83. public void buttonOnOffClick(View view) {
  84. if (((ToggleButton) view).isChecked()) {
  85. startAndBind();
  86. } else {
  87. stopAndUnbind();
  88. running = false;
  89. }
  90. }
  91. private void startAndBind() {
  92. startService(getServiceIntent());
  93. bindService(getServiceIntent(), mConnection, BIND_ABOVE_CLIENT);
  94. }
  95. private void stopAndUnbind() {
  96. mService.stopListeners();
  97. unbindService(mConnection);
  98. stopService(getServiceIntent());
  99. }
  100. private boolean isParanoid() {
  101. return ((CheckBox) findViewById(R.id.checkBoxParanoid)).isChecked();
  102. }
  103. private void initViewAnimator() {
  104. viewAnimator = (ViewAnimator) findViewById(R.id.viewAnimator);
  105. gestureDetector = new GestureDetector(this, simpleOnGestureListener);
  106. animFlipInLR = AnimationUtils.loadAnimation(this,
  107. R.anim.in_left_to_right);
  108. animFlipOutLR = AnimationUtils.loadAnimation(this,
  109. R.anim.out_left_to_right);
  110. animFlipInRL = AnimationUtils.loadAnimation(this,
  111. R.anim.in_right_to_left);
  112. animFlipOutRL = AnimationUtils.loadAnimation(this,
  113. R.anim.out_right_to_left);
  114. }
  115. private void initListView() {
  116. ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
  117. for (String protocol : getResources().getStringArray(R.array.protocols)) {
  118. HashMap<String, String> d = new HashMap<String, String>();
  119. d.put("light", String.valueOf(R.drawable.light_grey));
  120. d.put("protocol", protocol);
  121. d.put("connections", "-");
  122. data.add(d);
  123. }
  124. listView = (ListView) findViewById(R.id.listViewProtocols);
  125. adapter = new ListViewAdapter(getLayoutInflater(), data);
  126. listView.setAdapter(adapter);
  127. listView.setOnTouchListener(new OnTouchListener() {
  128. @Override
  129. public boolean onTouch(View v, MotionEvent event) {
  130. return gestureDetector.onTouchEvent(event);
  131. }
  132. });
  133. listView.setOnItemClickListener(new OnItemClickListener() {
  134. @Override
  135. public void onItemClick(AdapterView<?> parent, View view,
  136. int position, long id) {
  137. String protocolName = ((HashMap<String, String>) adapter
  138. .getItem(position)).get("protocol");
  139. if (isServiceRunning()) {
  140. mService.toggleListener(protocolName);
  141. }
  142. }
  143. });
  144. }
  145. private void swipeRightToLeft() {
  146. if (viewAnimator.getDisplayedChild() == 0) {
  147. viewAnimator.setInAnimation(animFlipInRL);
  148. viewAnimator.setOutAnimation(animFlipOutRL);
  149. viewAnimator.setDisplayedChild(1);
  150. }
  151. }
  152. private void swipeLeftToRight() {
  153. if (viewAnimator.getDisplayedChild() == 1) {
  154. viewAnimator.setInAnimation(animFlipInLR);
  155. viewAnimator.setOutAnimation(animFlipOutLR);
  156. viewAnimator.setDisplayedChild(0);
  157. }
  158. }
  159. SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener() {
  160. @Override
  161. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
  162. float velocityY) {
  163. float sensitvity = 50;
  164. if ((e1.getX() - e2.getX()) > sensitvity) {
  165. swipeRightToLeft();
  166. } else if ((e2.getX() - e1.getX()) > sensitvity) {
  167. swipeLeftToRight();
  168. }
  169. return true;
  170. }
  171. };
  172. private boolean isServiceRunning() {
  173. ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
  174. for (RunningServiceInfo service : manager
  175. .getRunningServices(Integer.MAX_VALUE)) {
  176. if (service.service.getClassName().equals(
  177. HoneyService.class.getName())) {
  178. return true;
  179. }
  180. }
  181. return false;
  182. }
  183. private Intent getServiceIntent() {
  184. return new Intent(this, HoneyService.class);
  185. }
  186. private ServiceConnection mConnection = new ServiceConnection() {
  187. @Override
  188. public void onServiceConnected(ComponentName name, IBinder service) {
  189. mService = ((LocalBinder) service).getService();
  190. if (!running) {
  191. if (isParanoid()) {
  192. mService.startListeners();
  193. } else {
  194. mService.startListener("SMB");
  195. }
  196. }
  197. running = true;
  198. updateUI();
  199. }
  200. @Override
  201. public void onServiceDisconnected(ComponentName name) {
  202. mService = null;
  203. }
  204. };
  205. private void registerReceiver() {
  206. LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver,
  207. new IntentFilter(BROADCAST));
  208. }
  209. private void unregisterReceiver() {
  210. LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
  211. }
  212. private BroadcastReceiver mReceiver = new BroadcastReceiver() {
  213. @Override
  214. public void onReceive(Context context, Intent intent) {
  215. updateUI();
  216. }
  217. };
  218. private void updateUI() {
  219. boolean activeListeners = false;
  220. boolean activeHandlers = false;
  221. for (HoneyListener listener : mService.getListeners()) {
  222. if (listener.isRunning()) {
  223. activeListeners = true;
  224. if (listener.getHandlerCount() == 0) {
  225. updateProtocolLight(LIGHT_GREEN, listener.getProtocolName());
  226. } else {
  227. activeHandlers = true;
  228. updateProtocolLight(LIGHT_RED, listener.getProtocolName());
  229. }
  230. updateProtocolConnections(listener.getHandlerCount(),
  231. listener.getProtocolName());
  232. } else {
  233. updateProtocolLight(LIGHT_GREY, listener.getProtocolName());
  234. }
  235. }
  236. if (activeListeners) {
  237. if (activeHandlers) {
  238. updateStatusLight(LIGHT_RED);
  239. } else {
  240. updateStatusLight(LIGHT_GREEN);
  241. }
  242. ((ToggleButton) findViewById(R.id.toggleButtonOnOff))
  243. .setChecked(true);
  244. findViewById(R.id.checkBoxParanoid).setEnabled(false);
  245. } else {
  246. updateStatusLight(LIGHT_GREY);
  247. ((ToggleButton) findViewById(R.id.toggleButtonOnOff))
  248. .setChecked(false);
  249. findViewById(R.id.checkBoxParanoid).setEnabled(true);
  250. }
  251. }
  252. private void updateStatusLight(int light) {
  253. switch (light) {
  254. case LIGHT_GREY:
  255. ((ImageView) findViewById(R.id.imageViewLight))
  256. .setImageResource(R.drawable.light_grey_large);
  257. break;
  258. case LIGHT_GREEN:
  259. ((ImageView) findViewById(R.id.imageViewLight))
  260. .setImageResource(R.drawable.light_green_large);
  261. break;
  262. case LIGHT_RED:
  263. ((ImageView) findViewById(R.id.imageViewLight))
  264. .setImageResource(R.drawable.light_red_large);
  265. break;
  266. case LIGHT_YELLOW:
  267. ((ImageView) findViewById(R.id.imageViewLight))
  268. .setImageResource(R.drawable.light_yellow_large);
  269. break;
  270. }
  271. }
  272. private void updateProtocolLight(int light, String protocolName) {
  273. for (int i = 0; i < adapter.getCount(); ++i) {
  274. HashMap<String, String> d = ((HashMap<String, String>) adapter
  275. .getItem(i));
  276. if (d.get("protocol").equals(protocolName)) {
  277. switch (light) {
  278. case LIGHT_GREY:
  279. d.put("light", String.valueOf(R.drawable.light_grey));
  280. d.put("connections", "-");
  281. break;
  282. case LIGHT_GREEN:
  283. d.put("light", String.valueOf(R.drawable.light_green));
  284. d.put("connections", "0");
  285. break;
  286. case LIGHT_RED:
  287. d.put("light", String.valueOf(R.drawable.light_red));
  288. break;
  289. case LIGHT_YELLOW:
  290. d.put("light", String.valueOf(R.drawable.light_yellow));
  291. d.put("connections", "0");
  292. break;
  293. }
  294. }
  295. }
  296. adapter.notifyDataSetChanged();
  297. }
  298. private void updateProtocolConnections(int connections, String protocolName) {
  299. for (int i = 0; i < adapter.getCount(); ++i) {
  300. HashMap<String, String> d = ((HashMap<String, String>) adapter
  301. .getItem(i));
  302. if (d.get("protocol").equals(protocolName)) {
  303. d.put("connections", String.valueOf(connections));
  304. }
  305. }
  306. adapter.notifyDataSetChanged();
  307. }
  308. }