MainActivity.java 21 KB

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