Commit d91952ef authored by Cathy Li's avatar Cathy Li Committed by Commit Bot

[ExploreSites][Touchless] Refactor TileWithTextView into TileView and TextView

and allow the new touchless suggestions files to depend on TileView

Bug: 936050
Change-Id: Ib611b788b532a72a8fce6e8b982c4e288b91408b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1518251Reviewed-by: default avatarTheresa <twellington@chromium.org>
Reviewed-by: default avatarJustin DeWitt <dewittj@chromium.org>
Commit-Queue: Cathy Li <chili@chromium.org>
Cr-Commit-Position: refs/heads/master@{#641114}
parent 1fec8e45
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2015 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file. -->
<!-- An icon tile. -->
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" >
<!-- The icon background. -->
<View
android:id="@+id/tile_view_icon_background"
android:layout_width="@dimen/tile_view_icon_size"
android:layout_height="@dimen/tile_view_icon_size"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/tile_view_icon_background_margin_top_modern"
android:background="@drawable/tile_view_icon_background_modern" />
<!-- The main icon. -->
<ImageView
android:id="@+id/tile_view_icon"
android:layout_width="@dimen/tile_view_icon_size_modern"
android:layout_height="@dimen/tile_view_icon_size_modern"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/tile_view_icon_margin_top_modern"
tools:ignore="ContentDescription" />
<!-- The touch highlight. -->
<View
android:id="@+id/tile_view_highlight"
android:layout_width="@dimen/tile_view_icon_size"
android:layout_height="@dimen/tile_view_icon_size"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/tile_view_icon_background_margin_top_modern"
android:background="@drawable/tile_view_highlight" />
<!-- The offline badge. -->
<ImageView
android:id="@+id/offline_badge"
android:layout_width="@dimen/tile_view_offline_badge_size_modern"
android:layout_height="@dimen/tile_view_offline_badge_size_modern"
android:layout_gravity="top|end"
android:layout_marginTop="@dimen/tile_view_offline_badge_margin_top_modern_condensed"
android:layout_marginEnd="@dimen/tile_view_offline_badge_margin_end_modern_condensed"
android:visibility="gone"
android:contentDescription="@string/accessibility_ntp_offline_badge"
app:srcCompat="@drawable/ic_offline_pin_blue_white" />
</merge>
\ No newline at end of file
...@@ -9,44 +9,7 @@ ...@@ -9,44 +9,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" > xmlns:tools="http://schemas.android.com/tools" >
<!-- The icon background. --> <include layout="@layout/tile_no_text_view_condensed" />
<View
android:id="@+id/tile_view_icon_background"
android:layout_width="@dimen/tile_view_icon_size"
android:layout_height="@dimen/tile_view_icon_size"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/tile_view_icon_background_margin_top_modern"
android:background="@drawable/tile_view_icon_background_modern" />
<!-- The main icon. -->
<ImageView
android:id="@+id/tile_view_icon"
android:layout_width="@dimen/tile_view_icon_size_modern"
android:layout_height="@dimen/tile_view_icon_size_modern"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/tile_view_icon_margin_top_modern"
tools:ignore="ContentDescription" />
<!-- The touch highlight. -->
<View
android:id="@+id/tile_view_highlight"
android:layout_width="@dimen/tile_view_icon_size"
android:layout_height="@dimen/tile_view_icon_size"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/tile_view_icon_background_margin_top_modern"
android:background="@drawable/tile_view_highlight" />
<!-- The offline badge. -->
<ImageView
android:id="@+id/offline_badge"
android:layout_width="@dimen/tile_view_offline_badge_size_modern"
android:layout_height="@dimen/tile_view_offline_badge_size_modern"
android:layout_gravity="top|end"
android:layout_marginTop="@dimen/tile_view_offline_badge_margin_top_modern_condensed"
android:layout_marginEnd="@dimen/tile_view_offline_badge_margin_end_modern_condensed"
android:visibility="gone"
android:contentDescription="@string/accessibility_ntp_offline_badge"
app:srcCompat="@drawable/ic_offline_pin_blue_white" />
<!-- The title. --> <!-- The title. -->
<TextView <TextView
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.widget.tile;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.FrameLayout;
import android.widget.ImageView;
import org.chromium.chrome.R;
/**
* The view for a tile with icon and text.
*
* Displays the title of the site beneath a large icon.
*/
public class TileView extends FrameLayout {
private ImageView mBadgeView;
protected ImageView mIconView;
/**
* Constructor for inflating from XML.
*/
public TileView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
mIconView = findViewById(R.id.tile_view_icon);
mBadgeView = findViewById(R.id.offline_badge);
}
/**
* Initializes the view. This should be called immediately after inflation.
*
* @param title The title of the tile.
* @param showOfflineBadge Whether to show the offline badge.
* @param icon The icon to display on the tile.
* @param titleLines The number of text lines to use for the tile title.
*/
public void initialize(boolean showOfflineBadge, Drawable icon) {
setOfflineBadgeVisibility(showOfflineBadge);
setIconDrawable(icon);
}
/**
* Renders the icon or clears it from the view if the icon is null.
*/
public void setIconDrawable(Drawable icon) {
mIconView.setImageDrawable(icon);
}
/** Shows or hides the offline badge to reflect the offline availability. */
public void setOfflineBadgeVisibility(boolean showOfflineBadge) {
mBadgeView.setVisibility(showOfflineBadge ? VISIBLE : GONE);
}
}
...@@ -7,8 +7,6 @@ package org.chromium.chrome.browser.widget.tile; ...@@ -7,8 +7,6 @@ package org.chromium.chrome.browser.widget.tile;
import android.content.Context; import android.content.Context;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import org.chromium.chrome.R; import org.chromium.chrome.R;
...@@ -18,10 +16,8 @@ import org.chromium.chrome.R; ...@@ -18,10 +16,8 @@ import org.chromium.chrome.R;
* *
* Displays the title of the site beneath a large icon. * Displays the title of the site beneath a large icon.
*/ */
public class TileWithTextView extends FrameLayout { public class TileWithTextView extends TileView {
private TextView mTitleView; private TextView mTitleView;
private ImageView mBadgeView;
protected ImageView mIconView;
/** /**
* Constructor for inflating from XML. * Constructor for inflating from XML.
...@@ -35,8 +31,6 @@ public class TileWithTextView extends FrameLayout { ...@@ -35,8 +31,6 @@ public class TileWithTextView extends FrameLayout {
super.onFinishInflate(); super.onFinishInflate();
mTitleView = findViewById(R.id.tile_view_title); mTitleView = findViewById(R.id.tile_view_title);
mIconView = findViewById(R.id.tile_view_icon);
mBadgeView = findViewById(R.id.offline_badge);
} }
/** /**
...@@ -48,9 +42,8 @@ public class TileWithTextView extends FrameLayout { ...@@ -48,9 +42,8 @@ public class TileWithTextView extends FrameLayout {
* @param titleLines The number of text lines to use for the tile title. * @param titleLines The number of text lines to use for the tile title.
*/ */
public void initialize(String title, boolean showOfflineBadge, Drawable icon, int titleLines) { public void initialize(String title, boolean showOfflineBadge, Drawable icon, int titleLines) {
super.initialize(showOfflineBadge, icon);
setTitle(title, titleLines); setTitle(title, titleLines);
setOfflineBadgeVisibility(showOfflineBadge);
setIconDrawable(icon);
} }
/** Sets the title text and number lines. */ /** Sets the title text and number lines. */
...@@ -58,16 +51,4 @@ public class TileWithTextView extends FrameLayout { ...@@ -58,16 +51,4 @@ public class TileWithTextView extends FrameLayout {
mTitleView.setLines(titleLines); mTitleView.setLines(titleLines);
mTitleView.setText(title); mTitleView.setText(title);
} }
/**
* Renders the icon or clears it from the view if the icon is null.
*/
public void setIconDrawable(Drawable icon) {
mIconView.setImageDrawable(icon);
}
/** Shows or hides the offline badge to reflect the offline availability. */
public void setOfflineBadgeVisibility(boolean showOfflineBadge) {
mBadgeView.setVisibility(showOfflineBadge ? VISIBLE : GONE);
}
} }
...@@ -1819,6 +1819,7 @@ chrome_java_sources = [ ...@@ -1819,6 +1819,7 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/widget/textbubble/ArrowBubbleDrawable.java", "java/src/org/chromium/chrome/browser/widget/textbubble/ArrowBubbleDrawable.java",
"java/src/org/chromium/chrome/browser/widget/textbubble/ImageTextBubble.java", "java/src/org/chromium/chrome/browser/widget/textbubble/ImageTextBubble.java",
"java/src/org/chromium/chrome/browser/widget/textbubble/TextBubble.java", "java/src/org/chromium/chrome/browser/widget/textbubble/TextBubble.java",
"java/src/org/chromium/chrome/browser/widget/tile/TileView.java",
"java/src/org/chromium/chrome/browser/widget/tile/TileWithTextView.java", "java/src/org/chromium/chrome/browser/widget/tile/TileWithTextView.java",
] ]
...@@ -1832,6 +1833,7 @@ if (notouch_build) { ...@@ -1832,6 +1833,7 @@ if (notouch_build) {
"touchless/java/src/org/chromium/chrome/browser/touchless/NoTouchActivity.java", "touchless/java/src/org/chromium/chrome/browser/touchless/NoTouchActivity.java",
"touchless/java/src/org/chromium/chrome/browser/touchless/SiteSuggestionsAdapter.java", "touchless/java/src/org/chromium/chrome/browser/touchless/SiteSuggestionsAdapter.java",
"touchless/java/src/org/chromium/chrome/browser/touchless/SiteSuggestionsController.java", "touchless/java/src/org/chromium/chrome/browser/touchless/SiteSuggestionsController.java",
"touchless/java/src/org/chromium/chrome/browser/touchless/SiteSuggestionsTileView.java",
"touchless/java/src/org/chromium/chrome/browser/touchless/SiteSuggestionsViewHolderFactory.java", "touchless/java/src/org/chromium/chrome/browser/touchless/SiteSuggestionsViewHolderFactory.java",
"touchless/java/src/org/chromium/chrome/browser/touchless/TouchlessDelegate.java", "touchless/java/src/org/chromium/chrome/browser/touchless/TouchlessDelegate.java",
"touchless/java/src/org/chromium/chrome/browser/touchless/TouchlessNewTabPage.java", "touchless/java/src/org/chromium/chrome/browser/touchless/TouchlessNewTabPage.java",
......
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2019 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file. -->
<org.chromium.chrome.browser.touchless.SiteSuggestionsTileView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/tile_view_width"
android:layout_height="wrap_content" >
<include
layout="@layout/tile_no_text_view_condensed"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</org.chromium.chrome.browser.touchless.SiteSuggestionsTileView>
...@@ -7,12 +7,9 @@ package org.chromium.chrome.browser.touchless; ...@@ -7,12 +7,9 @@ package org.chromium.chrome.browser.touchless;
import static org.chromium.chrome.browser.touchless.SiteSuggestionsController.SUGGESTIONS_KEY; import static org.chromium.chrome.browser.touchless.SiteSuggestionsController.SUGGESTIONS_KEY;
import android.content.Context; import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.widget.ImageView;
import org.chromium.chrome.browser.suggestions.SiteSuggestion; import org.chromium.chrome.browser.suggestions.SiteSuggestion;
import org.chromium.chrome.browser.widget.RoundedIconGenerator; import org.chromium.chrome.browser.widget.RoundedIconGenerator;
import org.chromium.chrome.touchless.R;
import org.chromium.ui.modelutil.ForwardingListObservable; import org.chromium.ui.modelutil.ForwardingListObservable;
import org.chromium.ui.modelutil.PropertyModel; import org.chromium.ui.modelutil.PropertyModel;
import org.chromium.ui.modelutil.RecyclerViewAdapter; import org.chromium.ui.modelutil.RecyclerViewAdapter;
...@@ -56,9 +53,8 @@ public class SiteSuggestionsAdapter ...@@ -56,9 +53,8 @@ public class SiteSuggestionsAdapter
int position, Void payload) { int position, Void payload) {
SiteSuggestion item = SiteSuggestion item =
mModel.get(SUGGESTIONS_KEY).get(position % mModel.get(SUGGESTIONS_KEY).size()); mModel.get(SUGGESTIONS_KEY).get(position % mModel.get(SUGGESTIONS_KEY).size());
// TODO(chili): Update when tileview is created with no textview. SiteSuggestionsTileView image = (SiteSuggestionsTileView) holder.itemView;
ImageView image = holder.itemView.findViewById(R.id.tile_view_icon); image.initialize(mIconGenerator);
image.setImageDrawable(new BitmapDrawable( image.updateIcon(null, item.title);
mContext.getResources(), mIconGenerator.generateIconForText(item.title)));
} }
} }
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.touchless;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import org.chromium.chrome.browser.util.ViewUtils;
import org.chromium.chrome.browser.widget.RoundedIconGenerator;
import org.chromium.chrome.browser.widget.tile.TileView;
import org.chromium.chrome.touchless.R;
/**
* View for a category name and site tiles.
*/
public class SiteSuggestionsTileView extends TileView {
private final int mIconSizePx;
// Used to generate textual icons.
private RoundedIconGenerator mIconGenerator;
public SiteSuggestionsTileView(Context ctx, AttributeSet attrs) {
super(ctx, attrs);
mIconSizePx = getResources().getDimensionPixelSize(R.dimen.tile_view_icon_size);
}
public void initialize(RoundedIconGenerator generator) {
mIconGenerator = generator;
}
/**
* Updates the icon in the tile.
*
* @param iconImage Bitmap icon to update.
* @param text Text to generate scrabble text from.
*/
public void updateIcon(Bitmap iconImage, String text) {
setIconDrawable(getDrawableForBitmap(iconImage, text));
}
/**
* Generates a Drawable from the Bitmap version of an icon.
*
* @param image The Bitmap image. Can be null.
* @param text The text to generate a scrabble text from if the bitmap is null.
* @return Drawable encapsulating either the scrabble tile or the bitmap.
*/
public Drawable getDrawableForBitmap(Bitmap image, String text) {
if (image == null) {
return new BitmapDrawable(getResources(), mIconGenerator.generateIconForText(text));
}
return ViewUtils.createRoundedBitmapDrawable(
Bitmap.createScaledBitmap(image, mIconSizePx, mIconSizePx, false),
ViewUtils.DEFAULT_FAVICON_CORNER_RADIUS);
}
}
...@@ -30,6 +30,6 @@ public class SiteSuggestionsViewHolderFactory implements RecyclerViewAdapter.Vie ...@@ -30,6 +30,6 @@ public class SiteSuggestionsViewHolderFactory implements RecyclerViewAdapter.Vie
public SiteSuggestionsViewHolder createViewHolder(ViewGroup parent, int viewType) { public SiteSuggestionsViewHolder createViewHolder(ViewGroup parent, int viewType) {
return new SiteSuggestionsViewHolder( return new SiteSuggestionsViewHolder(
LayoutInflater.from(parent.getContext()) LayoutInflater.from(parent.getContext())
.inflate(R.layout.explore_sites_tile_view, parent, false)); .inflate(R.layout.touchless_suggestions_tile_view, parent, false));
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment