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