Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Side by Side Diff: chrome/android/java_staging/src/org/chromium/chrome/browser/widget/newtab/NewTabButton.java

Issue 1141283003: Upstream oodles of Chrome for Android code into Chromium. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: final patch? Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.chrome.browser.widget.newtab;
6
7 import android.animation.Animator;
8 import android.animation.AnimatorSet;
9 import android.animation.ObjectAnimator;
10 import android.content.Context;
11 import android.graphics.Canvas;
12 import android.graphics.drawable.Drawable;
13 import android.util.AttributeSet;
14 import android.util.Property;
15 import android.widget.Button;
16
17 import com.google.android.apps.chrome.R;
18
19 import org.chromium.base.ApiCompatibilityUtils;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24 /**
25 * Button for creating new tabs.
26 */
27 public class NewTabButton extends Button implements Drawable.Callback {
28
29 private static final Property<Drawable, Integer> DRAWABLE_ALPHA_PROPERTY =
30 new Property<Drawable, Integer>(Integer.class, "alpha") {
31 @Override
32 public Integer get(Drawable d) {
33 // getAlpha() is only exposed on drawable in API 19+, so we rely on animations
34 // always setting the starting and ending values instead of relying on this
35 // property.
36 return 0;
37 }
38
39 @Override
40 public void set(Drawable d, Integer alpha) {
41 d.setAlpha(alpha);
42 }
43 };
44
45 private final Drawable mNormalDrawable;
46 private final Drawable mIncognitoDrawable;
47 private boolean mIsIncognito;
48 private AnimatorSet mTransitionAnimation;
49
50 /**
51 * Constructor for inflating from XML.
52 */
53 public NewTabButton(Context context, AttributeSet attrs) {
54 super(context, attrs);
55 mNormalDrawable = ApiCompatibilityUtils.getDrawable(
56 getResources(), R.drawable.btn_new_tab_white);
57 mNormalDrawable.setBounds(
58 0, 0, mNormalDrawable.getIntrinsicWidth(), mNormalDrawable.getIn trinsicHeight());
59 mNormalDrawable.setCallback(this);
60 mIncognitoDrawable = ApiCompatibilityUtils.getDrawable(
61 getResources(), R.drawable.btn_new_tab_incognito);
62 mIncognitoDrawable.setBounds(
63 0, 0,
64 mIncognitoDrawable.getIntrinsicWidth(), mIncognitoDrawable.getIn trinsicHeight());
65 mIncognitoDrawable.setCallback(this);
66 mIsIncognito = false;
67 }
68
69 @Override
70 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
71 int desiredWidth = Math.max(
72 mIncognitoDrawable.getIntrinsicWidth(), mNormalDrawable.getIntri nsicWidth());
73 desiredWidth += getPaddingLeft() + getPaddingRight();
74 widthMeasureSpec = MeasureSpec.makeMeasureSpec(desiredWidth, MeasureSpec .EXACTLY);
75 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
76 }
77
78 @Override
79 protected void onDraw(Canvas canvas) {
80 super.onDraw(canvas);
81
82 boolean isRtl = ApiCompatibilityUtils.isLayoutRtl(this);
83 int paddingStart = ApiCompatibilityUtils.getPaddingStart(this);
84 int widthWithoutPadding = getWidth() - paddingStart;
85
86 canvas.save();
87 if (!isRtl) canvas.translate(paddingStart, 0);
88
89 canvas.save();
90 canvas.translate(0, (getHeight() - mNormalDrawable.getIntrinsicHeight()) / 2.f);
91 if (isRtl) {
92 canvas.translate(widthWithoutPadding - mNormalDrawable.getIntrinsicW idth(), 0);
93 }
94 mNormalDrawable.draw(canvas);
95 canvas.restore();
96
97 if (mIsIncognito || (mTransitionAnimation != null && mTransitionAnimatio n.isRunning())) {
98 canvas.save();
99 canvas.translate(0, (getHeight() - mIncognitoDrawable.getIntrinsicHe ight()) / 2.f);
100 if (isRtl) {
101 canvas.translate(widthWithoutPadding - mIncognitoDrawable.getInt rinsicWidth(), 0);
102 }
103 mIncognitoDrawable.draw(canvas);
104 canvas.restore();
105 }
106
107 canvas.restore();
108 }
109
110 @Override
111 public void invalidateDrawable(Drawable dr) {
112 if (dr == mIncognitoDrawable || dr == mNormalDrawable) {
113 invalidate();
114 } else {
115 super.invalidateDrawable(dr);
116 }
117 }
118
119 /**
120 * Updates the visual state based on whether incognito or normal tabs are be ing created.
121 * @param incognito Whether the button is now used for creating incognito ta bs.
122 */
123 public void setIsIncognito(boolean incognito) {
124 if (mIsIncognito == incognito) return;
125 mIsIncognito = incognito;
126
127 if (mTransitionAnimation != null) {
128 mTransitionAnimation.cancel();
129 mTransitionAnimation = null;
130 }
131
132 Drawable fadeOutDrawable = incognito ? mNormalDrawable : mIncognitoDrawa ble;
133 Drawable fadeInDrawable = incognito ? mIncognitoDrawable : mNormalDrawab le;
134
135 if (getVisibility() != VISIBLE) {
136 fadeOutDrawable.setAlpha(0);
137 fadeInDrawable.setAlpha(255);
138 return;
139 }
140
141 List<Animator> animations = new ArrayList<Animator>();
142 Animator animation = ObjectAnimator.ofInt(
143 fadeOutDrawable, DRAWABLE_ALPHA_PROPERTY, 255, 0);
144 animation.setDuration(100);
145 animations.add(animation);
146
147 animation = ObjectAnimator.ofInt(
148 fadeInDrawable, DRAWABLE_ALPHA_PROPERTY, 0, 255);
149 animation.setStartDelay(150);
150 animation.setDuration(100);
151 animations.add(animation);
152
153 mTransitionAnimation = new AnimatorSet();
154 mTransitionAnimation.playTogether(animations);
155 mTransitionAnimation.start();
156 }
157
158 @Override
159 protected void drawableStateChanged() {
160 super.drawableStateChanged();
161
162 mNormalDrawable.setState(getDrawableState());
163 mIncognitoDrawable.setState(getDrawableState());
164 }
165 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698