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

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/compositor/bottombar/contextualsearch/ContextualSearchPanelBase.java

Issue 1337703002: [Contextual Search] Add support for crushed sprites and animate the search provider icon (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Manually scale crushed sprites Created 5 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/compositor/bottombar/contextualsearch/ContextualSearchPanelBase.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/compositor/bottombar/contextualsearch/ContextualSearchPanelBase.java b/chrome/android/java/src/org/chromium/chrome/browser/compositor/bottombar/contextualsearch/ContextualSearchPanelBase.java
index 3af21450a9a96a806af3dfc66ede06d07a96623d..af404675601b1999dcf5e81698df3705c342d91f 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/compositor/bottombar/contextualsearch/ContextualSearchPanelBase.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/compositor/bottombar/contextualsearch/ContextualSearchPanelBase.java
@@ -5,7 +5,10 @@
package org.chromium.chrome.browser.compositor.bottombar.contextualsearch;
import android.content.Context;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
import android.os.Handler;
+import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -203,6 +206,8 @@ abstract class ContextualSearchPanelBase implements ContextualSearchPromoHost {
*/
private PanelState mPanelState = PanelState.UNDEFINED;
+ private final int mDeviceDensityDpi;
+
/**
* Valid previous states for the Panel.
*/
@@ -225,6 +230,7 @@ abstract class ContextualSearchPanelBase implements ContextualSearchPromoHost {
*/
public ContextualSearchPanelBase(Context context) {
mContext = context;
+ mDeviceDensityDpi = mContext.getResources().getDisplayMetrics().densityDpi;
}
// ============================================================================================
@@ -739,6 +745,176 @@ abstract class ContextualSearchPanelBase implements ContextualSearchPromoHost {
}
// --------------------------------------------------------------------------------------------
+ // Search provider icon states
+ // --------------------------------------------------------------------------------------------
+
+ private static final int SEARCH_PROVIDER_ICON_LAST_SPRITE_FRAME = 18;
+
+ private boolean mIsSearchProviderIconSpriteVisible;
+ private boolean mShouldAnimateSearchProviderIconSprite;
+ private int mSearchProviderIconSpriteFrame;
+ private int mSearchProviderIconSpritePreviousFrame;
+ private Bitmap mSearchProviderIconSpriteBitmap;
+
+ /**
+ * @return Whether the search provider icon sprite is visible.
+ */
+ public boolean isSearchProviderIconSpriteVisible() {
+ return mIsSearchProviderIconSpriteVisible;
+ }
+
+ /**
+ * @param isSearchProviderIconSpriteVisible Whether the search provider icon sprite should be
+ * visible.
+ */
+ public void setIsSearchProviderIconSpriteVisible(boolean isSearchProviderIconSpriteVisible) {
+ mIsSearchProviderIconSpriteVisible = isSearchProviderIconSpriteVisible;
+ }
+
+ /**
+ * @return Whether the search provider icon sprite should be animated.
+ */
+ public boolean shouldAnimateSearchProviderIconSprite() {
+ return mShouldAnimateSearchProviderIconSprite;
+ }
+
+ /**
+ * @param shouldAnimateSearchProviderIconSprite Whether the search provider icon sprite should
+ * be animated.
+ */
+ public void setShouldAnimateSearchProviderIconSprite(
+ boolean shouldAnimateSearchProviderIconSprite) {
+ mSearchProviderIconSpritePreviousFrame = 0;
+ if (shouldAnimateSearchProviderIconSprite) {
+ mIsSearchProviderIconSpriteVisible = false;
+ mSearchProviderIconSpriteFrame = 0;
+ } else {
+ mIsSearchProviderIconSpriteVisible = true;
+ mSearchProviderIconSpriteFrame = SEARCH_PROVIDER_ICON_LAST_SPRITE_FRAME;
+ }
+ mShouldAnimateSearchProviderIconSprite = shouldAnimateSearchProviderIconSprite;
+ }
+
+ /**
+ * @return The size of the search provider icon sprite in px.
+ */
+ public float getSearchProviderIconSpriteSize() {
+ return mContext.getResources().getDimension(R.dimen.contextual_search_sprite_size);
+ }
+
+ /**
+ * @return The unscaled search provider icon sprite bitmap.
+ */
+ public Bitmap getSearchProviderIconSpriteBitmap() {
+ if (mSearchProviderIconSpriteBitmap == null) {
+ BitmapFactory.Options opts = new BitmapFactory.Options();
+ opts.inScaled = false;
+ opts.inDensity = getSearchProviderIconSpriteDensity();
+ mSearchProviderIconSpriteBitmap = BitmapFactory.decodeResource(mContext.getResources(),
+ R.drawable.google_icon_sprite, opts);
+ }
+
+ return mSearchProviderIconSpriteBitmap;
+ }
+
+ /**
+ * @return The scale to apply to the search provider icon sprite bitmap.
+ */
+ public float getSearchProviderIconSpriteScale() {
+ return (float) mDeviceDensityDpi / (float) getSearchProviderIconSpriteDensity();
+ }
+
+ /**
+ * Each search provider icon sprite frame is represented by a set of rectangles. Most frames
+ * consist of small rectangles representing the change from the previous frame.
+ * Each rectangle is represented using six consecutive values that specify the values to be used
+ * when creating the destination and source rectangles that get painted:
+ * 0: destination x 1: destination y 2: source x 3: source y 4: width 5: height
+ *
+ * @return The unscaled rectangles that need to be painted for the current sprite frame.
+ */
+ public int[] getSearchProviderIconSpriteFrameRects() {
+ int[] results = null;
+ int[][] allFrameRects = null;
+ if (getSearchProviderIconSpriteDensity() == DisplayMetrics.DENSITY_XXXHIGH) {
+ allFrameRects = ContextualSearchGoogleIconSpriteFrameRects.XXXHDPI_RECTS;
+ } else {
+ allFrameRects = ContextualSearchGoogleIconSpriteFrameRects.MDPI_RECTS;
+ }
+
+ // TODO(twellington): Move the logic in this method to crushed_sprite_layer.cc. We should
+ // pass which frame needs to be painted from Java -> native, and let the native layer
+ // handle the rest.
+ if (mSearchProviderIconSpriteFrame != mSearchProviderIconSpritePreviousFrame
+ || !shouldPaintPreviousSearchProviderIconSpriteFrames()) {
+
+ // Get the rectangles for the current frame.
+ int[] currentFrameRects = allFrameRects[mSearchProviderIconSpriteFrame];
+
+ // If we skipped some frames in the animation, we need to paint the rectangles
+ // for those frames if getSearchProviderIconSpritePaintPreviousFrames() is true.
+ if (mSearchProviderIconSpriteFrame != mSearchProviderIconSpritePreviousFrame + 1
+ && shouldPaintPreviousSearchProviderIconSpriteFrames()) {
+ int totalNumberOfRectValues = currentFrameRects.length;
+
+ // Determine the total number of rectangle values for all skipped frames.
+ for (int frame = mSearchProviderIconSpritePreviousFrame + 1;
+ frame < mSearchProviderIconSpriteFrame; frame++) {
+ int[] frameRects = allFrameRects[frame];
+ totalNumberOfRectValues += frameRects.length;
+ }
+
+ // Merge the rectangles for the skipped frames and the current frame into one int[].
+ results = new int[totalNumberOfRectValues];
+ int index = 0;
+ for (int frame = mSearchProviderIconSpritePreviousFrame + 1;
+ frame <= mSearchProviderIconSpriteFrame; frame++) {
+ int[] frameRects = allFrameRects[frame];
+ System.arraycopy(frameRects, 0, results, index, frameRects.length);
+ index = frameRects.length;
+ }
+ } else {
+ results = currentFrameRects;
+ }
+ }
+
+ mSearchProviderIconSpritePreviousFrame = mSearchProviderIconSpriteFrame;
+ return (results != null) ? results : new int[] {};
+ }
+
+ /**
+ * Sets the sprite frame to display based on the completion percentage.
+ *
+ * @param percentage The completion percentage.
+ */
+ public void setSearchProviderIconAnimationCompletion(float percentage) {
+ mSearchProviderIconSpriteFrame =
+ (int) (percentage * SEARCH_PROVIDER_ICON_LAST_SPRITE_FRAME);
+ }
+
+ /**
+ * The sprite frames in the search provider icon sprite are created by painting a
+ * set of rectangles on top of the previous frames, with the exception of the first and last
+ * frame.
+ *
+ * @return Whether the previous sprite frames should be painted.
+ */
+ public boolean shouldPaintPreviousSearchProviderIconSpriteFrames() {
+ return mSearchProviderIconSpriteFrame != 0
+ && mSearchProviderIconSpriteFrame != SEARCH_PROVIDER_ICON_LAST_SPRITE_FRAME;
+ }
+
+ /**
+ * @return The density to use for the search provider icon sprite bitmap and rectangles.
+ */
+ private int getSearchProviderIconSpriteDensity() {
+ if (mDeviceDensityDpi >= DisplayMetrics.DENSITY_XHIGH) {
+ return DisplayMetrics.DENSITY_XXXHIGH;
+ }
+ return DisplayMetrics.DENSITY_MEDIUM;
+ }
+
+ // --------------------------------------------------------------------------------------------
// Promo states
// --------------------------------------------------------------------------------------------

Powered by Google App Engine
This is Rietveld 408576698