OLD | NEW |
(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.content.Context; |
| 8 import android.content.res.Resources; |
| 9 import android.graphics.Bitmap; |
| 10 import android.graphics.Canvas; |
| 11 import android.graphics.Paint; |
| 12 import android.graphics.RectF; |
| 13 import android.graphics.drawable.BitmapDrawable; |
| 14 import android.graphics.drawable.ColorDrawable; |
| 15 import android.graphics.drawable.Drawable; |
| 16 import android.util.AttributeSet; |
| 17 import android.widget.LinearLayout; |
| 18 import android.widget.TextView; |
| 19 |
| 20 import com.google.android.apps.chrome.R; |
| 21 |
| 22 import org.chromium.base.ApiCompatibilityUtils; |
| 23 |
| 24 /** |
| 25 * Displays the title, thumbnail, and favicon of a most visited page. The item c
an be clicked, or |
| 26 * long-pressed to trigger a context menu with options to "open in new tab", "op
en in incognito |
| 27 * tab", or "remove". |
| 28 */ |
| 29 public class MostVisitedItemView extends LinearLayout { |
| 30 |
| 31 private static final int HIGHLIGHT_COLOR = 0x550099cc; |
| 32 private static final int MISSING_FAVICON_COLOR = 0xffe6e6e8; |
| 33 |
| 34 private TextView mTitleView; |
| 35 private MostVisitedThumbnail mThumbnailView; |
| 36 private int mFaviconSize; |
| 37 private int mTitlePaddingStart; |
| 38 private boolean mLastDrawnPressed; |
| 39 |
| 40 /** |
| 41 * Constructor for inflating from XML. |
| 42 */ |
| 43 public MostVisitedItemView(Context context, AttributeSet attrs) { |
| 44 super(context, attrs); |
| 45 } |
| 46 |
| 47 /** |
| 48 * Initializes the item. This must be called immediately after construction. |
| 49 * |
| 50 * @param title The title of the page. |
| 51 */ |
| 52 public void init(String title) { |
| 53 mTitleView = (TextView) findViewById(R.id.most_visited_title); |
| 54 mThumbnailView = (MostVisitedThumbnail) findViewById(R.id.most_visited_t
humbnail); |
| 55 |
| 56 mTitleView.setText(title); |
| 57 |
| 58 // Add padding to fill the space where the favicon will be shown. Once t
he favicon is |
| 59 // available (in setFavicon()), this extra padding will be removed and t
he favicon will be |
| 60 // added as a compound drawable. This prevents the text from jumping aro
und when the favicon |
| 61 // becomes available. |
| 62 mTitlePaddingStart = ApiCompatibilityUtils.getPaddingStart(mTitleView); |
| 63 mFaviconSize = getResources().getDimensionPixelSize(R.dimen.most_visited
_favicon_size); |
| 64 int extraPaddingStart = mFaviconSize + mTitleView.getCompoundDrawablePad
ding(); |
| 65 ApiCompatibilityUtils.setPaddingRelative(mTitleView, mTitlePaddingStart
+ extraPaddingStart, |
| 66 0, 0, 0); |
| 67 } |
| 68 |
| 69 /** |
| 70 * Update the thumbnail and trigger a redraw with the new thumbnail. |
| 71 */ |
| 72 public void setThumbnail(Bitmap thumbnail) { |
| 73 mThumbnailView.setThumbnail(thumbnail); |
| 74 } |
| 75 |
| 76 /** |
| 77 * Update the favicon and trigger a redraw with the new favicon. |
| 78 */ |
| 79 public void setFavicon(Bitmap favicon) { |
| 80 Resources res = getResources(); |
| 81 Drawable d; |
| 82 if (favicon != null) { |
| 83 d = new BitmapDrawable(res, favicon); |
| 84 } else { |
| 85 d = new ColorDrawable(MISSING_FAVICON_COLOR); |
| 86 } |
| 87 d.setBounds(0, 0, mFaviconSize, mFaviconSize); |
| 88 ApiCompatibilityUtils.setCompoundDrawablesRelative(mTitleView, d, null,
null, null); |
| 89 ApiCompatibilityUtils.setPaddingRelative(mTitleView, mTitlePaddingStart,
0, 0, 0); |
| 90 } |
| 91 |
| 92 @Override |
| 93 public void setPressed(boolean pressed) { |
| 94 super.setPressed(pressed); |
| 95 if (isPressed() != mLastDrawnPressed) invalidate(); |
| 96 } |
| 97 |
| 98 @Override |
| 99 protected void dispatchDraw(Canvas canvas) { |
| 100 super.dispatchDraw(canvas); |
| 101 |
| 102 // Draw highlight overlay over the child views when this view is pressed
. |
| 103 if (isPressed()) { |
| 104 Paint highlightPaint = new Paint(); |
| 105 highlightPaint.setColor(HIGHLIGHT_COLOR); |
| 106 highlightPaint.setAntiAlias(true); |
| 107 RectF highlightRect = new RectF(0, 0, getWidth(), getHeight()); |
| 108 int cornerRadius = getResources().getDimensionPixelOffset( |
| 109 R.dimen.most_visited_bg_corner_radius); |
| 110 canvas.drawRoundRect(highlightRect, cornerRadius, cornerRadius, high
lightPaint); |
| 111 } |
| 112 mLastDrawnPressed = isPressed(); |
| 113 } |
| 114 } |
OLD | NEW |