Example usage for android.view MotionEvent setAction

List of usage examples for android.view MotionEvent setAction

Introduction

In this page you can find the example usage for android.view MotionEvent setAction.

Prototype

public final void setAction(int action) 

Source Link

Document

Sets this event's action.

Usage

From source file:jmri.enginedriver.throttle.java

private void gestureEnd(MotionEvent event) {
    // Log.d("Engine_Driver", "gestureEnd action " + event.getAction() + " inProgress? " + gestureInProgress);
    mainapp.throttle_msg_handler.removeCallbacks(gestureStopped);
    if (gestureInProgress) {
        float deltaX = (event.getX() - gestureStartX);
        float deltaY = (event.getY() - gestureStartY);
        float absDeltaX = Math.abs(deltaX);
        float absDeltaY = Math.abs(deltaY);
        if ((absDeltaX > threaded_application.min_fling_distance)
                || (absDeltaY > threaded_application.min_fling_distance)) {
            // valid gesture. Change the event action to CANCEL so that it isn't processed by any control below the gesture overlay
            event.setAction(MotionEvent.ACTION_CANCEL);
            // process swipe in the direction with the largest change
            if (absDeltaX >= absDeltaY) {
                // swipe left/right
                if (!isScreenLocked) {
                    boolean swipeTurnouts = prefs.getBoolean("swipe_through_turnouts_preference",
                            getResources().getBoolean(R.bool.prefSwipeThroughTurnoutsDefaultValue));
                    swipeTurnouts = swipeTurnouts && mainapp.isTurnoutControlAllowed(); //also check the allowed flag
                    boolean swipeRoutes = prefs.getBoolean("swipe_through_routes_preference",
                            getResources().getBoolean(R.bool.prefSwipeThroughRoutesDefaultValue));
                    swipeRoutes = swipeRoutes && mainapp.isRouteControlAllowed(); //also check the allowed flag
                    // if swiping (to Turnouts or Routes screen) is enabled, process the swipe
                    if (swipeTurnouts || swipeRoutes) {
                        navigatingAway = true;
                        // left to right swipe goes to turnouts if enabled in prefs
                        if (deltaX > 0.0) {
                            // swipe left
                            Intent in;//from  w  w w .java2 s. c om
                            if (swipeTurnouts) {
                                in = new Intent().setClass(this, turnouts.class);
                            } else {
                                in = new Intent().setClass(this, routes.class);
                            }
                            startActivity(in);
                            connection_activity.overridePendingTransition(this, R.anim.push_right_in,
                                    R.anim.push_right_out);
                        }
                        // right to left swipe goes to routes if enabled in prefs
                        else {
                            // swipe right
                            Intent in;
                            if (swipeRoutes) {
                                in = new Intent().setClass(this, routes.class);
                            } else {
                                in = new Intent().setClass(this, turnouts.class);
                            }
                            startActivity(in);
                            connection_activity.overridePendingTransition(this, R.anim.push_left_in,
                                    R.anim.push_left_out);
                        }
                    }
                }
            } else {
                // swipe up/down
                if (deltaY > 0.0) {
                    // swipe down
                    if (!isScreenLocked) {
                        // enter or exit immersive mode only if the preference is set
                        if (immersiveModeIsOn) {
                            setImmersiveModeOff(webView);
                            Toast.makeText(getApplicationContext(),
                                    getApplicationContext().getResources()
                                            .getString(R.string.toastImmersiveModeDisabled),
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            setImmersiveModeOn(webView);
                        }
                    }
                } else {
                    // swipe up
                    switch (prefSwipeUpOption) {
                    case SWIPE_UP_OPTION_WEB:
                        showHideWebView(getApplicationContext().getResources()
                                .getString(R.string.toastSwipeUpViewHidden));
                        break;
                    case SWIPE_UP_OPTION_LOCK:
                        setRestoreScreenLockDim(getApplicationContext().getResources()
                                .getString(R.string.toastSwipeUpScreenLocked));
                        break;
                    case SWIPE_UP_OPTION_DIM:
                        setRestoreScreenDim(getApplicationContext().getResources()
                                .getString(R.string.toastSwipeUpScreenDimmed));
                        break;
                    case SWIPE_UP_OPTION_IMMERSIVE:
                        if (immersiveModeIsOn) {
                            setImmersiveModeOff(webView);
                            Toast.makeText(getApplicationContext(),
                                    getApplicationContext().getResources()
                                            .getString(R.string.toastImmersiveModeDisabled),
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            setImmersiveModeOn(webView);
                        }
                        break;
                    }
                }
            }
        } else {
            // gesture was not long enough
            gestureFailed(event);
        }
    }
}