MainActivity.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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_AUTO_CREATE);
  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_AUTO_CREATE);
  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 = (String) ((HashMap<?, ?>) adapter
  138. .getItem(position)).get("protocol");
  139. if (!isServiceRunning()) {
  140. startAndBind();
  141. }
  142. mService.toggleListener(protocolName);
  143. }
  144. });
  145. }
  146. private void swipeRightToLeft() {
  147. if (viewAnimator.getDisplayedChild() == 0) {
  148. viewAnimator.setInAnimation(animFlipInRL);
  149. viewAnimator.setOutAnimation(animFlipOutRL);
  150. viewAnimator.setDisplayedChild(1);
  151. }
  152. }
  153. private void swipeLeftToRight() {
  154. if (viewAnimator.getDisplayedChild() == 1) {
  155. viewAnimator.setInAnimation(animFlipInLR);
  156. viewAnimator.setOutAnimation(animFlipOutLR);
  157. viewAnimator.setDisplayedChild(0);
  158. }
  159. }
  160. SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener() {
  161. @Override
  162. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
  163. float velocityY) {
  164. float sensitvity = 50;
  165. if ((e1.getX() - e2.getX()) > sensitvity) {
  166. swipeRightToLeft();
  167. } else if ((e2.getX() - e1.getX()) > sensitvity) {
  168. swipeLeftToRight();
  169. }
  170. return true;
  171. }
  172. };
  173. private boolean isServiceRunning() {
  174. ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
  175. for (RunningServiceInfo service : manager
  176. .getRunningServices(Integer.MAX_VALUE)) {
  177. if (service.service.getClassName().equals(
  178. HoneyService.class.getName())) {
  179. return true;
  180. }
  181. }
  182. return false;
  183. }
  184. private Intent getServiceIntent() {
  185. return new Intent(this, HoneyService.class);
  186. }
  187. private ServiceConnection mConnection = new ServiceConnection() {
  188. @Override
  189. public void onServiceConnected(ComponentName name, IBinder service) {
  190. mService = ((LocalBinder) service).getService();
  191. if (!running) {
  192. if (isParanoid()) {
  193. mService.startListeners();
  194. } else {
  195. mService.startListener("SMB");
  196. }
  197. }
  198. running = true;
  199. updateUI();
  200. }
  201. @Override
  202. public void onServiceDisconnected(ComponentName name) {
  203. mService = null;
  204. }
  205. };
  206. private void registerReceiver() {
  207. LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver,
  208. new IntentFilter(BROADCAST));
  209. }
  210. private void unregisterReceiver() {
  211. LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
  212. }
  213. private BroadcastReceiver mReceiver = new BroadcastReceiver() {
  214. @Override
  215. public void onReceive(Context context, Intent intent) {
  216. updateUI();
  217. }
  218. };
  219. private void updateUI() {
  220. boolean activeListeners = false;
  221. boolean activeHandlers = false;
  222. for (HoneyListener listener : mService.getListeners()) {
  223. if (listener.isRunning()) {
  224. activeListeners = true;
  225. if (listener.getHandlerCount() == 0) {
  226. updateProtocolLight(LIGHT_GREEN, listener.getProtocolName());
  227. } else {
  228. activeHandlers = true;
  229. updateProtocolLight(LIGHT_RED, listener.getProtocolName());
  230. }
  231. updateProtocolConnections(listener.getHandlerCount(),
  232. listener.getProtocolName());
  233. } else {
  234. updateProtocolLight(LIGHT_GREY, listener.getProtocolName());
  235. }
  236. }
  237. if (activeListeners) {
  238. if (activeHandlers) {
  239. updateStatusLight(LIGHT_RED);
  240. } else {
  241. updateStatusLight(LIGHT_GREEN);
  242. }
  243. ((ToggleButton) findViewById(R.id.toggleButtonOnOff))
  244. .setChecked(true);
  245. findViewById(R.id.checkBoxParanoid).setEnabled(false);
  246. } else {
  247. updateStatusLight(LIGHT_GREY);
  248. ((ToggleButton) findViewById(R.id.toggleButtonOnOff))
  249. .setChecked(false);
  250. findViewById(R.id.checkBoxParanoid).setEnabled(true);
  251. }
  252. }
  253. private void updateStatusLight(int light) {
  254. switch (light) {
  255. case LIGHT_GREY:
  256. ((ImageView) findViewById(R.id.imageViewLight))
  257. .setImageResource(R.drawable.light_grey_large);
  258. break;
  259. case LIGHT_GREEN:
  260. ((ImageView) findViewById(R.id.imageViewLight))
  261. .setImageResource(R.drawable.light_green_large);
  262. break;
  263. case LIGHT_RED:
  264. ((ImageView) findViewById(R.id.imageViewLight))
  265. .setImageResource(R.drawable.light_red_large);
  266. break;
  267. case LIGHT_YELLOW:
  268. ((ImageView) findViewById(R.id.imageViewLight))
  269. .setImageResource(R.drawable.light_yellow_large);
  270. break;
  271. }
  272. }
  273. private void updateProtocolLight(int light, String protocolName) {
  274. for (int i = 0; i < adapter.getCount(); ++i) {
  275. HashMap<String, String> d = (HashMap<String, String>) adapter
  276. .getItem(i);
  277. if (d.get("protocol").equals(protocolName)) {
  278. switch (light) {
  279. case LIGHT_GREY:
  280. d.put("light", String.valueOf(R.drawable.light_grey));
  281. d.put("connections", "-");
  282. break;
  283. case LIGHT_GREEN:
  284. d.put("light", String.valueOf(R.drawable.light_green));
  285. d.put("connections", "0");
  286. break;
  287. case LIGHT_RED:
  288. d.put("light", String.valueOf(R.drawable.light_red));
  289. break;
  290. case LIGHT_YELLOW:
  291. d.put("light", String.valueOf(R.drawable.light_yellow));
  292. d.put("connections", "0");
  293. break;
  294. }
  295. }
  296. }
  297. adapter.notifyDataSetChanged();
  298. }
  299. private void updateProtocolConnections(int connections, String protocolName) {
  300. for (int i = 0; i < adapter.getCount(); ++i) {
  301. HashMap<String, String> d = ((HashMap<String, String>) adapter
  302. .getItem(i));
  303. if (d.get("protocol").equals(protocolName)) {
  304. d.put("connections", String.valueOf(connections));
  305. }
  306. }
  307. adapter.notifyDataSetChanged();
  308. }
  309. }