MainActivity.java 15 KB

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