MainActivity.java 12 KB

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