MainActivity.java 14 KB

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