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