1

My app is restricted to Portrait , still I wanted to know about my Activity's orientation changes

I need this to detect whether my camera has taken picture in Portrait mode or Landscape mode

I have used registerActivityLifecycleCallback in MyApplication class to restrict the app to Portrait , still I wanted to know whether user has rotated the screen when he is in Camera Activity

CameraActivity :

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
mOrientationEventListener=new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {
            @Override
            public void onOrientationChanged(int orientation) {
                int lastOrientation = mOrientation;

                Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();

                if (display.getOrientation() == Surface.ROTATION_0) {   // landscape oriented devices

                    CAMERA_ORIENTATION = 1;
                   // methods.showToast(CameraActivity.this,"ORIENTATION_LANDSCAPE");
                    Log.e(TAG, "onOrientationChanged: ORIENTATION_LANDSCAPE ");
                    Log.e(TAG, "onOrientationChanged: CAMERA_ORIENTATION  = "+CAMERA_ORIENTATION);

                    if (orientation >= 315 || orientation < 45) {
                        if (mOrientation != ORIENTATION_LANDSCAPE_NORMAL) {
                            mOrientation = ORIENTATION_LANDSCAPE_NORMAL;
                        }
                    } else if (orientation < 315 && orientation >= 225) {
                        if (mOrientation != ORIENTATION_PORTRAIT_INVERTED) {
                            mOrientation = ORIENTATION_PORTRAIT_INVERTED;
                        }
                    } else if (orientation < 225 && orientation >= 135) {
                        if (mOrientation != ORIENTATION_LANDSCAPE_INVERTED) {
                            mOrientation = ORIENTATION_LANDSCAPE_INVERTED;
                        }
                    } else if (orientation <135 && orientation > 45) {
                        if (mOrientation != ORIENTATION_PORTRAIT_NORMAL) {
                            mOrientation = ORIENTATION_PORTRAIT_NORMAL;
                        }
                    }
                } else {  // portrait oriented devices

                    CAMERA_ORIENTATION = 0;
                    //methods.showToast(CameraActivity.this, "ORIENTATION_PORTRAIT");
                    Log.e(TAG, "onOrientationChanged: ORIENTATION_PORTRAIT ");
                    Log.e(TAG, "onOrientationChanged: CAMERA_ORIENTATION  = "+CAMERA_ORIENTATION);

                    if (orientation >= 315 || orientation < 45) {
                        if (mOrientation != ORIENTATION_PORTRAIT_NORMAL) {
                            mOrientation = ORIENTATION_PORTRAIT_NORMAL;
                        }
                    } else if (orientation < 315 && orientation >= 225) {
                        if (mOrientation != ORIENTATION_LANDSCAPE_NORMAL) {
                            mOrientation = ORIENTATION_LANDSCAPE_NORMAL;
                        }
                    } else if (orientation < 225 && orientation >= 135) {
                        if (mOrientation != ORIENTATION_PORTRAIT_INVERTED) {
                            mOrientation = ORIENTATION_PORTRAIT_INVERTED;
                        }
                    } else if (orientation <135 && orientation > 45) {
                        if (mOrientation != ORIENTATION_LANDSCAPE_INVERTED) {
                            mOrientation = ORIENTATION_LANDSCAPE_INVERTED;
                        }
                    }
                }
            }
        };

        if (mOrientationEventListener.canDetectOrientation()){
            Toast.makeText(this, "Can DetectOrientation", Toast.LENGTH_LONG).show();
            mOrientationEventListener.enable();
        }
        else{
            Toast.makeText(this, "Can't DetectOrientation", Toast.LENGTH_LONG).show();
        }
karthik kolanji
  • 2,044
  • 5
  • 20
  • 56

1 Answers1

0

You can use SensorManager. getOrientation to get the orientation of the device in 3D space, and then translate that into landscape or portrait.

Alternatively, if you need updates you can register for the gravity sensor and see whether gravity is measuring on the x or y axis. That won't work if the user has it flat on a tabletop, but neither will the other method.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127