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

Commit c4b6b228 authored by Jonas Larsson's avatar Jonas Larsson
Browse files

Notification Power Widget: Real rotation lock using ICS API:s

Old implementation of auto rotate button just toggles auto rotation.
New implementation use the new rotation freeze/thaw API:s. Accelerometer
is toggled like before, but current rotation is kept ("frozen"). This
means that AutoRotateButton now works like the rotation lock button
on iOS devices and the native rotation lock button in ICS on tablets.

Change-Id: I8e32bafe46f316194fdf77c375e1adbb7edcf50f
parent f9d6bed4
Loading
Loading
Loading
Loading
+31 −16
Original line number Diff line number Diff line
@@ -2,16 +2,24 @@ package com.android.systemui.statusbar.powerwidget;

import com.android.systemui.R;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.provider.Settings;
import android.util.Log;
import android.view.IWindowManager;

import java.util.ArrayList;
import java.util.List;

public class AutoRotateButton extends PowerButton {

    private static final String TAG = "AutoRotateButton";

    private static final List<Uri> OBSERVED_URIS = new ArrayList<Uri>();
    static {
        OBSERVED_URIS.add(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION));
@@ -21,7 +29,7 @@ public class AutoRotateButton extends PowerButton {

    @Override
    protected void updateState() {
        if (getOrientationState(mView.getContext()) == 1) {
        if (getAutoRotation()) {
            mIcon = R.drawable.stat_orientation_on;
            mState = STATE_ENABLED;
        } else {
@@ -32,19 +40,9 @@ public class AutoRotateButton extends PowerButton {

    @Override
    protected void toggleState() {
        Context context = mView.getContext();
        if(getOrientationState(context) == 0) {
            Settings.System.putInt(
                    context.getContentResolver(),
                    Settings.System.ACCELEROMETER_ROTATION, 1);
        } else {
            Settings.System.putInt(
                    context.getContentResolver(),
                    Settings.System.ACCELEROMETER_ROTATION, 0);
        }
        setAutoRotation(!getAutoRotation());
    }


    @Override
    protected boolean handleLongClick() {
        Intent intent = new Intent("android.settings.DISPLAY_SETTINGS");
@@ -59,9 +57,26 @@ public class AutoRotateButton extends PowerButton {
        return OBSERVED_URIS;
    }

    private static int getOrientationState(Context context) {
        return Settings.System.getInt(
                context.getContentResolver(),
                Settings.System.ACCELEROMETER_ROTATION, 0);
    private boolean getAutoRotation() {
        ContentResolver cr = mView.getContext().getContentResolver();
        return 0 != Settings.System.getInt(cr, Settings.System.ACCELEROMETER_ROTATION, 0);
    }

    private void setAutoRotation(final boolean autorotate) {
        AsyncTask.execute(new Runnable() {
                public void run() {
                    try {
                        IWindowManager wm = IWindowManager.Stub.asInterface(
                                ServiceManager.getService(Context.WINDOW_SERVICE));
                        if (autorotate) {
                            wm.thawRotation();
                        } else {
                            wm.freezeRotation(-1);
                        }
                    } catch (RemoteException exc) {
                        Log.w(TAG, "Unable to save auto-rotate setting");
                    }
                }
            });
    }
}