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

Commit aa262423 authored by Ricardo Cerqueira's avatar Ricardo Cerqueira Committed by Bruno Martins
Browse files

Configurable 0, 90, 180 and 270 degree rotation



Change-Id: Ia1859c51d71ef9d01cec2d13c6468ed89c6ac53e
Contributor: jonasl

- Add system setting

Author: Tim Schumacher <timschumi@gmx.de>
Date:   Wed Nov 28 21:44:18 2018 +0100

    Add back ACCELEROMETER_ROTATION_ANGLES and update references

    This needed to move back into fw/b, because it needs to be
    accessed from inside the RotationPolicy. Previously
    (when this constant and Setting were located in lineage-sdk),
    the settings for the display rotation had no effect, because we
    couldn't query the correct set of settings.

    Change-Id: Icce249925a578c328db3884e5d332b20d6e7db6c
    Fixes: BUGBASH-2042

Author: eray orçunus <erayorcunus@gmail.com>
Date:   Mon Jun 22 22:47:40 2015 +0300

    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)

Author: Tim Schumacher <timschumi@gmx.de>
Date:   Thu May 2 19:48:39 2019 +0200

    RotationPolicy: Don't crash if configstore 1.1 isn't available

    Change-Id: I77301ec8c72393daa0003ca310eee07b767d4e69

Change-Id: Ia7bf8cb64258e1d602230a8f9ea227d3b56a4dab
parent a86b7b5a
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -756,6 +756,7 @@ java_defaults {
        "android.hidl.base-V1.0-java",
        "android.hardware.cas-V1.1-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.radio-V1.0-java",
+19 −0
Original line number Diff line number Diff line
@@ -4058,6 +4058,23 @@ public final class Settings {
        @Deprecated
        public static final String ANIMATOR_DURATION_SCALE = Global.ANIMATOR_DURATION_SCALE;
        /**
         * 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";
        /** @hide */
        public static final Validator ACCELEROMETER_ROTATION_ANGLES_VALIDATOR =
                NON_NEGATIVE_INTEGER_VALIDATOR;
        /**
         * Control whether the accelerometer will be used to change screen
         * orientation.  If 0, it will not be used unless explicitly requested
@@ -4755,6 +4772,8 @@ public final class Settings {
            VALIDATORS.put(WIFI_STATIC_DNS2, WIFI_STATIC_DNS2_VALIDATOR);
            VALIDATORS.put(SHOW_BATTERY_PERCENT, SHOW_BATTERY_PERCENT_VALIDATOR);
            VALIDATORS.put(NOTIFICATION_LIGHT_PULSE, BOOLEAN_VALIDATOR);
            VALIDATORS.put(ACCELEROMETER_ROTATION_ANGLES,
                    ACCELEROMETER_ROTATION_ANGLES_VALIDATOR);
        }
        /**
+85 −6
Original line number Diff line number Diff line
@@ -21,10 +21,14 @@ 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;
import android.os.RemoteException;
import android.os.SystemProperties;
import android.os.UserHandle;
import android.provider.Settings;
import android.util.Log;
@@ -35,6 +39,8 @@ import android.view.WindowManagerGlobal;

import com.android.internal.R;

import java.util.NoSuchElementException;

/**
 * Provides helper functions for configuring the display rotation policy.
 */
@@ -42,7 +48,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() {
    }
@@ -73,7 +79,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 {
@@ -113,7 +119,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 : getNaturalRotation();
        setRotationLockAtAngle(context, enabled, rotation);
    }

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

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

    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.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) {
@@ -194,6 +233,46 @@ public final class RotationPolicy {
        context.getContentResolver().unregisterContentObserver(listener.mObserver);
    }

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

    private static int getNaturalRotationConfig() {
        String primaryDisplayOrientation =
                SystemProperties.get("ro.surface_flinger.primary_display_orientation");
        if (primaryDisplayOrientation == "ORIENTATION_90") {
            return Surface.ROTATION_90;
        }
        if (primaryDisplayOrientation == "ORIENTATION_180") {
            return Surface.ROTATION_180;
        }
        if (primaryDisplayOrientation == "ORIENTATION_270") {
            return Surface.ROTATION_270;
        }

        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 | NoSuchElementException e) {
            // do nothing
        }

        return Surface.ROTATION_0;
    }

    /**
     * Listener that is invoked whenever a change occurs that might affect the rotation policy.
     */
+2 −3
Original line number Diff line number Diff line
@@ -16,8 +16,6 @@

package com.android.systemui.statusbar.phone;

import static com.android.internal.view.RotationPolicy.NATURAL_ROTATION;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
@@ -38,6 +36,7 @@ import android.view.accessibility.AccessibilityManager;

import com.android.internal.logging.MetricsLogger;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.internal.view.RotationPolicy;
import com.android.systemui.Dependency;
import com.android.systemui.Interpolators;
import com.android.systemui.R;
@@ -346,7 +345,7 @@ public class RotationButtonController {
    private boolean shouldOverrideUserLockPrefs(final int rotation) {
        // 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 boolean isRotationAnimationCCW(int from, int to) {
+21 −4
Original line number Diff line number Diff line
@@ -42,12 +42,15 @@ import android.util.SparseArray;
import android.view.Surface;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.view.RotationPolicy;
import com.android.server.LocalServices;
import com.android.server.UiThread;
import com.android.server.policy.WindowManagerPolicy;
import com.android.server.policy.WindowOrientationListener;
import com.android.server.statusbar.StatusBarManagerInternal;

import lineageos.providers.LineageSettings;

import java.io.PrintWriter;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@@ -92,6 +95,7 @@ public class DisplayRotation {
    private int mShowRotationSuggestions;

    private int mAllowAllRotations = -1;
    private int mUserRotationAngles = -1;
    private int mUserRotationMode = WindowManagerPolicy.USER_ROTATION_FREE;
    private int mUserRotation = Surface.ROTATION_0;

@@ -620,10 +624,13 @@ public class DisplayRotation {
                mAllowAllRotations = mContext.getResources().getBoolean(
                        com.android.internal.R.bool.config_allowAllRotations) ? 1 : 0;
            }
            if (sensorRotation != Surface.ROTATION_180
                    || mAllowAllRotations == 1
                    || orientation == ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR
                    || orientation == ActivityInfo.SCREEN_ORIENTATION_FULL_USER) {
            boolean allowed = true;
            if (orientation != ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR
                    && orientation != ActivityInfo.SCREEN_ORIENTATION_FULL_USER) {
                allowed = RotationPolicy.isRotationAllowed(sensorRotation,
                        mUserRotationAngles, mAllowAllRotations != 0);
            }
            if (allowed) {
                preferredRotation = sensorRotation;
            } else {
                preferredRotation = lastRotation;
@@ -860,6 +867,13 @@ public class DisplayRotation {
                shouldUpdateRotation = true;
            }

            final int userRotationAngles = Settings.System.getInt(resolver,
                    Settings.System.ACCELEROMETER_ROTATION_ANGLES, -1);
            if (mUserRotationAngles != userRotationAngles) {
                mUserRotationAngles = userRotationAngles;
                shouldUpdateRotation = true;
            }

            final int userRotationMode = Settings.System.getIntForUser(resolver,
                    Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT) != 0
                            ? WindowManagerPolicy.USER_ROTATION_FREE
@@ -975,6 +989,9 @@ public class DisplayRotation {
            resolver.registerContentObserver(Settings.System.getUriFor(
                    Settings.System.ACCELEROMETER_ROTATION), false, this,
                    UserHandle.USER_ALL);
            resolver.registerContentObserver(Settings.System.getUriFor(
                    Settings.System.ACCELEROMETER_ROTATION_ANGLES), false, this,
                    UserHandle.USER_ALL);
            resolver.registerContentObserver(Settings.System.getUriFor(
                    Settings.System.USER_ROTATION), false, this,
                    UserHandle.USER_ALL);