Search code examples
androidconfigurationandroid-cameraandroid-sensorsandroid-mediarecorder

What gets called with sensorLandscape


I have an application that I only want to run in landscape mode (e.g. landscape or reverse landscape) so I am using sensorLandscape in the manifest.

My problem is here: I also have a video recorder working (eg using the camera). Now I set the display orientation when i prepare the camera, but the problem is that if I change orientation that change stays (preview is upside down). When using sensor landscape how do I know when the UI is changed. I have tried using onConfigurationCanged() but unfortunately I need to have the app run on both pre honeycomb devices (mainly gingerbread) and post honeycomb devices (jelly bean).

Since in order to properly have onConfigurationChanged called i need to set target api to like 8, but sensor landscape is only available 9+. I need to set it to 8 because in the older api's you need to add screenSize to your "configChanges" otherwise it will not be called. So these calls have alot of incompatabilities between eachother.

Now that the background is done and you guys know what I have tried already. My question is How can I find out when my orientation changes (callback, or something else) so that I can change my camera display orientation?

Thank you in advance.


Solution

  • I have figured out how to get this done. It is a round about way but here it is: Set up a sensor manager and an oreitnation senosr

    public class ActivityRecordVideo extends ActivityBase implements SensorEventListener
    {
    
    private SensorManager mSensorManager;
    private Sensor mOrientation;
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mOrientation = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
    }
    
    public void onAccuracyChanged(Sensor arg0, int arg1) {
        // TODO Auto-generated method stub
    
    }
    
    public void onSensorChanged(SensorEvent event) {
        int rotation = getWindowManager().getDefaultDisplay().getRotation();
    
        if(camId != -1 && currentDisplayRotation != rotation)
        {
            if(!isRecording)
                setCameraOrientation(camId, cameraRecorder);
        }
    
    }
    

    I also set the listeners in onResume() and remove them in onPause().

    This allows for a camera orientation to flip with everything else (e.g. when in reverse landscape all views are in reverse landscape along with the camera preview)

    Also decided to show setCameraOrientation code, it is a stipped down version of the android developers code

    private void setCameraOrientation(int camId, Camera camera)
    {
        CameraInfo info = new CameraInfo();
        Camera.getCameraInfo(camId, info);
    
        currentDisplayRotation = getWindowManager().getDefaultDisplay().getRotation();
        int degrees = 0;
        switch(currentDisplayRotation)
        {
        case Surface.ROTATION_0: degrees = 0; break;
        case Surface.ROTATION_90: degrees = 90; break;
        case Surface.ROTATION_180: degrees = 180; break;
        case Surface.ROTATION_270: degrees = 270; break;
        }
    
        int result;
        result = (info.orientation + degrees) % 360;
        result = (360 - result) % 360;  //compensate for mirror effect
    
        if(Build.VERSION.SDK_INT < 14)
            camera.stopPreview();
        camera.setDisplayOrientation(result);
        if(Build.VERSION.SDK_INT < 14)
            camera.startPreview();
    }
    

    Hopefully this will help others