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

Side by Side Diff: chrome/android/java_staging/src/org/chromium/chrome/browser/ntp/LogoView.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.ntp;
6
7 import android.animation.Animator;
8 import android.animation.ObjectAnimator;
9 import android.content.Context;
10 import android.graphics.Bitmap;
11 import android.graphics.BitmapFactory;
12 import android.graphics.Canvas;
13 import android.graphics.Matrix;
14 import android.graphics.Paint;
15 import android.text.TextUtils;
16 import android.util.AttributeSet;
17 import android.util.Property;
18 import android.view.View;
19 import android.view.View.OnClickListener;
20
21 import com.google.android.apps.chrome.R;
22
23 import org.chromium.chrome.browser.LogoBridge.Logo;
24 import org.chromium.chrome.browser.ntp.NewTabPageView.NewTabPageManager;
25
26 import java.lang.ref.WeakReference;
27
28 /**
29 * This view shows the default search provider's logo and fades in a new logo if one becomes
30 * available.
31 */
32 public class LogoView extends View implements OnClickListener {
33
34 // Number of milliseconds for a new logo to fade in.
35 private static final int LOGO_TRANSITION_TIME_MS = 400;
36
37 // The default logo is shared across all NTPs.
38 private static WeakReference<Bitmap> sDefaultLogo;
39
40 private Paint mPaint;
41 private Bitmap mLogo;
42 private Bitmap mNewLogo;
43 private Matrix mLogoMatrix;
44 private Matrix mNewLogoMatrix;
45 private boolean mLogoIsDefault;
46 private boolean mNewLogoIsDefault;
47 private ObjectAnimator mAnimation;
48
49 /**
50 * A measure from 0 to 1 of how much the new logo has faded in. 0 shows the old logo, 1 shows
51 * the new logo, and intermediate values show the new logo cross-fading in o ver the old logo.
52 * Set to 0 when not transitioning.
53 */
54 private float mTransitionAmount;
55
56 private NewTabPageManager mManager;
57
58 private final Property<LogoView, Float> mTransitionProperty =
59 new Property<LogoView, Float>(Float.class, "") {
60 @Override
61 public Float get(LogoView logoView) {
62 return logoView.mTransitionAmount;
63 }
64
65 @Override
66 public void set(LogoView logoView, Float amount) {
67 assert amount >= 0f;
68 assert amount <= 1f;
69 if (logoView.mTransitionAmount != amount) {
70 logoView.mTransitionAmount = amount;
71 invalidate();
72 }
73 }
74 };
75
76 /**
77 * Constructor used to inflate a LogoView from XML.
78 */
79 public LogoView(Context context, AttributeSet attrs) {
80 super(context, attrs);
81
82 mLogo = getDefaultLogo();
83 mLogoMatrix = new Matrix();
84 mLogoIsDefault = true;
85
86 mPaint = new Paint();
87 mPaint.setFilterBitmap(true);
88
89 // Mark this view as non-clickable so that accessibility will ignore it. When a non-default
90 // logo is shown, this view will be marked clickable again.
91 setOnClickListener(this);
92 setClickable(false);
93 }
94
95 /**
96 * Sets the NewTabPageManager to notify when the logo is pressed.
97 */
98 public void setMananger(NewTabPageManager manager) {
99 mManager = manager;
100 }
101
102 /**
103 * Jumps to the end of the current logo animation, if any.
104 */
105 public void endAnimation() {
106 if (mAnimation != null) {
107 mAnimation.end();
108 mAnimation = null;
109 }
110 }
111
112 /**
113 * Fades in a new logo over the current logo.
114 *
115 * @param logo The new logo to fade in. May be null to reset to the default logo.
116 */
117 public void updateLogo(Logo logo) {
118 if (logo == null) {
119 updateLogo(getDefaultLogo(), null, true);
120 } else {
121 String contentDescription = TextUtils.isEmpty(logo.altText) ? null
122 : getResources().getString(R.string.accessibility_google_doo dle, logo.altText);
123 updateLogo(logo.image, contentDescription, false);
124 }
125 }
126
127 private void updateLogo(Bitmap logo, final String contentDescription, boolea n isDefaultLogo) {
128 if (mAnimation != null) mAnimation.end();
129
130 mNewLogo = logo;
131 mNewLogoMatrix = new Matrix();
132 mNewLogoIsDefault = isDefaultLogo;
133 setMatrix(mNewLogo, mNewLogoMatrix, mNewLogoIsDefault);
134
135 mAnimation = ObjectAnimator.ofFloat(this, mTransitionProperty, 0f, 1f);
136 mAnimation.setDuration(LOGO_TRANSITION_TIME_MS);
137 mAnimation.addListener(new Animator.AnimatorListener() {
138 @Override
139 public void onAnimationStart(Animator animation) {
140 }
141
142 @Override
143 public void onAnimationRepeat(Animator animation) {
144 }
145
146 @Override
147 public void onAnimationEnd(Animator animation) {
148 mLogo = mNewLogo;
149 mLogoMatrix = mNewLogoMatrix;
150 mLogoIsDefault = mNewLogoIsDefault;
151 mNewLogo = null;
152 mNewLogoMatrix = null;
153 mTransitionAmount = 0f;
154 mAnimation = null;
155 setContentDescription(contentDescription);
156 setClickable(!mNewLogoIsDefault);
157 }
158
159 @Override
160 public void onAnimationCancel(Animator animation) {
161 onAnimationEnd(animation);
162 invalidate();
163 }
164 });
165 mAnimation.start();
166 }
167
168 /**
169 * @return Whether a new logo is currently fading in over the old logo.
170 */
171 private boolean isTransitioning() {
172 return mTransitionAmount != 0f;
173 }
174
175 /**
176 * Sets the matrix to scale and translate the image so that it will be cente red in the LogoView
177 * and scaled to fit within the LogoView.
178 *
179 * @param preventUpscaling Whether the image should not be scaled up. If tru e, the image might
180 * not fill the entire view but will still be center ed.
181 */
182 private void setMatrix(Bitmap image, Matrix matrix, boolean preventUpscaling ) {
183 int width = getWidth();
184 int height = getHeight();
185 int imageWidth = image.getWidth();
186 int imageHeight = image.getHeight();
187
188 float scale = Math.min((float) width / imageWidth, (float) height / imag eHeight);
189 if (preventUpscaling) scale = Math.min(1.0f, scale);
190
191 int imageOffsetX = Math.round((width - imageWidth * scale) * 0.5f);
192 int imageOffsetY = Math.round((height - imageHeight * scale) * 0.5f);
193
194 matrix.setScale(scale, scale);
195 matrix.postTranslate(imageOffsetX, imageOffsetY);
196 }
197
198 /**
199 * @return The default logo.
200 */
201 private Bitmap getDefaultLogo() {
202 Bitmap defaultLogo = sDefaultLogo == null ? null : sDefaultLogo.get();
203 if (defaultLogo == null) {
204 defaultLogo = BitmapFactory.decodeResource(getResources(), R.drawabl e.google_logo);
205 sDefaultLogo = new WeakReference<Bitmap>(defaultLogo);
206 }
207 return defaultLogo;
208 }
209
210 @Override
211 protected void onDraw(Canvas canvas) {
212 if (mLogo != null && mTransitionAmount < 0.5f) {
213 mPaint.setAlpha((int) (255 * 2 * (0.5f - mTransitionAmount)));
214 canvas.save();
215 canvas.concat(mLogoMatrix);
216 canvas.drawBitmap(mLogo, 0, 0, mPaint);
217 canvas.restore();
218 }
219
220 if (mNewLogo != null && mTransitionAmount > 0.5f) {
221 mPaint.setAlpha((int) (255 * 2 * (mTransitionAmount - 0.5f)));
222 canvas.save();
223 canvas.concat(mNewLogoMatrix);
224 canvas.drawBitmap(mNewLogo, 0, 0, mPaint);
225 canvas.restore();
226 }
227 }
228
229 @Override
230 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
231 if (w != oldw || h != oldh) {
232 if (mLogo != null) setMatrix(mLogo, mLogoMatrix, mLogoIsDefault);
233 if (mNewLogo != null) setMatrix(mNewLogo, mNewLogoMatrix, mNewLogoIs Default);
234 }
235 }
236
237 @Override
238 public void onClick(View view) {
239 if (view == this && mManager != null && !isTransitioning()) {
240 mManager.openLogoLink();
241 }
242 }
243 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698