Index: chrome/android/java/src/org/chromium/chrome/browser/ntp/InterestsItemView.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ntp/InterestsItemView.java b/chrome/android/java/src/org/chromium/chrome/browser/ntp/InterestsItemView.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8fb0bbd1fe4ea72f68a2847d50346f54c7ffff88 |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/InterestsItemView.java |
@@ -0,0 +1,247 @@ |
+// 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. |
+ |
+package org.chromium.chrome.browser.ntp; |
+ |
+import android.content.Context; |
+import android.content.res.Resources; |
+import android.graphics.Color; |
+import android.graphics.Rect; |
+import android.graphics.drawable.BitmapDrawable; |
+import android.graphics.drawable.Drawable; |
+import android.os.AsyncTask; |
+import android.support.v4.graphics.drawable.RoundedBitmapDrawable; |
+import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory; |
+import android.support.v7.widget.AppCompatTextView; |
+import android.util.Log; |
+import android.util.LruCache; |
+import android.util.TypedValue; |
+import android.view.Gravity; |
+import android.view.View; |
+import android.view.View.OnClickListener; |
+ |
+import org.chromium.chrome.R; |
+import org.chromium.chrome.browser.ntp.InterestsPage.InterestsListener; |
+import org.chromium.chrome.browser.widget.RoundedIconGenerator; |
+ |
+import java.io.InputStream; |
+import java.net.URL; |
+ |
+/** |
+ * Displays the interest name along with an image of it. This item can be clicked. |
+ */ |
+class InterestsItemView extends AppCompatTextView implements OnClickListener { |
+ |
+ /** |
+ * Drawing-related values that can be shared between instances of InterestsItemView. |
+ */ |
+ static final class DrawingData { |
+ |
+ private final int mPadding; |
+ private final int mMinHeight; |
+ private final int mImageSize; |
+ private final int mTextSize; |
+ private final int mImageTextSize; |
+ |
+ /** |
+ * Initialize shared values used for drawing the image. |
+ * |
+ * @param context The view context in which the InterestsItemView will be drawn. |
+ */ |
+ DrawingData(Context context) { |
+ Resources res = context.getResources(); |
+ mPadding = res.getDimensionPixelOffset(R.dimen.ntp_list_item_padding); |
+ mMinHeight = res.getDimensionPixelSize(R.dimen.ntp_interest_item_min_height); |
+ mTextSize = res.getDimensionPixelSize(R.dimen.ntp_interest_item_text_size); |
+ mImageSize = res.getDimensionPixelSize(R.dimen.ntp_interest_item_image_size); |
+ mImageTextSize = res.getDimensionPixelSize(R.dimen.ntp_interest_item_image_text_size); |
+ |
+ } |
+ } |
+ |
+ private String mName; |
+ private String mImageUrl; |
+ private LruCache<String, Drawable> mImageCache; |
+ private Context mContext; |
+ private RoundedIconGenerator mIconGenerator; |
+ private DrawingData mDrawingData; |
+ private InterestsListener mListener; |
+ private DownloadImage mDownloadTask = null; |
Marc Treib
2015/09/18 13:26:03
Why is (only) this one explicitly initialized to n
tache
2015/09/21 15:35:54
Done.
|
+ |
+ /** |
+ * @param context The view context in which this item will be shown. |
+ * @param name The name of the interest. |
+ * @param imageUrl The URL for the interet's image. |
+ * @param imageCache A cache to store downloaded images. |
+ */ |
+ InterestsItemView(Context context, String name, String imageUrl, |
+ InterestsListener listener, |
+ LruCache<String, Drawable> imageCache) { |
Marc Treib
2015/09/18 13:26:03
Weird line breaks?
tache
2015/09/21 15:35:55
Done.
|
+ super(context); |
+ |
+ mContext = context; |
+ mListener = listener; |
+ mImageCache = imageCache; |
+ mDrawingData = new DrawingData(mContext); |
Marc Treib
2015/09/18 13:26:03
Wait, when you use it like this, then nothing is a
tache
2015/09/21 15:35:54
Now it shared in the InterestPageView and past to
|
+ |
+ mIconGenerator = new RoundedIconGenerator( |
+ mDrawingData.mImageSize, |
+ mDrawingData.mImageSize, |
+ mDrawingData.mImageSize / 2, |
+ Color.GRAY, |
+ mDrawingData.mImageTextSize); |
+ |
+ setTextSize(TypedValue.COMPLEX_UNIT_PX, mDrawingData.mTextSize); |
+ setMinimumHeight(mDrawingData.mMinHeight); |
+ setGravity(Gravity.CENTER); |
+ setTextAlignment(View.TEXT_ALIGNMENT_CENTER); |
+ |
+ setOnClickListener(this); |
+ |
+ reset(name, imageUrl); |
+ } |
+ |
+ /** |
+ * Resets the view contents so that it can be reused in the list view. |
+ * |
+ * @param name The title of the page. |
+ * @param imageUrl The URL of the page. |
+ */ |
Marc Treib
2015/09/18 13:26:03
Add @return please
tache
2015/09/21 15:35:54
Removed the return value, since it was not used an
|
+ public boolean reset(String name, String imageUrl) { |
+ // If there is an active download task for this view then it shouldn't update |
+ // the view when it finishes. |
+ if (mDownloadTask != null) { |
+ mDownloadTask.setShouldUpdateView(false); |
+ mDownloadTask = null; |
+ } |
+ // Reset drawable state so ripples don't continue when the view is reused. |
+ jumpDrawablesToCurrentState(); |
+ |
+ if (mName != null && mName.equals(name) && mImageUrl != null |
+ && mImageUrl.equals(imageUrl)) { |
+ return false; |
+ } |
+ |
+ mName = name; |
+ setText(mName); |
+ |
+ mImageUrl = imageUrl; |
+ Drawable image = mImageCache.get(mImageUrl); |
+ |
+ if (image == null) { |
+ // cache miss need to download the image and use a temporary image in the meantime. |
Marc Treib
2015/09/18 13:26:03
nit: Capitalize "Cache", also add a comma after "m
tache
2015/09/21 15:35:55
Done.
|
+ mDownloadTask = new DownloadImage(this, mImageCache); |
+ mDownloadTask.execute(mImageUrl); |
+ |
+ image = new BitmapDrawable( |
+ mContext.getResources(), |
+ mIconGenerator.generateIconForText(mName)); |
+ |
+ } |
+ |
+ setImage(image); |
+ |
+ return true; |
+ } |
+ |
+ /** |
+ * @return The image url for the interest. |
+ */ |
+ public String getImageUrl() { |
+ return mImageUrl; |
+ } |
+ |
+ /** |
+ * @return The name of the interest. |
+ */ |
+ public String getName() { |
+ return mName; |
+ } |
+ |
+ /** |
+ * Updates the image and triggers a redraw with the new image. |
+ * |
+ * @param image The new image to display. May be null. |
+ */ |
+ public void setImage(Drawable image) { |
+ if (image == null) { |
+ return; |
+ } |
+ |
+ setImageBounds(image); |
+ |
+ this.setCompoundDrawables(null, image, null, null); |
+ } |
+ |
+ private void setImageBounds(Drawable image) { |
+ image.setBounds(new Rect( |
+ 0, 0, mDrawingData.mImageSize, mDrawingData.mImageSize)); |
+ } |
+ |
+ @Override |
+ public void onClick(View v) { |
+ mListener.onInterestClicked(getName()); |
+ } |
+ |
+ private class DownloadImage extends AsyncTask<String, Integer, Drawable> { |
Marc Treib
2015/09/18 13:26:03
DownloadImageTask?
tache
2015/09/21 15:35:55
Done.
|
+ |
+ private InterestsItemView mView; |
+ private LruCache<String, Drawable> mCache; |
+ private String mUrl; |
+ private boolean mShouldUpdateView; |
+ |
+ public DownloadImage(InterestsItemView view, LruCache<String, Drawable> cache) { |
+ mView = view; |
+ mCache = cache; |
+ mShouldUpdateView = true; |
+ } |
+ |
+ public void setShouldUpdateView(boolean shouldUpdateView) { |
+ mShouldUpdateView = shouldUpdateView; |
+ } |
+ |
+ @Override |
+ protected Drawable doInBackground(String... arg0) { |
+ // This is done in a background thread |
+ mUrl = arg0[0]; |
+ return downloadImage(mUrl); |
+ } |
+ |
+ /** |
+ * Called after the image has been downloaded. This runs on the main thread. |
+ */ |
+ protected void onPostExecute(Drawable image) { |
Marc Treib
2015/09/18 13:26:03
Should this have @Override?
tache
2015/09/21 15:35:55
Done.
|
+ if (image != null) { |
+ mCache.put(mUrl, image); |
+ |
+ if (mShouldUpdateView) { |
+ mView.setImage(image); |
+ } |
+ } |
+ } |
+ |
+ private Drawable downloadImage(String url) { |
+ URL imageUrl; |
+ InputStream in; |
+ |
+ try { |
+ imageUrl = new URL(url); |
+ in = imageUrl.openStream(); |
+ |
+ return getRoundedImage(in); |
+ } catch (Exception e) { |
Marc Treib
2015/09/18 13:26:03
Be more specific than just "Exception" please.
tache
2015/09/21 15:35:54
Done.
|
+ Log.e("Error downloading image: ", e.toString()); |
+ } |
+ return null; |
+ } |
+ |
+ private Drawable getRoundedImage(InputStream in) { |
+ RoundedBitmapDrawable img = RoundedBitmapDrawableFactory.create(getResources(), in); |
+ |
+ img.setCircular(true); |
+ |
+ return img; |
+ } |
+ } |
+} |