MainActivity.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. unregisterNetReceiver();
  105. unregisterReceiver();
  106. super.onStop();
  107. }
  108. @Override
  109. protected void onDestroy(){
  110. super.onDestroy();
  111. if(!isServiceRunning()){
  112. deleteSessionData();
  113. }
  114. }
  115. @Override
  116. protected void onResume() {
  117. super.onResume();
  118. updateConnectionInfo();
  119. }
  120. public void buttonOnOffClick(View view) {
  121. if (((ToggleButton) view).isChecked()) {
  122. if (isParanoid()) {
  123. protocolClicked = "PANIC";
  124. } else {
  125. protocolClicked = "SMB";
  126. }
  127. startAndBind();
  128. } else {
  129. mService.stopListeners();
  130. stopAndUnbind();
  131. }
  132. }
  133. private void startAndBind() {
  134. startService(getServiceIntent());
  135. bindService(getServiceIntent(), mConnection, BIND_AUTO_CREATE);
  136. }
  137. private void stopAndUnbind() {
  138. unbindService(mConnection);
  139. stopService(getServiceIntent());
  140. }
  141. private ServiceConnection mConnection = new ServiceConnection() {
  142. @Override
  143. public void onServiceConnected(ComponentName name, IBinder service) {
  144. mService = ((LocalBinder) service).getService();
  145. if(protocolClicked != null && protocolClicked.equals("PANIC")){
  146. mService.startListeners();
  147. }else{
  148. mService.toggleListener(protocolClicked);
  149. }
  150. protocolClicked = null;
  151. updateUI();
  152. }
  153. @Override
  154. public void onServiceDisconnected(ComponentName name) {
  155. mService = null;
  156. }
  157. };
  158. private Intent getServiceIntent() {
  159. return new Intent(this, HoneyService.class);
  160. }
  161. private boolean isParanoid() {
  162. return ((CheckBox) findViewById(R.id.checkBoxParanoid)).isChecked();
  163. }
  164. private void initListView() {
  165. ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
  166. for (String protocol : getResources().getStringArray(R.array.protocols)) {
  167. HashMap<String, String> d = new HashMap<String, String>();
  168. d.put("light", String.valueOf(R.drawable.light_grey));
  169. d.put("protocol", protocol);
  170. d.put("connections", "-");
  171. data.add(d);
  172. }
  173. listView = (ListView) findViewById(R.id.listViewProtocols);
  174. adapter = new ListViewAdapter(getLayoutInflater(), data);
  175. listView.setAdapter(adapter);
  176. listView.setOnTouchListener(new OnTouchListener() {
  177. @Override
  178. public boolean onTouch(View v, MotionEvent event) {
  179. return gestureDetector.onTouchEvent(event);
  180. }
  181. });
  182. listView.setOnItemClickListener(new OnItemClickListener() {
  183. @Override
  184. public void onItemClick(AdapterView<?> parent, View view,
  185. int position, long id) {
  186. String protocolName = (String) ((HashMap<?, ?>) adapter
  187. .getItem(position)).get("protocol");
  188. if (isServiceRunning()) {
  189. mService.toggleListener(protocolName);
  190. if(!mService.hasRunningListeners())
  191. stopAndUnbind();
  192. }else{
  193. protocolClicked = protocolName;
  194. startAndBind();
  195. }
  196. }
  197. });
  198. }
  199. private boolean isServiceRunning() {
  200. ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
  201. for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
  202. if (service.service.getClassName().equals(HoneyService.class.getName())) {
  203. return true;
  204. }
  205. }
  206. return false;
  207. }
  208. // Delete session data
  209. private void deleteSessionData(){
  210. editor.clear();
  211. editor.commit();
  212. }
  213. private void registerReceiver() {
  214. LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver,
  215. new IntentFilter(BROADCAST));
  216. }
  217. private void unregisterReceiver() {
  218. LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
  219. }
  220. private BroadcastReceiver mReceiver = new BroadcastReceiver() {
  221. @Override
  222. public void onReceive(Context context, Intent intent) {
  223. updateUI();
  224. }
  225. };
  226. private void registerNetReceiver() {
  227. // register BroadcastReceiver on network state changes
  228. final IntentFilter intent = new IntentFilter();
  229. intent.addAction(android.net.ConnectivityManager.CONNECTIVITY_ACTION); //"android.net.conn.CONNECTIVITY_CHANGE"
  230. registerReceiver(netReceiver, intent);
  231. }
  232. private void unregisterNetReceiver() {
  233. LocalBroadcastManager.getInstance(this).unregisterReceiver(netReceiver);
  234. }
  235. private BroadcastReceiver netReceiver = new BroadcastReceiver() {
  236. @Override
  237. public void onReceive(Context context, Intent intent) {
  238. if (isServiceRunning()) {
  239. Toast.makeText(getApplicationContext(),"Connection changed! Services stopped!", Toast.LENGTH_LONG).show();
  240. mService.stopListeners();
  241. stopAndUnbind();
  242. }
  243. deleteSessionData();
  244. updateConnectionInfo();
  245. updateUI();
  246. }
  247. };
  248. private void updateUI() {
  249. boolean activeListeners = false;
  250. boolean activeHandlers = false;
  251. boolean yellowLight = false;
  252. for(String protocol : getResources().getStringArray(R.array.protocols)){
  253. if(pref.getBoolean(protocol + LISTENER, false)){
  254. activeListeners = true;
  255. int handlerCount = pref.getInt(protocol + HANDLER_COUNT, 0);
  256. if(handlerCount > 0){
  257. activeHandlers = true;
  258. updateProtocolLight(LIGHT_RED, protocol);
  259. updateProtocolConnections(handlerCount, protocol);
  260. } else{
  261. if(dbh.bssidSeen(protocol, HelperUtils.getBSSID(getApplicationContext()))){
  262. updateProtocolLight(LIGHT_YELLOW, protocol);
  263. yellowLight = true;
  264. } else{
  265. updateProtocolLight(LIGHT_GREEN, protocol);
  266. }
  267. updateProtocolConnections(0, protocol);
  268. }
  269. }else{
  270. updateProtocolLight(LIGHT_GREY, protocol);
  271. }
  272. }
  273. if (activeListeners) {
  274. if (activeHandlers) {
  275. updateStatusLight(LIGHT_RED);
  276. } else {
  277. if(yellowLight){
  278. updateStatusLight(LIGHT_YELLOW);
  279. } else {
  280. updateStatusLight(LIGHT_GREEN);
  281. }
  282. }
  283. ((ToggleButton) findViewById(R.id.toggleButtonOnOff))
  284. .setChecked(true);
  285. findViewById(R.id.checkBoxParanoid).setEnabled(false);
  286. } else {
  287. updateStatusLight(LIGHT_GREY);
  288. ((ToggleButton) findViewById(R.id.toggleButtonOnOff))
  289. .setChecked(false);
  290. findViewById(R.id.checkBoxParanoid).setEnabled(true);
  291. }
  292. }
  293. private void updateStatusLight(int light) {
  294. switch (light) {
  295. case LIGHT_GREY:
  296. ((ImageView) findViewById(R.id.imageViewLight))
  297. .setImageResource(R.drawable.light_grey_large);
  298. break;
  299. case LIGHT_GREEN:
  300. ((ImageView) findViewById(R.id.imageViewLight))
  301. .setImageResource(R.drawable.light_green_large);
  302. break;
  303. case LIGHT_RED:
  304. ((ImageView) findViewById(R.id.imageViewLight))
  305. .setImageResource(R.drawable.light_red_large);
  306. break;
  307. case LIGHT_YELLOW:
  308. ((ImageView) findViewById(R.id.imageViewLight))
  309. .setImageResource(R.drawable.light_yellow_large);
  310. break;
  311. }
  312. }
  313. private void updateProtocolLight(int light, String protocolName) {
  314. for (int i = 0; i < adapter.getCount(); ++i) {
  315. HashMap<String, String> d = (HashMap<String, String>) adapter
  316. .getItem(i);
  317. if (d.get("protocol").equals(protocolName)) {
  318. switch (light) {
  319. case LIGHT_GREY:
  320. d.put("light", String.valueOf(R.drawable.light_grey));
  321. d.put("connections", "-");
  322. break;
  323. case LIGHT_GREEN:
  324. d.put("light", String.valueOf(R.drawable.light_green));
  325. break;
  326. case LIGHT_RED:
  327. d.put("light", String.valueOf(R.drawable.light_red));
  328. break;
  329. case LIGHT_YELLOW:
  330. d.put("light", String.valueOf(R.drawable.light_yellow));
  331. break;
  332. }
  333. }
  334. }
  335. adapter.notifyDataSetChanged();
  336. }
  337. private void updateProtocolConnections(int connections, String protocolName) {
  338. for (int i = 0; i < adapter.getCount(); ++i) {
  339. HashMap<String, String> d = ((HashMap<String, String>) adapter
  340. .getItem(i));
  341. if (d.get("protocol").equals(protocolName)) {
  342. d.put("connections", String.valueOf(connections));
  343. }
  344. }
  345. adapter.notifyDataSetChanged();
  346. }
  347. private void updateConnectionInfo() {
  348. TextView ssidView = (TextView) findViewById(R.id.textViewSSIDValue);
  349. TextView bssidView = (TextView) findViewById(R.id.textViewBSSIDValue);
  350. TextView internalIPView = (TextView) findViewById(R.id.textViewInternalIPValue);
  351. TextView externalIPView = (TextView) findViewById(R.id.textViewExternalIPValue);
  352. String ssid = HelperUtils.getSSID(this);
  353. String bssid = HelperUtils.getBSSID(this);
  354. String internalIP = HelperUtils.getInternalIP(this);
  355. String externalIP = HelperUtils.getExternalIP(this);
  356. if (ssid != null)
  357. ssidView.setText(ssid);
  358. else
  359. ssidView.setText("-");
  360. if (bssid != null)
  361. bssidView.setText(bssid);
  362. else
  363. bssidView.setText("-");
  364. if (internalIP != null)
  365. internalIPView.setText(internalIP);
  366. else
  367. internalIPView.setText("-");
  368. if (externalIP != null)
  369. externalIPView.setText(externalIP);
  370. else
  371. externalIPView.setText("-");
  372. ssidView.invalidate();
  373. bssidView.invalidate();
  374. internalIPView.invalidate();
  375. }
  376. /*############# AB HIER KOMMEN HILFSFUNKTIONEN FÜR GESTEN ##################*/
  377. @Override
  378. public boolean onTouchEvent(MotionEvent event) {
  379. return gestureDetector.onTouchEvent(event);
  380. }
  381. private void initViewAnimator() {
  382. viewAnimator = (ViewAnimator) findViewById(R.id.viewAnimator);
  383. gestureDetector = new GestureDetector(this, simpleOnGestureListener);
  384. animFlipInLR = AnimationUtils.loadAnimation(this,
  385. R.anim.in_left_to_right);
  386. animFlipOutLR = AnimationUtils.loadAnimation(this,
  387. R.anim.out_left_to_right);
  388. animFlipInRL = AnimationUtils.loadAnimation(this,
  389. R.anim.in_right_to_left);
  390. animFlipOutRL = AnimationUtils.loadAnimation(this,
  391. R.anim.out_right_to_left);
  392. }
  393. private void swipeRightToLeft() {
  394. if (viewAnimator.getDisplayedChild() == 0) {
  395. viewAnimator.setInAnimation(animFlipInRL);
  396. viewAnimator.setOutAnimation(animFlipOutRL);
  397. viewAnimator.setDisplayedChild(1);
  398. }
  399. }
  400. private void swipeLeftToRight() {
  401. if (viewAnimator.getDisplayedChild() == 1) {
  402. viewAnimator.setInAnimation(animFlipInLR);
  403. viewAnimator.setOutAnimation(animFlipOutLR);
  404. viewAnimator.setDisplayedChild(0);
  405. }
  406. }
  407. SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener() {
  408. @Override
  409. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
  410. float velocityY) {
  411. float sensitvity = 50;
  412. if ((e1.getX() - e2.getX()) > sensitvity) {
  413. swipeRightToLeft();
  414. } else if ((e2.getX() - e1.getX()) > sensitvity) {
  415. swipeLeftToRight();
  416. }
  417. return true;
  418. }
  419. };
  420. public void showLog(View view){
  421. startActivity(new Intent(this, ViewLog.class));
  422. }
  423. }