Index: chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetsManager.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetsManager.java b/chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetsManager.java |
index 16d74ade3a6fa20d24fc1f42f006bc3241f95486..8c637b873bf80526f346ff1e151e2271dc968e49 100644 |
--- a/chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetsManager.java |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetsManager.java |
@@ -4,21 +4,12 @@ |
package org.chromium.chrome.browser.ntp.snippets; |
-import android.graphics.drawable.Drawable; |
-import android.os.AsyncTask; |
-import android.support.v7.widget.RecyclerView; |
-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 org.chromium.chrome.browser.ntp.NewTabPageView.NewTabPageManager; |
import org.chromium.chrome.browser.ntp.snippets.SnippetsBridge.SnippetsObserver; |
import org.chromium.chrome.browser.profiles.Profile; |
-import java.io.IOException; |
-import java.io.InputStream; |
-import java.net.MalformedURLException; |
-import java.net.URL; |
import java.util.ArrayList; |
import java.util.List; |
@@ -26,17 +17,6 @@ import java.util.List; |
* A class that stores and manages the article snippets that will be shown on the NTP. |
*/ |
public class SnippetsManager { |
newt (away)
2016/03/24 19:47:39
I still don't understand why it's useful to keep t
|
- private static final String TAG = "SnippetsManager"; |
- |
- /** |
- * Describes the header of a group of similar card snippets |
- */ |
- public static final int SNIPPET_ITEM_TYPE_HEADER = 1; |
- /** |
- * Describes a single card snippet |
- */ |
- public static final int SNIPPET_ITEM_TYPE_SNIPPET = 2; |
- |
/** |
* Enum values for recording {@code SNIPPETS_STATE_HISTOGRAM} histogram. |
* Stored here as it is recorded in multiple places. |
@@ -48,134 +28,82 @@ public class SnippetsManager { |
public static final String SNIPPETS_STATE_HISTOGRAM = "NewTabPage.Snippets.Interactions"; |
private NewTabPageManager mNewTabPageManager; |
- private SnippetsAdapter mDataAdapter; |
+ private List<NewTabPageListItem> mListSnippetItems = new ArrayList<>(); |
private SnippetsBridge mSnippetsBridge; |
+ private SnippetsManagerObserver mObserver; |
- /** Base type for anything to add to the snippets view |
+ /** |
+ * An observer that will be called when new snippets are downloaded |
*/ |
- public interface SnippetListItem { |
+ public interface SnippetsManagerObserver { |
/** |
- * Returns the type of this snippet item (SNIPPET_ITEM_TYPE_HEADER or |
- * SNIPPET_ITEM_TYPE_SNIPPET). This is so we can distinguish between different elements |
- * that are held in a single RecyclerView holder. |
+ * Called when a new set of snippets are downloaded. |
* |
- * @return the type of this list item. |
+ * @param listSnippets the list of new snippets and headers |
*/ |
- public int getType(); |
+ public void onSnippetsReceived(List<NewTabPageListItem> listSnippets); |
} |
- /** Represents the data for a header of a group of snippets |
+ /** |
+ * Represents the data for a header of a group of snippets |
*/ |
- public static class SnippetHeader implements SnippetListItem { |
- public final String mRecommendationBasis; |
- |
- public SnippetHeader(String recommendationBasis) { |
- mRecommendationBasis = recommendationBasis; |
- } |
+ public static class SnippetHeader implements NewTabPageListItem { |
+ public SnippetHeader() {} |
@Override |
public int getType() { |
- return SNIPPET_ITEM_TYPE_HEADER; |
+ return NewTabPageCardsManager.SNIPPET_ITEM_TYPE_HEADER; |
} |
} |
/** |
- * Represents the data for a card snippet. |
+ * Creates a SnippetManager object that will handle all receiving all the snippets data |
+ * |
+ * @param tabManager the tab manager to use for loading the article |
+ * @param profile the Profile of the current user. |
*/ |
- public static class SnippetArticle implements SnippetListItem { |
- public final String mTitle; |
- public final String mPublisher; |
- public final String mPreviewText; |
- public final String mUrl; |
- public final String mThumbnailPath; |
- 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) { |
- mThumbnailView.setImageDrawable(thumbnail); |
- } |
- } |
- |
- public SnippetArticle(String title, String publisher, String previewText, String url, |
- String thumbnailPath, int position) { |
- mTitle = title; |
- mPublisher = publisher; |
- mPreviewText = previewText; |
- mUrl = url; |
- mThumbnailPath = thumbnailPath; |
- mPosition = position; |
- } |
- |
- @Override |
- public int getType() { |
- return 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, mThumbnailPath); |
- } |
- } |
- |
public SnippetsManager(NewTabPageManager tabManager, Profile profile) { |
mNewTabPageManager = tabManager; |
- mDataAdapter = new SnippetsAdapter(this); |
mSnippetsBridge = new SnippetsBridge(profile, new SnippetsObserver() { |
@Override |
- public void onSnippetsAvailable( |
- String[] titles, String[] urls, String[] thumbnailUrls, String[] previewText) { |
- List<SnippetListItem> listItems = new ArrayList<SnippetListItem>(); |
+ public void onSnippetsAvailable(String[] titles, String[] urls, String[] thumbnailUrls, |
+ String[] previewText, long[] timestamps) { |
+ mListSnippetItems.clear(); |
+ if (titles.length > 0) { |
+ mListSnippetItems.add(new SnippetHeader()); |
+ } |
for (int i = 0; i < titles.length; ++i) { |
- SnippetArticle article = new SnippetArticle( |
- titles[i], "", previewText[i], urls[i], thumbnailUrls[i], i); |
- listItems.add(article); |
+ SnippetArticle article = new SnippetArticle(titles[i], "", previewText[i], |
+ urls[i], thumbnailUrls[i], timestamps[i], i); |
+ mListSnippetItems.add(article); |
} |
- mDataAdapter.setSnippetListItems(listItems); |
+ notifyObserver(); |
} |
}); |
} |
- public void setSnippetsView(RecyclerView mSnippetsView) { |
- mSnippetsView.setAdapter(mDataAdapter); |
+ /** |
+ * Sets the SnippetsManagerObserver for this class. If we currently have a set of snippets, |
+ * {@link SnippetsManagerObserver#onSnippetsReceived(List)} will immediately be called on the |
+ * newly set observer. |
+ * |
+ * @param observer the SnippetsManagerObserver to call when receiving new snippets. |
+ */ |
+ public void setObserver(SnippetsManagerObserver observer) { |
+ mObserver = observer; |
+ if (mListSnippetItems != null) { |
+ notifyObserver(); |
+ } |
+ } |
+ |
+ private void notifyObserver() { |
+ if (mObserver != null) { |
+ mObserver.onSnippetsReceived(mListSnippetItems); |
+ } |
+ } |
+ |
+ public List<NewTabPageListItem> getSnippetItems() { |
+ return mListSnippetItems; |
} |
public void loadUrl(String url) { |