diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 6ef78285f2..c3ddfd62b8 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -71,7 +71,7 @@ android:clearTaskOnLaunch="true" android:stateNotNeeded="true" android:windowSoftInputMode="adjustPan" - android:screenOrientation="nosensor" + android:screenOrientation="unspecified" android:configChanges="keyboard|keyboardHidden|navigation" android:resizeableActivity="true" android:resumeWhilePausing="true" diff --git a/quickstep/src/com/android/launcher3/uioverrides/OverviewState.java b/quickstep/src/com/android/launcher3/uioverrides/OverviewState.java index 3f305e93f8..854fb4f6e8 100644 --- a/quickstep/src/com/android/launcher3/uioverrides/OverviewState.java +++ b/quickstep/src/com/android/launcher3/uioverrides/OverviewState.java @@ -17,6 +17,7 @@ package com.android.launcher3.uioverrides; import static com.android.launcher3.LauncherAnimUtils.OVERVIEW_TRANSITION_MS; import static com.android.launcher3.anim.Interpolators.ACCEL_2; +import static com.android.launcher3.states.RotationHelper.REQUEST_ROTATE; import android.graphics.Rect; import android.view.View; @@ -73,6 +74,11 @@ public class OverviewState extends LauncherState { rv.setOverviewStateEnabled(false); } + @Override + public void onStateTransitionEnd(Launcher launcher) { + launcher.getRotationHelper().setCurrentStateRequest(REQUEST_ROTATE); + } + @Override public View getFinalFocus(Launcher launcher) { return launcher.getOverviewPanel(); diff --git a/quickstep/src/com/android/quickstep/WindowTransformSwipeHandler.java b/quickstep/src/com/android/quickstep/WindowTransformSwipeHandler.java index 25f395fd9e..f49106e620 100644 --- a/quickstep/src/com/android/quickstep/WindowTransformSwipeHandler.java +++ b/quickstep/src/com/android/quickstep/WindowTransformSwipeHandler.java @@ -19,6 +19,8 @@ import static com.android.launcher3.LauncherState.FAST_OVERVIEW; import static com.android.launcher3.LauncherState.OVERVIEW; import static com.android.launcher3.allapps.AllAppsTransitionController.ALL_APPS_PROGRESS; import static com.android.launcher3.anim.Interpolators.LINEAR; +import static com.android.launcher3.states.RotationHelper.REQUEST_LOCK; +import static com.android.launcher3.states.RotationHelper.REQUEST_NONE; import static com.android.quickstep.QuickScrubController.QUICK_SWITCH_START_DURATION; import static com.android.quickstep.TouchConsumer.INTERACTION_NORMAL; import static com.android.quickstep.TouchConsumer.INTERACTION_QUICK_SCRUB; @@ -33,7 +35,6 @@ import android.animation.ObjectAnimator; import android.annotation.TargetApi; import android.app.ActivityManager.RunningTaskInfo; import android.content.Context; -import android.content.pm.ActivityInfo; import android.content.res.Resources; import android.graphics.Matrix; import android.graphics.Matrix.ScaleToFit; @@ -364,7 +365,8 @@ public class WindowTransformSwipeHandler extends BaseSwipeInteractionHandler { // For the duration of the gesture, lock the screen orientation to ensure that we do not // rotate mid-quickscrub - mLauncher.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED); + mLauncher.getRotationHelper().setStateHandlerRequest(REQUEST_LOCK); + mRecentsView = mLauncher.getOverviewPanel(); mQuickScrubController = mRecentsView.getQuickScrubController(); mLauncherLayoutListener = new LauncherLayoutListener(mLauncher); @@ -755,7 +757,8 @@ public class WindowTransformSwipeHandler extends BaseSwipeInteractionHandler { mLauncherLayoutListener.close(false); // Restore the requested orientation to the user preference after the gesture has ended - mLauncher.updateRequestedOrientation(); + mLauncher.getRotationHelper().setStateHandlerRequest(REQUEST_NONE); + mRecentsView.setFirstTaskIconScaledDown(false /* isScaledDown */, false /* animate */); } diff --git a/res/values/strings.xml b/res/values/strings.xml index d6abe221bc..d8b68e74b4 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -173,12 +173,6 @@ Overview - - Allow Home screen rotation - - When phone is rotated - - Current Display setting doesn\'t permit rotation Notification dots diff --git a/res/xml/launcher_preferences.xml b/res/xml/launcher_preferences.xml index 28a35b8291..7bb19f3343 100644 --- a/res/xml/launcher_preferences.xml +++ b/res/xml/launcher_preferences.xml @@ -37,13 +37,6 @@ android:persistent="true" /> - - ()); - public static final String ALLOW_ROTATION_PREFERENCE_KEY = "pref_allowRotation"; - public static boolean isPropertyEnabled(String propertyName) { return Log.isLoggable(propertyName, Log.VERBOSE); } - public static boolean isAllowRotationPrefEnabled(Context context) { - return getPrefs(context).getBoolean(ALLOW_ROTATION_PREFERENCE_KEY, - getAllowRotationDefaultValue(context)); - } - - public static boolean getAllowRotationDefaultValue(Context context) { - if (ATLEAST_NOUGAT) { - // If the device was scaled, used the original dimensions to determine if rotation - // is allowed of not. - Resources res = context.getResources(); - int originalSmallestWidth = res.getConfiguration().smallestScreenWidthDp - * res.getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEVICE_STABLE; - return originalSmallestWidth >= 600; - } - return false; - } - /** * Given a coordinate relative to the descendant, find the coordinate in a parent view's * coordinates. diff --git a/src/com/android/launcher3/dragndrop/AddItemActivity.java b/src/com/android/launcher3/dragndrop/AddItemActivity.java index db199c1175..95e1034365 100644 --- a/src/com/android/launcher3/dragndrop/AddItemActivity.java +++ b/src/com/android/launcher3/dragndrop/AddItemActivity.java @@ -16,6 +16,11 @@ package com.android.launcher3.dragndrop; +import static com.android.launcher3.logging.LoggerUtils.newCommandAction; +import static com.android.launcher3.logging.LoggerUtils.newContainerTarget; +import static com.android.launcher3.logging.LoggerUtils.newItemTarget; +import static com.android.launcher3.logging.LoggerUtils.newLauncherEvent; + import android.annotation.TargetApi; import android.app.ActivityOptions; import android.appwidget.AppWidgetManager; @@ -23,7 +28,6 @@ import android.content.ClipData; import android.content.ClipDescription; import android.content.Intent; import android.content.pm.LauncherApps.PinItemRequest; -import android.content.res.Configuration; import android.graphics.Canvas; import android.graphics.Point; import android.graphics.PointF; @@ -43,7 +47,6 @@ import com.android.launcher3.LauncherAppState; import com.android.launcher3.LauncherAppWidgetHost; import com.android.launcher3.LauncherAppWidgetProviderInfo; import com.android.launcher3.R; -import com.android.launcher3.Utilities; import com.android.launcher3.compat.AppWidgetManagerCompat; import com.android.launcher3.compat.LauncherAppsCompatVO; import com.android.launcher3.model.WidgetItem; @@ -55,11 +58,6 @@ import com.android.launcher3.widget.PendingAddWidgetInfo; import com.android.launcher3.widget.WidgetHostViewLoader; import com.android.launcher3.widget.WidgetImageView; -import static com.android.launcher3.logging.LoggerUtils.newCommandAction; -import static com.android.launcher3.logging.LoggerUtils.newContainerTarget; -import static com.android.launcher3.logging.LoggerUtils.newItemTarget; -import static com.android.launcher3.logging.LoggerUtils.newLauncherEvent; - @TargetApi(Build.VERSION_CODES.O) public class AddItemActivity extends BaseActivity implements OnLongClickListener, OnTouchListener { @@ -154,15 +152,7 @@ public class AddItemActivity extends BaseActivity implements OnLongClickListener .setPackage(getPackageName()) .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); - if (!getResources().getBoolean(R.bool.allow_rotation) && - !Utilities.isAllowRotationPrefEnabled(this) && - (getResources().getConfiguration().orientation == - Configuration.ORIENTATION_LANDSCAPE && !isInMultiWindowMode())) { - // If we are starting the drag in landscape even though home is locked in portrait, - // restart the home activity to temporarily allow rotation. - homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); - } - + listener.initWhenReady(); startActivity(homeIntent, ActivityOptions.makeCustomAnimation(this, 0, android.R.anim.fade_out).toBundle()); mFinishOnPause = true; diff --git a/src/com/android/launcher3/dragndrop/BaseItemDragListener.java b/src/com/android/launcher3/dragndrop/BaseItemDragListener.java index 9638a75d8d..df4a7c1f56 100644 --- a/src/com/android/launcher3/dragndrop/BaseItemDragListener.java +++ b/src/com/android/launcher3/dragndrop/BaseItemDragListener.java @@ -17,6 +17,8 @@ package com.android.launcher3.dragndrop; import static com.android.launcher3.LauncherState.NORMAL; +import static com.android.launcher3.states.RotationHelper.REQUEST_LOCK; +import static com.android.launcher3.states.RotationHelper.REQUEST_NONE; import android.content.ClipDescription; import android.content.Intent; @@ -79,6 +81,7 @@ public abstract class BaseItemDragListener extends InternalStateHandler implemen AbstractFloatingView.closeAllOpenViews(launcher, alreadyOnHome); launcher.getStateManager().goToState(NORMAL, alreadyOnHome /* animated */); launcher.getDragLayer().setOnDragListener(this); + launcher.getRotationHelper().setStateHandlerRequest(REQUEST_LOCK); mLauncher = launcher; mDragController = launcher.getDragController(); @@ -157,6 +160,7 @@ public abstract class BaseItemDragListener extends InternalStateHandler implemen } private void postCleanup() { + clearReference(); if (mLauncher != null) { // Remove any drag params from the launcher intent since the drag operation is complete. Intent newIntent = new Intent(mLauncher.getIntent()); @@ -164,16 +168,12 @@ public abstract class BaseItemDragListener extends InternalStateHandler implemen mLauncher.setIntent(newIntent); } - new Handler(Looper.getMainLooper()).post(new Runnable() { - @Override - public void run() { - removeListener(); - } - }); + new Handler(Looper.getMainLooper()).post(this::removeListener); } public void removeListener() { if (mLauncher != null) { + mLauncher.getRotationHelper().setStateHandlerRequest(REQUEST_NONE); mLauncher.getDragLayer().setOnDragListener(null); } } diff --git a/src/com/android/launcher3/states/RotationHelper.java b/src/com/android/launcher3/states/RotationHelper.java new file mode 100644 index 0000000000..8f83648198 --- /dev/null +++ b/src/com/android/launcher3/states/RotationHelper.java @@ -0,0 +1,139 @@ +/* + * Copyright (C) 2018 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.launcher3.states; + +import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LOCKED; +import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_NOSENSOR; +import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; +import static android.provider.Settings.System.ACCELEROMETER_ROTATION; +import static android.provider.Settings.System.getUriFor; + +import android.app.Activity; +import android.content.ContentResolver; +import android.database.ContentObserver; +import android.os.Handler; +import android.provider.Settings; + +import com.android.launcher3.R; + +/** + * Utility class to manage launcher rotation + */ +public class RotationHelper extends ContentObserver { + + public static final int REQUEST_NONE = 0; + public static final int REQUEST_ROTATE = 1; + public static final int REQUEST_LOCK = 2; + + private final Activity mActivity; + private final ContentResolver mCr; + + private final boolean mIgnoreAutoRotateSettings; + private boolean mAutoRotateEnabled; + + /** + * Rotation request made by {@link InternalStateHandler}. This supersedes any other request. + */ + private int mStateHandlerRequest = REQUEST_NONE; + /** + * Rotation request made by a Launcher State + */ + private int mCurrentStateRequest = REQUEST_NONE; + + // This is used to defer setting rotation flags until the activity is being created + private boolean mInitialized; + public boolean mDestroyed; + + private int mLastActivityFlags = -1; + + public RotationHelper(Activity activity) { + super(new Handler()); + mActivity = activity; + + // On large devices we do not handle auto-rotate differently. + mIgnoreAutoRotateSettings = mActivity.getResources().getBoolean(R.bool.allow_rotation); + if (!mIgnoreAutoRotateSettings) { + mCr = mActivity.getContentResolver(); + mCr.registerContentObserver(getUriFor(ACCELEROMETER_ROTATION), false, this); + mAutoRotateEnabled = Settings.System.getInt(mCr, ACCELEROMETER_ROTATION, 1) == 1; + } else { + mCr = null; + } + } + + @Override + public void onChange(boolean selfChange) { + mAutoRotateEnabled = Settings.System.getInt(mCr, ACCELEROMETER_ROTATION, 1) == 1; + notifyChange(); + } + + public void setStateHandlerRequest(int request) { + if (mStateHandlerRequest != request) { + mStateHandlerRequest = request; + notifyChange(); + } + } + + public void setCurrentStateRequest(int request) { + if (mCurrentStateRequest != request) { + mCurrentStateRequest = request; + notifyChange(); + } + } + + public void initialize() { + if (!mInitialized) { + mInitialized = true; + notifyChange(); + } + } + + public void destroy() { + if (!mDestroyed) { + mDestroyed = true; + if (mCr != null) { + mCr.unregisterContentObserver(this); + } + } + } + + private void notifyChange() { + if (!mInitialized || mDestroyed) { + return; + } + + final int activityFlags; + if (mStateHandlerRequest != REQUEST_NONE) { + activityFlags = mStateHandlerRequest == REQUEST_LOCK ? + SCREEN_ORIENTATION_LOCKED : SCREEN_ORIENTATION_UNSPECIFIED; + } else if (mCurrentStateRequest == REQUEST_LOCK) { + activityFlags = SCREEN_ORIENTATION_LOCKED; + } else if (mIgnoreAutoRotateSettings || mCurrentStateRequest == REQUEST_ROTATE) { + activityFlags = SCREEN_ORIENTATION_UNSPECIFIED; + } else if (mAutoRotateEnabled) { + // If auto rotation is on, lock to device orientation + activityFlags = SCREEN_ORIENTATION_NOSENSOR; + } else { + // If auto rotation is off, allow rotation on the activity, in case the user is using + // forced rotation. + activityFlags = SCREEN_ORIENTATION_UNSPECIFIED; + } + if (activityFlags != mLastActivityFlags) { + mLastActivityFlags = activityFlags; + mActivity.setRequestedOrientation(mLastActivityFlags); + } + } +} diff --git a/src/com/android/launcher3/states/SpringLoadedState.java b/src/com/android/launcher3/states/SpringLoadedState.java index 4f8456f254..89a9e2d7d1 100644 --- a/src/com/android/launcher3/states/SpringLoadedState.java +++ b/src/com/android/launcher3/states/SpringLoadedState.java @@ -16,10 +16,9 @@ package com.android.launcher3.states; import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_TRANSITION_MS; +import static com.android.launcher3.states.RotationHelper.REQUEST_LOCK; -import android.content.pm.ActivityInfo; import android.graphics.Rect; -import android.os.Handler; import android.view.View; import com.android.launcher3.DeviceProfile; @@ -38,10 +37,6 @@ public class SpringLoadedState extends LauncherState { FLAG_DISABLE_ACCESSIBILITY | FLAG_DISABLE_RESTORE | FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED | FLAG_DISABLE_PAGE_CLIPPING | FLAG_PAGE_BACKGROUNDS; - // Determines how long to wait after a rotation before restoring the screen orientation to - // match the sensor state. - private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500; - public SpringLoadedState(int id) { super(id, ContainerType.OVERVIEW, SPRING_LOADED_TRANSITION_MS, STATE_FLAGS); } @@ -84,30 +79,16 @@ public class SpringLoadedState extends LauncherState { ws.showPageIndicatorAtCurrentScroll(); ws.getPageIndicator().setShouldAutoHide(false); - // Lock the orientation: - if (launcher.isRotationEnabled()) { - launcher.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED); - } - // Prevent any Un/InstallShortcutReceivers from updating the db while we are // in spring loaded mode InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_DRAG_AND_DROP); + launcher.getRotationHelper().setCurrentStateRequest(REQUEST_LOCK); } @Override public void onStateDisabled(final Launcher launcher) { launcher.getWorkspace().getPageIndicator().setShouldAutoHide(true); - // Unlock rotation lock - if (launcher.isRotationEnabled()) { - new Handler().postDelayed(new Runnable() { - @Override - public void run() { - launcher.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); - } - }, RESTORE_SCREEN_ORIENTATION_DELAY); - } - // Re-enable any Un/InstallShortcutReceiver and now process any queued items InstallShortcutReceiver.disableAndFlushInstallQueue( InstallShortcutReceiver.FLAG_DRAG_AND_DROP, launcher); diff --git a/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java b/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java index 9e551272d6..7d1da882f8 100644 --- a/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java +++ b/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java @@ -78,11 +78,6 @@ public abstract class AbstractLauncherUiTest { } protected void lockRotation(boolean naturalOrientation) throws RemoteException { - Utilities.getPrefs(mTargetContext) - .edit() - .putBoolean(Utilities.ALLOW_ROTATION_PREFERENCE_KEY, !naturalOrientation) - .commit(); - if (naturalOrientation) { mDevice.setOrientationNatural(); } else {