Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | package org.mozilla.gecko.toolbar; |
michael@0 | 7 | |
michael@0 | 8 | import org.mozilla.gecko.animation.Rotate3DAnimation; |
michael@0 | 9 | import org.mozilla.gecko.R; |
michael@0 | 10 | import org.mozilla.gecko.widget.ThemedTextSwitcher; |
michael@0 | 11 | |
michael@0 | 12 | import android.content.Context; |
michael@0 | 13 | import android.os.Build; |
michael@0 | 14 | import android.view.accessibility.AccessibilityNodeInfo; |
michael@0 | 15 | import android.view.animation.Animation; |
michael@0 | 16 | import android.view.animation.AnimationSet; |
michael@0 | 17 | import android.view.animation.AlphaAnimation; |
michael@0 | 18 | import android.view.LayoutInflater; |
michael@0 | 19 | import android.view.View; |
michael@0 | 20 | import android.util.AttributeSet; |
michael@0 | 21 | import android.widget.ViewSwitcher; |
michael@0 | 22 | |
michael@0 | 23 | public class TabCounter extends ThemedTextSwitcher |
michael@0 | 24 | implements ViewSwitcher.ViewFactory { |
michael@0 | 25 | |
michael@0 | 26 | private static final float CENTER_X = 0.5f; |
michael@0 | 27 | private static final float CENTER_Y = 1.25f; |
michael@0 | 28 | private static final int DURATION = 500; |
michael@0 | 29 | private static final float Z_DISTANCE = 200; |
michael@0 | 30 | |
michael@0 | 31 | private final AnimationSet mFlipInForward; |
michael@0 | 32 | private final AnimationSet mFlipInBackward; |
michael@0 | 33 | private final AnimationSet mFlipOutForward; |
michael@0 | 34 | private final AnimationSet mFlipOutBackward; |
michael@0 | 35 | private final LayoutInflater mInflater; |
michael@0 | 36 | |
michael@0 | 37 | private int mCount = 0; |
michael@0 | 38 | |
michael@0 | 39 | private enum FadeMode { |
michael@0 | 40 | FADE_IN, |
michael@0 | 41 | FADE_OUT |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | public TabCounter(Context context, AttributeSet attrs) { |
michael@0 | 45 | super(context, attrs); |
michael@0 | 46 | mInflater = LayoutInflater.from(context); |
michael@0 | 47 | |
michael@0 | 48 | mFlipInForward = createAnimation(-90, 0, FadeMode.FADE_IN, -1 * Z_DISTANCE, false); |
michael@0 | 49 | mFlipInBackward = createAnimation(90, 0, FadeMode.FADE_IN, Z_DISTANCE, false); |
michael@0 | 50 | mFlipOutForward = createAnimation(0, -90, FadeMode.FADE_OUT, -1 * Z_DISTANCE, true); |
michael@0 | 51 | mFlipOutBackward = createAnimation(0, 90, FadeMode.FADE_OUT, Z_DISTANCE, true); |
michael@0 | 52 | |
michael@0 | 53 | removeAllViews(); |
michael@0 | 54 | setFactory(this); |
michael@0 | 55 | |
michael@0 | 56 | if (Build.VERSION.SDK_INT >= 16) { |
michael@0 | 57 | // This adds the TextSwitcher to the a11y node tree, where we in turn |
michael@0 | 58 | // could make it return an empty info node. If we don't do this the |
michael@0 | 59 | // TextSwitcher's child TextViews get picked up, and we don't want |
michael@0 | 60 | // that since the tabs ImageButton is already properly labeled for |
michael@0 | 61 | // accessibility. |
michael@0 | 62 | setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES); |
michael@0 | 63 | setAccessibilityDelegate(new View.AccessibilityDelegate() { |
michael@0 | 64 | @Override |
michael@0 | 65 | public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {} |
michael@0 | 66 | }); |
michael@0 | 67 | } |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | void setCountWithAnimation(int count) { |
michael@0 | 71 | // Don't animate from initial state |
michael@0 | 72 | if (mCount == 0) { |
michael@0 | 73 | setCount(count); |
michael@0 | 74 | return; |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | if (mCount == count) { |
michael@0 | 78 | return; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | if (count < mCount) { |
michael@0 | 82 | setInAnimation(mFlipInBackward); |
michael@0 | 83 | setOutAnimation(mFlipOutForward); |
michael@0 | 84 | } else { |
michael@0 | 85 | setInAnimation(mFlipInForward); |
michael@0 | 86 | setOutAnimation(mFlipOutBackward); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | // Eliminate screen artifact. Set explicit In/Out animation pair order. This will always |
michael@0 | 90 | // animate pair in In->Out child order, prevent alternating use of the Out->In case. |
michael@0 | 91 | setDisplayedChild(0); |
michael@0 | 92 | |
michael@0 | 93 | // Set In value, trigger animation to Out value |
michael@0 | 94 | setCurrentText(String.valueOf(mCount)); |
michael@0 | 95 | setText(String.valueOf(count)); |
michael@0 | 96 | |
michael@0 | 97 | mCount = count; |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | void setCount(int count) { |
michael@0 | 101 | setCurrentText(String.valueOf(count)); |
michael@0 | 102 | mCount = count; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | // Alpha animations in editing mode cause action bar corruption on the |
michael@0 | 106 | // Nexus 7 (bug 961749). As a workaround, skip these animations in editing |
michael@0 | 107 | // mode. |
michael@0 | 108 | void onEnterEditingMode() { |
michael@0 | 109 | final int childCount = getChildCount(); |
michael@0 | 110 | for (int i = 0; i < childCount; i++) { |
michael@0 | 111 | getChildAt(i).clearAnimation(); |
michael@0 | 112 | } |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | private AnimationSet createAnimation(float startAngle, float endAngle, |
michael@0 | 116 | FadeMode fadeMode, |
michael@0 | 117 | float zEnd, boolean reverse) { |
michael@0 | 118 | final Context context = getContext(); |
michael@0 | 119 | AnimationSet set = new AnimationSet(context, null); |
michael@0 | 120 | set.addAnimation(new Rotate3DAnimation(startAngle, endAngle, CENTER_X, CENTER_Y, zEnd, reverse)); |
michael@0 | 121 | set.addAnimation(fadeMode == FadeMode.FADE_IN ? new AlphaAnimation(0.0f, 1.0f) : |
michael@0 | 122 | new AlphaAnimation(1.0f, 0.0f)); |
michael@0 | 123 | set.setDuration(DURATION); |
michael@0 | 124 | set.setInterpolator(context, android.R.anim.accelerate_interpolator); |
michael@0 | 125 | return set; |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | @Override |
michael@0 | 129 | public View makeView() { |
michael@0 | 130 | return mInflater.inflate(R.layout.tabs_counter, null); |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | } |