MainActivity.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. package de.tudarmstadt.informatik.hostage.ui;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import org.apache.http.HttpEntity;
  5. import org.apache.http.HttpResponse;
  6. import org.apache.http.client.HttpClient;
  7. import org.apache.http.client.methods.HttpGet;
  8. import org.apache.http.impl.client.DefaultHttpClient;
  9. import org.apache.http.util.EntityUtils;
  10. import org.json.JSONObject;
  11. import android.app.Activity;
  12. import android.app.ActivityManager;
  13. import android.app.ActivityManager.RunningServiceInfo;
  14. import android.content.BroadcastReceiver;
  15. import android.content.ComponentName;
  16. import android.content.Context;
  17. import android.content.Intent;
  18. import android.content.IntentFilter;
  19. import android.content.ServiceConnection;
  20. import android.content.SharedPreferences;
  21. import android.content.SharedPreferences.Editor;
  22. import android.net.ConnectivityManager;
  23. import android.os.AsyncTask;
  24. import android.os.Bundle;
  25. import android.os.IBinder;
  26. import android.support.v4.content.LocalBroadcastManager;
  27. import android.view.GestureDetector;
  28. import android.view.GestureDetector.SimpleOnGestureListener;
  29. import android.view.Menu;
  30. import android.view.MenuItem;
  31. import android.view.MotionEvent;
  32. import android.view.View;
  33. import android.view.View.OnTouchListener;
  34. import android.view.animation.Animation;
  35. import android.view.animation.AnimationUtils;
  36. import android.widget.AdapterView;
  37. import android.widget.AdapterView.OnItemClickListener;
  38. import android.widget.CheckBox;
  39. import android.widget.ImageView;
  40. import android.widget.ListView;
  41. import android.widget.TextView;
  42. import android.widget.Toast;
  43. import android.widget.ToggleButton;
  44. import android.widget.ViewAnimator;
  45. import de.tudarmstadt.informatik.hostage.HoneyService;
  46. import de.tudarmstadt.informatik.hostage.HoneyService.LocalBinder;
  47. import de.tudarmstadt.informatik.hostage.PlayGroundActivity;
  48. import de.tudarmstadt.informatik.hostage.R;
  49. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  50. import de.tudarmstadt.informatik.hostage.logging.Logger;
  51. import de.tudarmstadt.informatik.hostage.logging.SQLLogger;
  52. /**
  53. * MainActivity is the central activity for the GUI of the application.
  54. * MainActivity is launched when the application is first started.
  55. * It shows the user: <br>
  56. * - information about the network<br>
  57. * - light indicators for recorded attacks on each protocol<br>
  58. * - amount of attacks on each protocols<br>
  59. * The user can start and stop services.
  60. * @author Mihai Plasoianu
  61. * @author Lars Pandikow
  62. * @author Wulf Pfeiffer
  63. */
  64. public class MainActivity extends Activity {
  65. // String constants for whole application
  66. /**
  67. * Used for Broadcast between service and GUI. String: "de.tudarmstadt.informatik.hostage.BROADCAST"
  68. */
  69. public static final String BROADCAST = "de.tudarmstadt.informatik.hostage.BROADCAST";
  70. /**
  71. * Used to store session related data in a SharedPrefereces. String: "de.tudarmstadt.informatik.hostage.SESSION_DATA"
  72. */
  73. public static final String SESSION_DATA = "de.tudarmstadt.informatik.hostage.SESSION_DATA";
  74. public static final String LISTENER = "_LISTENER";
  75. public static final String HANDLER_COUNT = "_HANDLER_COUNT";
  76. public static final String SSID = "SSID";
  77. public static final String BSSID = "BSSID";
  78. public static final String INTERNAL_IP = "INTERNAL_IP";
  79. public static final String EXTERNAL_IP = "EXTERNAL_IP";
  80. /**
  81. * Integer representing a grey light.
  82. */
  83. public static final int LIGHT_GREY = 0x01;
  84. /**
  85. * Integer representing a green light.
  86. */
  87. public static final int LIGHT_GREEN = 0x02;
  88. /**
  89. * Integer representing a red light.
  90. */
  91. public static final int LIGHT_RED = 0x03;
  92. /**
  93. * Integer representing a yellow light.
  94. */
  95. public static final int LIGHT_YELLOW = 0x04;
  96. private static Context context;
  97. private HoneyService mService;
  98. private boolean serviceBound;
  99. private SharedPreferences sessionPref;
  100. private Editor sessionEditor;
  101. private Logger logger;
  102. // variables for the swipe animation
  103. private ViewAnimator viewAnimator;
  104. private GestureDetector gestureDetector;
  105. private Animation animFlipInLR;
  106. private Animation animFlipOutLR;
  107. private Animation animFlipInRL;
  108. private Animation animFlipOutRL;
  109. private ListView listView;
  110. private ListViewAdapter adapter;
  111. private String protocolClicked;
  112. @Override
  113. protected void onCreate(Bundle savedInstanceState) {
  114. super.onCreate(savedInstanceState);
  115. MainActivity.context = getApplicationContext(); //set context
  116. setContentView(R.layout.activity_main);
  117. // Create dynamic view elements
  118. initViewAnimator();
  119. initListView();
  120. // Initialize Class variables
  121. sessionPref = getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
  122. logger = new SQLLogger(this);
  123. sessionEditor = sessionPref.edit();
  124. }
  125. @Override
  126. public boolean onCreateOptionsMenu(Menu menu) {
  127. getMenuInflater().inflate(R.menu.main, menu);
  128. return true;
  129. }
  130. @Override
  131. public boolean onOptionsItemSelected(MenuItem item) {
  132. // Handle item selection
  133. switch (item.getItemId()) {
  134. case R.id.action_settings:
  135. startActivity(new Intent(this, SettingsActivity.class));
  136. break;
  137. case R.id.action_about:
  138. startActivity(new Intent(this, AboutActivity.class));
  139. break;
  140. default:
  141. }
  142. return super.onOptionsItemSelected(item);
  143. }
  144. @Override
  145. protected void onStart() {
  146. super.onStart();
  147. //Register Broadcast Receiver
  148. registerReceiver();
  149. registerNetReceiver();
  150. // Bind service if running, else check for connection change and delete sessionData
  151. if (isServiceRunning()) {
  152. bindService(getServiceIntent(), mConnection, BIND_AUTO_CREATE);
  153. } else {
  154. String bssid_old = sessionPref.getString(MainActivity.BSSID, "");
  155. String bssid_new = HelperUtils.getBSSID(this);
  156. if(bssid_new == null || !bssid_new.equals(bssid_old)){
  157. deleteSessionData();
  158. }
  159. }
  160. // Update UI
  161. updateUI();
  162. updateConnectionInfText();
  163. }
  164. @Override
  165. protected void onStop() {
  166. //Unbind running service
  167. if (isServiceRunning()) {
  168. unbindService(mConnection);
  169. }
  170. // Unregister Broadcast Receiver
  171. unregisterNetReceiver();
  172. unregisterReceiver();
  173. super.onStop();
  174. }
  175. @Override
  176. protected void onDestroy(){
  177. super.onDestroy();
  178. // If service not running delete session data
  179. if(!isServiceRunning()){
  180. deleteSessionData();
  181. }
  182. }
  183. /**
  184. * Called when User presses on/off button.
  185. * @param view
  186. */
  187. public void buttonOnOffClick(View view) {
  188. if (((ToggleButton) view).isChecked()) {
  189. if (isParanoid()) {
  190. protocolClicked = "PANIC";
  191. } else {
  192. protocolClicked = "SMB";
  193. }
  194. startAndBind();
  195. } else {
  196. mService.stopListeners();
  197. stopAndUnbind();
  198. }
  199. }
  200. /**
  201. * Starts the ViewLog activity, when the Button is pressed.
  202. * @see ViewLog
  203. * @param view View elements which triggers the method call.
  204. */
  205. public void showLog(View view){
  206. startActivity(new Intent(this, ViewLog.class));
  207. }
  208. public void startPlayGround(View view){
  209. startActivity(new Intent(this, PlayGroundActivity.class));
  210. }
  211. /**
  212. * If mobile phone is connected to a wireless network starts the background service ands binds itself to it.
  213. * Else notifies the user that service could not be started.
  214. */
  215. private void startAndBind() {
  216. startService(getServiceIntent());
  217. bindService();
  218. }
  219. /**
  220. * Binds service to Activity
  221. * @see HoneyService
  222. */
  223. private void bindService(){
  224. bindService(getServiceIntent(), mConnection, BIND_AUTO_CREATE);
  225. serviceBound = true;
  226. }
  227. /**
  228. * Stops service and unbinds it.
  229. * @see HoneyService
  230. */
  231. private void stopAndUnbind() {
  232. unbindService();
  233. stopService(getServiceIntent());
  234. }
  235. /**
  236. * Unbinds service.
  237. * @see HoneyService
  238. */
  239. private void unbindService(){
  240. unbindService(mConnection);
  241. serviceBound = false;
  242. }
  243. /**
  244. * Connection to bind the background service
  245. * @see HoneyService
  246. */
  247. private ServiceConnection mConnection = new ServiceConnection() {
  248. /**
  249. * After the service is bound, check which has been clicked and start it.
  250. * @see android.content.ServiceConnection#onServiceConnected(android.content.ComponentName)
  251. */
  252. @Override
  253. public void onServiceConnected(ComponentName name, IBinder service) {
  254. mService = ((LocalBinder) service).getService();
  255. if(protocolClicked != null && protocolClicked.equals("PANIC")){
  256. mService.startListeners();
  257. }else if (protocolClicked != null){
  258. mService.toggleListener(protocolClicked);
  259. }
  260. protocolClicked = null;
  261. }
  262. /**
  263. * After the service is unbound, delete reference.
  264. * @see android.content.ServiceConnection#onServiceDisconnected(android.content.ComponentName)
  265. */
  266. @Override
  267. public void onServiceDisconnected(ComponentName name) {
  268. mService = null;
  269. }
  270. };
  271. /**
  272. * Returns an intent to start HoneyService.
  273. * @return An Intent to start HoneyService
  274. */
  275. private Intent getServiceIntent() {
  276. return new Intent(this, HoneyService.class);
  277. }
  278. /**
  279. * Checks if user selected paranoid mode.
  280. * @return True when paranoid mode is selected, else returns false.
  281. */
  282. private boolean isParanoid() {
  283. return ((CheckBox) findViewById(R.id.checkBoxParanoid)).isChecked();
  284. }
  285. /**
  286. * Initializes the ListView. Creating its contents dynamic from protocol res/values/protocols.xml
  287. */
  288. private void initListView() {
  289. ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
  290. for (String protocol : getResources().getStringArray(R.array.protocols)) {
  291. HashMap<String, String> d = new HashMap<String, String>();
  292. d.put("light", String.valueOf(R.drawable.light_grey));
  293. d.put("protocol", protocol);
  294. d.put("connections", "-");
  295. data.add(d);
  296. }
  297. listView = (ListView) findViewById(R.id.listViewProtocols);
  298. adapter = new ListViewAdapter(getLayoutInflater(), data);
  299. listView.setAdapter(adapter);
  300. listView.setOnTouchListener(new OnTouchListener() {
  301. @Override
  302. public boolean onTouch(View v, MotionEvent event) {
  303. return gestureDetector.onTouchEvent(event);
  304. }
  305. });
  306. listView.setOnItemClickListener(new OnItemClickListener() {
  307. @Override
  308. public void onItemClick(AdapterView<?> parent, View view,
  309. int position, long id) {
  310. String protocolName = (String) ((HashMap<?, ?>) adapter
  311. .getItem(position)).get("protocol");
  312. if (isServiceRunning()) {
  313. mService.toggleListener(protocolName);
  314. if(!mService.hasRunningListeners()){
  315. stopAndUnbind();
  316. }
  317. }else{
  318. protocolClicked = protocolName;
  319. startAndBind();
  320. }
  321. }
  322. });
  323. }
  324. /**
  325. * Checks if a {@link HoneyService} instance is running.
  326. * @return True if {@link HoneyService} is running, else false.
  327. */
  328. private boolean isServiceRunning() {
  329. ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
  330. for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
  331. if (service.service.getClassName().equals(HoneyService.class.getName())) {
  332. return true;
  333. }
  334. }
  335. return false;
  336. }
  337. /**
  338. * Deletes all session related Data.
  339. */
  340. private void deleteSessionData(){
  341. sessionEditor.clear();
  342. sessionEditor.commit();
  343. }
  344. /**
  345. * Register broadcast receiver for custom broadcast.
  346. * @see #BROADCAST
  347. */
  348. private void registerReceiver() {
  349. LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver,
  350. new IntentFilter(BROADCAST));
  351. }
  352. /**
  353. * Unregisters broadcast receiver for custom broadcast.
  354. * @see #BROADCAST
  355. */
  356. private void unregisterReceiver() {
  357. LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
  358. }
  359. /**
  360. * Receiver for custom broadcast.
  361. * @see #BROADCAST
  362. */
  363. private BroadcastReceiver mReceiver = new BroadcastReceiver() {
  364. @Override
  365. public void onReceive(Context context, Intent intent) {
  366. // Update user interface.
  367. updateUI();
  368. }
  369. };
  370. /**
  371. * Register broadcast receiver for network state changes.
  372. * @see ConnectivityManager#CONNECTIVITY_ACTION
  373. */
  374. private void registerNetReceiver() {
  375. IntentFilter intent = new IntentFilter();
  376. intent.addAction(ConnectivityManager.CONNECTIVITY_ACTION); //"android.net.conn.CONNECTIVITY_CHANGE"
  377. registerReceiver(netReceiver, intent);
  378. }
  379. /**
  380. * Unregister broadcast receiver for network state changes.
  381. */
  382. private void unregisterNetReceiver() {
  383. unregisterReceiver(netReceiver);
  384. }
  385. /**
  386. * Receiver for network state change events.
  387. */
  388. private BroadcastReceiver netReceiver = new BroadcastReceiver() {
  389. @Override
  390. public void onReceive(Context context, Intent intent) {
  391. /* String bssid_old = sessionPref.getString(BSSID, "");
  392. String bssid_new = HelperUtils.getBSSID(context);
  393. if ((bssid_new == null || !bssid_new.equals(bssid_old)) && serviceBound) {
  394. Toast.makeText(getApplicationContext(),"Connection changed! Services stopped!", Toast.LENGTH_LONG).show();
  395. unbindService();
  396. }
  397. */
  398. updateConnectionInfText();
  399. }
  400. };
  401. /**
  402. * Updates Information shown by the GUI.
  403. */
  404. private void updateUI() {
  405. boolean activeListeners = false;
  406. boolean activeHandlers = false;
  407. boolean yellowLight = false;
  408. //Check for all protocols if listeners are active and attacks have been recorded
  409. //Update protocol lights and connection information.
  410. for(String protocol : getResources().getStringArray(R.array.protocols)){
  411. //Check if protocol is active
  412. if(sessionPref.getBoolean(protocol + LISTENER, false)){
  413. activeListeners = true;
  414. int handlerCount = sessionPref.getInt(protocol + HANDLER_COUNT, 0);
  415. //Check if attacks have been recorded in this session.
  416. if(handlerCount > 0){
  417. activeHandlers = true;
  418. updateProtocolLight(LIGHT_RED, protocol);
  419. updateProtocolConnections(handlerCount, protocol);
  420. } else{
  421. //Check if the bssid of the wireless network has already been recorded as infected.
  422. if(logger.bssidSeen(protocol, HelperUtils.getBSSID(getApplicationContext()))){
  423. updateProtocolLight(LIGHT_YELLOW, protocol);
  424. yellowLight = true;
  425. } else{
  426. updateProtocolLight(LIGHT_GREEN, protocol);
  427. }
  428. updateProtocolConnections(0, protocol);
  429. }
  430. }else{
  431. updateProtocolLight(LIGHT_GREY, protocol);
  432. }
  433. }
  434. //Update the big attack indicator.
  435. if (activeListeners) {
  436. if (activeHandlers) {
  437. updateStatusLight(LIGHT_RED);
  438. } else {
  439. if(yellowLight){
  440. updateStatusLight(LIGHT_YELLOW);
  441. } else {
  442. updateStatusLight(LIGHT_GREEN);
  443. }
  444. }
  445. ((ToggleButton) findViewById(R.id.toggleButtonOnOff))
  446. .setChecked(true);
  447. findViewById(R.id.checkBoxParanoid).setEnabled(false);
  448. } else {
  449. updateStatusLight(LIGHT_GREY);
  450. ((ToggleButton) findViewById(R.id.toggleButtonOnOff))
  451. .setChecked(false);
  452. findViewById(R.id.checkBoxParanoid).setEnabled(true);
  453. }
  454. }
  455. /**
  456. * Sets the big light indicator.
  457. * @param light Integer code to set the light color.
  458. * @see #LIGHT_GREY
  459. * @see #LIGHT_GREEN
  460. * @see #LIGHT_RED
  461. * @see #LIGHT_YELLOW
  462. */
  463. private void updateStatusLight(int light) {
  464. switch (light) {
  465. case LIGHT_GREY:
  466. ((ImageView) findViewById(R.id.imageViewLight))
  467. .setImageResource(R.drawable.light_grey_large);
  468. break;
  469. case LIGHT_GREEN:
  470. ((ImageView) findViewById(R.id.imageViewLight))
  471. .setImageResource(R.drawable.light_green_large);
  472. break;
  473. case LIGHT_RED:
  474. ((ImageView) findViewById(R.id.imageViewLight))
  475. .setImageResource(R.drawable.light_red_large);
  476. break;
  477. case LIGHT_YELLOW:
  478. ((ImageView) findViewById(R.id.imageViewLight))
  479. .setImageResource(R.drawable.light_yellow_large);
  480. break;
  481. }
  482. }
  483. /**
  484. * Sets the light indicator for a given protocol.
  485. * @param light Integer code to set the light color.
  486. * @param protocolName Name of the protocol which should be updated.
  487. */
  488. private void updateProtocolLight(int light, String protocolName) {
  489. for (int i = 0; i < adapter.getCount(); ++i) {
  490. HashMap<String, String> d = (HashMap<String, String>) adapter
  491. .getItem(i);
  492. if (d.get("protocol").equals(protocolName)) {
  493. switch (light) {
  494. case LIGHT_GREY:
  495. d.put("light", String.valueOf(R.drawable.light_grey));
  496. d.put("connections", "-");
  497. break;
  498. case LIGHT_GREEN:
  499. d.put("light", String.valueOf(R.drawable.light_green));
  500. break;
  501. case LIGHT_RED:
  502. d.put("light", String.valueOf(R.drawable.light_red));
  503. break;
  504. case LIGHT_YELLOW:
  505. d.put("light", String.valueOf(R.drawable.light_yellow));
  506. break;
  507. }
  508. }
  509. }
  510. adapter.notifyDataSetChanged();
  511. }
  512. /**
  513. * Sets the connections count for a given protocol.
  514. * @param connections New value for recorded connections.
  515. * @param protocolName Name of the protocol which should be updated.
  516. */
  517. private void updateProtocolConnections(int connections, String protocolName) {
  518. for (int i = 0; i < adapter.getCount(); ++i) {
  519. HashMap<String, String> d = ((HashMap<String, String>) adapter
  520. .getItem(i));
  521. if (d.get("protocol").equals(protocolName)) {
  522. d.put("connections", String.valueOf(connections));
  523. }
  524. }
  525. adapter.notifyDataSetChanged();
  526. }
  527. /**
  528. * Gets Information about connection state and updates the GUI.
  529. */
  530. private void updateConnectionInfText() {
  531. TextView ssidView = (TextView) findViewById(R.id.textViewSSIDValue);
  532. TextView bssidView = (TextView) findViewById(R.id.textViewBSSIDValue);
  533. TextView internalIPView = (TextView) findViewById(R.id.textViewInternalIPValue);
  534. TextView externalIPView = (TextView) findViewById(R.id.textViewExternalIPValue);
  535. externalIPView.setText("Loading...");
  536. //Update the connection information
  537. updateConnectionInfo();
  538. SetExternalIPTask async = new SetExternalIPTask();
  539. async.execute(new String[]{"http://ip2country.sourceforge.net/ip2c.php?format=JSON"});
  540. //Get connection information
  541. String ssid = sessionPref.getString(SSID, null);
  542. String bssid = sessionPref.getString(BSSID, null);
  543. String internalIP = sessionPref.getString(INTERNAL_IP, null);
  544. //Set text fields
  545. if (ssid != null)
  546. ssidView.setText(ssid);
  547. else
  548. ssidView.setText("-");
  549. if (bssid != null)
  550. bssidView.setText(bssid);
  551. else
  552. bssidView.setText("-");
  553. if (internalIP != null)
  554. internalIPView.setText(internalIP);
  555. else
  556. internalIPView.setText("-");
  557. }
  558. /**
  559. * Updates the connection info and saves them in the the SharedPreferences for session data.
  560. * @param context Needs a context to get system recourses.
  561. * @see MainActivity#SESSION_DATA
  562. */
  563. private void updateConnectionInfo() {
  564. SharedPreferences pref = context.getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
  565. Editor editor = pref.edit();
  566. editor.putString(MainActivity.SSID, HelperUtils.getSSID(context));
  567. editor.putString(MainActivity.BSSID, HelperUtils.getBSSID(context));
  568. editor.putString(MainActivity.INTERNAL_IP, HelperUtils.getInternalIP(context));
  569. editor.commit();
  570. }
  571. /**
  572. * Task to find out the external IP.
  573. * @author Lars Pandikow
  574. */
  575. private class SetExternalIPTask extends AsyncTask<String, Void, String>{
  576. @Override
  577. protected String doInBackground(String... url) {
  578. String ipAddress = null;
  579. try {
  580. HttpClient httpclient = new DefaultHttpClient();
  581. HttpGet httpget = new HttpGet(url[0]);
  582. HttpResponse response;
  583. response = httpclient.execute(httpget);
  584. HttpEntity entity = response.getEntity();
  585. entity.getContentLength();
  586. String str = EntityUtils.toString(entity);
  587. JSONObject json_data = new JSONObject(str);
  588. ipAddress = json_data.getString("ip");
  589. } catch (Exception e) {
  590. e.printStackTrace();
  591. }
  592. return ipAddress;
  593. }
  594. @Override
  595. protected void onPostExecute(String result){
  596. sessionEditor.putString(MainActivity.EXTERNAL_IP, result);
  597. sessionEditor.commit();
  598. TextView externalIPView = (TextView) findViewById(R.id.textViewExternalIPValue);
  599. if (result != null)
  600. externalIPView.setText(result);
  601. else
  602. externalIPView.setText("-");
  603. }
  604. };
  605. /*############# Help functions for animation ##################*/
  606. @Override
  607. public boolean onTouchEvent(MotionEvent event) {
  608. return gestureDetector.onTouchEvent(event);
  609. }
  610. /**
  611. * Initializes variables for screen animation
  612. */
  613. private void initViewAnimator() {
  614. viewAnimator = (ViewAnimator) findViewById(R.id.viewAnimator);
  615. gestureDetector = new GestureDetector(this, simpleOnGestureListener);
  616. animFlipInLR = AnimationUtils.loadAnimation(this,
  617. R.anim.in_left_to_right);
  618. animFlipOutLR = AnimationUtils.loadAnimation(this,
  619. R.anim.out_left_to_right);
  620. animFlipInRL = AnimationUtils.loadAnimation(this,
  621. R.anim.in_right_to_left);
  622. animFlipOutRL = AnimationUtils.loadAnimation(this,
  623. R.anim.out_right_to_left);
  624. }
  625. /**
  626. * Called when a swipe to the Left is registered.
  627. */
  628. private void swipeRightToLeft() {
  629. if (viewAnimator.getDisplayedChild() == 0) {
  630. viewAnimator.setInAnimation(animFlipInRL);
  631. viewAnimator.setOutAnimation(animFlipOutRL);
  632. viewAnimator.setDisplayedChild(1);
  633. }
  634. }
  635. /**
  636. * Called when a swipe to the Right is registered.
  637. */
  638. private void swipeLeftToRight() {
  639. if (viewAnimator.getDisplayedChild() == 1) {
  640. viewAnimator.setInAnimation(animFlipInLR);
  641. viewAnimator.setOutAnimation(animFlipOutLR);
  642. viewAnimator.setDisplayedChild(0);
  643. }
  644. }
  645. SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener() {
  646. @Override
  647. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
  648. float velocityY) {
  649. float sensitvity = 50;
  650. if ((e1.getX() - e2.getX()) > sensitvity) {
  651. swipeRightToLeft();
  652. } else if ((e2.getX() - e1.getX()) > sensitvity) {
  653. swipeLeftToRight();
  654. }
  655. return true;
  656. }
  657. };
  658. /**
  659. * Returns the context of the App.
  660. * @return context.
  661. */
  662. public static Context getContext() {
  663. return MainActivity.context;
  664. }
  665. }