MainActivity.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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.content.SharedPreferences;
  14. import android.content.SharedPreferences.Editor;
  15. import android.net.ConnectivityManager;
  16. import android.os.Bundle;
  17. import android.os.IBinder;
  18. import android.support.v4.content.LocalBroadcastManager;
  19. import android.util.Log;
  20. import android.view.GestureDetector;
  21. import android.view.GestureDetector.SimpleOnGestureListener;
  22. import android.view.Menu;
  23. import android.view.MenuItem;
  24. import android.view.MotionEvent;
  25. import android.view.View;
  26. import android.view.View.OnTouchListener;
  27. import android.view.animation.Animation;
  28. import android.view.animation.AnimationUtils;
  29. import android.widget.AdapterView;
  30. import android.widget.AdapterView.OnItemClickListener;
  31. import android.widget.CheckBox;
  32. import android.widget.ImageView;
  33. import android.widget.ListView;
  34. import android.widget.TextView;
  35. import android.widget.Toast;
  36. import android.widget.ToggleButton;
  37. import android.widget.ViewAnimator;
  38. import de.tudarmstadt.informatik.hostage.HoneyService;
  39. import de.tudarmstadt.informatik.hostage.HoneyService.LocalBinder;
  40. import de.tudarmstadt.informatik.hostage.R;
  41. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  42. import de.tudarmstadt.informatik.hostage.logging.DatabaseHandler;
  43. public class MainActivity extends Activity {
  44. public static final String BROADCAST = "de.tudarmstadt.informatik.hostage.BROADCAST";
  45. public static final String PREF_NAME = "de.tudarmstadt.informatik.hostage.SESSION_DATA";
  46. public static final String LISTENER = "_LISTENER";
  47. public static final String HANDLER_COUNT = "_HANDLER_COUNT";
  48. public static final int LIGHT_GREY = 0x01;
  49. public static final int LIGHT_GREEN = 0x02;
  50. public static final int LIGHT_RED = 0x03;
  51. public static final int LIGHT_YELLOW = 0x04;
  52. private HoneyService mService;
  53. private SharedPreferences pref;
  54. private Editor editor;
  55. private DatabaseHandler dbh;
  56. private ViewAnimator viewAnimator;
  57. private GestureDetector gestureDetector;
  58. private Animation animFlipInLR;
  59. private Animation animFlipOutLR;
  60. private Animation animFlipInRL;
  61. private Animation animFlipOutRL;
  62. private ListView listView;
  63. private ListViewAdapter adapter;
  64. private String protocolClicked;
  65. @Override
  66. protected void onCreate(Bundle savedInstanceState) {
  67. super.onCreate(savedInstanceState);
  68. setContentView(R.layout.activity_main);
  69. initViewAnimator();
  70. initListView();
  71. pref = getSharedPreferences(MainActivity.PREF_NAME, Context.MODE_PRIVATE);
  72. dbh = new DatabaseHandler(getApplicationContext());
  73. editor = pref.edit();
  74. }
  75. @Override
  76. public boolean onCreateOptionsMenu(Menu menu) {
  77. getMenuInflater().inflate(R.menu.main, menu);
  78. return true;
  79. }
  80. @Override
  81. public boolean onOptionsItemSelected(MenuItem item) {
  82. // Handle item selection
  83. switch (item.getItemId()) {
  84. case R.id.action_settings:
  85. startActivity(new Intent(this, SettingsActivity.class));
  86. default:
  87. return super.onOptionsItemSelected(item);
  88. }
  89. }
  90. @Override
  91. protected void onStart() {
  92. super.onStart();
  93. registerReceiver();
  94. registerNetReceiver();
  95. if (isServiceRunning()) {
  96. bindService(getServiceIntent(), mConnection, BIND_AUTO_CREATE);
  97. }
  98. }
  99. @Override
  100. protected void onStop() {
  101. if (isServiceRunning()) {
  102. unbindService(mConnection);
  103. }
  104. unregisterReceiver();
  105. unregisterNetReceiver();
  106. super.onStop();
  107. }
  108. @Override
  109. protected void onDestroy(){
  110. super.onDestroy();
  111. //TODO vielleicht wieder einfügen
  112. /*
  113. if(!isServiceRunning()){
  114. editor.clear();
  115. editor.commit();
  116. }
  117. */
  118. }
  119. @Override
  120. protected void onResume() {
  121. super.onResume();
  122. updateConnectionInfo();
  123. }
  124. public void buttonOnOffClick(View view) {
  125. if (((ToggleButton) view).isChecked()) {
  126. if (isParanoid()) {
  127. protocolClicked = "PANIC";
  128. } else {
  129. protocolClicked = "SMB";
  130. }
  131. startAndBind();
  132. } else {
  133. mService.stopListeners();
  134. stopAndUnbind();
  135. }
  136. }
  137. private void startAndBind() {
  138. startService(getServiceIntent());
  139. bindService(getServiceIntent(), mConnection, BIND_AUTO_CREATE);
  140. }
  141. private void stopAndUnbind() {
  142. unbindService(mConnection);
  143. stopService(getServiceIntent());
  144. editor.clear();
  145. editor.commit();
  146. }
  147. private ServiceConnection mConnection = new ServiceConnection() {
  148. @Override
  149. public void onServiceConnected(ComponentName name, IBinder service) {
  150. mService = ((LocalBinder) service).getService();
  151. if(protocolClicked != null && protocolClicked.equals("PANIC")){
  152. mService.startListeners();
  153. }else{
  154. mService.toggleListener(protocolClicked);
  155. }
  156. protocolClicked = null;
  157. updateUI();
  158. }
  159. @Override
  160. public void onServiceDisconnected(ComponentName name) {
  161. mService = null;
  162. }
  163. };
  164. private Intent getServiceIntent() {
  165. return new Intent(this, HoneyService.class);
  166. }
  167. private boolean isParanoid() {
  168. return ((CheckBox) findViewById(R.id.checkBoxParanoid)).isChecked();
  169. }
  170. private void initListView() {
  171. ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
  172. for (String protocol : getResources().getStringArray(R.array.protocols)) {
  173. HashMap<String, String> d = new HashMap<String, String>();
  174. d.put("light", String.valueOf(R.drawable.light_grey));
  175. d.put("protocol", protocol);
  176. d.put("connections", "-");
  177. data.add(d);
  178. }
  179. listView = (ListView) findViewById(R.id.listViewProtocols);
  180. adapter = new ListViewAdapter(getLayoutInflater(), data);
  181. listView.setAdapter(adapter);
  182. listView.setOnTouchListener(new OnTouchListener() {
  183. @Override
  184. public boolean onTouch(View v, MotionEvent event) {
  185. return gestureDetector.onTouchEvent(event);
  186. }
  187. });
  188. listView.setOnItemClickListener(new OnItemClickListener() {
  189. @Override
  190. public void onItemClick(AdapterView<?> parent, View view,
  191. int position, long id) {
  192. String protocolName = (String) ((HashMap<?, ?>) adapter
  193. .getItem(position)).get("protocol");
  194. if (isServiceRunning()) {
  195. mService.toggleListener(protocolName);
  196. if(!mService.hasRunningListeners())
  197. stopAndUnbind();
  198. }else{
  199. protocolClicked = protocolName;
  200. startAndBind();
  201. }
  202. }
  203. });
  204. }
  205. private boolean isServiceRunning() {
  206. ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
  207. for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
  208. if (service.service.getClassName().equals(HoneyService.class.getName())) {
  209. return true;
  210. }
  211. }
  212. return false;
  213. }
  214. private void registerReceiver() {
  215. LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver,
  216. new IntentFilter(BROADCAST));
  217. }
  218. private void unregisterReceiver() {
  219. LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
  220. }
  221. private void registerNetReceiver() {
  222. LocalBroadcastManager.getInstance(this).registerReceiver(netReceiver,
  223. new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
  224. }
  225. private void unregisterNetReceiver() {
  226. LocalBroadcastManager.getInstance(this).unregisterReceiver(netReceiver);
  227. }
  228. private BroadcastReceiver mReceiver = new BroadcastReceiver() {
  229. @Override
  230. public void onReceive(Context context, Intent intent) {
  231. updateUI();
  232. }
  233. };
  234. private BroadcastReceiver netReceiver = new BroadcastReceiver() {
  235. @Override
  236. public void onReceive(Context context, Intent intent) {
  237. updateConnectionInfo();
  238. }
  239. };
  240. private void updateUI() {
  241. // SharedPreferences pref = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
  242. boolean activeListeners = false;
  243. boolean activeHandlers = false;
  244. boolean yellowLight = false;
  245. for(String protocol : getResources().getStringArray(R.array.protocols)){
  246. if(pref.getBoolean(protocol + LISTENER, false)){
  247. activeListeners = true;
  248. int handlerCount = pref.getInt(protocol + HANDLER_COUNT, 0);
  249. if(handlerCount > 0){
  250. activeHandlers = true;
  251. updateProtocolLight(LIGHT_RED, protocol);
  252. updateProtocolConnections(handlerCount, protocol);
  253. } else{
  254. if(dbh.bssidSeen(protocol, HelperUtils.getBSSID(getApplicationContext()))){
  255. updateProtocolLight(LIGHT_YELLOW, protocol);
  256. yellowLight = true;
  257. } else{
  258. updateProtocolLight(LIGHT_GREEN, protocol);
  259. }
  260. updateProtocolConnections(0, protocol);
  261. }
  262. }else{
  263. updateProtocolLight(LIGHT_GREY, protocol);
  264. }
  265. }
  266. if (activeListeners) {
  267. if (activeHandlers) {
  268. updateStatusLight(LIGHT_RED);
  269. } else {
  270. if(yellowLight){
  271. updateStatusLight(LIGHT_YELLOW);
  272. } else {
  273. updateStatusLight(LIGHT_GREEN);
  274. }
  275. }
  276. ((ToggleButton) findViewById(R.id.toggleButtonOnOff))
  277. .setChecked(true);
  278. findViewById(R.id.checkBoxParanoid).setEnabled(false);
  279. } else {
  280. updateStatusLight(LIGHT_GREY);
  281. ((ToggleButton) findViewById(R.id.toggleButtonOnOff))
  282. .setChecked(false);
  283. findViewById(R.id.checkBoxParanoid).setEnabled(true);
  284. }
  285. }
  286. private void updateStatusLight(int light) {
  287. switch (light) {
  288. case LIGHT_GREY:
  289. ((ImageView) findViewById(R.id.imageViewLight))
  290. .setImageResource(R.drawable.light_grey_large);
  291. break;
  292. case LIGHT_GREEN:
  293. ((ImageView) findViewById(R.id.imageViewLight))
  294. .setImageResource(R.drawable.light_green_large);
  295. break;
  296. case LIGHT_RED:
  297. ((ImageView) findViewById(R.id.imageViewLight))
  298. .setImageResource(R.drawable.light_red_large);
  299. break;
  300. case LIGHT_YELLOW:
  301. ((ImageView) findViewById(R.id.imageViewLight))
  302. .setImageResource(R.drawable.light_yellow_large);
  303. break;
  304. }
  305. }
  306. private void updateProtocolLight(int light, String protocolName) {
  307. for (int i = 0; i < adapter.getCount(); ++i) {
  308. HashMap<String, String> d = (HashMap<String, String>) adapter
  309. .getItem(i);
  310. if (d.get("protocol").equals(protocolName)) {
  311. switch (light) {
  312. case LIGHT_GREY:
  313. d.put("light", String.valueOf(R.drawable.light_grey));
  314. d.put("connections", "-");
  315. break;
  316. case LIGHT_GREEN:
  317. d.put("light", String.valueOf(R.drawable.light_green));
  318. break;
  319. case LIGHT_RED:
  320. d.put("light", String.valueOf(R.drawable.light_red));
  321. break;
  322. case LIGHT_YELLOW:
  323. d.put("light", String.valueOf(R.drawable.light_yellow));
  324. break;
  325. }
  326. }
  327. }
  328. adapter.notifyDataSetChanged();
  329. }
  330. private void updateProtocolConnections(int connections, String protocolName) {
  331. for (int i = 0; i < adapter.getCount(); ++i) {
  332. HashMap<String, String> d = ((HashMap<String, String>) adapter
  333. .getItem(i));
  334. if (d.get("protocol").equals(protocolName)) {
  335. d.put("connections", String.valueOf(connections));
  336. }
  337. }
  338. adapter.notifyDataSetChanged();
  339. }
  340. private void updateConnectionInfo() {
  341. TextView ssidView = (TextView) findViewById(R.id.textViewSSIDValue);
  342. TextView bssidView = (TextView) findViewById(R.id.textViewBSSIDValue);
  343. TextView internalIPView = (TextView) findViewById(R.id.textViewInternalIPValue);
  344. TextView externalIPView = (TextView) findViewById(R.id.textViewExternalIPValue);
  345. String ssid = HelperUtils.getSSID(getApplicationContext());
  346. String bssid = HelperUtils.getBSSID(getApplicationContext());
  347. String internalIP = HelperUtils.getInternalIP(getApplicationContext());
  348. String externalIP = HelperUtils.getExternalIP(getApplicationContext());
  349. if (ssid != null)
  350. ssidView.setText(ssid);
  351. else
  352. ssidView.setText("-");
  353. if (bssid != null)
  354. bssidView.setText(bssid);
  355. else
  356. bssidView.setText("-");
  357. if (internalIP != null)
  358. internalIPView.setText(internalIP);
  359. else
  360. internalIPView.setText("-");
  361. if (externalIP != null)
  362. externalIPView.setText(externalIP);
  363. else
  364. externalIPView.setText("-");
  365. ssidView.invalidate();
  366. bssidView.invalidate();
  367. internalIPView.invalidate();
  368. }
  369. /*############# AB HIER KOMMEN HILFSFUNKTIONEN FÜR GESTEN ##################*/
  370. @Override
  371. public boolean onTouchEvent(MotionEvent event) {
  372. return gestureDetector.onTouchEvent(event);
  373. }
  374. private void initViewAnimator() {
  375. viewAnimator = (ViewAnimator) findViewById(R.id.viewAnimator);
  376. gestureDetector = new GestureDetector(this, simpleOnGestureListener);
  377. animFlipInLR = AnimationUtils.loadAnimation(this,
  378. R.anim.in_left_to_right);
  379. animFlipOutLR = AnimationUtils.loadAnimation(this,
  380. R.anim.out_left_to_right);
  381. animFlipInRL = AnimationUtils.loadAnimation(this,
  382. R.anim.in_right_to_left);
  383. animFlipOutRL = AnimationUtils.loadAnimation(this,
  384. R.anim.out_right_to_left);
  385. }
  386. private void swipeRightToLeft() {
  387. if (viewAnimator.getDisplayedChild() == 0) {
  388. viewAnimator.setInAnimation(animFlipInRL);
  389. viewAnimator.setOutAnimation(animFlipOutRL);
  390. viewAnimator.setDisplayedChild(1);
  391. }
  392. }
  393. private void swipeLeftToRight() {
  394. if (viewAnimator.getDisplayedChild() == 1) {
  395. viewAnimator.setInAnimation(animFlipInLR);
  396. viewAnimator.setOutAnimation(animFlipOutLR);
  397. viewAnimator.setDisplayedChild(0);
  398. }
  399. }
  400. SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener() {
  401. @Override
  402. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
  403. float velocityY) {
  404. float sensitvity = 50;
  405. if ((e1.getX() - e2.getX()) > sensitvity) {
  406. swipeRightToLeft();
  407. } else if ((e2.getX() - e1.getX()) > sensitvity) {
  408. swipeLeftToRight();
  409. }
  410. return true;
  411. }
  412. };
  413. //TODO NACH TEST ENTFERNEN
  414. public void showLog(View view){
  415. startActivity(new Intent(this, ViewLog.class));
  416. }
  417. }