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.InputStream; | |
29 import java.net.URL; | |
30 | |
31 /** | |
32 * Displays the interest name along with an image of it. This item can be clicke d. | |
33 */ | |
34 class InterestsItemView extends AppCompatTextView implements OnClickListener { | |
35 | |
36 /** | |
37 * Drawing-related values that can be shared between instances of InterestsI temView. | |
38 */ | |
39 static final class DrawingData { | |
40 | |
41 private final int mPadding; | |
42 private final int mMinHeight; | |
43 private final int mImageSize; | |
44 private final int mTextSize; | |
45 private final int mImageTextSize; | |
46 | |
47 /** | |
48 * Initialize shared values used for drawing the image. | |
49 * | |
50 * @param context The view context in which the InterestsItemView will b e drawn. | |
51 */ | |
52 DrawingData(Context context) { | |
53 Resources res = context.getResources(); | |
54 mPadding = res.getDimensionPixelOffset(R.dimen.ntp_list_item_padding ); | |
55 mMinHeight = res.getDimensionPixelSize(R.dimen.ntp_interest_item_min _height); | |
56 mTextSize = res.getDimensionPixelSize(R.dimen.ntp_interest_item_text _size); | |
57 mImageSize = res.getDimensionPixelSize(R.dimen.ntp_interest_item_ima ge_size); | |
58 mImageTextSize = res.getDimensionPixelSize(R.dimen.ntp_interest_item _image_text_size); | |
59 | |
60 } | |
61 } | |
62 | |
63 private String mName; | |
64 private String mImageUrl; | |
65 private LruCache<String, Drawable> mImageCache; | |
66 private Context mContext; | |
67 private RoundedIconGenerator mIconGenerator; | |
68 private DrawingData mDrawingData; | |
69 private InterestsListener mListener; | |
70 private DownloadImage mDownloadTask = null; | |
Marc Treib
2015/09/18 13:26:03
Why is (only) this one explicitly initialized to n
tache
2015/09/21 15:35:54
Done.
| |
71 | |
72 /** | |
73 * @param context The view context in which this item will be shown. | |
74 * @param name The name of the interest. | |
75 * @param imageUrl The URL for the interet's image. | |
76 * @param imageCache A cache to store downloaded images. | |
77 */ | |
78 InterestsItemView(Context context, String name, String imageUrl, | |
79 InterestsListener listener, | |
80 LruCache<String, Drawable> imageCache) { | |
Marc Treib
2015/09/18 13:26:03
Weird line breaks?
tache
2015/09/21 15:35:55
Done.
| |
81 super(context); | |
82 | |
83 mContext = context; | |
84 mListener = listener; | |
85 mImageCache = imageCache; | |
86 mDrawingData = new DrawingData(mContext); | |
Marc Treib
2015/09/18 13:26:03
Wait, when you use it like this, then nothing is a
tache
2015/09/21 15:35:54
Now it shared in the InterestPageView and past to
| |
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. | |
107 * | |
108 * @param name The title of the page. | |
109 * @param imageUrl The URL of the page. | |
110 */ | |
Marc Treib
2015/09/18 13:26:03
Add @return please
tache
2015/09/21 15:35:54
Removed the return value, since it was not used an
| |
111 public boolean 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); | |
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 | |
122 && mImageUrl.equals(imageUrl)) { | |
123 return false; | |
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 i n the meantime. | |
Marc Treib
2015/09/18 13:26:03
nit: Capitalize "Cache", also add a comma after "m
tache
2015/09/21 15:35:55
Done.
| |
134 mDownloadTask = new DownloadImage(this, mImageCache); | |
135 mDownloadTask.execute(mImageUrl); | |
136 | |
137 image = new BitmapDrawable( | |
138 mContext.getResources(), | |
139 mIconGenerator.generateIconForText(mName)); | |
140 | |
141 } | |
142 | |
143 setImage(image); | |
144 | |
145 return true; | |
146 } | |
147 | |
148 /** | |
149 * @return The image url for the interest. | |
150 */ | |
151 public String getImageUrl() { | |
152 return mImageUrl; | |
153 } | |
154 | |
155 /** | |
156 * @return The name of the interest. | |
157 */ | |
158 public String getName() { | |
159 return mName; | |
160 } | |
161 | |
162 /** | |
163 * Updates the image and triggers a redraw with the new image. | |
164 * | |
165 * @param image The new image to display. May be null. | |
166 */ | |
167 public void setImage(Drawable image) { | |
168 if (image == null) { | |
169 return; | |
170 } | |
171 | |
172 setImageBounds(image); | |
173 | |
174 this.setCompoundDrawables(null, image, null, null); | |
175 } | |
176 | |
177 private void setImageBounds(Drawable image) { | |
178 image.setBounds(new Rect( | |
179 0, 0, mDrawingData.mImageSize, mDrawingData.mImageSize)); | |
180 } | |
181 | |
182 @Override | |
183 public void onClick(View v) { | |
184 mListener.onInterestClicked(getName()); | |
185 } | |
186 | |
187 private class DownloadImage extends AsyncTask<String, Integer, Drawable> { | |
Marc Treib
2015/09/18 13:26:03
DownloadImageTask?
tache
2015/09/21 15:35:55
Done.
| |
188 | |
189 private InterestsItemView mView; | |
190 private LruCache<String, Drawable> mCache; | |
191 private String mUrl; | |
192 private boolean mShouldUpdateView; | |
193 | |
194 public DownloadImage(InterestsItemView view, LruCache<String, Drawable> cache) { | |
195 mView = view; | |
196 mCache = cache; | |
197 mShouldUpdateView = true; | |
198 } | |
199 | |
200 public void setShouldUpdateView(boolean shouldUpdateView) { | |
201 mShouldUpdateView = shouldUpdateView; | |
202 } | |
203 | |
204 @Override | |
205 protected Drawable doInBackground(String... arg0) { | |
206 // This is done in a background thread | |
207 mUrl = arg0[0]; | |
208 return downloadImage(mUrl); | |
209 } | |
210 | |
211 /** | |
212 * Called after the image has been downloaded. This runs on the main thr ead. | |
213 */ | |
214 protected void onPostExecute(Drawable image) { | |
Marc Treib
2015/09/18 13:26:03
Should this have @Override?
tache
2015/09/21 15:35:55
Done.
| |
215 if (image != null) { | |
216 mCache.put(mUrl, image); | |
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 (Exception e) { | |
Marc Treib
2015/09/18 13:26:03
Be more specific than just "Exception" please.
tache
2015/09/21 15:35:54
Done.
| |
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 |