MainActivity.java 20 KB

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