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

Unverified Commit 5a874090 authored by Ricardo Cerqueira's avatar Ricardo Cerqueira Committed by Michael Bestas
Browse files

Configurable 0, 90, 180 and 270 degree rotation



Co-authored-by: default avatareray orçunus <erayorcunus@gmail.com>
Co-authored-by: default avatarJonas Larsson <jonas@hallerud.se>
Co-authored-by: default avatarMichael Bestas <mkbestas@lineageos.org>
Co-authored-by: default avatarTim Schumacher <timschumi@gmx.de>
Co-authored-by: default avatarTom Pratt <tom.pratt@volvocars.com>
Change-Id: Ia7bf8cb64258e1d602230a8f9ea227d3b56a4dab
parent b4527e8c
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -263,6 +263,7 @@ java_library {

        "com.android.sysprop.localization",
        "PlatformProperties",
        "SurfaceFlingerProperties",
    ],
    sdk_version: "core_platform",
    installable: false,
+13 −0
Original line number Diff line number Diff line
@@ -5890,6 +5890,19 @@ public final class Settings {
        @Readable
        public static final String ACCELEROMETER_ROTATION = "accelerometer_rotation";
        /**
         * Control the type of rotation which can be performed using the accelerometer
         * if ACCELEROMETER_ROTATION is enabled.
         * Value is a bitwise combination of
         * 1 = 0 degrees (portrait)
         * 2 = 90 degrees (left)
         * 4 = 180 degrees (inverted portrait)
         * 8 = 270 degrees (right)
         * Setting to 0 is effectively orientation lock
         * @hide
         */
        public static final String ACCELEROMETER_ROTATION_ANGLES = "accelerometer_rotation_angles";
        /**
         * Default screen rotation when no other policy applies.
         * When {@link #ACCELEROMETER_ROTATION} is zero and no on-screen Activity expresses a
+72 −7
Original line number Diff line number Diff line
@@ -26,6 +26,8 @@ import android.os.Handler;
import android.os.RemoteException;
import android.os.UserHandle;
import android.provider.Settings;
import android.sysprop.SurfaceFlingerProperties;
import android.sysprop.SurfaceFlingerProperties.primary_display_orientation_values;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.IWindowManager;
@@ -34,6 +36,8 @@ import android.view.WindowManagerGlobal;

import com.android.internal.R;

import java.util.Optional;

/**
 * Provides helper functions for configuring the display rotation policy.
 */
@@ -41,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;
    private static int sNaturalRotation = -1;

    private RotationPolicy() {
    }
@@ -72,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)) {
            return Configuration.ORIENTATION_UNDEFINED;
        }
        final DisplayMetrics metrics = context.getResources().getDisplayMetrics();
@@ -106,9 +110,9 @@ public final class RotationPolicy {
     * Enables or disables rotation lock from the system UI toggle.
     */
    public static void setRotationLock(Context context, final boolean enabled, String caller) {
        final int rotation = areAllRotationsAllowed(context)
        final int rotation = isCurrentRotationAllowed(context)
                || useCurrentRotationOnRotationLockChange(context) ? CURRENT_ROTATION
                : NATURAL_ROTATION;
                : getNaturalRotation();
        setRotationLockAtAngle(context, enabled, rotation, caller);
    }

@@ -135,11 +139,43 @@ public final class RotationPolicy {
                Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, enabled ? 1 : 0,
                        UserHandle.USER_CURRENT);

        setRotationLock(enabled, NATURAL_ROTATION, caller);
        setRotationLock(enabled, getNaturalRotation(), caller);
    }

    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 areAllRotationsAllowed(Context context) {
        return context.getResources().getBoolean(R.bool.config_allowAllRotations);
    private static boolean isCurrentRotationAllowed(Context context) {
        int userRotationAngles = Settings.System.getIntForUser(context.getContentResolver(),
                Settings.System.ACCELEROMETER_ROTATION_ANGLES, -1, UserHandle.USER_CURRENT);
        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 boolean useCurrentRotationOnRotationLockChange(Context context) {
@@ -196,6 +232,35 @@ public final class RotationPolicy {
        context.getContentResolver().unregisterContentObserver(listener.mObserver);
    }

    public static int getNaturalRotation() {
        if (sNaturalRotation == -1) {
            sNaturalRotation = getNaturalRotationConfig();
        }
        return sNaturalRotation;
    }

    private static int getNaturalRotationConfig() {
        primary_display_orientation_values orientation =
                primary_display_orientation_values.ORIENTATION_0;
        Optional<primary_display_orientation_values> primaryDisplayOrientation =
                SurfaceFlingerProperties.primary_display_orientation();
        if (primaryDisplayOrientation.isPresent()) {
            orientation = primaryDisplayOrientation.get();
        }

        if (orientation == primary_display_orientation_values.ORIENTATION_90) {
            return Surface.ROTATION_90;
        }
        if (orientation == primary_display_orientation_values.ORIENTATION_180) {
            return Surface.ROTATION_180;
        }
        if (orientation == primary_display_orientation_values.ORIENTATION_270) {
            return Surface.ROTATION_270;
        }

        return Surface.ROTATION_0;
    }

    /**
     * Listener that is invoked whenever a change occurs that might affect the rotation policy.
     */
+1 −0
Original line number Diff line number Diff line
@@ -175,6 +175,7 @@ public class SystemSettingsValidators {
                System.TIME_12_24, new DiscreteValueValidator(new String[] {"12", "24", null}));
        VALIDATORS.put(System.SETUP_WIZARD_HAS_RUN, BOOLEAN_VALIDATOR);
        VALIDATORS.put(System.ACCELEROMETER_ROTATION, BOOLEAN_VALIDATOR);
        VALIDATORS.put(System.ACCELEROMETER_ROTATION_ANGLES, NON_NEGATIVE_INTEGER_VALIDATOR);
        VALIDATORS.put(System.USER_ROTATION, new InclusiveIntegerRangeValidator(0, 3));
        VALIDATORS.put(System.DTMF_TONE_WHEN_DIALING, BOOLEAN_VALIDATOR);
        VALIDATORS.put(System.SOUND_EFFECTS_ENABLED, BOOLEAN_VALIDATOR);
+1 −2
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ package com.android.systemui.shared.rotation;
import static android.content.pm.PackageManager.FEATURE_PC;
import static android.view.Display.DEFAULT_DISPLAY;

import static com.android.internal.view.RotationPolicy.NATURAL_ROTATION;
import static com.android.systemui.shared.system.QuickStepContract.isGesturalMode;

import android.animation.Animator;
@@ -626,7 +625,7 @@ public class RotationButtonController {
        }
        // Only override user prefs when returning to the natural rotation (normally portrait).
        // Don't let apps that force landscape or 180 alter user lock.
        return rotation == NATURAL_ROTATION;
        return rotation == RotationPolicy.getNaturalRotation();
    }

    private void rescheduleRotationTimeout(final boolean reasonHover) {
Loading