| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 | 4 |
| 5 package org.chromium.chrome.browser.suggestions; | 5 package org.chromium.chrome.browser.suggestions; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.util.AttributeSet; | 8 import android.util.AttributeSet; |
| 9 import android.view.View; | |
| 10 import android.widget.FrameLayout; | 9 import android.widget.FrameLayout; |
| 11 import android.widget.ImageView; | 10 import android.widget.ImageView; |
| 12 import android.widget.TextView; | 11 import android.widget.TextView; |
| 13 | 12 |
| 14 import org.chromium.chrome.R; | 13 import org.chromium.chrome.R; |
| 15 import org.chromium.chrome.browser.ntp.TitleUtil; | 14 import org.chromium.chrome.browser.ntp.TitleUtil; |
| 16 | 15 |
| 17 /** | 16 /** |
| 18 * The view for a site suggestion tile. Displays the title of the site beneath a
large icon. If a | 17 * The view for a site suggestion tile. Displays the title of the site beneath a
large icon. If a |
| 19 * large icon isn't available, displays a rounded rectangle with a single letter
in its place. | 18 * large icon isn't available, displays a rounded rectangle with a single letter
in its place. |
| 20 */ | 19 */ |
| 21 public class TileView extends FrameLayout { | 20 public class TileView extends FrameLayout { |
| 22 /** | 21 /** The url currently associated to this tile. */ |
| 23 * The tile that holds the data to populate this view. | 22 private String mUrl; |
| 24 */ | 23 |
| 25 private Tile mTile; | 24 private TextView mTitleView; |
| 25 private ImageView mIconView; |
| 26 private ImageView mBadgeView; |
| 26 | 27 |
| 27 /** | 28 /** |
| 28 * Constructor for inflating from XML. | 29 * Constructor for inflating from XML. |
| 29 */ | 30 */ |
| 30 public TileView(Context context, AttributeSet attrs) { | 31 public TileView(Context context, AttributeSet attrs) { |
| 31 super(context, attrs); | 32 super(context, attrs); |
| 32 } | 33 } |
| 33 | 34 |
| 35 @Override |
| 36 protected void onFinishInflate() { |
| 37 super.onFinishInflate(); |
| 38 |
| 39 mTitleView = (TextView) findViewById(R.id.tile_view_title); |
| 40 mIconView = (ImageView) findViewById(R.id.tile_view_icon); |
| 41 mBadgeView = (ImageView) findViewById(R.id.offline_badge); |
| 42 } |
| 43 |
| 34 /** | 44 /** |
| 35 * Initializes the view using the data held by {@code tile}. This should be
called immediately | 45 * Initializes the view using the data held by {@code tile}. This should be
called immediately |
| 36 * after inflation. | 46 * after inflation. |
| 37 * @param tile The tile that holds the data to populate this view. | 47 * @param tile The tile that holds the data to populate this view. |
| 38 * @param titleLines The number of text lines to use for the tile title. | 48 * @param titleLines The number of text lines to use for the tile title. |
| 39 */ | 49 */ |
| 40 public void initialize(Tile tile, int titleLines) { | 50 public void initialize(Tile tile, int titleLines) { |
| 41 mTile = tile; | 51 mTitleView.setLines(titleLines); |
| 42 TextView titleView = (TextView) findViewById(R.id.tile_view_title); | 52 mUrl = tile.getUrl(); |
| 43 titleView.setLines(titleLines); | 53 renderTile(tile); |
| 44 titleView.setText(TitleUtil.getTitleForDisplay(mTile.getTitle(), mTile.g
etUrl())); | |
| 45 renderIcon(); | |
| 46 findViewById(R.id.offline_badge) | |
| 47 .setVisibility(mTile.isOfflineAvailable() ? View.VISIBLE : View.
GONE); | |
| 48 } | 54 } |
| 49 | 55 |
| 50 /** | 56 /** @return The url associated to this view. */ |
| 51 * @return The tile that holds the data to populate this view. | 57 public String getUrl() { |
| 52 */ | 58 return mUrl; |
| 53 public Tile getTile() { | |
| 54 return mTile; | |
| 55 } | 59 } |
| 56 | 60 |
| 57 /** | 61 /** |
| 58 * Renders the icon held by the {@link Tile} or clears it from the view if t
he icon is null. | 62 * Renders the icon held by the {@link Tile} or clears it from the view if t
he icon is null. |
| 59 */ | 63 */ |
| 60 public void renderIcon() { | 64 public void renderIcon(Tile tile) { |
| 61 ((ImageView) findViewById(R.id.tile_view_icon)).setImageDrawable(mTile.g
etIcon()); | 65 mIconView.setImageDrawable(tile.getIcon()); |
| 66 } |
| 67 |
| 68 /** Updates the view if there have been changes since the last time. */ |
| 69 public void updateIfDataChanged(Tile tile) { |
| 70 if (!isUpToDate(tile)) renderTile(tile); |
| 71 } |
| 72 |
| 73 private boolean isUpToDate(Tile tile) { |
| 74 assert mUrl.equals(tile.getUrl()); |
| 75 |
| 76 if (tile.getIcon() != mIconView.getDrawable()) return false; |
| 77 if (tile.isOfflineAvailable() != (mBadgeView.getVisibility() == VISIBLE)
) return false; |
| 78 // We don't check the title since it's not likely to change, but that co
uld also be done. |
| 79 return true; |
| 80 } |
| 81 |
| 82 private void renderTile(Tile tile) { |
| 83 // A TileView should not be reused across tiles having different urls, a
s registered |
| 84 // callbacks and handlers use it to look up the data and notify the rest
of the system. |
| 85 assert mUrl.equals(tile.getUrl()); |
| 86 mTitleView.setText(TitleUtil.getTitleForDisplay(tile.getTitle(), tile.ge
tUrl())); |
| 87 mBadgeView.setVisibility(tile.isOfflineAvailable() ? VISIBLE : GONE); |
| 88 renderIcon(tile); |
| 62 } | 89 } |
| 63 } | 90 } |
| OLD | NEW |