Selim Cinek | c27437b | 2014-05-14 10:23:33 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.statusbar; |
| 18 | |
| 19 | import android.view.View; |
| 20 | import android.view.ViewPropertyAnimator; |
| 21 | import android.view.animation.AnimationUtils; |
| 22 | import android.view.animation.Interpolator; |
| 23 | |
| 24 | import java.util.HashMap; |
| 25 | import java.util.Map; |
| 26 | |
| 27 | /** |
| 28 | * A state of a {@link com.android.systemui.statusbar.SpeedBumpDotsLayout} |
| 29 | */ |
| 30 | public class SpeedBumpDotsState { |
| 31 | |
| 32 | public static final int HIDDEN = 1; |
| 33 | public static final int SHOWN = 2; |
| 34 | private static final int VISIBILITY_ANIMATION_DELAY_PER_ELEMENT = 80; |
| 35 | |
| 36 | private final SpeedBumpDotsLayout mHostView; |
| 37 | private final HashMap<View, ViewState> mStateMap = new HashMap<View, ViewState>(); |
| 38 | private final Interpolator mFastOutSlowInInterpolator; |
| 39 | private int mActiveState = 0; |
| 40 | |
| 41 | public SpeedBumpDotsState(SpeedBumpDotsLayout hostLayout) { |
| 42 | mHostView = hostLayout; |
| 43 | mFastOutSlowInInterpolator = AnimationUtils |
| 44 | .loadInterpolator(hostLayout.getContext(), |
| 45 | android.R.interpolator.fast_out_slow_in); |
| 46 | } |
| 47 | |
| 48 | public SpeedBumpDotsLayout getHostView() { |
| 49 | return mHostView; |
| 50 | } |
| 51 | |
| 52 | public void resetViewStates() { |
| 53 | int numChildren = mHostView.getChildCount(); |
| 54 | for (int i = 0; i < numChildren; i++) { |
| 55 | View child = mHostView.getChildAt(i); |
| 56 | ViewState viewState = mStateMap.get(child); |
| 57 | if (viewState == null) { |
| 58 | viewState = new ViewState(); |
| 59 | mStateMap.put(child, viewState); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | public ViewState getViewStateForView(View requestedView) { |
| 65 | return mStateMap.get(requestedView); |
| 66 | } |
| 67 | |
| 68 | public void apply() { |
| 69 | int childCount = mHostView.getChildCount(); |
| 70 | for (int i = 0; i < childCount; i++) { |
| 71 | View child = mHostView.getChildAt(i); |
| 72 | ViewState viewState = mStateMap.get(child); |
Selim Cinek | 9c80964 | 2014-05-24 18:02:16 +0200 | [diff] [blame] | 73 | |
| 74 | child.setTranslationX(viewState.xTranslation); |
| 75 | child.setTranslationY(viewState.yTranslation); |
| 76 | child.setScaleX(viewState.scale); |
| 77 | child.setScaleY(viewState.scale); |
| 78 | child.setAlpha(viewState.alpha); |
Selim Cinek | c27437b | 2014-05-14 10:23:33 +0200 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | public void animateToState() { |
| 83 | int childCount = mHostView.getChildCount(); |
| 84 | int middleIndex = (childCount - 1) / 2; |
| 85 | long delayPerElement = VISIBILITY_ANIMATION_DELAY_PER_ELEMENT; |
| 86 | boolean isAppearing = getActiveState() == SHOWN; |
| 87 | boolean isDisappearing = getActiveState() == HIDDEN; |
| 88 | for (int i = 0; i < childCount; i++) { |
| 89 | int delayIndex; |
| 90 | if (i <= middleIndex) { |
| 91 | delayIndex = i * 2; |
| 92 | } else { |
| 93 | int distToMiddle = i - middleIndex; |
| 94 | delayIndex = (childCount - 1) - (distToMiddle - 1) * 2; |
| 95 | } |
| 96 | long startDelay = 0; |
| 97 | if (isAppearing || isDisappearing) { |
| 98 | if (isDisappearing) { |
| 99 | delayIndex = childCount - 1 - delayIndex; |
| 100 | } |
| 101 | startDelay = delayIndex * delayPerElement; |
| 102 | } |
| 103 | View child = mHostView.getChildAt(i); |
| 104 | ViewState viewState = mStateMap.get(child); |
| 105 | child.animate().setInterpolator(mFastOutSlowInInterpolator) |
| 106 | .setStartDelay(startDelay) |
Selim Cinek | 9c80964 | 2014-05-24 18:02:16 +0200 | [diff] [blame] | 107 | .alpha(viewState.alpha) |
Selim Cinek | c27437b | 2014-05-14 10:23:33 +0200 | [diff] [blame] | 108 | .translationX(viewState.xTranslation) |
| 109 | .translationY(viewState.yTranslation) |
| 110 | .scaleX(viewState.scale).scaleY(viewState.scale); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | public int getActiveState() { |
| 115 | return mActiveState; |
| 116 | } |
| 117 | |
| 118 | public void setActiveState(int mActiveState) { |
| 119 | this.mActiveState = mActiveState; |
| 120 | } |
| 121 | |
| 122 | public static class ViewState { |
| 123 | float xTranslation; |
| 124 | float yTranslation; |
| 125 | float alpha; |
| 126 | float scale; |
| 127 | } |
| 128 | } |