2015-03-02 11:51:23 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2015 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;
|
|
|
|
|
|
2017-10-25 15:47:38 -07:00
|
|
|
import static com.android.launcher3.LauncherState.NORMAL;
|
|
|
|
|
|
2015-03-02 11:51:23 -08:00
|
|
|
import android.animation.Animator;
|
|
|
|
|
import android.animation.AnimatorListenerAdapter;
|
|
|
|
|
import android.animation.AnimatorSet;
|
2017-10-20 17:05:27 -07:00
|
|
|
import android.os.Handler;
|
|
|
|
|
import android.os.Looper;
|
2015-03-02 11:51:23 -08:00
|
|
|
import android.view.View;
|
2015-05-26 23:03:31 -07:00
|
|
|
|
2017-10-20 17:05:27 -07:00
|
|
|
import com.android.launcher3.anim.AnimationSuccessListener;
|
2017-10-27 11:05:26 -07:00
|
|
|
import com.android.launcher3.anim.AnimatorPlaybackController;
|
2017-12-07 12:45:49 -08:00
|
|
|
import com.android.launcher3.anim.AnimatorSetBuilder;
|
2017-11-07 12:23:58 -08:00
|
|
|
import com.android.launcher3.uioverrides.UiFactory;
|
2015-05-26 23:03:31 -07:00
|
|
|
|
2015-03-02 11:51:23 -08:00
|
|
|
/**
|
|
|
|
|
* TODO: figure out what kind of tests we can write for this
|
|
|
|
|
*
|
|
|
|
|
* Things to test when changing the following class.
|
|
|
|
|
* - Home from workspace
|
|
|
|
|
* - from center screen
|
|
|
|
|
* - from other screens
|
|
|
|
|
* - Home from all apps
|
|
|
|
|
* - from center screen
|
|
|
|
|
* - from other screens
|
|
|
|
|
* - Back from all apps
|
|
|
|
|
* - from center screen
|
|
|
|
|
* - from other screens
|
|
|
|
|
* - Launch app from workspace and quit
|
|
|
|
|
* - with back
|
|
|
|
|
* - with home
|
|
|
|
|
* - Launch app from all apps and quit
|
|
|
|
|
* - with back
|
|
|
|
|
* - with home
|
|
|
|
|
* - Go to a screen that's not the default, then all
|
|
|
|
|
* apps, and launch and app, and go back
|
|
|
|
|
* - with back
|
|
|
|
|
* -with home
|
|
|
|
|
* - On workspace, long press power and go back
|
|
|
|
|
* - with back
|
|
|
|
|
* - with home
|
|
|
|
|
* - On all apps, long press power and go back
|
|
|
|
|
* - with back
|
|
|
|
|
* - with home
|
|
|
|
|
* - On workspace, power off
|
|
|
|
|
* - On all apps, power off
|
|
|
|
|
* - Launch an app and turn off the screen while in that app
|
|
|
|
|
* - Go back with home key
|
|
|
|
|
* - Go back with back key TODO: make this not go to workspace
|
|
|
|
|
* - From all apps
|
|
|
|
|
* - From workspace
|
2018-01-11 13:59:53 -08:00
|
|
|
* - Enter and exit car mode (becase it causes an extra configuration changed)
|
2015-03-02 11:51:23 -08:00
|
|
|
* - From all apps
|
|
|
|
|
* - From the center workspace
|
|
|
|
|
* - From another workspace
|
|
|
|
|
*/
|
2017-10-23 17:14:52 -07:00
|
|
|
public class LauncherStateManager {
|
2015-03-02 11:51:23 -08:00
|
|
|
|
2017-10-23 17:14:52 -07:00
|
|
|
public static final String TAG = "StateManager";
|
2015-03-02 11:51:23 -08:00
|
|
|
|
2017-10-16 11:46:41 -07:00
|
|
|
private final AnimationConfig mConfig = new AnimationConfig();
|
2017-10-20 17:05:27 -07:00
|
|
|
private final Handler mUiHandler;
|
|
|
|
|
private final Launcher mLauncher;
|
2015-03-02 11:51:23 -08:00
|
|
|
|
2017-11-07 12:23:58 -08:00
|
|
|
private StateHandler[] mStateHandlers;
|
2017-10-25 15:47:38 -07:00
|
|
|
private LauncherState mState = NORMAL;
|
|
|
|
|
|
2018-01-11 13:59:53 -08:00
|
|
|
private LauncherState mLastStableState = NORMAL;
|
|
|
|
|
private LauncherState mCurrentStableState = NORMAL;
|
|
|
|
|
|
2017-11-08 14:38:23 -08:00
|
|
|
private StateListener mStateListener;
|
|
|
|
|
|
2017-11-07 12:23:58 -08:00
|
|
|
public LauncherStateManager(Launcher l) {
|
2017-10-20 17:05:27 -07:00
|
|
|
mUiHandler = new Handler(Looper.getMainLooper());
|
2015-03-02 11:51:23 -08:00
|
|
|
mLauncher = l;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-25 15:47:38 -07:00
|
|
|
public LauncherState getState() {
|
|
|
|
|
return mState;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-08 13:39:39 -08:00
|
|
|
public StateHandler[] getStateHandlers() {
|
2017-11-07 12:23:58 -08:00
|
|
|
if (mStateHandlers == null) {
|
|
|
|
|
mStateHandlers = UiFactory.getStateHandler(mLauncher);
|
|
|
|
|
}
|
|
|
|
|
return mStateHandlers;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-08 14:38:23 -08:00
|
|
|
public void setStateListener(StateListener stateListener) {
|
|
|
|
|
mStateListener = stateListener;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-23 17:14:52 -07:00
|
|
|
/**
|
|
|
|
|
* @see #goToState(LauncherState, boolean, Runnable)
|
|
|
|
|
*/
|
|
|
|
|
public void goToState(LauncherState state) {
|
2018-01-11 09:56:07 -08:00
|
|
|
goToState(state, mLauncher.isStarted() /* animated */, 0, null);
|
2017-10-23 17:14:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see #goToState(LauncherState, boolean, Runnable)
|
|
|
|
|
*/
|
|
|
|
|
public void goToState(LauncherState state, boolean animated) {
|
|
|
|
|
goToState(state, animated, 0, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Changes the Launcher state to the provided state.
|
|
|
|
|
*
|
|
|
|
|
* @param animated false if the state should change immediately without any animation,
|
|
|
|
|
* true otherwise
|
|
|
|
|
* @paras onCompleteRunnable any action to perform at the end of the transition, of null.
|
|
|
|
|
*/
|
2017-10-20 17:05:27 -07:00
|
|
|
public void goToState(LauncherState state, boolean animated, Runnable onCompleteRunnable) {
|
2017-10-23 17:14:52 -07:00
|
|
|
goToState(state, animated, 0, onCompleteRunnable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Changes the Launcher state to the provided state after the given delay.
|
|
|
|
|
*/
|
|
|
|
|
public void goToState(LauncherState state, long delay, Runnable onCompleteRunnable) {
|
|
|
|
|
goToState(state, true, delay, onCompleteRunnable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Changes the Launcher state to the provided state after the given delay.
|
|
|
|
|
*/
|
|
|
|
|
public void goToState(LauncherState state, long delay) {
|
|
|
|
|
goToState(state, true, delay, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void goToState(LauncherState state, boolean animated, long delay,
|
|
|
|
|
Runnable onCompleteRunnable) {
|
2017-10-27 11:05:26 -07:00
|
|
|
if (mLauncher.isInState(state) && mConfig.mCurrentAnimation == null) {
|
2017-10-23 17:14:52 -07:00
|
|
|
// Run any queued runnable
|
|
|
|
|
if (onCompleteRunnable != null) {
|
|
|
|
|
onCompleteRunnable.run();
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-10 15:21:15 -07:00
|
|
|
// Cancel the current animation
|
2017-10-16 11:46:41 -07:00
|
|
|
mConfig.reset();
|
2015-03-02 11:51:23 -08:00
|
|
|
|
2017-10-16 11:46:41 -07:00
|
|
|
if (!animated) {
|
2017-12-06 10:25:07 -08:00
|
|
|
onStateTransitionStart(state);
|
2017-11-07 12:23:58 -08:00
|
|
|
for (StateHandler handler : getStateHandlers()) {
|
|
|
|
|
handler.setState(state);
|
|
|
|
|
}
|
2017-11-08 14:38:23 -08:00
|
|
|
if (mStateListener != null) {
|
|
|
|
|
mStateListener.onStateSetImmediately(state);
|
|
|
|
|
}
|
2017-12-06 10:25:07 -08:00
|
|
|
onStateTransitionEnd(state);
|
2015-10-02 16:22:08 -07:00
|
|
|
|
2017-10-20 17:05:27 -07:00
|
|
|
// Run any queued runnable
|
2016-06-06 14:19:02 -07:00
|
|
|
if (onCompleteRunnable != null) {
|
|
|
|
|
onCompleteRunnable.run();
|
|
|
|
|
}
|
2016-08-03 15:14:43 -07:00
|
|
|
return;
|
2016-06-06 14:19:02 -07:00
|
|
|
}
|
2015-03-02 11:51:23 -08:00
|
|
|
|
2017-10-27 11:05:26 -07:00
|
|
|
// Since state NORMAL can be reached from multiple states, just assume that the
|
|
|
|
|
// transition plays in reverse and use the same duration as previous state.
|
|
|
|
|
mConfig.duration = state == NORMAL ? mState.transitionDuration : state.transitionDuration;
|
|
|
|
|
|
2017-12-07 12:45:49 -08:00
|
|
|
AnimatorSet animation = createAnimationToNewWorkspaceInternal(
|
|
|
|
|
state, new AnimatorSetBuilder(), onCompleteRunnable);
|
2017-10-20 17:05:27 -07:00
|
|
|
Runnable runnable = new StartAnimRunnable(animation, state.getFinalFocus(mLauncher));
|
2017-10-23 17:14:52 -07:00
|
|
|
if (delay > 0) {
|
|
|
|
|
mUiHandler.postDelayed(runnable, delay);
|
2017-10-10 15:21:15 -07:00
|
|
|
} else {
|
2017-10-27 11:05:26 -07:00
|
|
|
mUiHandler.post(runnable);
|
2017-10-10 15:21:15 -07:00
|
|
|
}
|
|
|
|
|
}
|
2015-03-02 11:51:23 -08:00
|
|
|
|
2017-10-27 11:05:26 -07:00
|
|
|
/**
|
|
|
|
|
* Creates a {@link AnimatorPlaybackController} that can be used for a controlled
|
|
|
|
|
* state transition.
|
|
|
|
|
* @param state the final state for the transition.
|
|
|
|
|
* @param duration intended duration for normal playback. Use higher duration for better
|
|
|
|
|
* accuracy.
|
|
|
|
|
*/
|
2017-11-06 13:00:42 -08:00
|
|
|
public AnimatorPlaybackController createAnimationToNewWorkspace(
|
2017-10-27 11:05:26 -07:00
|
|
|
LauncherState state, long duration) {
|
2017-12-07 12:45:49 -08:00
|
|
|
return createAnimationToNewWorkspace(state, new AnimatorSetBuilder(), duration);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AnimatorPlaybackController createAnimationToNewWorkspace(
|
|
|
|
|
LauncherState state, AnimatorSetBuilder builder, long duration) {
|
2017-10-20 17:05:27 -07:00
|
|
|
mConfig.reset();
|
2017-10-27 11:05:26 -07:00
|
|
|
mConfig.userControlled = true;
|
|
|
|
|
mConfig.duration = duration;
|
|
|
|
|
return AnimatorPlaybackController.wrap(
|
2017-12-07 12:45:49 -08:00
|
|
|
createAnimationToNewWorkspaceInternal(state, builder, null), duration);
|
2017-10-27 11:05:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected AnimatorSet createAnimationToNewWorkspaceInternal(final LauncherState state,
|
2017-12-07 12:45:49 -08:00
|
|
|
AnimatorSetBuilder builder, final Runnable onCompleteRunnable) {
|
2017-11-07 12:23:58 -08:00
|
|
|
for (StateHandler handler : getStateHandlers()) {
|
2017-12-07 12:45:49 -08:00
|
|
|
builder.startTag(handler);
|
2017-12-07 11:42:33 -08:00
|
|
|
handler.setStateWithAnimation(state, builder, mConfig);
|
2017-11-07 12:23:58 -08:00
|
|
|
}
|
2017-12-07 12:45:49 -08:00
|
|
|
|
|
|
|
|
final AnimatorSet animation = builder.build();
|
2017-10-20 17:05:27 -07:00
|
|
|
animation.addListener(new AnimationSuccessListener() {
|
2017-10-25 15:47:38 -07:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onAnimationStart(Animator animation) {
|
|
|
|
|
// Change the internal state only when the transition actually starts
|
2017-12-06 10:25:07 -08:00
|
|
|
onStateTransitionStart(state);
|
2017-11-08 14:38:23 -08:00
|
|
|
if (mStateListener != null) {
|
|
|
|
|
mStateListener.onStateTransitionStart(state);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onAnimationEnd(Animator animation) {
|
|
|
|
|
super.onAnimationEnd(animation);
|
|
|
|
|
if (mStateListener != null) {
|
|
|
|
|
mStateListener.onStateTransitionComplete(mState);
|
|
|
|
|
}
|
2017-10-25 15:47:38 -07:00
|
|
|
}
|
|
|
|
|
|
2017-10-16 11:46:41 -07:00
|
|
|
@Override
|
2017-10-20 17:05:27 -07:00
|
|
|
public void onAnimationSuccess(Animator animator) {
|
2017-10-16 11:46:41 -07:00
|
|
|
// Run any queued runnables
|
|
|
|
|
if (onCompleteRunnable != null) {
|
|
|
|
|
onCompleteRunnable.run();
|
|
|
|
|
}
|
2017-12-06 10:25:07 -08:00
|
|
|
onStateTransitionEnd(state);
|
2017-10-16 11:46:41 -07:00
|
|
|
}
|
|
|
|
|
});
|
2017-10-20 17:05:27 -07:00
|
|
|
mConfig.setAnimation(animation);
|
|
|
|
|
return mConfig.mCurrentAnimation;
|
2015-03-02 11:51:23 -08:00
|
|
|
}
|
|
|
|
|
|
2017-12-06 10:25:07 -08:00
|
|
|
private void onStateTransitionStart(LauncherState state) {
|
2017-10-25 15:47:38 -07:00
|
|
|
mState.onStateDisabled(mLauncher);
|
|
|
|
|
mState = state;
|
|
|
|
|
mState.onStateEnabled(mLauncher);
|
2017-11-14 16:55:22 -08:00
|
|
|
mLauncher.getAppWidgetHost().setResumed(state == LauncherState.NORMAL);
|
2017-12-06 10:25:07 -08:00
|
|
|
|
|
|
|
|
if (state.disablePageClipping) {
|
|
|
|
|
// Only disable clipping if needed, otherwise leave it as previous value.
|
|
|
|
|
mLauncher.getWorkspace().setClipChildren(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void onStateTransitionEnd(LauncherState state) {
|
2018-01-11 13:59:53 -08:00
|
|
|
// Only change the stable states after the transitions have finished
|
|
|
|
|
if (state != mCurrentStableState) {
|
|
|
|
|
mLastStableState = state.getHistoryForState(mCurrentStableState);
|
|
|
|
|
mCurrentStableState = state;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-06 10:25:07 -08:00
|
|
|
mLauncher.getWorkspace().setClipChildren(!state.disablePageClipping);
|
|
|
|
|
mLauncher.getUserEventDispatcher().resetElapsedContainerMillis();
|
2017-12-22 11:11:33 -08:00
|
|
|
mLauncher.finishAutoCancelActionMode();
|
2017-10-25 15:47:38 -07:00
|
|
|
}
|
|
|
|
|
|
2018-01-11 13:59:53 -08:00
|
|
|
public LauncherState getLastState() {
|
|
|
|
|
return mLastStableState;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 11:51:23 -08:00
|
|
|
/**
|
|
|
|
|
* Cancels the current animation.
|
|
|
|
|
*/
|
2017-10-20 17:05:27 -07:00
|
|
|
public void cancelAnimation() {
|
|
|
|
|
mConfig.reset();
|
2015-08-03 14:40:11 -07:00
|
|
|
}
|
2016-10-26 19:12:47 -07:00
|
|
|
|
|
|
|
|
private class StartAnimRunnable implements Runnable {
|
|
|
|
|
|
|
|
|
|
private final AnimatorSet mAnim;
|
|
|
|
|
private final View mViewToFocus;
|
|
|
|
|
|
|
|
|
|
public StartAnimRunnable(AnimatorSet anim, View viewToFocus) {
|
|
|
|
|
mAnim = anim;
|
|
|
|
|
mViewToFocus = viewToFocus;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
2017-10-20 17:05:27 -07:00
|
|
|
if (mConfig.mCurrentAnimation != mAnim) {
|
2016-10-26 19:12:47 -07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (mViewToFocus != null) {
|
|
|
|
|
mViewToFocus.requestFocus();
|
|
|
|
|
}
|
|
|
|
|
mAnim.start();
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-16 11:46:41 -07:00
|
|
|
|
2017-10-20 17:05:27 -07:00
|
|
|
public static class AnimationConfig extends AnimatorListenerAdapter {
|
2017-10-25 15:47:38 -07:00
|
|
|
public long duration;
|
2017-10-27 11:05:26 -07:00
|
|
|
public boolean userControlled;
|
2017-10-16 11:46:41 -07:00
|
|
|
|
2017-10-20 17:05:27 -07:00
|
|
|
private AnimatorSet mCurrentAnimation;
|
2017-10-16 11:46:41 -07:00
|
|
|
|
|
|
|
|
public void reset() {
|
2017-10-25 15:47:38 -07:00
|
|
|
duration = 0;
|
2017-10-27 11:05:26 -07:00
|
|
|
userControlled = false;
|
2017-10-20 17:05:27 -07:00
|
|
|
|
|
|
|
|
if (mCurrentAnimation != null) {
|
|
|
|
|
mCurrentAnimation.setDuration(0);
|
|
|
|
|
mCurrentAnimation.cancel();
|
|
|
|
|
mCurrentAnimation = null;
|
|
|
|
|
}
|
2017-10-16 11:46:41 -07:00
|
|
|
}
|
|
|
|
|
|
2017-10-20 17:05:27 -07:00
|
|
|
@Override
|
|
|
|
|
public void onAnimationEnd(Animator animation) {
|
|
|
|
|
if (mCurrentAnimation == animation) {
|
|
|
|
|
mCurrentAnimation = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setAnimation(AnimatorSet animation) {
|
|
|
|
|
mCurrentAnimation = animation;
|
|
|
|
|
mCurrentAnimation.addListener(this);
|
|
|
|
|
}
|
2017-10-16 11:46:41 -07:00
|
|
|
}
|
2017-11-07 12:23:58 -08:00
|
|
|
|
|
|
|
|
public interface StateHandler {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Updates the UI to {@param state} without any animations
|
|
|
|
|
*/
|
|
|
|
|
void setState(LauncherState state);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the UI to {@param state} by animating any changes.
|
|
|
|
|
*/
|
2017-12-07 11:42:33 -08:00
|
|
|
void setStateWithAnimation(LauncherState toState,
|
2017-12-07 12:45:49 -08:00
|
|
|
AnimatorSetBuilder builder, AnimationConfig config);
|
2017-11-07 12:23:58 -08:00
|
|
|
}
|
2017-11-08 14:38:23 -08:00
|
|
|
|
|
|
|
|
public interface StateListener {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Called when the state is set without an animation.
|
|
|
|
|
*/
|
|
|
|
|
void onStateSetImmediately(LauncherState state);
|
|
|
|
|
|
|
|
|
|
void onStateTransitionStart(LauncherState toState);
|
|
|
|
|
void onStateTransitionComplete(LauncherState finalState);
|
|
|
|
|
}
|
2015-05-26 23:03:31 -07:00
|
|
|
}
|