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

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/ntp/InterestsItemView.java

Issue 1351303003: Add the UI for the Interests Prototype. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@ntp-interests-jni
Patch Set: Fetch the oauth token from the native code. 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/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..e088e301638007fff6b0aa6f349d0d8cf585d653
--- /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.IOException;
+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 DownloadImageTask mDownloadTask;
+
+ /**
+ * @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, DrawingData drawingData) {
+ super(context);
+
+ mContext = context;
+ mListener = listener;
+ mImageCache = imageCache;
+ mDrawingData = drawingData;
+
+ 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.
newt (away) 2015/09/29 17:33:22 s/list/grid
PEConn 2015/11/18 15:26:56 Done.
+ *
+ * @param name The title of the page.
+ * @param imageUrl The URL of the page.
+ */
+ public void 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);
newt (away) 2015/09/29 17:33:22 How about mDownloadTask.cancel(), and then checkin
PEConn 2015/11/18 15:26:56 This code was refactored out.
+ mDownloadTask = null;
+ }
+ // Reset drawable state so ripples don't continue when the view is reused.
+ jumpDrawablesToCurrentState();
+
+ if (mName != null && mName.equals(name) && mImageUrl != null
newt (away) 2015/09/29 17:33:21 You can use TextUtils.equals(name, mName) && TextU
PEConn 2015/11/18 15:26:57 Done.
+ && mImageUrl.equals(imageUrl)) {
+ return;
+ }
+
+ 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.
+ mDownloadTask = new DownloadImageTask(this, mImageCache);
+ mDownloadTask.execute(mImageUrl);
+
+ image = new BitmapDrawable(
+ mContext.getResources(),
+ mIconGenerator.generateIconForText(mName));
+ }
+
+ setImage(image);
+
+ return;
+ }
+
+ /**
+ * @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) {
newt (away) 2015/09/29 17:33:22 Simply ignoring null images is surprising behavior
PEConn 2015/11/18 15:26:56 Done.
+ return;
+ }
+
+ setImageBounds(image);
+
+ this.setCompoundDrawables(null, image, null, null);
newt (away) 2015/09/29 17:33:21 remove "this."
PEConn 2015/11/18 15:26:56 Done.
+ }
+
+ private void setImageBounds(Drawable image) {
newt (away) 2015/09/29 17:33:22 Why is this a separate method? I'd just inline it
PEConn 2015/11/18 15:26:57 Done.
+ image.setBounds(new Rect(
+ 0, 0, mDrawingData.mImageSize, mDrawingData.mImageSize));
+ }
+
+ @Override
+ public void onClick(View v) {
+ mListener.onInterestClicked(getName());
+ }
+
+ private class DownloadImageTask extends AsyncTask<String, Integer, Drawable> {
Bernhard Bauer 2015/09/29 14:33:28 The progress type should be Void if it's unused.
newt (away) 2015/09/29 17:33:21 We should ideally use the Chrome network stack her
+
+ private InterestsItemView mView;
Bernhard Bauer 2015/09/29 14:33:28 If this is not a static class, it will have its ou
newt (away) 2015/09/29 17:33:22 Alternatively, make this a static class, which is
PEConn 2015/11/18 15:26:57 The DownloadImageTask is pretty closely tied to th
+ private LruCache<String, Drawable> mCache;
+ private String mUrl;
+ private boolean mShouldUpdateView;
+
+ public DownloadImageTask(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) {
newt (away) 2015/09/29 17:33:22 s/arg0/args
PEConn 2015/11/18 15:26:56 Done.
+ // 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.
+ */
+ @Override
+ protected void onPostExecute(Drawable image) {
+ if (image != null) {
+ mCache.put(mUrl, image);
Bernhard Bauer 2015/09/29 14:33:28 I think you could simplify this a bit: Keep the ca
PEConn 2015/11/18 15:26:56 Done.
+
+ 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 (IOException e) {
+ 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;
+ }
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698