MainActivity.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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. startService(getServiceIntent());
  213. bindService();
  214. }
  215. /**
  216. * Binds service to Activity
  217. * @see HoneyService
  218. */
  219. private void bindService(){
  220. bindService(getServiceIntent(), mConnection, BIND_AUTO_CREATE);
  221. serviceBound = true;
  222. }
  223. /**
  224. * Stops service and unbinds it.
  225. * @see HoneyService
  226. */
  227. private void stopAndUnbind() {
  228. unbindService();
  229. stopService(getServiceIntent());
  230. }
  231. /**
  232. * Unbinds service.
  233. * @see HoneyService
  234. */
  235. private void unbindService(){
  236. unbindService(mConnection);
  237. serviceBound = false;
  238. }
  239. /**
  240. * Connection to bind the background service
  241. * @see HoneyService
  242. */
  243. private ServiceConnection mConnection = new ServiceConnection() {
  244. /**
  245. * After the service is bound, check which has been clicked and start it.
  246. * @see android.content.ServiceConnection#onServiceConnected(android.content.ComponentName)
  247. */
  248. public void onServiceConnected(ComponentName name, IBinder service) {
  249. mService = ((LocalBinder) service).getService();
  250. if(protocolClicked != null && protocolClicked.equals("PANIC")){
  251. mService.startListeners();
  252. }else if (protocolClicked != null){
  253. mService.toggleListener(protocolClicked);
  254. }
  255. protocolClicked = null;
  256. }
  257. /**
  258. * After the service is unbound, delete reference.
  259. * @see android.content.ServiceConnection#onServiceDisconnected(android.content.ComponentName)
  260. */
  261. public void onServiceDisconnected(ComponentName name) {
  262. mService = null;
  263. }
  264. };
  265. /**
  266. * Returns an intent to start HoneyService.
  267. * @return An Intent to start HoneyService
  268. */
  269. private Intent getServiceIntent() {
  270. return new Intent(this, HoneyService.class);
  271. }
  272. /**
  273. * Checks if user selected paranoid mode.
  274. * @return True when paranoid mode is selected, else returns false.
  275. */
  276. private boolean isParanoid() {
  277. return ((CheckBox) findViewById(R.id.checkBoxParanoid)).isChecked();
  278. }
  279. /**
  280. * Initializes the ListView. Creating its contents dynamic from protocol res/values/protocols.xml
  281. */
  282. private void initListView() {
  283. ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
  284. for (String protocol : getResources().getStringArray(R.array.protocols)) {
  285. HashMap<String, String> d = new HashMap<String, String>();
  286. d.put("light", String.valueOf(R.drawable.light_grey));
  287. d.put("protocol", protocol);
  288. d.put("connections", "-");
  289. data.add(d);
  290. }
  291. listView = (ListView) findViewById(R.id.listViewProtocols);
  292. adapter = new ListViewAdapter(getLayoutInflater(), data);
  293. listView.setAdapter(adapter);
  294. listView.setOnTouchListener(new OnTouchListener() {
  295. public boolean onTouch(View v, MotionEvent event) {
  296. return gestureDetector.onTouchEvent(event);
  297. }
  298. });
  299. listView.setOnItemClickListener(new OnItemClickListener() {
  300. public void onItemClick(AdapterView<?> parent, View view,
  301. int position, long id) {
  302. String protocolName = (String) ((HashMap<?, ?>) adapter
  303. .getItem(position)).get("protocol");
  304. if (isServiceRunning()) {
  305. mService.toggleListener(protocolName);
  306. if(!mService.hasRunningListeners()){
  307. stopAndUnbind();
  308. }
  309. }else{
  310. protocolClicked = protocolName;
  311. startAndBind();
  312. }
  313. }
  314. });
  315. }
  316. /**
  317. * Checks if a {@link HoneyService} instance is running.
  318. * @return True if {@link HoneyService} is running, else false.
  319. */
  320. private boolean isServiceRunning() {
  321. ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
  322. for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
  323. if (service.service.getClassName().equals(HoneyService.class.getName())) {
  324. return true;
  325. }
  326. }
  327. return false;
  328. }
  329. /**
  330. * Deletes all session related Data.
  331. */
  332. private void deleteSessionData(){
  333. sessionEditor.clear();
  334. sessionEditor.commit();
  335. }
  336. /**
  337. * Register broadcast receiver for custom broadcast.
  338. * @see #BROADCAST
  339. */
  340. private void registerReceiver() {
  341. LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver,
  342. new IntentFilter(BROADCAST));
  343. }
  344. /**
  345. * Unregisters broadcast receiver for custom broadcast.
  346. * @see #BROADCAST
  347. */
  348. private void unregisterReceiver() {
  349. LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
  350. }
  351. /**
  352. * Receiver for custom broadcast.
  353. * @see #BROADCAST
  354. */
  355. private BroadcastReceiver mReceiver = new BroadcastReceiver() {
  356. @Override
  357. public void onReceive(Context context, Intent intent) {
  358. // Update user interface.
  359. updateUI();
  360. }
  361. };
  362. /**
  363. * Register broadcast receiver for network state changes.
  364. * @see ConnectivityManager#CONNECTIVITY_ACTION
  365. */
  366. private void registerNetReceiver() {
  367. IntentFilter intent = new IntentFilter();
  368. intent.addAction(ConnectivityManager.CONNECTIVITY_ACTION); //"android.net.conn.CONNECTIVITY_CHANGE"
  369. registerReceiver(netReceiver, intent);
  370. }
  371. /**
  372. * Unregister broadcast receiver for network state changes.
  373. */
  374. private void unregisterNetReceiver() {
  375. unregisterReceiver(netReceiver);
  376. }
  377. /**
  378. * Receiver for network state change events.
  379. */
  380. private BroadcastReceiver netReceiver = new BroadcastReceiver() {
  381. @Override
  382. public void onReceive(Context context, Intent intent) {
  383. /* String bssid_old = sessionPref.getString(BSSID, "");
  384. String bssid_new = HelperUtils.getBSSID(context);
  385. if ((bssid_new == null || !bssid_new.equals(bssid_old)) && serviceBound) {
  386. Toast.makeText(getApplicationContext(),"Connection changed! Services stopped!", Toast.LENGTH_LONG).show();
  387. unbindService();
  388. }
  389. */
  390. updateConnectionInfText();
  391. }
  392. };
  393. /**
  394. * Updates Information shown by the GUI.
  395. */
  396. private void updateUI() {
  397. boolean activeListeners = false;
  398. boolean activeHandlers = false;
  399. boolean yellowLight = false;
  400. //Check for all protocols if listeners are active and attacks have been recorded
  401. //Update protocol lights and connection information.
  402. for(String protocol : getResources().getStringArray(R.array.protocols)){
  403. //Check if protocol is active
  404. if(sessionPref.getBoolean(protocol + LISTENER, false)){
  405. activeListeners = true;
  406. int handlerCount = sessionPref.getInt(protocol + HANDLER_COUNT, 0);
  407. //Check if attacks have been recorded in this session.
  408. if(handlerCount > 0){
  409. activeHandlers = true;
  410. updateProtocolLight(LIGHT_RED, protocol);
  411. updateProtocolConnections(handlerCount, protocol);
  412. } else{
  413. //Check if the bssid of the wireless network has already been recorded as infected.
  414. if(logger.bssidSeen(protocol, HelperUtils.getBSSID(getApplicationContext()))){
  415. updateProtocolLight(LIGHT_YELLOW, protocol);
  416. yellowLight = true;
  417. } else{
  418. updateProtocolLight(LIGHT_GREEN, protocol);
  419. }
  420. updateProtocolConnections(0, protocol);
  421. }
  422. }else{
  423. updateProtocolLight(LIGHT_GREY, protocol);
  424. }
  425. }
  426. //Update the big attack indicator.
  427. if (activeListeners) {
  428. if (activeHandlers) {
  429. updateStatusLight(LIGHT_RED);
  430. } else {
  431. if(yellowLight){
  432. updateStatusLight(LIGHT_YELLOW);
  433. } else {
  434. updateStatusLight(LIGHT_GREEN);
  435. }
  436. }
  437. ((ToggleButton) findViewById(R.id.toggleButtonOnOff))
  438. .setChecked(true);
  439. findViewById(R.id.checkBoxParanoid).setEnabled(false);
  440. } else {
  441. updateStatusLight(LIGHT_GREY);
  442. ((ToggleButton) findViewById(R.id.toggleButtonOnOff))
  443. .setChecked(false);
  444. findViewById(R.id.checkBoxParanoid).setEnabled(true);
  445. }
  446. }
  447. /**
  448. * Sets the big light indicator.
  449. * @param light Integer code to set the light color.
  450. * @see #LIGHT_GREY
  451. * @see #LIGHT_GREEN
  452. * @see #LIGHT_RED
  453. * @see #LIGHT_YELLOW
  454. */
  455. private void updateStatusLight(int light) {
  456. switch (light) {
  457. case LIGHT_GREY:
  458. ((ImageView) findViewById(R.id.imageViewLight))
  459. .setImageResource(R.drawable.light_grey_large);
  460. break;
  461. case LIGHT_GREEN:
  462. ((ImageView) findViewById(R.id.imageViewLight))
  463. .setImageResource(R.drawable.light_green_large);
  464. break;
  465. case LIGHT_RED:
  466. ((ImageView) findViewById(R.id.imageViewLight))
  467. .setImageResource(R.drawable.light_red_large);
  468. break;
  469. case LIGHT_YELLOW:
  470. ((ImageView) findViewById(R.id.imageViewLight))
  471. .setImageResource(R.drawable.light_yellow_large);
  472. break;
  473. }
  474. }
  475. /**
  476. * Sets the light indicator for a given protocol.
  477. * @param light Integer code to set the light color.
  478. * @param protocolName Name of the protocol which should be updated.
  479. */
  480. private void updateProtocolLight(int light, String protocolName) {
  481. for (int i = 0; i < adapter.getCount(); ++i) {
  482. HashMap<String, String> d = (HashMap<String, String>) adapter
  483. .getItem(i);
  484. if (d.get("protocol").equals(protocolName)) {
  485. switch (light) {
  486. case LIGHT_GREY:
  487. d.put("light", String.valueOf(R.drawable.light_grey));
  488. d.put("connections", "-");
  489. break;
  490. case LIGHT_GREEN:
  491. d.put("light", String.valueOf(R.drawable.light_green));
  492. break;
  493. case LIGHT_RED:
  494. d.put("light", String.valueOf(R.drawable.light_red));
  495. break;
  496. case LIGHT_YELLOW:
  497. d.put("light", String.valueOf(R.drawable.light_yellow));
  498. break;
  499. }
  500. }
  501. }
  502. adapter.notifyDataSetChanged();
  503. }
  504. /**
  505. * Sets the connections count for a given protocol.
  506. * @param connections New value for recorded connections.
  507. * @param protocolName Name of the protocol which should be updated.
  508. */
  509. private void updateProtocolConnections(int connections, String protocolName) {
  510. for (int i = 0; i < adapter.getCount(); ++i) {
  511. HashMap<String, String> d = ((HashMap<String, String>) adapter
  512. .getItem(i));
  513. if (d.get("protocol").equals(protocolName)) {
  514. d.put("connections", String.valueOf(connections));
  515. }
  516. }
  517. adapter.notifyDataSetChanged();
  518. }
  519. /**
  520. * Gets Information about connection state and updates the GUI.
  521. */
  522. private void updateConnectionInfText() {
  523. TextView ssidView = (TextView) findViewById(R.id.textViewSSIDValue);
  524. TextView bssidView = (TextView) findViewById(R.id.textViewBSSIDValue);
  525. TextView internalIPView = (TextView) findViewById(R.id.textViewInternalIPValue);
  526. TextView externalIPView = (TextView) findViewById(R.id.textViewExternalIPValue);
  527. externalIPView.setText("Loading...");
  528. //Update the connection information
  529. updateConnectionInfo();
  530. SetExternalIPTask async = new SetExternalIPTask();
  531. async.execute(new String[]{"http://ip2country.sourceforge.net/ip2c.php?format=JSON"});
  532. //Get connection information
  533. String ssid = sessionPref.getString(SSID, null);
  534. String bssid = sessionPref.getString(BSSID, null);
  535. String internalIP = sessionPref.getString(INTERNAL_IP, null);
  536. //Set text fields
  537. if (ssid != null)
  538. ssidView.setText(ssid);
  539. else
  540. ssidView.setText("-");
  541. if (bssid != null)
  542. bssidView.setText(bssid);
  543. else
  544. bssidView.setText("-");
  545. if (internalIP != null)
  546. internalIPView.setText(internalIP);
  547. else
  548. internalIPView.setText("-");
  549. }
  550. /**
  551. * Updates the connection info and saves them in the the SharedPreferences for session data.
  552. * @param context Needs a context to get system recourses.
  553. * @see MainActivity#SESSION_DATA
  554. */
  555. private void updateConnectionInfo() {
  556. SharedPreferences pref = context.getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
  557. Editor editor = pref.edit();
  558. editor.putString(MainActivity.SSID, HelperUtils.getSSID(context));
  559. editor.putString(MainActivity.BSSID, HelperUtils.getBSSID(context));
  560. editor.putString(MainActivity.INTERNAL_IP, HelperUtils.getInternalIP(context));
  561. editor.commit();
  562. }
  563. /**
  564. * Task to find out the external IP.
  565. * @author Lars Pandikow
  566. */
  567. private class SetExternalIPTask extends AsyncTask<String, Void, String>{
  568. @Override
  569. protected String doInBackground(String... url) {
  570. String ipAddress = null;
  571. try {
  572. HttpClient httpclient = new DefaultHttpClient();
  573. HttpGet httpget = new HttpGet(url[0]);
  574. HttpResponse response;
  575. response = httpclient.execute(httpget);
  576. HttpEntity entity = response.getEntity();
  577. entity.getContentLength();
  578. String str = EntityUtils.toString(entity);
  579. JSONObject json_data = new JSONObject(str);
  580. ipAddress = json_data.getString("ip");
  581. } catch (Exception e) {
  582. e.printStackTrace();
  583. }
  584. return ipAddress;
  585. }
  586. @Override
  587. protected void onPostExecute(String result){
  588. sessionEditor.putString(MainActivity.EXTERNAL_IP, result);
  589. sessionEditor.commit();
  590. TextView externalIPView = (TextView) findViewById(R.id.textViewExternalIPValue);
  591. if (result != null)
  592. externalIPView.setText(result);
  593. else
  594. externalIPView.setText("-");
  595. }
  596. };
  597. /*############# Help functions for animation ##################*/
  598. @Override
  599. public boolean onTouchEvent(MotionEvent event) {
  600. return gestureDetector.onTouchEvent(event);
  601. }
  602. /**
  603. * Initializes variables for screen animation
  604. */
  605. private void initViewAnimator() {
  606. viewAnimator = (ViewAnimator) findViewById(R.id.viewAnimator);
  607. gestureDetector = new GestureDetector(this, simpleOnGestureListener);
  608. animFlipInLR = AnimationUtils.loadAnimation(this,
  609. R.anim.in_left_to_right);
  610. animFlipOutLR = AnimationUtils.loadAnimation(this,
  611. R.anim.out_left_to_right);
  612. animFlipInRL = AnimationUtils.loadAnimation(this,
  613. R.anim.in_right_to_left);
  614. animFlipOutRL = AnimationUtils.loadAnimation(this,
  615. R.anim.out_right_to_left);
  616. }
  617. /**
  618. * Called when a swipe to the Left is registered.
  619. */
  620. private void swipeRightToLeft() {
  621. if (viewAnimator.getDisplayedChild() == 0) {
  622. viewAnimator.setInAnimation(animFlipInRL);
  623. viewAnimator.setOutAnimation(animFlipOutRL);
  624. viewAnimator.setDisplayedChild(1);
  625. }
  626. }
  627. /**
  628. * Called when a swipe to the Right is registered.
  629. */
  630. private void swipeLeftToRight() {
  631. if (viewAnimator.getDisplayedChild() == 1) {
  632. viewAnimator.setInAnimation(animFlipInLR);
  633. viewAnimator.setOutAnimation(animFlipOutLR);
  634. viewAnimator.setDisplayedChild(0);
  635. }
  636. }
  637. SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener() {
  638. @Override
  639. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
  640. float velocityY) {
  641. float sensitvity = 50;
  642. if ((e1.getX() - e2.getX()) > sensitvity) {
  643. swipeRightToLeft();
  644. } else if ((e2.getX() - e1.getX()) > sensitvity) {
  645. swipeLeftToRight();
  646. }
  647. return true;
  648. }
  649. };
  650. /**
  651. * Returns the context of the App.
  652. * @return context.
  653. */
  654. public static Context getContext() {
  655. return MainActivity.context;
  656. }
  657. }