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

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

Issue 1459593002: Added a UI for the Interests Prototype. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.chrome.browser.ntp;
6
7 import android.content.Context;
8 import android.content.res.Resources;
9 import android.graphics.Bitmap;
10 import android.graphics.BitmapFactory;
11 import android.graphics.Color;
12 import android.graphics.Rect;
13 import android.graphics.drawable.BitmapDrawable;
14 import android.graphics.drawable.Drawable;
15 import android.media.ThumbnailUtils;
16 import android.os.AsyncTask;
17 import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
18 import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
19 import android.support.v7.widget.AppCompatTextView;
20 import android.text.TextUtils;
21 import android.util.LruCache;
22 import android.util.TypedValue;
23 import android.view.Gravity;
24 import android.view.View;
25 import android.view.View.OnClickListener;
26
27 import org.chromium.base.Log;
28 import org.chromium.base.ObserverList;
29 import org.chromium.chrome.R;
30 import org.chromium.chrome.browser.ntp.InterestsPage.InterestsClickListener;
31 import org.chromium.chrome.browser.ntp.InterestsService.Interest;
32 import org.chromium.chrome.browser.widget.RoundedIconGenerator;
33
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.net.URL;
37
38 /**
39 * Displays the interest name along with an image of it. This item can be clicke d.
40 */
41 class InterestsItemView extends AppCompatTextView implements OnClickListener {
42
43 private static final String TAG = "InterestsItemView";
44
45 // Color codes to provide a variety of background colours for the letter til es.
46 // TODO(peconn): Move this, along with getTileColor into a separate class.
47 private static final String[] COLOR_CODES = {"#f16364", "#f58559", "#f9a43e" , "#e4c62e",
newt (away) 2015/12/14 21:18:39 Just use an int array: private static final i
PEConn 2015/12/15 11:50:15 Done.
48 "#67bf74", "#59a2be", "#2093cd", "#ad62a7"};
49
50 /**
51 * Drawing-related values that can be shared between instances of InterestsI temView.
52 */
53 static final class DrawingData {
54
55 private final int mPadding;
56 private final int mMinHeight;
57 private final int mImageSize;
58 private final int mTextSize;
59 private final int mImageTextSize;
60
61 private final RoundedIconGenerator mIconGenerator;
62
63 /**
64 * Initialize shared values used for drawing the image.
65 *
66 * @param context The view context in which the InterestsItemView will b e drawn.
67 */
68 DrawingData(Context context) {
69 Resources res = context.getResources();
70 mPadding = res.getDimensionPixelOffset(R.dimen.ntp_list_item_padding );
71 mMinHeight = res.getDimensionPixelSize(R.dimen.ntp_interest_item_min _height);
72 mTextSize = res.getDimensionPixelSize(R.dimen.ntp_interest_item_text _size);
73 mImageSize = res.getDimensionPixelSize(R.dimen.ntp_interest_item_ima ge_size);
74 mImageTextSize = res.getDimensionPixelSize(R.dimen.ntp_interest_item _image_text_size);
75 mIconGenerator = new RoundedIconGenerator(
76 mImageSize, mImageSize, mImageSize / 2, Color.GRAY, mImageTextSi ze);
77 }
78 }
79
80 private Interest mInterest;
81
82 private final Context mContext;
83 private final DrawingData mDrawingData;
84 private final LruCache<String, ImageHolder> mImageCache;
85 private final InterestsClickListener mListener;
86
87 /**
88 * @param context The view context in which this item will be shown.
89 * @param interest The interest to display.
90 * @param listener Callback object for when a view is pressed.
91 * @param imageCache A cache to store downloaded images.
92 * @param drawingData Information about the view size.
93 */
94 InterestsItemView(Context context, Interest interest, InterestsClickListener listener,
95 LruCache<String, ImageHolder> imageCache, DrawingData drawingData) {
96 super(context);
97
98 mContext = context;
99 mListener = listener;
100 mImageCache = imageCache;
101 mDrawingData = drawingData;
102
103 setTextSize(TypedValue.COMPLEX_UNIT_PX, mDrawingData.mTextSize);
104 setMinimumHeight(mDrawingData.mMinHeight);
105 setGravity(Gravity.CENTER);
106 setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
107
108 setOnClickListener(this);
109
110 reset(interest);
111 }
112
113 /**
114 * Resets the view contents so that it can be reused in the grid view.
115 *
116 * @param interest The interest to display.
117 */
118 public void reset(Interest interest) {
119 // Reset Drawable state so ripples don't continue when the View is reuse d.
120 jumpDrawablesToCurrentState();
121
122 // Exit early if this View is already displaying the Interest given.
123 if (mInterest != null
124 && TextUtils.equals(interest.getName(), mInterest.getName())
125 && TextUtils.equals(interest.getImageUrl(), mInterest.getImageUr l())) {
126 mInterest = interest;
127 return;
128 }
129
130 mInterest = interest;
131
132 setText(mInterest.getName());
133
134 ImageHolder placeholder = mImageCache.get(mInterest.getImageUrl());
newt (away) 2015/12/14 21:18:39 s/placeholder/holder/
PEConn 2015/12/15 11:50:15 Done.
135 if (placeholder == null) {
136 // Create a new placeholder, add it to the cache and set it download ing.
137 placeholder = new ImageHolder();
138 mImageCache.put(mInterest.getImageUrl(), placeholder);
139 new ImageDownloadTask(mInterest.getImageUrl(), placeholder, getResou rces()).execute();
140 }
141
142 if (placeholder.getImageDrawable() != null) {
143 setImage(placeholder.getImageDrawable());
144 } else {
145 // Add a callback to a subclass that will call setImage once the pla ceholder is filled.
146 placeholder.addListener(new ImageDownloadedCallback());
147
148 // Display a letter tile in the meantime.
149 mDrawingData.mIconGenerator.setBackgroundColor(getTileColor(mInteres t.getName()));
150 setImage(new BitmapDrawable(mContext.getResources(),
151 mDrawingData.mIconGenerator.generateIconForText(mInterest.ge tName())));
152 }
153 }
154
155 /**
156 * @return The image URL for the interest.
157 */
158 public String getImageUrl() {
159 return mInterest.getImageUrl();
160 }
161
162 /**
163 * @return The name of the interest.
164 */
165 public String getName() {
166 return mInterest.getName();
167 }
168
169 private void setImage(Drawable image) {
170 image.setBounds(new Rect(0, 0, mDrawingData.mImageSize, mDrawingData.mIm ageSize));
171 setCompoundDrawables(null, image, null, null);
172 }
173
174 private int getTileColor(String str) {
175 // Rough copy of LetterTileDrawable.pickColor.
176 // TODO(peconn): Move this to a more general class.
177 return Color.parseColor(COLOR_CODES[Math.abs(str.hashCode() % COLOR_CODE S.length)]);
178 }
179
180 @Override
181 public void onClick(View v) {
182 mListener.onInterestClicked(getName());
183 }
184
185 /*
186 * An AsyncTask that downloads an image, formats it then puts it in the give n placeholder.
187 */
188 private static class ImageDownloadTask extends AsyncTask<Void, Void, Drawabl e> {
189
190 private final String mUrl;
191 private final ImageHolder mImagePlaceholder;
newt (away) 2015/12/14 21:18:39 s/Placeholder/Holder/
PEConn 2015/12/15 11:50:15 Done.
192 private final Resources mResources;
193
194 public ImageDownloadTask(String url, ImageHolder placeholder, Resources resources) {
newt (away) 2015/12/14 21:18:39 s/placeholder/holder/
PEConn 2015/12/15 11:50:15 Done.
195 mUrl = url;
196 mImagePlaceholder = placeholder;
197 mResources = resources;
198 }
199
200 @Override
201 protected Drawable doInBackground(Void... voids) {
202 // This is run on a background thread.
203 try {
204 // TODO(peconn): Replace this with something from the C++ Chrome stack.
205 URL imageUrl = new URL(mUrl);
206 InputStream in = imageUrl.openStream();
207
208 Bitmap raw = BitmapFactory.decodeStream(in);
209 int dimension = Math.min(raw.getHeight(), raw.getWidth());
210 RoundedBitmapDrawable img = RoundedBitmapDrawableFactory.create( mResources,
211 ThumbnailUtils.extractThumbnail(raw, dimension, dimensio n));
212 img.setCircular(true);
213
214 return img;
215 } catch (IOException e) {
216 Log.e(TAG, "Error downloading image: " + e.toString());
217 }
218 return null;
219 }
220
221 @Override
222 protected void onPostExecute(Drawable image) {
223 // This is run on the main thread.
224 mImagePlaceholder.set(image, mUrl);
225 }
226 }
227
228 /*
229 * A callback class that will set it's parent's image.
230 */
231 private class ImageDownloadedCallback {
232 public void onImageDownloaded(Drawable image, String url) {
233 if (image == null) {
234 return;
235 }
236 // If the Interest this View is displaying has changed while downloa ding, do not update
237 // the image.
238 if (url == mInterest.getImageUrl()) {
239 setImage(image);
240 }
241 }
242 }
243
244 /*
245 * A placeholder for an Image that allows listeners to subscribe to when it is set. It is
246 * like a listenable future that doesn't calculate the value itself. It can only be
247 * accessed on one thread. It can only be set once.
248 */
249 static class ImageHolder {
250 private final ObserverList<ImageDownloadedCallback> mCallbacks = new Obs erverList<>();
251 private Drawable mImage;
252 private String mUrl;
253
254 public void set(Drawable image, String url) {
255 assert mImage == null;
256 mImage = image;
257 mUrl = url;
258
259 for (ImageDownloadedCallback callbacks : mCallbacks) {
260 callbacks.onImageDownloaded(image, mUrl);
261 }
262
263 mCallbacks.clear();
264 }
265
266 public void addListener(ImageDownloadedCallback callback) {
267 if (mImage == null) {
268 mCallbacks.addObserver(callback);
269 } else {
270 callback.onImageDownloaded(mImage, mUrl);
271 }
272 }
273
274 public Drawable getImageDrawable() {
275 return mImage;
276 }
277 }
278 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698