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

Commit 845d5337 authored by Steve Kondik's avatar Steve Kondik
Browse files

Add system preference for 180-degree orientation.

parent 036bad44
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -1481,6 +1481,12 @@ public final class Settings {
            NOTIFICATION_SCREEN_ON
        };

        /**
         * Should 180 degree orientation be enabled. 
         * @hide
         */
        public static final String USE_180_ORIENTATION = "use_180_orientation";
        
        // Settings moved to Settings.Secure

        /**
+102 −87
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.provider.Settings;
import android.util.Config;
import android.util.Log;

@@ -40,6 +41,7 @@ public abstract class WindowOrientationListener {
    private Sensor mSensor;
    private SensorEventListener mSensorEventListener;
    private int mSensorRotation = -1;
    private Context mContext;
    
    /**
     * Creates a new WindowOrientationListener.
@@ -60,6 +62,7 @@ public abstract class WindowOrientationListener {
     * SENSOR_DELAY_NORMAL} for simple screen orientation change detection.
     */
    public WindowOrientationListener(Context context, int rate) {
        mContext = context;
        mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
        mRate = rate;
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
@@ -148,14 +151,12 @@ public abstract class WindowOrientationListener {

        // new orientation h4x start here
        // -- optedoblivion

        private static final int LANDSCAPE_LOWER_2 = 125;
        private static final int LP_LOWER_2 = 40;
        private static final int PL_UPPER_2 = 65;
        private static final int PL_LOWER_2 = 90;
        private static final int LP_UPPER_2 = 1;
        private static final int PORTRAIT_LOWER_2 = 300;

        private static final int LP_LOWER_3 = 220;
        private static final int PL_UPPER_3 = 245;
        private static final int LP_UPPER_3 = 181;
@@ -211,8 +212,10 @@ public abstract class WindowOrientationListener {
            float zyangle = (float) Math.asin(Z / gravity) * OneEightyOverPi;
            int rotation = -1;
            if ((zyangle <= PIVOT_UPPER) && (zyangle >= PIVOT_LOWER)) {
                               
                // Check orientation only if the phone is flat enough
                // Don't trust the angle if the magnitude is small compared to the y value
                // Don't trust the angle if the magnitude is small compared to
                // the y value
                float angle = (float) Math.atan2(Y, -X) * OneEightyOverPi;
                int orientation = 90 - (int) Math.round(angle);
                // normalize to 0 - 359 range
@@ -237,7 +240,8 @@ public abstract class WindowOrientationListener {
                        } else {
                            threshold = LP_LOWER + (LP_LF_UPPER * delta);
                        }
                        rotation = (orientation >= threshold) ? Surface.ROTATION_0 : Surface.ROTATION_90;
                        rotation = (orientation >= threshold) ? Surface.ROTATION_0
                                : Surface.ROTATION_90;
                    } else {
                        if (delta < 0) {
                            // Delta is negative
@@ -245,7 +249,8 @@ public abstract class WindowOrientationListener {
                        } else {
                            threshold = PL_UPPER - (PL_LF_UPPER * delta);
                        }
                        rotation = (orientation <= threshold) ? Surface.ROTATION_90: Surface.ROTATION_0;
                        rotation = (orientation <= threshold) ? Surface.ROTATION_90
                                : Surface.ROTATION_0;
                    }
                } else if ((orientation >= LANDSCAPE_LOWER) && (orientation < LP_LOWER)) {
                    rotation = Surface.ROTATION_90;
@@ -262,14 +267,16 @@ public abstract class WindowOrientationListener {
                        } else {
                            threshold = LP_LOWER_2 + (LP_LF_UPPER_2 * delta);
                        }
                        rotation = (orientation <= threshold) ? Surface.ROTATION_0 : Surface.ROTATION_270;
                        rotation = (orientation <= threshold) ? Surface.ROTATION_0
                                : Surface.ROTATION_270;
                    } else {
                        if (delta < 0) {
                            threshold = PL_UPPER_2 + (PL_LF_LOWER_2 * delta);
                        } else {
                            threshold = PL_UPPER_2 - (PL_LF_UPPER_2 * delta);
                        }
			rotation = (orientation >= threshold) ? Surface.ROTATION_270 : Surface.ROTATION_0; 
                        rotation = (orientation >= threshold) ? Surface.ROTATION_270
                                : Surface.ROTATION_0;
                    }
                } else if ((orientation <= LANDSCAPE_LOWER_2) && (orientation > LP_LOWER_2)) {
                    rotation = Surface.ROTATION_270;
@@ -282,14 +289,16 @@ public abstract class WindowOrientationListener {
                        } else {
                            threshold = LP_LOWER_3 + (LP_LF_UPPER_3 * delta);
                        }
			rotation = (orientation <= threshold) ? Surface.ROTATION_180 : Surface.ROTATION_90;
                        rotation = (orientation <= threshold) ? Surface.ROTATION_180
                                : Surface.ROTATION_90;
                    } else {
                        if (delta < 0) {
                            threshold = PL_UPPER_3 + (PL_LF_LOWER_3 * delta);
                        } else {
                            threshold = PL_UPPER_3 - (PL_LF_UPPER_3 * delta);
                        }
			rotation = (orientation >= threshold) ? Surface.ROTATION_90 : Surface.ROTATION_180;
                        rotation = (orientation >= threshold) ? Surface.ROTATION_90
                                : Surface.ROTATION_180;
                    }
                } else if ((orientation <= LP_LOWER_3) && (orientation >= LP_LOWER_4)) {
                    rotation = Surface.ROTATION_180;
@@ -302,23 +311,29 @@ public abstract class WindowOrientationListener {
                        } else {
                            threshold = LP_LOWER_4 + (LP_LF_UPPER_4 * delta);
                        }
			rotation = (orientation >= threshold) ? Surface.ROTATION_180 : Surface.ROTATION_270;
                        rotation = (orientation >= threshold) ? Surface.ROTATION_180
                                : Surface.ROTATION_270;
                    } else {
                        if (delta < 0) {
                            threshold = PL_UPPER_4 + (PL_LF_UPPER_4 * delta);
                        } else {
                            threshold = PL_UPPER_4 - (PL_LF_UPPER_4 * delta);
                        }
			rotation = (orientation <= threshold) ? Surface.ROTATION_270 : Surface.ROTATION_180;
                        rotation = (orientation <= threshold) ? Surface.ROTATION_270
                                : Surface.ROTATION_180;
                    }
                }
                // End orientation h4x
                if ((rotation != -1) && (rotation != mSensorRotation)) {
                    if (Surface.ROTATION_180 != rotation || (Surface.ROTATION_180 == rotation && 
                            (Settings.System.getInt(mContext.getContentResolver(), 
                                    Settings.System.USE_180_ORIENTATION, 0) > 0))) {            
                        mSensorRotation = rotation;
                        onOrientationChanged(mSensorRotation);
                    }
                }
            }
        }

        public void onAccuracyChanged(Sensor sensor, int accuracy) {