mobile/android/base/toolbar/TabCounter.java

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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/. */
     6 package org.mozilla.gecko.toolbar;
     8 import org.mozilla.gecko.animation.Rotate3DAnimation;
     9 import org.mozilla.gecko.R;
    10 import org.mozilla.gecko.widget.ThemedTextSwitcher;
    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;
    23 public class TabCounter extends ThemedTextSwitcher
    24                         implements ViewSwitcher.ViewFactory {
    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;
    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;
    37     private int mCount = 0;
    39     private enum FadeMode {
    40         FADE_IN,
    41         FADE_OUT
    42     }
    44     public TabCounter(Context context, AttributeSet attrs) {
    45         super(context, attrs);
    46         mInflater = LayoutInflater.from(context);
    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);
    53         removeAllViews();
    54         setFactory(this);
    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     }
    70     void setCountWithAnimation(int count) {
    71         // Don't animate from initial state
    72         if (mCount == 0) {
    73             setCount(count);
    74             return;
    75         }
    77         if (mCount == count) {
    78             return;
    79         }
    81         if (count < mCount) {
    82             setInAnimation(mFlipInBackward);
    83             setOutAnimation(mFlipOutForward);
    84         } else {
    85             setInAnimation(mFlipInForward);
    86             setOutAnimation(mFlipOutBackward);
    87         }
    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);
    93         // Set In value, trigger animation to Out value
    94         setCurrentText(String.valueOf(mCount));
    95         setText(String.valueOf(count));
    97         mCount = count;
    98     }
   100     void setCount(int count) {
   101         setCurrentText(String.valueOf(count));
   102         mCount = count;
   103     }
   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     }
   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     }
   128     @Override
   129     public View makeView() {
   130         return mInflater.inflate(R.layout.tabs_counter, null);
   131     }
   133 }

mercurial