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

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

Issue 1871343006: [NTP Snippets] 📷 Add placeholders and start caching thumbnails (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments Created 4 years, 8 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/snippets/SnippetArticle.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetArticle.java b/chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetArticle.java
index 7d99986fe6f1df09a45022777b0ac8074a927bc2..f4c81f84d07acc219a0e4f0fc67260460467d352 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetArticle.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetArticle.java
@@ -3,27 +3,14 @@
// found in the LICENSE file.
package org.chromium.chrome.browser.ntp.snippets;
-import android.graphics.drawable.Drawable;
-import android.os.AsyncTask;
-import android.view.View;
-import android.widget.ImageView;
+import android.graphics.Bitmap;
-import org.chromium.base.Log;
-import org.chromium.base.StreamUtil;
import org.chromium.chrome.browser.ntp.cards.NewTabPageListItem;
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.MalformedURLException;
-import java.net.URL;
-
/**
* Represents the data for an article card on the NTP.
*/
public class SnippetArticle implements NewTabPageListItem {
- private static final String TAG = "SnippetArticle";
-
- private static final int FADE_IN_ANIMATION_TIME_MS = 300;
public final String mTitle;
public final String mPublisher;
@@ -33,54 +20,8 @@
public final long mTimestamp;
public final int mPosition;
- private ThumbnailRenderingTask mThumbnailRenderingTask;
-
- // Async task to create the thumbnail from a URL
- // TODO(maybelle): This task to retrieve the thumbnail from the web is temporary while
- // we are prototyping this feature for clank. For the real production feature, we
- // will likely have to download/decode the thumbnail on the native side.
- private static class ThumbnailRenderingTask extends AsyncTask<String, Void, Drawable> {
- private ImageView mThumbnailView;
-
- ThumbnailRenderingTask(ImageView thumbnailView) {
- mThumbnailView = thumbnailView;
-
- // The view might be already holding the thumbnail from another snippet, as the view
- // is recycled. We start by hiding it to avoid having a stale image be displayed while
- // the new one is being downloaded.
- mThumbnailView.setVisibility(View.INVISIBLE);
- }
-
- @Override
- protected Drawable doInBackground(String... params) {
- if (params[0].isEmpty()) return null;
- InputStream is = null;
- try {
- is = (InputStream) new URL(params[0]).getContent();
- return Drawable.createFromStream(is, "thumbnail");
- } catch (MalformedURLException e) {
- Log.e(TAG, "Could not get image thumbnail due to malformed URL", e);
- } catch (IOException e) {
- Log.e(TAG, "Could not get image thumbnail", e);
- } finally {
- StreamUtil.closeQuietly(is);
- }
- return null;
- }
-
- @Override
- protected void onPostExecute(Drawable thumbnail) {
- if (thumbnail == null) {
- mThumbnailView.setVisibility(View.GONE);
- } else {
- // Fade in the image thumbnail
- mThumbnailView.setImageDrawable(thumbnail);
- mThumbnailView.setVisibility(View.VISIBLE);
- mThumbnailView.setAlpha(0f);
- mThumbnailView.animate().alpha(1f).setDuration(FADE_IN_ANIMATION_TIME_MS);
- }
- }
- }
+ /** Bitmap of the thumbnail, fetched lazily, when the RecyclerView wants to show the snippet. */
+ private Bitmap mThumbnailBitmap;
/**
* Creates a SnippetArticle object that will hold the data
@@ -109,15 +50,15 @@ public int getType() {
}
/**
- * Retrieves this SnippetArticle's thumbnail asynchronously and sets it onto the given
- * ImageView.
- *
- * @param view The ImageView to set the thumbnail onto.
+ * Returns this article's tumbnail as a {@link Bitmap}. Can return {@code null} as it is
+ * initially unset.
*/
- public void setThumbnailOnView(ImageView view) {
- if (mThumbnailRenderingTask != null) mThumbnailRenderingTask.cancel(true);
+ Bitmap getThumbnailBitmap() {
+ return mThumbnailBitmap;
+ }
- mThumbnailRenderingTask = new ThumbnailRenderingTask(view);
- mThumbnailRenderingTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, mThumbnailUrl);
+ /** Sets the tumbnail bitmap for this article. */
+ void setThumbnailBitmap(Bitmap bitmap) {
+ mThumbnailBitmap = bitmap;
}
}

Powered by Google App Engine
This is Rietveld 408576698