johnfo
johnfo

Reputation: 1842

Obeying android screen orientation-lock prior to v4.3

Android 4.3 introduces userLandscape, userPortrait and fullUser values for setting screenOrientation - with corresponding SCREEN_ORIENTATION_XX constants. These basically say that if the screen orientation is locked then auto-screen rotation won't occur for the associated display, but it unlocked will work.

Simple question: how is this done prior to 4.3? Is it a matter of setting a constrained value and monitoring the global setting so that the screen orientation is changed at run-time to reflect that. Or is there a better way?

Upvotes: 2

Views: 486

Answers (1)

TechCor
TechCor

Reputation: 21

I was having trouble finding an answer to this as well. SENSOR_LANDSCAPE appears to ignore the device orientation lock, so here is what I ended up using.

// Is the device set to allow auto-rotation?
if (Settings.System.getInt(getContentResolver(),Settings.System.ACCELEROMETER_ROTATION, 0) == 1)
{
    // Unlock rotation
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
else
{
    // Lock rotation
    setRequestedOrientation(getScreenOrientation());
}

Getting the current screen orientation is a bit more involved. I used one of the answers provided here:

How do I get the CURRENT orientation (ActivityInfo.SCREEN_ORIENTATION_*) of an Android device?

private int getScreenOrientation()
{
    WindowManager winMan = (WindowManager)getSystemService(Activity.WINDOW_SERVICE);
    int rotation = winMan.getDefaultDisplay().getRotation();
    DisplayMetrics dm = new DisplayMetrics();
    winMan.getDefaultDisplay().getMetrics(dm);
    int width = dm.widthPixels;
    int height = dm.heightPixels;
    int orientation;
    // if the device's natural orientation is portrait:
    if ((rotation == Surface.ROTATION_0
            || rotation == Surface.ROTATION_180) && height > width ||
        (rotation == Surface.ROTATION_90
            || rotation == Surface.ROTATION_270) && width > height)
    {
        switch(rotation)
        {
            case Surface.ROTATION_0:
                orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                break;
            case Surface.ROTATION_90:
                orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                break;
            case Surface.ROTATION_180:
                orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
                break;
            case Surface.ROTATION_270:
                orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
                break;
            default:
                Log.e(TAG, "Unknown screen orientation. Defaulting to portrait.");
                orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                break;
        }
    }
    // if the device's natural orientation is landscape or if the device
    // is square:
    else
    {
        switch(rotation)
        {
            case Surface.ROTATION_0:
                orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                break;
            case Surface.ROTATION_90:
                orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                break;
            case Surface.ROTATION_180:
                orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
                break;
            case Surface.ROTATION_270:
                orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
                break;
            default:
                Log.e(TAG, "Unknown screen orientation. Defaulting to landscape.");
                orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                break;              
        }
    }

    return orientation;
}

Upvotes: 2

Related Questions