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

Commit 7fc77cad authored by Rahul Chaturvedi's avatar Rahul Chaturvedi
Browse files

Add the Allow Rotation setting to Launcher3.

This CL adds a Settings activity along with the code needed to provide
a "Allow Rotation" setting to all phones and tablets. This setting is
set to false for phones and true for tablets. On changing the setting
from unlocked to locked, the launcher (and the Settings activity)
will get locked to the orientation the user was in when he disabled
"Allow Rotation". This is consistent with how the natural rotation
feature of Android works.

Change-Id: I8a1c59d1fa0bb9262530cad96e0a9bdbab0d9344
parent cadc29c7
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -148,6 +148,12 @@
            </intent-filter>
        </activity>

        <activity
            android:name="com.android.launcher3.SettingsActivity"
            android:label="@string/settings_button_text"
            android:autoRemoveFromRecents="true">
        </activity>

        <!-- Debugging tools -->
        <activity
            android:name="com.android.launcher3.MemoryDumpActivity"
+6 −1
Original line number Diff line number Diff line
@@ -1132,6 +1132,11 @@ public class WallpaperPickerActivity extends WallpaperCropActivity {

    @Override
    public boolean enableRotation() {
        return Utilities.isRotationEnabled(getContext());
        // Check if rotation is enabled for this device.
        if (Utilities.isRotationAllowedForDevice(getContext()))
            return true;

        // Check if the user has specifically enabled rotation via preferences.
        return Utilities.isAllowRotationPrefEnabled(getApplicationContext());
    }
}
+6 −0
Original line number Diff line number Diff line
@@ -166,6 +166,12 @@
    <!-- Text for settings button -->
    <string name="settings_button_text">Settings</string>

    <!-- Strings for settings -->
    <!-- Title for Allow Rotation setting. [CHAR LIMIT=50] [DO NOT TRANSLATE] -->
    <string name="allow_rotation_title">Allow rotation</string>
    <!-- Summary for Allow Rotation setting. [CHAR LIMIT=150] [DO NOT TRANSLATE] -->
    <string name="allow_rotation_summary">Allow rotation of the home screen</string>

    <!-- Label on an icon that references an uninstalled package, for which we have no information about when it might be installed. [CHAR_LIMIT=15] -->
    <string name="package_state_unknown">Unknown</string>

+27 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2015 Google Inc.

     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.
-->

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <SwitchPreference
            android:key="pref_allowRotation"
            android:title="@string/allow_rotation_title"
            android:summary="@string/allow_rotation_summary"
            android:defaultValue="@bool/allow_rotation"
            android:persistent="true"
    />

</PreferenceScreen>
+53 −5
Original line number Diff line number Diff line
@@ -221,7 +221,8 @@ public class Launcher extends Activity
    public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";

    /** The different states that Launcher can be in. */
    enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED };
    enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED }

    @Thunk State mState = State.WORKSPACE;
    @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;

@@ -405,6 +406,24 @@ public class Launcher extends Activity

    FocusIndicatorView mFocusHandler;

    private boolean mRotationEnabled = false;
    private boolean mPreferenceObserverRegistered = false;

    final private SharedPreferences.OnSharedPreferenceChangeListener mSettingsObserver =
            new SharedPreferences.OnSharedPreferenceChangeListener() {
        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
                if (mRotationEnabled = sharedPreferences.getBoolean(
                        Utilities.ALLOW_ROTATION_PREFERENCE_KEY, false)) {
                    unlockScreenOrientation(true);
                } else {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (DEBUG_STRICT_MODE) {
@@ -505,7 +524,19 @@ public class Launcher extends Activity
        IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        registerReceiver(mCloseSystemDialogsReceiver, filter);

        // On large interfaces, we want the screen to auto-rotate based on the current orientation
        mRotationEnabled = Utilities.isRotationAllowedForDevice(getApplicationContext());
        // In case we are on a device with locked rotation, we should look at preferences to check
        // if the user has specifically allowed rotation.
        if (!mRotationEnabled) {
            getSharedPreferences(LauncherFiles.ROTATION_PREF_FILE,
                    Context.MODE_MULTI_PROCESS).registerOnSharedPreferenceChangeListener(
                    mSettingsObserver);
            mPreferenceObserverRegistered = true;
            mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
        }

        // On large interfaces, or on devices that a user has specifically enabled screen rotation,
        // we want the screen to auto-rotate based on the current orientation
        unlockScreenOrientation(true);

        if (mLauncherCallbacks != null) {
@@ -1300,8 +1331,11 @@ public class Launcher extends Activity
    protected boolean hasSettings() {
        if (mLauncherCallbacks != null) {
            return mLauncherCallbacks.hasSettings();
        } else {
            // On devices with a locked orientation, we will at least have the allow rotation
            // setting.
            return !Utilities.isRotationAllowedForDevice(this);
        }
        return false;
    }

    public void addToCustomContentPage(View customContent,
@@ -2083,6 +2117,13 @@ public class Launcher extends Activity
    public void onDestroy() {
        super.onDestroy();

        if (mPreferenceObserverRegistered) {
            getSharedPreferences(LauncherFiles.ROTATION_PREF_FILE,
                    Context.MODE_MULTI_PROCESS).unregisterOnSharedPreferenceChangeListener(
                    mSettingsObserver);
            mPreferenceObserverRegistered = false;
        }

        // Remove all pending runnables
        mHandler.removeMessages(ADVANCE_MSG);
        mHandler.removeMessages(0);
@@ -2865,6 +2906,8 @@ public class Launcher extends Activity
        if (LOGD) Log.d(TAG, "onClickSettingsButton");
        if (mLauncherCallbacks != null) {
            mLauncherCallbacks.onClickSettingsButton(v);
        } else {
            showSettingsActivity();
        }
    }

@@ -4472,7 +4515,7 @@ public class Launcher extends Activity
    }

    public void lockScreenOrientation() {
        if (Utilities.isRotationEnabled(this)) {
        if (mRotationEnabled) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
                setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
                        .getConfiguration().orientation));
@@ -4481,8 +4524,9 @@ public class Launcher extends Activity
            }
        }
    }

    public void unlockScreenOrientation(boolean immediate) {
        if (Utilities.isRotationEnabled(this)) {
        if (mRotationEnabled) {
            if (immediate) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
            } else {
@@ -4587,6 +4631,10 @@ public class Launcher extends Activity
        editor.apply();
    }

    private void showSettingsActivity() {
        startActivity(new Intent(this, SettingsActivity.class));
    }

    /**
     * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
     * screen that must be displayed and dismissed.
Loading