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

Commit b1687104 authored by Steve Kondik's avatar Steve Kondik
Browse files

Updated rotation patch from Optedoblivian and Elanthis.

parent 23e9417a
Loading
Loading
Loading
Loading
+50 −178
Original line number Diff line number Diff line
@@ -124,207 +124,80 @@ public abstract class WindowOrientationListener {
        // angle will not result in any orientation changes. If phone faces uses,
        // the device is leaning forward.
        private static final int PIVOT_LOWER = -10;
        
        // Elanthis rotate code starts
        // Upper threshold limit for switching from portrait to landscape
        private static final int PL_UPPER = 295;
        private static final int PL_UPPER = 65;
        // Lower threshold limit for switching from landscape to portrait
        private static final int LP_LOWER = 320;
        // Lower threshold limt for switching from portrait to landscape
        private static final int PL_LOWER = 270;
        // Upper threshold limit for switching from landscape to portrait
        private static final int LP_UPPER = 359;
        // Minimum angle which is considered landscape
        private static final int LANDSCAPE_LOWER = 235;
        // Minimum angle which is considered portrait
        private static final int PORTRAIT_LOWER = 60;

        // Internal value used for calculating linear variant
        private static final float PL_LF_UPPER =
            ((float)(PL_UPPER-PL_LOWER))/((float)(PIVOT_UPPER-PIVOT));
        private static final float PL_LF_LOWER =
            ((float)(PL_UPPER-PL_LOWER))/((float)(PIVOT-PIVOT_LOWER));
        //  Internal value used for calculating linear variant
        private static final float LP_LF_UPPER =
            ((float)(LP_UPPER - LP_LOWER))/((float)(PIVOT_UPPER-PIVOT));
        private static final float LP_LF_LOWER =
            ((float)(LP_UPPER - LP_LOWER))/((float)(PIVOT-PIVOT_LOWER)); 


        // 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;
        private static final int PL_UPPER_4 = 115;
        private static final int LP_LOWER_4 = 140;
        private static final int LP_UPPER_4 = 179;

        // Internal value used for calculating linear variant
        private static final float PL_LF_UPPER_2 =
            ((float)(PL_UPPER_2-PL_LOWER_2))/((float)(PIVOT_UPPER-PIVOT));
        private static final float PL_LF_LOWER_2 =
            ((float)(PL_UPPER_2-PL_LOWER_2))/((float)(PIVOT-PIVOT_LOWER));
        //  Internal value used for calculating linear variant
        private static final float LP_LF_UPPER_2 =
            ((float)(LP_UPPER_2 - LP_LOWER_2))/((float)(PIVOT_UPPER-PIVOT));
        private static final float LP_LF_LOWER_2 =
            ((float)(LP_UPPER_2 - LP_LOWER_2))/((float)(PIVOT-PIVOT_LOWER)); 

        // Internal value used for calculating linear variant
        private static final float PL_LF_UPPER_3 =
            ((float)(PL_UPPER_3-PL_LOWER))/((float)(PIVOT_UPPER-PIVOT));
        private static final float PL_LF_LOWER_3 =
            ((float)(PL_UPPER_3-PL_LOWER))/((float)(PIVOT-PIVOT_LOWER));
        //  Internal value used for calculating linear variant
        private static final float LP_LF_UPPER_3 =
            ((float)(LP_UPPER_3 - LP_LOWER_3))/((float)(PIVOT_UPPER-PIVOT));
        private static final float LP_LF_LOWER_3 =
            ((float)(LP_UPPER_3 - LP_LOWER_3))/((float)(PIVOT-PIVOT_LOWER)); 

        // Internal value used for calculating linear variant
        private static final float PL_LF_UPPER_4 =
            ((float)(PL_UPPER_4-PL_LOWER_2))/((float)(PIVOT_UPPER-PIVOT));
        private static final float PL_LF_LOWER_4 =
            ((float)(PL_UPPER_4-PL_LOWER_2))/((float)(PIVOT-PIVOT_LOWER));
        //  Internal value used for calculating linear variant
        private static final float LP_LF_UPPER_4 =
            ((float)(LP_UPPER_4 - LP_LOWER_4))/((float)(PIVOT_UPPER-PIVOT));
        private static final float LP_LF_LOWER_4 =
            ((float)(LP_UPPER_4 - LP_LOWER_4))/((float)(PIVOT-PIVOT_LOWER)); 

	// End h4x vars        


        private static final int LP_LOWER = 25;

        private static final float OneEightyOverPi = 57.29577957855f;
        
        public void onSensorChanged(SensorEvent event) {
            float[] values = event.values;
            float X = values[_DATA_X];
            float Y = values[_DATA_Y];
            float Z = values[_DATA_Z];
            float OneEightyOverPi = 57.29577957855f;
            float gravity = (float) Math.sqrt(X * X + Y * Y + Z * Z);
            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
                float angle = (float) Math.atan2(Y, -X) * OneEightyOverPi;
                int orientation = 90 - (int) Math.round(angle);
                // normalize to 0 - 359 range
                while (orientation >= 360) {
                    orientation -= 360;
                }
                while (orientation < 0) {
                orientation %= 360;
                if (orientation < 0) 
                    orientation += 360;
                
                int quadrant = orientation / 90;
                orientation %= 90;
                
                // If you are in the 2nd or 4th quadrant we should reorient to
                // get the mirror of the angle
                if (quadrant == 1 || quadrant == 3)
                    orientation = 90 - orientation;

                boolean landscape = (mSensorRotation == Surface.ROTATION_90)
                        || (mSensorRotation == Surface.ROTATION_270);

                boolean newrot = false;
                // If the sensor doesn't help you, figure out which way it should be
                if (mSensorRotation == -1)
                {
                    if (orientation <= LP_LOWER)
                    {
                        newrot = true;
                        landscape = true;
                    }
                // Orientation values between LANDSCAPE_LOWER and PL_LOWER
                // are considered landscape.
                // Ignore orientation values between 0 and LANDSCAPE_LOWER
                // For orientation values between LP_UPPER and PL_LOWER,
                // the threshold gets set linearly around PIVOT.
                if ((orientation >= PL_LOWER) && (orientation <= LP_UPPER)) {
                    float threshold;
                    float delta = zyangle - PIVOT;
                    if (mSensorRotation == Surface.ROTATION_90) {
                        if (delta < 0) {
                            // Delta is negative
                            threshold = LP_LOWER - (LP_LF_LOWER * delta);
                        } else {
                            threshold = LP_LOWER + (LP_LF_UPPER * delta);
                        }
                        rotation = (orientation >= threshold) ? Surface.ROTATION_0
                                : Surface.ROTATION_90;
                    } else {
                        if (delta < 0) {
                            // Delta is negative
                            threshold = PL_UPPER + (PL_LF_LOWER * delta);
                        } else {
                            threshold = PL_UPPER - (PL_LF_UPPER * delta);
                        }
                        rotation = (orientation <= threshold) ? Surface.ROTATION_90
                                : Surface.ROTATION_0;
                    }
                } else if ((orientation >= LANDSCAPE_LOWER) && (orientation < LP_LOWER)) {
                    rotation = Surface.ROTATION_90;
                } else if ((orientation >= PL_UPPER) || (orientation <= PORTRAIT_LOWER)) {
                    rotation = Surface.ROTATION_0;
                    // Start orientation h4x
                    // --optedoblivion
                } else if ((orientation <= PL_LOWER_2) && (orientation >= LP_UPPER_2)) {
                    float threshold;
                    float delta = zyangle - PIVOT;
                    if (mSensorRotation == Surface.ROTATION_270) {
                        if (delta < 0) {
                            threshold = LP_LOWER_2 - (LP_LF_LOWER_2 * delta);
                        } else {
                            threshold = LP_LOWER_2 + (LP_LF_UPPER_2 * delta);
                        }
                        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);
                    else if (orientation >= PL_UPPER)
                    {
                        newrot = true;
                        landscape = false;
                    }
                        rotation = (orientation >= threshold) ? Surface.ROTATION_270
                                : Surface.ROTATION_0;
                }
                } else if ((orientation <= LANDSCAPE_LOWER_2) && (orientation > LP_LOWER_2)) {
                else if (landscape)
                    newrot = (orientation <= LP_LOWER);
                else
                    newrot = orientation >= PL_UPPER;

                if (landscape ^ newrot)
                {
                    if (quadrant == 0 || quadrant == 1)
                    rotation = Surface.ROTATION_270;
                } else if ((orientation <= PL_LOWER) && (orientation >= LP_UPPER_3)) {
                    float threshold;
                    float delta = zyangle - PIVOT;
                    if (mSensorRotation == Surface.ROTATION_90) {
                        if (delta < 0) {
                            threshold = LP_LOWER_3 - (LP_LF_LOWER_3 * delta);
                        } else {
                            threshold = LP_LOWER_3 + (LP_LF_UPPER_3 * delta);
                        }
                        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;
                    else
                        rotation = Surface.ROTATION_90;
                    }
                } else if ((orientation <= LP_LOWER_3) && (orientation >= LP_LOWER_4)) {
                else
                {
                    if (quadrant == 0 || quadrant == 3)
                        rotation = Surface.ROTATION_0;
                    else
                    rotation = Surface.ROTATION_180;
                } else if ((orientation >= PL_LOWER_2) && (orientation <= LP_UPPER_4)) {
                    float threshold;
                    float delta = zyangle - PIVOT;
                    if (mSensorRotation == Surface.ROTATION_270) {
                        if (delta < 0) {
                            threshold = LP_LOWER_4 - (LP_LF_LOWER_4 * delta);
                        } else {
                            threshold = LP_LOWER_4 + (LP_LF_UPPER_4 * delta);
                        }
                        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;
                    }

                }
                // End orientation h4x
                if ((rotation != -1) && (rotation != mSensorRotation)) {
            if (mSensorRotation != rotation) {
            // End Elanthis rotate code
                    if (Surface.ROTATION_180 != rotation || (Surface.ROTATION_180 == rotation && 
                            (Settings.System.getInt(mContext.getContentResolver(), 
                                    Settings.System.USE_180_ORIENTATION, 0) > 0))) {            
@@ -333,7 +206,6 @@ public abstract class WindowOrientationListener {
                    }
                }
            }
        }

        public void onAccuracyChanged(Sensor sensor, int accuracy) {