michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.toolbar; michael@0: michael@0: import org.mozilla.gecko.animation.Rotate3DAnimation; michael@0: import org.mozilla.gecko.R; michael@0: import org.mozilla.gecko.widget.ThemedTextSwitcher; michael@0: michael@0: import android.content.Context; michael@0: import android.os.Build; michael@0: import android.view.accessibility.AccessibilityNodeInfo; michael@0: import android.view.animation.Animation; michael@0: import android.view.animation.AnimationSet; michael@0: import android.view.animation.AlphaAnimation; michael@0: import android.view.LayoutInflater; michael@0: import android.view.View; michael@0: import android.util.AttributeSet; michael@0: import android.widget.ViewSwitcher; michael@0: michael@0: public class TabCounter extends ThemedTextSwitcher michael@0: implements ViewSwitcher.ViewFactory { michael@0: michael@0: private static final float CENTER_X = 0.5f; michael@0: private static final float CENTER_Y = 1.25f; michael@0: private static final int DURATION = 500; michael@0: private static final float Z_DISTANCE = 200; michael@0: michael@0: private final AnimationSet mFlipInForward; michael@0: private final AnimationSet mFlipInBackward; michael@0: private final AnimationSet mFlipOutForward; michael@0: private final AnimationSet mFlipOutBackward; michael@0: private final LayoutInflater mInflater; michael@0: michael@0: private int mCount = 0; michael@0: michael@0: private enum FadeMode { michael@0: FADE_IN, michael@0: FADE_OUT michael@0: } michael@0: michael@0: public TabCounter(Context context, AttributeSet attrs) { michael@0: super(context, attrs); michael@0: mInflater = LayoutInflater.from(context); michael@0: michael@0: mFlipInForward = createAnimation(-90, 0, FadeMode.FADE_IN, -1 * Z_DISTANCE, false); michael@0: mFlipInBackward = createAnimation(90, 0, FadeMode.FADE_IN, Z_DISTANCE, false); michael@0: mFlipOutForward = createAnimation(0, -90, FadeMode.FADE_OUT, -1 * Z_DISTANCE, true); michael@0: mFlipOutBackward = createAnimation(0, 90, FadeMode.FADE_OUT, Z_DISTANCE, true); michael@0: michael@0: removeAllViews(); michael@0: setFactory(this); michael@0: michael@0: if (Build.VERSION.SDK_INT >= 16) { michael@0: // This adds the TextSwitcher to the a11y node tree, where we in turn michael@0: // could make it return an empty info node. If we don't do this the michael@0: // TextSwitcher's child TextViews get picked up, and we don't want michael@0: // that since the tabs ImageButton is already properly labeled for michael@0: // accessibility. michael@0: setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES); michael@0: setAccessibilityDelegate(new View.AccessibilityDelegate() { michael@0: @Override michael@0: public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {} michael@0: }); michael@0: } michael@0: } michael@0: michael@0: void setCountWithAnimation(int count) { michael@0: // Don't animate from initial state michael@0: if (mCount == 0) { michael@0: setCount(count); michael@0: return; michael@0: } michael@0: michael@0: if (mCount == count) { michael@0: return; michael@0: } michael@0: michael@0: if (count < mCount) { michael@0: setInAnimation(mFlipInBackward); michael@0: setOutAnimation(mFlipOutForward); michael@0: } else { michael@0: setInAnimation(mFlipInForward); michael@0: setOutAnimation(mFlipOutBackward); michael@0: } michael@0: michael@0: // Eliminate screen artifact. Set explicit In/Out animation pair order. This will always michael@0: // animate pair in In->Out child order, prevent alternating use of the Out->In case. michael@0: setDisplayedChild(0); michael@0: michael@0: // Set In value, trigger animation to Out value michael@0: setCurrentText(String.valueOf(mCount)); michael@0: setText(String.valueOf(count)); michael@0: michael@0: mCount = count; michael@0: } michael@0: michael@0: void setCount(int count) { michael@0: setCurrentText(String.valueOf(count)); michael@0: mCount = count; michael@0: } michael@0: michael@0: // Alpha animations in editing mode cause action bar corruption on the michael@0: // Nexus 7 (bug 961749). As a workaround, skip these animations in editing michael@0: // mode. michael@0: void onEnterEditingMode() { michael@0: final int childCount = getChildCount(); michael@0: for (int i = 0; i < childCount; i++) { michael@0: getChildAt(i).clearAnimation(); michael@0: } michael@0: } michael@0: michael@0: private AnimationSet createAnimation(float startAngle, float endAngle, michael@0: FadeMode fadeMode, michael@0: float zEnd, boolean reverse) { michael@0: final Context context = getContext(); michael@0: AnimationSet set = new AnimationSet(context, null); michael@0: set.addAnimation(new Rotate3DAnimation(startAngle, endAngle, CENTER_X, CENTER_Y, zEnd, reverse)); michael@0: set.addAnimation(fadeMode == FadeMode.FADE_IN ? new AlphaAnimation(0.0f, 1.0f) : michael@0: new AlphaAnimation(1.0f, 0.0f)); michael@0: set.setDuration(DURATION); michael@0: set.setInterpolator(context, android.R.anim.accelerate_interpolator); michael@0: return set; michael@0: } michael@0: michael@0: @Override michael@0: public View makeView() { michael@0: return mInflater.inflate(R.layout.tabs_counter, null); michael@0: } michael@0: michael@0: }