OLD | NEW |
---|---|
(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.Color; | |
10 import android.graphics.Rect; | |
11 import android.graphics.drawable.BitmapDrawable; | |
12 import android.graphics.drawable.Drawable; | |
13 import android.os.AsyncTask; | |
14 import android.support.v4.graphics.drawable.RoundedBitmapDrawable; | |
15 import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory; | |
16 import android.support.v7.widget.AppCompatTextView; | |
17 import android.util.Log; | |
18 import android.util.LruCache; | |
19 import android.util.TypedValue; | |
20 import android.view.Gravity; | |
21 import android.view.View; | |
22 import android.view.View.OnClickListener; | |
23 | |
24 import org.chromium.chrome.R; | |
25 import org.chromium.chrome.browser.ntp.InterestsPage.InterestsListener; | |
26 import org.chromium.chrome.browser.widget.RoundedIconGenerator; | |
27 | |
28 import java.io.IOException; | |
29 import java.io.InputStream; | |
30 import java.net.URL; | |
31 | |
32 /** | |
33 * Displays the interest name along with an image of it. This item can be clicke d. | |
34 */ | |
35 class InterestsItemView extends AppCompatTextView implements OnClickListener { | |
36 | |
37 /** | |
38 * Drawing-related values that can be shared between instances of InterestsI temView. | |
39 */ | |
40 static final class DrawingData { | |
41 | |
42 private final int mPadding; | |
43 private final int mMinHeight; | |
44 private final int mImageSize; | |
45 private final int mTextSize; | |
46 private final int mImageTextSize; | |
47 | |
48 /** | |
49 * Initialize shared values used for drawing the image. | |
50 * | |
51 * @param context The view context in which the InterestsItemView will b e drawn. | |
52 */ | |
53 DrawingData(Context context) { | |
54 Resources res = context.getResources(); | |
55 mPadding = res.getDimensionPixelOffset(R.dimen.ntp_list_item_padding ); | |
56 mMinHeight = res.getDimensionPixelSize(R.dimen.ntp_interest_item_min _height); | |
57 mTextSize = res.getDimensionPixelSize(R.dimen.ntp_interest_item_text _size); | |
58 mImageSize = res.getDimensionPixelSize(R.dimen.ntp_interest_item_ima ge_size); | |
59 mImageTextSize = res.getDimensionPixelSize(R.dimen.ntp_interest_item _image_text_size); | |
60 | |
61 } | |
62 } | |
63 | |
64 private String mName; | |
65 private String mImageUrl; | |
66 private LruCache<String, Drawable> mImageCache; | |
67 private Context mContext; | |
68 private RoundedIconGenerator mIconGenerator; | |
69 private DrawingData mDrawingData; | |
70 private InterestsListener mListener; | |
71 private DownloadImageTask mDownloadTask; | |
72 | |
73 /** | |
74 * @param context The view context in which this item will be shown. | |
75 * @param name The name of the interest. | |
76 * @param imageUrl The URL for the interet's image. | |
77 * @param imageCache A cache to store downloaded images. | |
78 */ | |
79 InterestsItemView(Context context, String name, String imageUrl, InterestsLi stener listener, | |
80 LruCache<String, Drawable> imageCache, DrawingData drawingData) { | |
81 super(context); | |
82 | |
83 mContext = context; | |
84 mListener = listener; | |
85 mImageCache = imageCache; | |
86 mDrawingData = drawingData; | |
87 | |
88 mIconGenerator = new RoundedIconGenerator( | |
89 mDrawingData.mImageSize, | |
90 mDrawingData.mImageSize, | |
91 mDrawingData.mImageSize / 2, | |
92 Color.GRAY, | |
93 mDrawingData.mImageTextSize); | |
94 | |
95 setTextSize(TypedValue.COMPLEX_UNIT_PX, mDrawingData.mTextSize); | |
96 setMinimumHeight(mDrawingData.mMinHeight); | |
97 setGravity(Gravity.CENTER); | |
98 setTextAlignment(View.TEXT_ALIGNMENT_CENTER); | |
99 | |
100 setOnClickListener(this); | |
101 | |
102 reset(name, imageUrl); | |
103 } | |
104 | |
105 /** | |
106 * 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.
| |
107 * | |
108 * @param name The title of the page. | |
109 * @param imageUrl The URL of the page. | |
110 */ | |
111 public void reset(String name, String imageUrl) { | |
112 // If there is an active download task for this view then it shouldn't u pdate | |
113 // the view when it finishes. | |
114 if (mDownloadTask != null) { | |
115 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.
| |
116 mDownloadTask = null; | |
117 } | |
118 // Reset drawable state so ripples don't continue when the view is reuse d. | |
119 jumpDrawablesToCurrentState(); | |
120 | |
121 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.
| |
122 && mImageUrl.equals(imageUrl)) { | |
123 return; | |
124 } | |
125 | |
126 mName = name; | |
127 setText(mName); | |
128 | |
129 mImageUrl = imageUrl; | |
130 Drawable image = mImageCache.get(mImageUrl); | |
131 | |
132 if (image == null) { | |
133 // Cache miss, need to download the image and use a temporary image in the meantime. | |
134 mDownloadTask = new DownloadImageTask(this, mImageCache); | |
135 mDownloadTask.execute(mImageUrl); | |
136 | |
137 image = new BitmapDrawable( | |
138 mContext.getResources(), | |
139 mIconGenerator.generateIconForText(mName)); | |
140 } | |
141 | |
142 setImage(image); | |
143 | |
144 return; | |
145 } | |
146 | |
147 /** | |
148 * @return The image url for the interest. | |
149 */ | |
150 public String getImageUrl() { | |
151 return mImageUrl; | |
152 } | |
153 | |
154 /** | |
155 * @return The name of the interest. | |
156 */ | |
157 public String getName() { | |
158 return mName; | |
159 } | |
160 | |
161 /** | |
162 * Updates the image and triggers a redraw with the new image. | |
163 * | |
164 * @param image The new image to display. May be null. | |
165 */ | |
166 public void setImage(Drawable image) { | |
167 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.
| |
168 return; | |
169 } | |
170 | |
171 setImageBounds(image); | |
172 | |
173 this.setCompoundDrawables(null, image, null, null); | |
newt (away)
2015/09/29 17:33:21
remove "this."
PEConn
2015/11/18 15:26:56
Done.
| |
174 } | |
175 | |
176 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.
| |
177 image.setBounds(new Rect( | |
178 0, 0, mDrawingData.mImageSize, mDrawingData.mImageSize)); | |
179 } | |
180 | |
181 @Override | |
182 public void onClick(View v) { | |
183 mListener.onInterestClicked(getName()); | |
184 } | |
185 | |
186 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
| |
187 | |
188 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
| |
189 private LruCache<String, Drawable> mCache; | |
190 private String mUrl; | |
191 private boolean mShouldUpdateView; | |
192 | |
193 public DownloadImageTask(InterestsItemView view, LruCache<String, Drawab le> cache) { | |
194 mView = view; | |
195 mCache = cache; | |
196 mShouldUpdateView = true; | |
197 } | |
198 | |
199 public void setShouldUpdateView(boolean shouldUpdateView) { | |
200 mShouldUpdateView = shouldUpdateView; | |
201 } | |
202 | |
203 @Override | |
204 protected Drawable doInBackground(String... arg0) { | |
newt (away)
2015/09/29 17:33:22
s/arg0/args
PEConn
2015/11/18 15:26:56
Done.
| |
205 // This is done in a background thread | |
206 mUrl = arg0[0]; | |
207 return downloadImage(mUrl); | |
208 } | |
209 | |
210 /** | |
211 * Called after the image has been downloaded. This runs on the main thr ead. | |
212 */ | |
213 @Override | |
214 protected void onPostExecute(Drawable image) { | |
215 if (image != null) { | |
216 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.
| |
217 | |
218 if (mShouldUpdateView) { | |
219 mView.setImage(image); | |
220 } | |
221 } | |
222 } | |
223 | |
224 private Drawable downloadImage(String url) { | |
225 URL imageUrl; | |
226 InputStream in; | |
227 | |
228 try { | |
229 imageUrl = new URL(url); | |
230 in = imageUrl.openStream(); | |
231 | |
232 return getRoundedImage(in); | |
233 } catch (IOException e) { | |
234 Log.e("Error downloading image: ", e.toString()); | |
235 } | |
236 return null; | |
237 } | |
238 | |
239 private Drawable getRoundedImage(InputStream in) { | |
240 RoundedBitmapDrawable img = RoundedBitmapDrawableFactory.create(getR esources(), in); | |
241 | |
242 img.setCircular(true); | |
243 | |
244 return img; | |
245 } | |
246 } | |
247 } | |
OLD | NEW |