Chromium Code Reviews| 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 |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5bd5390f3cc2e616294ed4a0924b985f2bb8d45c |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetArticle.java |
| @@ -0,0 +1,119 @@ |
| +// Copyright 2016 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.snippets; |
| + |
| +import android.graphics.drawable.Drawable; |
| +import android.os.AsyncTask; |
| +import android.view.View; |
| +import android.widget.ImageView; |
| + |
| +import org.chromium.base.Log; |
| +import org.chromium.base.StreamUtil; |
| +import org.chromium.chrome.browser.ntp.NewTabPageCardsManager; |
| +import org.chromium.chrome.browser.ntp.NewTabPageCardsManager.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 = "SnippetsManager"; |
| + |
| + private static final int FADE_IN_ANIMATION_TIME_MS = 500; |
|
newt (away)
2016/03/24 19:47:39
300ms would be more standard
dgn
2016/03/28 21:33:02
Done.
|
| + |
| + public final String mTitle; |
| + public final String mPublisher; |
| + public final String mPreviewText; |
| + public final String mUrl; |
| + public final String mThumbnailUrl; |
| + 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; |
| + } |
| + |
| + @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); |
| + } |
| + } |
| + } |
| + |
| + /** |
| + * Creates a SnippetArticle object that will hold the data |
| + * @param title the title of the article |
| + * @param publisher the canonical publisher name (e.g., New York Times) |
| + * @param previewText the snippet preview text |
| + * @param url the URL of the article |
| + * @param thumbnailPath the URL of the thumbnail |
| + * @param timestamp the time in ms when this article was published |
| + * @param position the position of this article in the list of snippets |
| + */ |
| + public SnippetArticle(String title, String publisher, String previewText, String url, |
| + String thumbnailUrl, long timestamp, int position) { |
| + mTitle = title; |
| + mPublisher = publisher; |
| + mPreviewText = previewText; |
| + mUrl = url; |
| + mThumbnailUrl = thumbnailUrl; |
| + mTimestamp = timestamp; |
| + mPosition = position; |
| + } |
| + |
| + @Override |
| + public int getType() { |
| + return NewTabPageCardsManager.SNIPPET_ITEM_TYPE_SNIPPET; |
| + } |
| + |
| + /** |
| + * Retrieves this SnippetArticle's thumbnail asynchronously and sets it onto the given |
| + * ImageView. |
| + * |
| + * @param view The ImageView to set the thumbnail onto. |
| + */ |
| + public void setThumbnailOnView(ImageView view) { |
| + if (mThumbnailRenderingTask != null) mThumbnailRenderingTask.cancel(true); |
| + |
| + mThumbnailRenderingTask = new ThumbnailRenderingTask(view); |
| + mThumbnailRenderingTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, mThumbnailUrl); |
| + } |
| +} |