Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3da54b5c authored by eray orçunus's avatar eray orçunus Committed by Michael Bestas
Browse files

Rotation related corrections



- There are some conditions deleted while placing rotation angles code, I added them.

- Rotation lock was screwed up since CM 12. Fixed it by fetching allowed rotations from CM's
allowed rotations setting.

- Also, a CAF commit had killed rotation lock ability.

[port to 15.1]:
 - ACCELEROMETER_ROTATION_ANGLES moved to LineageSDK
 - Slight change of the WindowManager API

[port to 16.0]:
 - adjust context
 - ACCELEROMETER_ROTATION_ANGLES moved to Settings
 - Use the configstore API

Change-Id: I8f1b468249c68e7b6514d1a96bdb3fc638af84fd
Signed-off-by: default avatareray orçunus <erayorcunus@gmail.com>
(cherry picked from commit a62720d5)
parent 9248ee4f
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -669,6 +669,7 @@ java_library {
        "framework-protos",
        "android.hidl.base-V1.0-java",
        "android.hardware.cas-V1.0-java",
        "android.hardware.configstore-V1.1-java",
        "android.hardware.contexthub-V1.0-java",
        "android.hardware.health-V1.0-java-constants",
        "android.hardware.thermal-V1.0-java-constants",
+62 −5
Original line number Diff line number Diff line
@@ -21,6 +21,9 @@ import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.database.ContentObserver;
import android.graphics.Point;
import android.hardware.configstore.V1_1.DisplayOrientation;
import android.hardware.configstore.V1_1.ISurfaceFlingerConfigs;
import android.hardware.configstore.V1_1.OptionalDisplayOrientation;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Handler;
@@ -42,7 +45,7 @@ public final class RotationPolicy {
    private static final String TAG = "RotationPolicy";
    private static final int CURRENT_ROTATION = -1;

    public static final int NATURAL_ROTATION = Surface.ROTATION_0;
    public static final int NATURAL_ROTATION = getNaturalRotation();

    private RotationPolicy() {
    }
@@ -73,7 +76,7 @@ public final class RotationPolicy {
     * otherwise Configuration.ORIENTATION_UNDEFINED if any orientation is lockable.
     */
    public static int getRotationLockOrientation(Context context) {
        if (!areAllRotationsAllowed(context)) {
        if (!isCurrentRotationAllowed(context)) {
            final Point size = new Point();
            final IWindowManager wm = WindowManagerGlobal.getWindowManagerService();
            try {
@@ -109,7 +112,8 @@ public final class RotationPolicy {
     * Enables or disables rotation lock from the system UI toggle.
     */
    public static void setRotationLock(Context context, final boolean enabled) {
        final int rotation = areAllRotationsAllowed(context) ? CURRENT_ROTATION : NATURAL_ROTATION;
        final int rotation = isCurrentRotationAllowed(context)
                ? CURRENT_ROTATION : NATURAL_ROTATION;
        setRotationLockAtAngle(context, enabled, rotation);
    }

@@ -138,8 +142,40 @@ public final class RotationPolicy {
        setRotationLock(enabled, NATURAL_ROTATION);
    }

    private static boolean areAllRotationsAllowed(Context context) {
        return context.getResources().getBoolean(R.bool.config_allowAllRotations);
    public static boolean isRotationAllowed(int rotation,
            int userRotationAngles, boolean allowAllRotations) {
        if (userRotationAngles < 0) {
            // Not set by user so use these defaults
            userRotationAngles = allowAllRotations ?
                    (1 | 2 | 4 | 8) : // All angles
                    (1 | 2 | 8); // All except 180
        }
        switch (rotation) {
            case Surface.ROTATION_0:
                return (userRotationAngles & 1) != 0;
            case Surface.ROTATION_90:
                return (userRotationAngles & 2) != 0;
            case Surface.ROTATION_180:
                return (userRotationAngles & 4) != 0;
            case Surface.ROTATION_270:
                return (userRotationAngles & 8) != 0;
        }
        return false;
    }

    private static boolean isCurrentRotationAllowed(Context context) {
        int userRotationAngles = Settings.System.getInt(context.getContentResolver(),
                Settings.System.ACCELEROMETER_ROTATION_ANGLES, -1);
        boolean allowAllRotations = context.getResources().getBoolean(
                com.android.internal.R.bool.config_allowAllRotations);
        final IWindowManager wm = WindowManagerGlobal.getWindowManagerService();
        try {
            return isRotationAllowed(wm.getDefaultDisplayRotation(), userRotationAngles,
                    allowAllRotations);
        } catch (RemoteException exc) {
            Log.w(TAG, "Unable to getWindowManagerService.getDefaultDisplayRotation()");
        }
        return false;
    }

    private static void setRotationLock(final boolean enabled, final int rotation) {
@@ -190,6 +226,27 @@ public final class RotationPolicy {
        context.getContentResolver().unregisterContentObserver(listener.mObserver);
    }

    public static int getNaturalRotation() {
        OptionalDisplayOrientation orientation;

        try {
            orientation =
                    ISurfaceFlingerConfigs.getService().primaryDisplayOrientation();
            switch (orientation.value) {
                case DisplayOrientation.ORIENTATION_90:
                    return Surface.ROTATION_90;
                case DisplayOrientation.ORIENTATION_180:
                    return Surface.ROTATION_180;
                case DisplayOrientation.ORIENTATION_270:
                    return Surface.ROTATION_270;
            }
        } catch (RemoteException e) {
            // do nothing
        }

        return Surface.ROTATION_0;
    }

    /**
     * Listener that is invoked whenever a change occurs that might affect the rotation policy.
     */
+5 −20
Original line number Diff line number Diff line
@@ -288,6 +288,7 @@ import com.android.internal.statusbar.IStatusBarService;
import com.android.internal.util.ArrayUtils;
import com.android.internal.util.ScreenshotHelper;
import com.android.internal.util.ScreenShapeHelper;
import com.android.internal.view.RotationPolicy;
import com.android.internal.widget.PointerLocationView;
import com.android.server.GestureLauncherService;
import com.android.server.LocalServices;
@@ -8073,27 +8074,11 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                    mAllowAllRotations = mContext.getResources().getBoolean(
                            com.android.internal.R.bool.config_allowAllRotations) ? 1 : 0;
                }
                // Rotation setting bitmask (1=0; 2=90; 4=180; 8=270)
                boolean allowed = true;
                if (mUserRotationAngles < 0) {
                    // Not set by user so use these defaults
                    mUserRotationAngles = mAllowAllRotations == 1 ?
                            (1 | 2 | 4 | 8) : // All angles
                                (1 | 2 | 8); // All except 180
                }
                switch (sensorRotation) {
                    case Surface.ROTATION_0:
                        allowed = (mUserRotationAngles & 1) != 0;
                        break;
                    case Surface.ROTATION_90:
                        allowed = (mUserRotationAngles & 2) != 0;
                        break;
                    case Surface.ROTATION_180:
                        allowed = (mUserRotationAngles & 4) != 0;
                        break;
                    case Surface.ROTATION_270:
                        allowed = (mUserRotationAngles & 8) != 0;
                        break;
                if (orientation != ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR
                        && orientation != ActivityInfo.SCREEN_ORIENTATION_FULL_USER) {
                    allowed = RotationPolicy.isRotationAllowed(sensorRotation,
                            mUserRotationAngles, mAllowAllRotations != 0);
                }
                if (allowed) {
                    preferredRotation = sensorRotation;