OLD | NEW |
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 Loading... |
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; |
| 121 |
93 mThumbnailUrl = thumbnailUrl; | 122 mThumbnailUrl = thumbnailUrl; |
94 } | 123 } |
95 | 124 |
96 /** | 125 /** |
97 * @return The URL used to fetch a thumbnail to display in the Bar. Will ret
urn an empty string | 126 * @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. | 127 * if no thumbnail is available. |
99 */ | 128 */ |
100 public String getThumbnailUrl() { | 129 public String getThumbnailUrl() { |
101 return mThumbnailUrl != null ? mThumbnailUrl : ""; | 130 return mThumbnailUrl != null ? mThumbnailUrl : ""; |
102 } | 131 } |
103 | 132 |
104 /** | 133 /** |
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. | 134 * @return Whether the thumbnail is visible. |
132 */ | 135 */ |
133 public boolean getThumbnailVisible() { | 136 public boolean getThumbnailVisible() { |
134 return mThumbnailVisible; | 137 return mThumbnailVisible; |
135 } | 138 } |
136 | 139 |
137 /** | 140 /** |
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. | 141 * Called when the thumbnail has finished being fetched. |
147 * @param success Whether fetching the thumbnail was successful. | 142 * @param success Whether fetching the thumbnail was successful. |
148 */ | 143 */ |
149 public void onThumbnailFetched(boolean success) { | 144 public void onThumbnailFetched(boolean success) { |
150 // Check if the thumbnail URL was cleared before the thumbnail fetch com
pleted. This may | 145 // 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. | 146 // occur if the user taps to refine the search. |
152 mThumbnailVisible = success && !TextUtils.isEmpty(mThumbnailUrl); | 147 mThumbnailVisible = success && !TextUtils.isEmpty(mThumbnailUrl); |
153 if (!mThumbnailVisible) return; | 148 if (!mThumbnailVisible) return; |
154 | 149 |
155 // TODO(twellington): if the icon sprite is animating wait to start the
thumbnail visibility | 150 // TODO(twellington): if the icon sprite is animating wait to start the
thumbnail visibility |
156 // animation. | 151 // animation. |
157 animateThumbnailVisibility(true); | 152 animateStaticImageVisibility(true); |
158 } | 153 } |
159 | 154 |
160 private void onThumbnailHidden() { | 155 // =========================================================================
=================== |
| 156 // Static Image -- either a thumbnail or static icon |
| 157 // =========================================================================
=================== |
| 158 |
| 159 /** |
| 160 * The height and width of the static image. |
| 161 */ |
| 162 private int mStaticImageSize; |
| 163 |
| 164 /** |
| 165 * The static image visibility percentage, which dictates how and where to d
raw the static |
| 166 * image. The static image is not visible at all at 0.f and completely visib
le at 1.f. |
| 167 */ |
| 168 private float mStaticImageVisibilityPercentage = 0.f; |
| 169 |
| 170 /** |
| 171 * Hides the static image if it is visible and makes the icon sprite visible
. Also resets the |
| 172 * thumbnail URL and static icon resource id. |
| 173 * @param animate Whether hiding the thumbnail should be animated. |
| 174 */ |
| 175 public void hideStaticIcon(boolean animate) { |
| 176 getIconSpriteControl().setIsVisible(true); |
| 177 if ((mThumbnailVisible || mStaticIconVisible) && animate) { |
| 178 animateStaticImageVisibility(false); |
| 179 } else { |
| 180 mOverlayPanelAnimation.cancelAnimation(this, AnimationType.STATIC_IM
AGE_VISIBILITY); |
| 181 onStaticImageHidden(); |
| 182 } |
| 183 } |
| 184 |
| 185 /** |
| 186 * @return The height and width of the static image in px. |
| 187 */ |
| 188 public int getStaticImageSize() { |
| 189 if (mStaticImageSize == 0) { |
| 190 mStaticImageSize = mContext.getResources().getDimensionPixelSize( |
| 191 R.dimen.contextual_search_static_image_size); |
| 192 } |
| 193 return mStaticImageSize; |
| 194 } |
| 195 |
| 196 /** |
| 197 * @return The static image visibility percentage, which dictates how and wh
ere to draw the |
| 198 * static image. The static image is not visible at all at 0.f and c
ompletely visible at |
| 199 * 1.f. The static image may be either a thumbnail or static icon. |
| 200 */ |
| 201 public float getStaticImageVisibilityPercentage() { |
| 202 return mStaticImageVisibilityPercentage; |
| 203 } |
| 204 |
| 205 /** |
| 206 * Called when the static image finishes hiding to reset thumbnail and stati
c icon values. |
| 207 */ |
| 208 private void onStaticImageHidden() { |
| 209 mStaticIconResourceId = 0; |
| 210 mStaticIconVisible = false; |
| 211 |
161 mThumbnailUrl = ""; | 212 mThumbnailUrl = ""; |
162 mThumbnailVisible = false; | 213 mThumbnailVisible = false; |
163 getIconSpriteControl().setIsVisible(true); | 214 getIconSpriteControl().setIsVisible(true); |
164 mThumbnailVisibilityPercentage = 0.f; | 215 mStaticImageVisibilityPercentage = 0.f; |
165 } | 216 } |
166 | 217 |
167 // =========================================================================
=================== | 218 // =========================================================================
=================== |
168 // Thumbnail Animation | 219 // Thumbnail Animation |
169 // =========================================================================
=================== | 220 // =========================================================================
=================== |
170 | 221 |
171 private Interpolator mThumbnailVisibilityInterpolator; | 222 private Interpolator mStaticImageVisibilityInterpolator; |
172 | 223 |
173 private void animateThumbnailVisibility(boolean visible) { | 224 private void animateStaticImageVisibility(boolean visible) { |
174 if (mThumbnailVisibilityInterpolator == null) { | 225 if (mStaticImageVisibilityInterpolator == null) { |
175 mThumbnailVisibilityInterpolator = PathInterpolatorCompat.create(0.4
f, 0.f, 0.6f, 1.f); | 226 mStaticImageVisibilityInterpolator = |
| 227 PathInterpolatorCompat.create(0.4f, 0.f, 0.6f, 1.f); |
176 } | 228 } |
177 | 229 |
178 mOverlayPanelAnimation.cancelAnimation(this, AnimationType.THUMBNAIL_VIS
IBILITY); | 230 mOverlayPanelAnimation.cancelAnimation(this, AnimationType.STATIC_IMAGE_
VISIBILITY); |
179 | 231 |
180 float endValue = visible ? 1.f : 0.f; | 232 float endValue = visible ? 1.f : 0.f; |
181 mOverlayPanelAnimation.addToAnimation(this, AnimationType.THUMBNAIL_VISI
BILITY, | 233 mOverlayPanelAnimation.addToAnimation(this, AnimationType.STATIC_IMAGE_V
ISIBILITY, |
182 mThumbnailVisibilityPercentage, endValue, | 234 mStaticImageVisibilityPercentage, endValue, |
183 OverlayPanelAnimation.BASE_ANIMATION_DURATION_MS, 0, false, | 235 OverlayPanelAnimation.BASE_ANIMATION_DURATION_MS, 0, false, |
184 mThumbnailVisibilityInterpolator); | 236 mStaticImageVisibilityInterpolator); |
185 } | 237 } |
186 | 238 |
187 @Override | 239 @Override |
188 public void setProperty(AnimationType prop, float val) { | 240 public void setProperty(AnimationType prop, float val) { |
189 if (prop == AnimationType.THUMBNAIL_VISIBILITY) { | 241 if (prop == AnimationType.STATIC_IMAGE_VISIBILITY) { |
190 mThumbnailVisibilityPercentage = val; | 242 mStaticImageVisibilityPercentage = val; |
191 } | 243 } |
192 } | 244 } |
193 | 245 |
194 @Override | 246 @Override |
195 public void onPropertyAnimationFinished(AnimationType prop) { | 247 public void onPropertyAnimationFinished(AnimationType prop) { |
196 if (prop == AnimationType.THUMBNAIL_VISIBILITY) { | 248 if (prop == AnimationType.STATIC_IMAGE_VISIBILITY) { |
197 if (mThumbnailVisibilityPercentage == 0.f) { | 249 if (mStaticImageVisibilityPercentage == 0.f) { |
198 onThumbnailHidden(); | 250 onStaticImageHidden(); |
199 } else { | 251 } else { |
200 getIconSpriteControl().setIsVisible(false); | 252 getIconSpriteControl().setIsVisible(false); |
201 } | 253 } |
202 } | 254 } |
203 } | 255 } |
204 | |
205 } | 256 } |
OLD | NEW |