MainActivity.java 9.8 KB

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