MainActivity.java 15 KB

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