2015-04-20 18:26:57 -07: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-16 11:46:41 -07:00
|
|
|
import static com.android.launcher3.LauncherAnimUtils.DRAWABLE_ALPHA;
|
|
|
|
|
import static com.android.launcher3.LauncherAnimUtils.SCALE_PROPERTY;
|
|
|
|
|
|
2015-04-20 18:26:57 -07:00
|
|
|
import android.animation.Animator;
|
|
|
|
|
import android.animation.AnimatorListenerAdapter;
|
|
|
|
|
import android.animation.AnimatorSet;
|
2015-06-05 00:13:25 -07:00
|
|
|
import android.animation.ObjectAnimator;
|
2015-04-20 18:26:57 -07:00
|
|
|
import android.animation.TimeInterpolator;
|
|
|
|
|
import android.animation.ValueAnimator;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.res.Resources;
|
2017-10-16 11:46:41 -07:00
|
|
|
import android.util.Property;
|
2015-04-20 18:26:57 -07:00
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.accessibility.AccessibilityManager;
|
2015-05-04 15:50:25 -07:00
|
|
|
|
2017-10-23 17:14:52 -07:00
|
|
|
import com.android.launcher3.LauncherStateManager.AnimationConfig;
|
2016-10-26 18:32:38 -07:00
|
|
|
import com.android.launcher3.anim.AnimationLayerSet;
|
2017-10-26 15:36:10 -07:00
|
|
|
import com.android.launcher3.anim.Interpolators;
|
2015-04-20 18:26:57 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A convenience class to update a view's visibility state after an alpha animation.
|
|
|
|
|
*/
|
|
|
|
|
class AlphaUpdateListener extends AnimatorListenerAdapter implements ValueAnimator.AnimatorUpdateListener {
|
|
|
|
|
private static final float ALPHA_CUTOFF_THRESHOLD = 0.01f;
|
|
|
|
|
|
|
|
|
|
private View mView;
|
|
|
|
|
private boolean mAccessibilityEnabled;
|
2016-06-21 16:37:13 -07:00
|
|
|
private boolean mCanceled = false;
|
2015-04-20 18:26:57 -07:00
|
|
|
|
|
|
|
|
public AlphaUpdateListener(View v, boolean accessibilityEnabled) {
|
|
|
|
|
mView = v;
|
|
|
|
|
mAccessibilityEnabled = accessibilityEnabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onAnimationUpdate(ValueAnimator arg0) {
|
|
|
|
|
updateVisibility(mView, mAccessibilityEnabled);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void updateVisibility(View view, boolean accessibilityEnabled) {
|
|
|
|
|
// We want to avoid the extra layout pass by setting the views to GONE unless
|
|
|
|
|
// accessibility is on, in which case not setting them to GONE causes a glitch.
|
|
|
|
|
int invisibleState = accessibilityEnabled ? View.GONE : View.INVISIBLE;
|
|
|
|
|
if (view.getAlpha() < ALPHA_CUTOFF_THRESHOLD && view.getVisibility() != invisibleState) {
|
|
|
|
|
view.setVisibility(invisibleState);
|
|
|
|
|
} else if (view.getAlpha() > ALPHA_CUTOFF_THRESHOLD
|
|
|
|
|
&& view.getVisibility() != View.VISIBLE) {
|
|
|
|
|
view.setVisibility(View.VISIBLE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-21 16:37:13 -07:00
|
|
|
@Override
|
|
|
|
|
public void onAnimationCancel(Animator animation) {
|
|
|
|
|
mCanceled = true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-20 18:26:57 -07:00
|
|
|
@Override
|
|
|
|
|
public void onAnimationEnd(Animator arg0) {
|
2016-06-21 16:37:13 -07:00
|
|
|
if (mCanceled) return;
|
2015-04-20 18:26:57 -07:00
|
|
|
updateVisibility(mView, mAccessibilityEnabled);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onAnimationStart(Animator arg0) {
|
|
|
|
|
// We want the views to be visible for animation, so fade-in/out is visible
|
|
|
|
|
mView.setVisibility(View.VISIBLE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Manages the animations between each of the workspace states.
|
|
|
|
|
*/
|
|
|
|
|
public class WorkspaceStateTransitionAnimation {
|
|
|
|
|
|
2017-11-07 12:23:58 -08:00
|
|
|
public static final PropertySetter NO_ANIM_PROPERTY_SETTER = new PropertySetter();
|
2015-04-20 18:26:57 -07:00
|
|
|
|
2017-10-16 11:46:41 -07:00
|
|
|
public final int mWorkspaceScrimAlpha;
|
2015-04-20 18:26:57 -07:00
|
|
|
|
2017-10-16 11:46:41 -07:00
|
|
|
private final Launcher mLauncher;
|
|
|
|
|
private final Workspace mWorkspace;
|
2015-04-20 18:26:57 -07:00
|
|
|
|
2017-10-16 11:46:41 -07:00
|
|
|
private final boolean mWorkspaceFadeInAdjacentScreens;
|
2015-04-20 18:26:57 -07:00
|
|
|
|
2017-10-16 11:46:41 -07:00
|
|
|
private float mNewScale;
|
2015-04-20 18:26:57 -07:00
|
|
|
|
|
|
|
|
public WorkspaceStateTransitionAnimation(Launcher launcher, Workspace workspace) {
|
|
|
|
|
mLauncher = launcher;
|
|
|
|
|
mWorkspace = workspace;
|
|
|
|
|
|
2015-05-06 11:42:25 -07:00
|
|
|
DeviceProfile grid = mLauncher.getDeviceProfile();
|
2015-04-20 18:26:57 -07:00
|
|
|
Resources res = launcher.getResources();
|
2017-10-16 11:46:41 -07:00
|
|
|
mWorkspaceScrimAlpha = res.getInteger(R.integer.config_workspaceScrimAlpha);
|
2015-04-20 18:26:57 -07:00
|
|
|
mWorkspaceFadeInAdjacentScreens = grid.shouldFadeAdjacentWorkspaceScreens();
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-17 17:17:16 -07:00
|
|
|
public void setState(LauncherState toState) {
|
2017-10-16 11:46:41 -07:00
|
|
|
setWorkspaceProperty(toState, NO_ANIM_PROPERTY_SETTER);
|
2015-07-08 15:44:27 -07:00
|
|
|
}
|
|
|
|
|
|
2017-10-25 15:47:38 -07:00
|
|
|
public void setStateWithAnimation(LauncherState toState, AnimatorSet anim,
|
|
|
|
|
AnimationLayerSet layerViews, AnimationConfig config) {
|
2017-10-17 17:17:16 -07:00
|
|
|
AnimatedPropertySetter propertySetter =
|
2017-10-25 15:47:38 -07:00
|
|
|
new AnimatedPropertySetter(config.duration, layerViews, anim);
|
2017-10-17 17:17:16 -07:00
|
|
|
setWorkspaceProperty(toState, propertySetter);
|
2015-04-20 18:26:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float getFinalScale() {
|
|
|
|
|
return mNewScale;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:18:17 -07:00
|
|
|
/**
|
|
|
|
|
* Starts a transition animation for the workspace.
|
|
|
|
|
*/
|
2017-10-17 17:17:16 -07:00
|
|
|
private void setWorkspaceProperty(LauncherState state, PropertySetter propertySetter) {
|
2017-10-19 16:15:09 -07:00
|
|
|
float[] scaleAndTranslationY = state.getWorkspaceScaleAndTranslation(mLauncher);
|
2017-10-23 15:04:10 -07:00
|
|
|
mNewScale = scaleAndTranslationY[0];
|
2017-10-19 16:15:09 -07:00
|
|
|
final float finalWorkspaceTranslationY = scaleAndTranslationY[1];
|
2015-04-20 18:26:57 -07:00
|
|
|
|
2015-07-08 15:44:27 -07:00
|
|
|
int toPage = mWorkspace.getPageNearestToCenterOfScreen();
|
2017-10-16 11:46:41 -07:00
|
|
|
final int childCount = mWorkspace.getChildCount();
|
2015-04-20 18:26:57 -07:00
|
|
|
for (int i = 0; i < childCount; i++) {
|
2017-10-24 10:32:40 -07:00
|
|
|
applyChildState(state, (CellLayout) mWorkspace.getChildAt(i), i, toPage,
|
|
|
|
|
propertySetter);
|
2017-10-16 11:46:41 -07:00
|
|
|
}
|
2015-04-20 18:26:57 -07:00
|
|
|
|
2017-10-26 15:36:10 -07:00
|
|
|
propertySetter.setFloat(mWorkspace, SCALE_PROPERTY, mNewScale, Interpolators.ZOOM_IN);
|
2017-10-16 11:46:41 -07:00
|
|
|
propertySetter.setFloat(mWorkspace, View.TRANSLATION_Y,
|
2017-10-26 15:36:10 -07:00
|
|
|
finalWorkspaceTranslationY, Interpolators.ZOOM_IN);
|
2017-10-16 11:46:41 -07:00
|
|
|
|
|
|
|
|
// Set scrim
|
|
|
|
|
propertySetter.setInt(mLauncher.getDragLayer().getScrim(), DRAWABLE_ALPHA,
|
2017-10-26 15:36:10 -07:00
|
|
|
state.hasScrim ? mWorkspaceScrimAlpha : 0, Interpolators.DEACCEL_1_5);
|
2017-10-16 11:46:41 -07:00
|
|
|
}
|
|
|
|
|
|
2017-10-24 10:32:40 -07:00
|
|
|
public void applyChildState(LauncherState state, CellLayout cl, int childIndex) {
|
|
|
|
|
applyChildState(state, cl, childIndex, mWorkspace.getPageNearestToCenterOfScreen(),
|
|
|
|
|
NO_ANIM_PROPERTY_SETTER);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void applyChildState(LauncherState state, CellLayout cl, int childIndex,
|
|
|
|
|
int centerPage, PropertySetter propertySetter) {
|
|
|
|
|
propertySetter.setInt(cl.getScrimBackground(),
|
2017-10-26 15:36:10 -07:00
|
|
|
DRAWABLE_ALPHA, state.hasScrim ? 255 : 0, Interpolators.ZOOM_IN);
|
2017-10-24 10:32:40 -07:00
|
|
|
|
|
|
|
|
// Only animate the page alpha when we actually fade pages
|
|
|
|
|
if (mWorkspaceFadeInAdjacentScreens) {
|
|
|
|
|
float finalAlpha = state == LauncherState.NORMAL && childIndex != centerPage ? 0 : 1f;
|
|
|
|
|
propertySetter.setFloat(cl.getShortcutsAndWidgets(), View.ALPHA,
|
2017-10-26 15:36:10 -07:00
|
|
|
finalAlpha, Interpolators.ZOOM_IN);
|
2017-10-24 10:32:40 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-07 12:23:58 -08:00
|
|
|
public static class PropertySetter {
|
2017-10-16 11:46:41 -07:00
|
|
|
|
|
|
|
|
public void setViewAlpha(Animator anim, View view, float alpha) {
|
|
|
|
|
if (anim != null) {
|
|
|
|
|
anim.end();
|
|
|
|
|
return;
|
2015-04-20 18:26:57 -07:00
|
|
|
}
|
2017-10-16 11:46:41 -07:00
|
|
|
view.setAlpha(alpha);
|
|
|
|
|
AlphaUpdateListener.updateVisibility(view, isAccessibilityEnabled(view));
|
2015-04-20 18:26:57 -07:00
|
|
|
}
|
|
|
|
|
|
2017-10-16 11:46:41 -07:00
|
|
|
public <T> void setFloat(T target, Property<T, Float> property, float value,
|
|
|
|
|
TimeInterpolator interpolator) {
|
|
|
|
|
property.set(target, value);
|
|
|
|
|
}
|
2017-07-28 11:56:47 -07:00
|
|
|
|
2017-10-16 11:46:41 -07:00
|
|
|
public <T> void setInt(T target, Property<T, Integer> property, int value,
|
|
|
|
|
TimeInterpolator interpolator) {
|
|
|
|
|
property.set(target, value);
|
|
|
|
|
}
|
2017-07-28 11:56:47 -07:00
|
|
|
|
2017-10-16 11:46:41 -07:00
|
|
|
protected boolean isAccessibilityEnabled(View v) {
|
|
|
|
|
AccessibilityManager am = (AccessibilityManager)
|
|
|
|
|
v.getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
|
|
|
|
|
return am.isEnabled();
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-21 16:37:13 -07:00
|
|
|
|
2017-11-07 12:23:58 -08:00
|
|
|
public static class AnimatedPropertySetter extends PropertySetter {
|
2017-10-16 11:46:41 -07:00
|
|
|
|
|
|
|
|
private final long mDuration;
|
|
|
|
|
private final AnimationLayerSet mLayerViews;
|
|
|
|
|
private final AnimatorSet mStateAnimator;
|
|
|
|
|
|
2017-11-07 12:23:58 -08:00
|
|
|
public AnimatedPropertySetter(
|
|
|
|
|
long duration, AnimationLayerSet layerView, AnimatorSet anim) {
|
2017-10-16 11:46:41 -07:00
|
|
|
mDuration = duration;
|
|
|
|
|
mLayerViews = layerView;
|
|
|
|
|
mStateAnimator = anim;
|
|
|
|
|
}
|
2016-10-26 19:12:47 -07:00
|
|
|
|
2017-10-16 11:46:41 -07:00
|
|
|
@Override
|
|
|
|
|
public void setViewAlpha(Animator anim, View view, float alpha) {
|
|
|
|
|
if (anim == null) {
|
|
|
|
|
if (view.getAlpha() == alpha) {
|
|
|
|
|
return;
|
2015-04-20 18:26:57 -07:00
|
|
|
}
|
2017-10-16 11:46:41 -07:00
|
|
|
anim = ObjectAnimator.ofFloat(view, View.ALPHA, alpha);
|
|
|
|
|
anim.addListener(new AlphaUpdateListener(view, isAccessibilityEnabled(view)));
|
2015-05-04 15:50:25 -07:00
|
|
|
}
|
2017-10-16 11:46:41 -07:00
|
|
|
|
|
|
|
|
anim.setDuration(mDuration).setInterpolator(getFadeInterpolator(alpha));
|
|
|
|
|
mLayerViews.addView(view);
|
|
|
|
|
mStateAnimator.play(anim);
|
2015-04-20 18:26:57 -07:00
|
|
|
}
|
|
|
|
|
|
2017-10-16 11:46:41 -07:00
|
|
|
@Override
|
|
|
|
|
public <T> void setFloat(T target, Property<T, Float> property, float value,
|
|
|
|
|
TimeInterpolator interpolator) {
|
|
|
|
|
if (property.get(target) == value) {
|
|
|
|
|
return;
|
2015-04-20 18:26:57 -07:00
|
|
|
}
|
2017-10-16 11:46:41 -07:00
|
|
|
Animator anim = ObjectAnimator.ofFloat(target, property, value);
|
|
|
|
|
anim.setDuration(mDuration).setInterpolator(interpolator);
|
|
|
|
|
mStateAnimator.play(anim);
|
2015-04-20 18:26:57 -07:00
|
|
|
}
|
|
|
|
|
|
2017-10-16 11:46:41 -07:00
|
|
|
@Override
|
|
|
|
|
public <T> void setInt(T target, Property<T, Integer> property, int value,
|
|
|
|
|
TimeInterpolator interpolator) {
|
|
|
|
|
if (property.get(target) == value) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Animator anim = ObjectAnimator.ofInt(target, property, value);
|
|
|
|
|
anim.setDuration(mDuration).setInterpolator(interpolator);
|
|
|
|
|
mStateAnimator.play(anim);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private TimeInterpolator getFadeInterpolator(float finalAlpha) {
|
2017-10-26 15:36:10 -07:00
|
|
|
return finalAlpha == 0 ? Interpolators.DEACCEL_2 : null;
|
2015-04-20 18:26:57 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|