Example usage for android.view OrientationEventListener enable

List of usage examples for android.view OrientationEventListener enable

Introduction

In this page you can find the example usage for android.view OrientationEventListener enable.

Prototype

public void enable() 

Source Link

Document

Enables the OrientationEventListener so it will monitor the sensor and call #onOrientationChanged when the device orientation changes.

Usage

From source file:org.openremote.android.console.GroupActivity.java

/**
 * Inits a orientation listener, set request orientation be sensor when the current screen's orientation equals device orientation.
 *//*w ww  .  jav  a  2s .com*/
private void initOrientationListener() {
    OrientationEventListener orientationListener = new OrientationEventListener(this) {
        @Override
        public void onOrientationChanged(int orientation) {
            if (currentScreen == null) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
                return;
            }
            if (orientation > 315 || orientation < 45 || (orientation > 135 && orientation < 225)) {
                // portrait
                if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
                        && !currentScreen.isLandscape() && currentScreen.getInverseScreenId() > 0) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
                } else if (!currentScreen.isLandscape() && currentScreen.getInverseScreenId() == 0) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                }
            } else if ((orientation > 225 && orientation < 315) || (orientation > 45 && orientation < 135)) {
                // landscape
                if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
                        && currentScreen.isLandscape() && currentScreen.getInverseScreenId() > 0) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
                } else if (currentScreen.isLandscape() && currentScreen.getInverseScreenId() == 0) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                }
            }
        }
    };
    orientationListener.enable();
}

From source file:com.intel.xdk.device.Device.java

@Override
public void initialize(CordovaInterface cordova, final CordovaWebView webView) {
    super.initialize(cordova, webView);

    this.activity = cordova.getActivity();
    this.webView = webView;

    //remote site support
    remoteLayout = new AbsoluteLayout(activity);
    remoteLayout.setBackgroundColor(Color.BLACK);
    //hide the remote site display until needed
    remoteLayout.setVisibility(View.GONE);
    //create the close button
    remoteClose = new ImageButton(activity);
    remoteClose.setBackgroundColor(Color.TRANSPARENT);
    Drawable remoteCloseImage = null;/*from   w  w  w  .  j  av  a  2  s . c o  m*/
    remoteCloseImage = activity.getResources().getDrawable(
            activity.getResources().getIdentifier("remote_close", "drawable", activity.getPackageName()));

    File remoteCloseImageFile = new File(activity.getFilesDir(), "_intelxdk/remote_close.png");
    if (remoteCloseImageFile.exists()) {
        remoteCloseImage = (Drawable.createFromPath(remoteCloseImageFile.getAbsolutePath()));
    } else {
        remoteCloseImage = (activity.getResources().getDrawable(
                activity.getResources().getIdentifier("remote_close", "drawable", activity.getPackageName())));
    }

    //set the button image
    //remoteClose.setImageDrawable(remoteCloseImage);
    remoteClose.setBackgroundDrawable(remoteCloseImage);
    //set up the button click action
    remoteClose.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            closeRemoteSite();
        }
    });
    //add the close button
    remoteLayout.addView(remoteClose);

    final ViewGroup parent = (ViewGroup) webView.getEngine().getView().getParent();
    activity.runOnUiThread(new Runnable() {
        public void run() {
            if (parent != null) {
                //add layout to activity root layout
                parent.addView(remoteLayout, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER));

            }
        }
    });

    //Initialize the orientation.
    lastOrientation = "unknown";

    //Listen to the orientation change.
    OrientationEventListener listener = new OrientationEventListener(activity) {

        @Override
        public void onOrientationChanged(int orientation) {
            //Log.d("orientation","orientation: " + orientation);

            String currentOrientation = "unknown";
            boolean orientationChanged = false;
            //int displayOrientation = 0;

            if (orientation > 345 || orientation < 15) {
                currentOrientation = "portrait";
                displayOrientation = 0;
            } else if (orientation > 75 && orientation < 105) {
                currentOrientation = "landscape";
                displayOrientation = 90;
            } else if (orientation > 165 && orientation < 195) {
                currentOrientation = "portrait";
                displayOrientation = 180;
            } else if (orientation > 255 && orientation < 285) {
                currentOrientation = "landscape";
                displayOrientation = -90;
            }

            if (currentOrientation.equals("unknown")) {
                currentOrientation = lastOrientation;
            }

            if (!currentOrientation.equals(lastOrientation)) {
                orientationChanged = true;
                Log.d("orientation", "Orientation changes from " + lastOrientation + " to " + currentOrientation
                        + ", current orientation: " + orientation + ".");
            }

            if (orientationChanged) {
                String js = "javascript:try{intel.xdk.device.orientation='" + displayOrientation
                        + "';}catch(e){}var e = document.createEvent('Events');e.initEvent('intel.xdk.device.orientation.change', true, true);e.success=true;e.orientation='"
                        + displayOrientation + "';document.dispatchEvent(e);";
                injectJS(js);
            }

            lastOrientation = currentOrientation;
        }

    };

    listener.enable();
    registerScreenStatusReceiver();

    //cache references to methods for use in injectJS
    try {
        evaluateJavascript = webView.getClass().getMethod("evaluateJavascript", String.class,
                ValueCallback.class);
    } catch (Exception e) {
    }

    try {
        sendJavascript = webView.getClass().getMethod("sendJavascript", String.class);
    } catch (Exception e) {
    }

    emptyVC = new ValueCallback<String>() {
        @Override
        public void onReceiveValue(String s) {
        }
    };

}

From source file:com.aimfire.demo.CamcorderActivity.java

public void setCameraRotation() {
    if (BuildConfig.DEBUG)
        Log.d(TAG, "setCameraRotation");
    OrientationEventListener listener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {
        @Override/* www  . ja v a  2 s.c  om*/
        public void onOrientationChanged(int orientation) {
            if (orientation == ORIENTATION_UNKNOWN)
                return;

            Camera.CameraInfo info = new Camera.CameraInfo();
            Camera.getCameraInfo(mCameraId, info);
            orientation = (orientation + 45) / 90 * 90;
            int rotation = 0;
            if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                rotation = (info.orientation - orientation + 360) % 360;
            } else { // back-facing camera
                rotation = (info.orientation + orientation) % 360;
            }
            Camera.Parameters parms = mCamera.getParameters();
            parms.setRotation(rotation);
            mCamera.setParameters(parms);
        }
    };

    if (listener.canDetectOrientation()) {
        listener.enable();
    }
}