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

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/compositor/bottombar/contextualsearch/ContextualSearchImageControl.java

Issue 2449653002: [Contextual Search] Add support for a static icon (Closed)
Patch Set: [Contextual Search] Add support for a static icon Created 4 years, 1 month 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 package org.chromium.chrome.browser.compositor.bottombar.contextualsearch; 4 package org.chromium.chrome.browser.compositor.bottombar.contextualsearch;
5 5
6 import android.content.Context; 6 import android.content.Context;
7 import android.support.v4.view.animation.PathInterpolatorCompat; 7 import android.support.v4.view.animation.PathInterpolatorCompat;
8 import android.text.TextUtils; 8 import android.text.TextUtils;
9 import android.view.animation.Interpolator; 9 import android.view.animation.Interpolator;
10 10
11 import org.chromium.chrome.R; 11 import org.chromium.chrome.R;
12 import org.chromium.chrome.browser.compositor.bottombar.OverlayPanelAnimation; 12 import org.chromium.chrome.browser.compositor.bottombar.OverlayPanelAnimation;
13 import org.chromium.chrome.browser.compositor.layouts.ChromeAnimation; 13 import org.chromium.chrome.browser.compositor.layouts.ChromeAnimation;
14 14
15 /** 15 /**
16 * Controls the image shown in the Bar. Owns the {@link ContextualSearchIconSpri teControl} and 16 * Controls the image shown in the Bar. Owns the {@link ContextualSearchIconSpri teControl} and
17 * details about the thumbnail, and handles animating between the two. 17 * details about the thumbnail, and handles animating between the two.
18 */ 18 */
19 public class ContextualSearchImageControl 19 public class ContextualSearchImageControl
20 implements ChromeAnimation.Animatable<ContextualSearchImageControl.Anima tionType> { 20 implements ChromeAnimation.Animatable<ContextualSearchImageControl.Anima tionType> {
21 /** 21 /**
22 * Animation properties. 22 * Animation properties.
23 */ 23 */
24 protected enum AnimationType { 24 protected enum AnimationType {
25 THUMBNAIL_VISIBILITY 25 STATIC_IMAGE_VISIBILITY
26 } 26 }
27 27
28 /** The current context. */ 28 /** The current context. */
29 private final Context mContext; 29 private final Context mContext;
30 30
31 /** The OverlayPanelAnimation used to add animations. */ 31 /** The OverlayPanelAnimation used to add animations. */
32 private final OverlayPanelAnimation mOverlayPanelAnimation; 32 private final OverlayPanelAnimation mOverlayPanelAnimation;
33 33
34 public ContextualSearchImageControl(OverlayPanelAnimation overlayPanelAnimat ion, 34 public ContextualSearchImageControl(OverlayPanelAnimation overlayPanelAnimat ion,
35 Context context) { 35 Context context) {
(...skipping 19 matching lines...) Expand all
55 } 55 }
56 56
57 /** 57 /**
58 * @param shouldAnimateIconSprite Whether the search provider icon sprite sh ould be animated. 58 * @param shouldAnimateIconSprite Whether the search provider icon sprite sh ould be animated.
59 */ 59 */
60 public void setShouldAnimateIconSprite(boolean shouldAnimateIconSprite) { 60 public void setShouldAnimateIconSprite(boolean shouldAnimateIconSprite) {
61 getIconSpriteControl().setShouldAnimateAppearance(shouldAnimateIconSprit e); 61 getIconSpriteControl().setShouldAnimateAppearance(shouldAnimateIconSprit e);
62 } 62 }
63 63
64 // ========================================================================= =================== 64 // ========================================================================= ===================
65 // Static Icon
66 // ========================================================================= ===================
67
68 /**
69 * The resource id of the static icon to display.
70 */
71 private int mStaticIconResourceId;
72
73 /**
74 * Whether the static icon is visible.
75 */
76 private boolean mStaticIconVisible;
77
78 /**
79 * @param resId The resource id of the static icon to display.
80 */
81 public void setStaticIconResourceId(int resId) {
82 mStaticIconResourceId = resId;
83 mStaticIconVisible = true;
84 animateStaticImageVisibility(true);
85 }
86
87 /**
88 * @return The resource id of the static icon to display.
89 */
90 public int getStaticIconResourceId() {
91 return mStaticIconResourceId;
92 }
93
94 /**
95 * @return Whether the static icon is visible.
96 */
97 public boolean getStaticIconVisible() {
98 return mStaticIconVisible;
99 }
100
101 // ========================================================================= ===================
65 // Thumbnail 102 // Thumbnail
66 // ========================================================================= =================== 103 // ========================================================================= ===================
67 104
68 /** 105 /**
69 * The URL of the thumbnail to display. 106 * The URL of the thumbnail to display.
70 */ 107 */
71 private String mThumbnailUrl; 108 private String mThumbnailUrl;
72 109
73 /** 110 /**
74 * The height and width of the thumbnail.
75 */
76 private int mThumbnailSize;
77
78 /**
79 * Whether the thumbnail is visible. 111 * Whether the thumbnail is visible.
80 */ 112 */
81 private boolean mThumbnailVisible; 113 private boolean mThumbnailVisible;
82 114
83 /** 115 /**
84 * The thumbnail visibility percentage, which dictates how and where to draw the thumbnail.
85 * The thumbnail is not visible at all at 0.f and completely visible at 1.f.
86 */
87 private float mThumbnailVisibilityPercentage = 0.f;
88
89 /**
90 * @param thumbnailUrl The URL of the thumbnail to display 116 * @param thumbnailUrl The URL of the thumbnail to display
91 */ 117 */
92 public void setThumbnailUrl(String thumbnailUrl) { 118 public void setThumbnailUrl(String thumbnailUrl) {
119 // If a static icon is showing, the thumbnail should not be shown.
120 if (mStaticIconVisible) return;
Donn Denman 2016/10/24 23:56:20 Nit: add a blank line after (since there's a retur
Theresa 2016/10/25 00:32:48 Done.
93 mThumbnailUrl = thumbnailUrl; 121 mThumbnailUrl = thumbnailUrl;
94 } 122 }
95 123
96 /** 124 /**
97 * @return The URL used to fetch a thumbnail to display in the Bar. Will ret urn an empty string 125 * @return The URL used to fetch a thumbnail to display in the Bar. Will ret urn an empty string
98 * if no thumbnail is available. 126 * if no thumbnail is available.
99 */ 127 */
100 public String getThumbnailUrl() { 128 public String getThumbnailUrl() {
101 return mThumbnailUrl != null ? mThumbnailUrl : ""; 129 return mThumbnailUrl != null ? mThumbnailUrl : "";
102 } 130 }
103 131
104 /** 132 /**
105 * Hides the thumbnail if it is visible and makes the icon sprite visible. A lso resets the
106 * thumbnail URL.
107 * @param animate Whether hiding the thumbnail should be animated.
108 */
109 public void hideThumbnail(boolean animate) {
110 getIconSpriteControl().setIsVisible(true);
111 if (mThumbnailVisible && animate) {
112 animateThumbnailVisibility(false);
113 } else {
114 mOverlayPanelAnimation.cancelAnimation(this, AnimationType.THUMBNAIL _VISIBILITY);
115 onThumbnailHidden();
116 }
117 }
118
119 /**
120 * @return The height and width of the thumbnail in px.
121 */
122 public int getThumbnailSize() {
123 if (mThumbnailSize == 0) {
124 mThumbnailSize = mContext.getResources().getDimensionPixelSize(
125 R.dimen.contextual_search_thumbnail_size);
126 }
127 return mThumbnailSize;
128 }
129
130 /**
131 * @return Whether the thumbnail is visible. 133 * @return Whether the thumbnail is visible.
132 */ 134 */
133 public boolean getThumbnailVisible() { 135 public boolean getThumbnailVisible() {
134 return mThumbnailVisible; 136 return mThumbnailVisible;
135 } 137 }
136 138
137 /** 139 /**
138 * @return The thumbnail visibility percentage, which dictates how and where to draw the
139 * thumbnail. The thumbnail is not visible at all at 0.f and complet ely visible at 1.f.
140 */
141 public float getThumbnailVisibilityPercentage() {
142 return mThumbnailVisibilityPercentage;
143 }
144
145 /**
146 * Called when the thumbnail has finished being fetched. 140 * Called when the thumbnail has finished being fetched.
147 * @param success Whether fetching the thumbnail was successful. 141 * @param success Whether fetching the thumbnail was successful.
148 */ 142 */
149 public void onThumbnailFetched(boolean success) { 143 public void onThumbnailFetched(boolean success) {
150 // Check if the thumbnail URL was cleared before the thumbnail fetch com pleted. This may 144 // Check if the thumbnail URL was cleared before the thumbnail fetch com pleted. This may
151 // occur if the user taps to refine the search. 145 // occur if the user taps to refine the search.
152 mThumbnailVisible = success && !TextUtils.isEmpty(mThumbnailUrl); 146 mThumbnailVisible = success && !TextUtils.isEmpty(mThumbnailUrl);
153 if (!mThumbnailVisible) return; 147 if (!mThumbnailVisible) return;
154 148
155 // TODO(twellington): if the icon sprite is animating wait to start the thumbnail visibility 149 // TODO(twellington): if the icon sprite is animating wait to start the thumbnail visibility
156 // animation. 150 // animation.
157 animateThumbnailVisibility(true); 151 animateStaticImageVisibility(true);
158 } 152 }
159 153
160 private void onThumbnailHidden() { 154 // ========================================================================= ===================
155 // Static Image -- either a thumbnail or static icon
156 // ========================================================================= ===================
157
158 /**
159 * The height and width of the static image.
160 */
161 private int mStaticImageSize;
162
163 /**
164 * The static image visibility percentage, which dictates how and where to d raw the static
165 * image. The static image is not visible at all at 0.f and completely visib le at 1.f.
166 */
167 private float mStaticImageVisibilityPercentage = 0.f;
168
169 /**
170 * Hides the static image if it is visible and makes the icon sprite visible . Also resets the
171 * thumbnail URL and static icon resource id.
172 * @param animate Whether hiding the thumbnail should be animated.
173 */
174 public void hideStaticIcon(boolean animate) {
175 getIconSpriteControl().setIsVisible(true);
176 if ((mThumbnailVisible || mStaticIconVisible) && animate) {
177 animateStaticImageVisibility(false);
178 } else {
179 mOverlayPanelAnimation.cancelAnimation(this, AnimationType.STATIC_IM AGE_VISIBILITY);
180 onStaticImageHidden();
181 }
182 }
183
184 /**
185 * @return The height and width of the thumbnail in px.
Donn Denman 2016/10/24 23:56:20 Nit: change thumbnail to static image.
Theresa 2016/10/25 00:32:48 Done.
186 */
187 public int getStaticImageSize() {
188 if (mStaticImageSize == 0) {
189 mStaticImageSize = mContext.getResources().getDimensionPixelSize(
190 R.dimen.contextual_search_static_image_size);
191 }
192 return mStaticImageSize;
193 }
194
195 /**
196 * @return The static image visibility percentage, which dictates how and wh ere to draw the
197 * static image. The static image is not visible at all at 0.f and c ompletely visible at
198 * 1.f. The static image may be either a thumbnail or static icon.
199 */
200 public float getStaticImageVisibilityPercentage() {
201 return mStaticImageVisibilityPercentage;
202 }
203
204 private void onStaticImageHidden() {
Donn Denman 2016/10/24 23:56:20 Nit: add javadoc.
Theresa 2016/10/25 00:32:48 Done.
205 mStaticIconResourceId = 0;
206 mStaticIconVisible = false;
207
161 mThumbnailUrl = ""; 208 mThumbnailUrl = "";
162 mThumbnailVisible = false; 209 mThumbnailVisible = false;
163 getIconSpriteControl().setIsVisible(true); 210 getIconSpriteControl().setIsVisible(true);
164 mThumbnailVisibilityPercentage = 0.f; 211 mStaticImageVisibilityPercentage = 0.f;
165 } 212 }
166 213
167 // ========================================================================= =================== 214 // ========================================================================= ===================
168 // Thumbnail Animation 215 // Thumbnail Animation
169 // ========================================================================= =================== 216 // ========================================================================= ===================
170 217
171 private Interpolator mThumbnailVisibilityInterpolator; 218 private Interpolator mStaticImageVisibilityInterpolator;
172 219
173 private void animateThumbnailVisibility(boolean visible) { 220 private void animateStaticImageVisibility(boolean visible) {
174 if (mThumbnailVisibilityInterpolator == null) { 221 if (mStaticImageVisibilityInterpolator == null) {
175 mThumbnailVisibilityInterpolator = PathInterpolatorCompat.create(0.4 f, 0.f, 0.6f, 1.f); 222 mStaticImageVisibilityInterpolator =
223 PathInterpolatorCompat.create(0.4f, 0.f, 0.6f, 1.f);
176 } 224 }
177 225
178 mOverlayPanelAnimation.cancelAnimation(this, AnimationType.THUMBNAIL_VIS IBILITY); 226 mOverlayPanelAnimation.cancelAnimation(this, AnimationType.STATIC_IMAGE_ VISIBILITY);
179 227
180 float endValue = visible ? 1.f : 0.f; 228 float endValue = visible ? 1.f : 0.f;
181 mOverlayPanelAnimation.addToAnimation(this, AnimationType.THUMBNAIL_VISI BILITY, 229 mOverlayPanelAnimation.addToAnimation(this, AnimationType.STATIC_IMAGE_V ISIBILITY,
182 mThumbnailVisibilityPercentage, endValue, 230 mStaticImageVisibilityPercentage, endValue,
183 OverlayPanelAnimation.BASE_ANIMATION_DURATION_MS, 0, false, 231 OverlayPanelAnimation.BASE_ANIMATION_DURATION_MS, 0, false,
184 mThumbnailVisibilityInterpolator); 232 mStaticImageVisibilityInterpolator);
185 } 233 }
186 234
187 @Override 235 @Override
188 public void setProperty(AnimationType prop, float val) { 236 public void setProperty(AnimationType prop, float val) {
189 if (prop == AnimationType.THUMBNAIL_VISIBILITY) { 237 if (prop == AnimationType.STATIC_IMAGE_VISIBILITY) {
190 mThumbnailVisibilityPercentage = val; 238 mStaticImageVisibilityPercentage = val;
191 } 239 }
192 } 240 }
193 241
194 @Override 242 @Override
195 public void onPropertyAnimationFinished(AnimationType prop) { 243 public void onPropertyAnimationFinished(AnimationType prop) {
196 if (prop == AnimationType.THUMBNAIL_VISIBILITY) { 244 if (prop == AnimationType.STATIC_IMAGE_VISIBILITY) {
197 if (mThumbnailVisibilityPercentage == 0.f) { 245 if (mStaticImageVisibilityPercentage == 0.f) {
198 onThumbnailHidden(); 246 onStaticImageHidden();
199 } else { 247 } else {
200 getIconSpriteControl().setIsVisible(false); 248 getIconSpriteControl().setIsVisible(false);
201 } 249 }
202 } 250 }
203 } 251 }
204
205 } 252 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698