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

Commit 13614910 authored by Jonas Larsson's avatar Jonas Larsson Committed by Steve Kondik
Browse files

Configurable 90, 180 and 270 degree rotation

Ported from my CM6 commit. Configurable in CMParts.

Change-Id: Iafd43cfb0ea16ed597cd15f069dfb3a7145538ad
parent 8b643a1c
Loading
Loading
Loading
Loading
+10 −5
Original line number Diff line number Diff line
@@ -1807,12 +1807,17 @@ public final class Settings {
        public static final String ACCELEROMETER_ROTATION = "accelerometer_rotation";

         /**
         * Control weather 180 degree rotation should be included if
         * ACCELEROMETER_ROTATION is enabled. If 0 no 180 degree rotation will be
         * executed, if 1 the 180 degree rotation is executed when ACCELEROMETER_ROTATION is true.
         * Control the type of rotation which can be performed using the accelerometer
         * if ACCELEROMETER_ROTATION is enabled.
         * Value is a bitwise combination of
         * 1 = 90 degrees (left)
         * 2 = 180 degrees (inverted)
         * 4 = 270 degrees (right)
         * Normal portrait (0 degrees) is always enabled
         * Default is 5 (90 & 270 degrees), like stock Android
         * @hide
         */
        public static final String ACCELEROMETER_ROTATE_180 = "accelerometer_rotate_180";
        public static final String ACCELEROMETER_ROTATION_MODE = "accelerometer_rotation_mode";

        /**
         * Specifies the number of recent apps to show (8, 12, 16)
@@ -2825,7 +2830,7 @@ public final class Settings {
            TIME_12_24,
            DATE_FORMAT,
            ACCELEROMETER_ROTATION,
            ACCELEROMETER_ROTATE_180,
            ACCELEROMETER_ROTATION_MODE,
            DTMF_TONE_WHEN_DIALING,
            DTMF_TONE_TYPE_WHEN_DIALING,
            EMERGENCY_TONE,
+68 −5
Original line number Diff line number Diff line
@@ -16,11 +16,14 @@

package android.view;

import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Handler;
import android.provider.Settings;
import android.util.Config;
import android.util.Log;
@@ -40,12 +43,15 @@ public abstract class WindowOrientationListener {
    private static final String TAG = "WindowOrientationListener";
    private static final boolean DEBUG = false;
    private static final boolean localLOGV = DEBUG || Config.DEBUG;
    private static Context mContext;
    private static int sAccelerometerMode = 5;
    private SensorManager mSensorManager;
    private boolean mEnabled = false;
    private int mRate;
    private Sensor mSensor;
    private SensorEventListenerImpl mSensorEventListener;
    private Context mContext;
    private Handler mHandler;
    private SettingsObserver mSettingsObserver;

    /**
     * Creates a new WindowOrientationListener.
@@ -71,13 +77,14 @@ public abstract class WindowOrientationListener {
     */
    private WindowOrientationListener(Context context, int rate) {
        mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
        mContext = context;
        mRate = rate;
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        if (mSensor != null) {
            // Create listener only if sensors do exist
            mSensorEventListener = new SensorEventListenerImpl(this);
        }
        mContext = context;
        mHandler = new Handler();
    }

    /**
@@ -93,6 +100,10 @@ public abstract class WindowOrientationListener {
            if (localLOGV) Log.d(TAG, "WindowOrientationListener enabled");
            mSensorManager.registerListener(mSensorEventListener, mSensor, mRate);
            mEnabled = true;
            if (mSettingsObserver == null) {
                mSettingsObserver = new SettingsObserver(mHandler);
            }
            mSettingsObserver.observe();
        }
    }

@@ -108,6 +119,10 @@ public abstract class WindowOrientationListener {
            if (localLOGV) Log.d(TAG, "WindowOrientationListener disabled");
            mSensorManager.unregisterListener(mSensorEventListener);
            mEnabled = false;
            if (mSettingsObserver != null) {
                mSettingsObserver.stop();
                mSettingsObserver = null;
            }
        }
    }

@@ -124,6 +139,38 @@ public abstract class WindowOrientationListener {
        return lastRotation;
    }

    class SettingsObserver extends ContentObserver {
        SettingsObserver(Handler handler) {
            super(handler);
        }

        void observe() {
            ContentResolver resolver = mContext.getContentResolver();
            resolver.registerContentObserver(Settings.System.getUriFor(
                    Settings.System.ACCELEROMETER_ROTATION_MODE), false, this);
            if (localLOGV) Log.i(TAG, "SettingsObserver enabled");
            update();
        }

        public void stop() {
            ContentResolver resolver = mContext.getContentResolver();
            resolver.unregisterContentObserver(this);
            if (localLOGV) Log.i(TAG, "SettingsObserver disabled");
        }

        @Override
        public void onChange(boolean selfChange) {
            update();
        }

        public void update() {
            ContentResolver resolver = mContext.getContentResolver();
            sAccelerometerMode = Settings.System.getInt(resolver,
                    Settings.System.ACCELEROMETER_ROTATION_MODE, 5);
            if (localLOGV) Log.i(TAG, "sAccelerometerMode=" + sAccelerometerMode);
        }
    }

    /**
     * This class filters the raw accelerometer data and tries to detect actual changes in
     * orientation. This is a very ill-defined problem so there are a lot of tweakable parameters,
@@ -307,9 +354,7 @@ public abstract class WindowOrientationListener {

        private void calculateNewRotation(float orientation, float tiltAngle) {
            if (localLOGV) Log.i(TAG, orientation + ", " + tiltAngle + ", " + mRotation);
            final boolean allow180Rotation = mAllow180Rotation ||
                    (Settings.System.getInt(mContext.getContentResolver(),
                                            Settings.System.ACCELEROMETER_ROTATE_180, 0) != 0);
            final boolean allow180Rotation = mAllow180Rotation || (sAccelerometerMode & 2) != 0;
            int thresholdRanges[][] = allow180Rotation
                    ? THRESHOLDS_WITH_180[mRotation] : THRESHOLDS[mRotation];
            int row = -1;
@@ -328,6 +373,24 @@ public abstract class WindowOrientationListener {
                return;
            }

            boolean allowed = rotation == ROTATION_0;
            if (!allowed) {
                switch (rotation) {
                    case ROTATION_90:
                        allowed = (sAccelerometerMode & 1) != 0;
                        break;
                    case ROTATION_180:
                        allowed = (sAccelerometerMode & 2) != 0;
                        break;
                    case ROTATION_270:
                        allowed = (sAccelerometerMode & 4) != 0;
                        break;
                }
            }
            if (!allowed) {
                if (localLOGV) Log.i(TAG, " not allowed rotation = " + rotation);
                return;
            }
            if (localLOGV) Log.i(TAG, "orientation " + orientation + " gives new rotation = "
                    + rotation);
            mRotation = rotation;