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

Commit 207673cd authored by Jeff Brown's avatar Jeff Brown
Browse files

Implement new rotation policy.

Rotation lock does not override NOSENSOR mode anymore.

Centralize the rotation policy settings into a new class shared by
the System UI and Settings applications.

Add a new setting to specify whether rotation-lock is being hidden
because the "auto-rotate screen" option has been toggled in the
Accessibility settings panel.

Bug: 6523269
Change-Id: I15173280d25bc5d101e89a9c65913aefc53fc33a
parent 433927c5
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
@@ -1749,6 +1749,20 @@ public final class Settings {
         */
        public static final String USER_ROTATION = "user_rotation";

        /**
         * Control whether the rotation lock toggle in the System UI should be hidden.
         * Typically this is done for accessibility purposes to make it harder for
         * the user to accidentally toggle the rotation lock while the display rotation
         * has been locked for accessibility.
         *
         * If 0, then rotation lock toggle is not hidden for accessibility (although it may be
         * unavailable for other reasons).  If 1, then the rotation lock toggle is hidden.
         *
         * @hide
         */
        public static final String HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY =
                "hide_rotation_lock_toggle_for_accessibility";

        /**
         * Whether the phone vibrates when it is ringing due to an incoming call. This will
         * be used by Phone and Setting apps; it shouldn't affect other apps.
@@ -2029,6 +2043,7 @@ public final class Settings {
            DATE_FORMAT,
            ACCELEROMETER_ROTATION,
            USER_ROTATION,
            HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY,
            DTMF_TONE_WHEN_DIALING,
            DTMF_TONE_TYPE_WHEN_DIALING,
            EMERGENCY_TONE,
+157 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.view;

import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.provider.Settings;
import android.util.Log;
import android.view.IWindowManager;
import android.view.Surface;

/**
 * Provides helper functions for configuring the display rotation policy.
 */
public final class RotationPolicy {
    private static final String TAG = "RotationPolicy";

    private RotationPolicy() {
    }

    /**
     * Returns true if the device supports the rotation-lock toggle feature
     * in the system UI or system bar.
     *
     * When the rotation-lock toggle is supported, the "auto-rotate screen" option in
     * Display settings should be hidden, but it should remain available in Accessibility
     * settings.
     */
    public static boolean isRotationLockToggleSupported(Context context) {
        return context.getResources().getConfiguration().smallestScreenWidthDp >= 600;
    }

    /**
     * Returns true if the rotation-lock toggle should be shown in the UI.
     */
    public static boolean isRotationLockToggleVisible(Context context) {
        return isRotationLockToggleSupported(context) &&
                Settings.System.getInt(context.getContentResolver(),
                        Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 0) == 0;
    }

    /**
     * Returns true if rotation lock is enabled.
     */
    public static boolean isRotationLocked(Context context) {
        return Settings.System.getInt(context.getContentResolver(),
                Settings.System.ACCELEROMETER_ROTATION, 0) == 0;
    }

    /**
     * Enables or disables rotation lock.
     *
     * Should be used by the rotation lock toggle.
     */
    public static void setRotationLock(Context context, final boolean enabled) {
        Settings.System.putInt(context.getContentResolver(),
                Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 0);

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

    /**
     * Enables or disables rotation lock and adjusts whether the rotation lock toggle
     * should be hidden for accessibility purposes.
     *
     * Should be used by Display settings and Accessibility settings.
     */
    public static void setRotationLockForAccessibility(Context context, final boolean enabled) {
        Settings.System.putInt(context.getContentResolver(),
                Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, enabled ? 1 : 0);

        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    IWindowManager wm = IWindowManager.Stub.asInterface(
                            ServiceManager.getService(Context.WINDOW_SERVICE));
                    if (enabled) {
                        wm.freezeRotation(Surface.ROTATION_0);
                    } else {
                        wm.thawRotation();
                    }
                } catch (RemoteException exc) {
                    Log.w(TAG, "Unable to save auto-rotate setting");
                }
            }
        });
    }

    /**
     * Registers a listener for rotation policy changes.
     */
    public static void registerRotationPolicyListener(Context context,
            RotationPolicyListener listener) {
        context.getContentResolver().registerContentObserver(Settings.System.getUriFor(
                Settings.System.ACCELEROMETER_ROTATION),
                false, listener.mObserver);
        context.getContentResolver().registerContentObserver(Settings.System.getUriFor(
                Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY),
                false, listener.mObserver);
    }

    /**
     * Unregisters a listener for rotation policy changes.
     */
    public static void unregisterRotationPolicyListener(Context context,
            RotationPolicyListener listener) {
        context.getContentResolver().unregisterContentObserver(listener.mObserver);
    }

    /**
     * Listener that is invoked whenever a change occurs that might affect the rotation policy.
     */
    public static abstract class RotationPolicyListener {
        final ContentObserver mObserver = new ContentObserver(new Handler()) {
            public void onChange(boolean selfChange, Uri uri) {
                RotationPolicyListener.this.onChange();
            }
        };

        public abstract void onChange();
    }
}
 No newline at end of file
+0 −75
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
** Copyright 2012, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License"); 
** you may not use this file except in compliance with the License. 
** You may obtain a copy of the License at 
**
**     http://www.apache.org/licenses/LICENSE-2.0 
**
** Unless required by applicable law or agreed to in writing, software 
** distributed under the License is distributed on an "AS IS" BASIS, 
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
** See the License for the specific language governing permissions and 
** limitations under the License.
-->

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:systemui="http://schemas.android.com/apk/res/com.android.systemui"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/notification_panel_header_padding_top"
    android:background="@drawable/notification_header_bg"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    android:baselineAligned="false"
    >
    <com.android.systemui.statusbar.policy.Clock
        android:id="@+id/clock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:singleLine="true"
        android:textAppearance="@style/TextAppearance.StatusBar.Expanded.Clock"
        />

    <com.android.systemui.statusbar.policy.DateView android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:textAppearance="@style/TextAppearance.StatusBar.Expanded.Date"
        />

    <com.android.systemui.statusbar.RotationToggle android:id="@+id/rotation_lock_button"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_margin="8dp"
        android:button="@drawable/ic_notify_rotation"
        android:contentDescription="@string/accessibility_rotation_lock_off"
        />

    <ImageView android:id="@+id/settings_button"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:scaleType="center"
        android:src="@drawable/ic_notify_quicksettings"
        android:contentDescription="@string/accessibility_settings_button"
        />

    <Space
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        />

    <ImageView android:id="@+id/clear_all_button"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:scaleType="center"
        android:src="@drawable/ic_notify_clear"
        android:contentDescription="@string/accessibility_clear_all"
        />            
</LinearLayout>
 No newline at end of file
+9 −0
Original line number Diff line number Diff line
@@ -43,6 +43,15 @@
        android:textAppearance="@style/TextAppearance.StatusBar.Expanded.Date"
        />

    <com.android.systemui.statusbar.RotationToggle android:id="@+id/rotation_lock_button"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_margin="8dp"
        android:button="@drawable/ic_notify_rotation"
        android:contentDescription="@string/accessibility_rotation_lock_off"
        android:clickable="true"
        />

    <ImageView android:id="@+id/settings_button"
        android:layout_width="48dp"
        android:layout_height="48dp"
+3 −1
Original line number Diff line number Diff line
@@ -89,7 +89,9 @@
                android:layout_marginRight="5dp"
                />
    </LinearLayout>
    <View style="@style/StatusBarPanelSettingsPanelSeparator" />
    <View
            android:id="@+id/rotate_separator"
            style="@style/StatusBarPanelSettingsPanelSeparator" />

    <!-- Brightness -->
    <LinearLayout style="@style/StatusBarPanelSettingsRow" >
Loading