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.content.res.TypedArray; | |
10 import android.graphics.Color; | |
11 import android.graphics.Rect; | |
12 import android.graphics.drawable.BitmapDrawable; | |
13 import android.graphics.drawable.Drawable; | |
14 import android.os.AsyncTask; | |
15 import android.support.v4.graphics.drawable.RoundedBitmapDrawable; | |
16 import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory; | |
17 import android.support.v7.widget.AppCompatTextView; | |
18 import android.text.TextUtils; | |
19 import android.util.LruCache; | |
20 import android.util.TypedValue; | |
21 import android.view.Gravity; | |
22 import android.view.View; | |
23 import android.view.View.OnClickListener; | |
24 | |
25 import org.chromium.base.Log; | |
26 import org.chromium.chrome.R; | |
27 import org.chromium.chrome.browser.ntp.InterestsPage.InterestsListener; | |
28 import org.chromium.chrome.browser.ntp.InterestsService.Interest; | |
29 import org.chromium.chrome.browser.widget.RoundedIconGenerator; | |
30 | |
31 import java.io.IOException; | |
32 import java.io.InputStream; | |
33 import java.net.URL; | |
34 | |
35 /** | |
36 * Displays the interest name along with an image of it. This item can be clicke d. | |
37 */ | |
38 class InterestsItemView extends AppCompatTextView implements OnClickListener { | |
39 | |
40 private static final String TAG = "interests_item_view"; | |
41 | |
42 /** | |
43 * Drawing-related values that can be shared between instances of InterestsI temView. | |
44 */ | |
45 static final class DrawingData { | |
46 | |
47 private final int mPadding; | |
48 private final int mMinHeight; | |
49 private final int mImageSize; | |
50 private final int mTextSize; | |
51 private final int mImageTextSize; | |
52 | |
53 /** | |
54 * Initialize shared values used for drawing the image. | |
55 * | |
56 * @param context The view context in which the InterestsItemView will b e drawn. | |
57 */ | |
58 DrawingData(Context context) { | |
59 Resources res = context.getResources(); | |
60 mPadding = res.getDimensionPixelOffset(R.dimen.ntp_list_item_padding ); | |
61 mMinHeight = res.getDimensionPixelSize(R.dimen.ntp_interest_item_min _height); | |
62 mTextSize = res.getDimensionPixelSize(R.dimen.ntp_interest_item_text _size); | |
63 mImageSize = res.getDimensionPixelSize(R.dimen.ntp_interest_item_ima ge_size); | |
64 mImageTextSize = res.getDimensionPixelSize(R.dimen.ntp_interest_item _image_text_size); | |
65 } | |
66 } | |
67 | |
68 private Interest mInterest; | |
69 private DownloadImageTask mDownloadTask; | |
70 | |
71 private final Context mContext; | |
72 private final DrawingData mDrawingData; | |
73 private final LruCache<String, Drawable> mImageCache; | |
74 private final InterestsListener mListener; | |
75 private final RoundedIconGenerator mIconGenerator; | |
76 | |
77 /** | |
78 * @param context The view context in which this item will be shown. | |
79 * @param name The name of the interest. | |
80 * @param imageUrl The URL for the interet's image. | |
Marc Treib
2015/11/19 12:42:18
name and imageUrl don't exist; listener and drawin
PEConn
2015/11/20 11:23:40
Done.
| |
81 * @param imageCache A cache to store downloaded images. | |
82 */ | |
83 InterestsItemView(Context context, Interest interest, InterestsListener list ener, | |
84 LruCache<String, Drawable> imageCache, DrawingData drawingData) { | |
85 super(context); | |
86 | |
87 mContext = context; | |
88 mListener = listener; | |
89 mImageCache = imageCache; | |
90 mDrawingData = drawingData; | |
91 | |
92 setTextSize(TypedValue.COMPLEX_UNIT_PX, mDrawingData.mTextSize); | |
93 setMinimumHeight(mDrawingData.mMinHeight); | |
94 setGravity(Gravity.CENTER); | |
95 setTextAlignment(View.TEXT_ALIGNMENT_CENTER); | |
96 | |
97 mIconGenerator = new RoundedIconGenerator( | |
98 mDrawingData.mImageSize, | |
99 mDrawingData.mImageSize, | |
100 mDrawingData.mImageSize / 2, | |
101 Color.GRAY, | |
102 mDrawingData.mImageTextSize); | |
103 | |
104 setOnClickListener(this); | |
105 | |
106 reset(interest); | |
107 } | |
108 | |
109 /** | |
110 * Resets the view contents so that it can be reused in the grid view. | |
111 * | |
112 * @param name The title of the page. | |
113 * @param imageUrl The URL of the page. | |
114 */ | |
115 public void reset(Interest interest) { | |
116 // Reset Drawable state so ripples don't continue when the View is reuse d. | |
117 jumpDrawablesToCurrentState(); | |
118 | |
119 // Exit early if this View is already displaying the Interest given. | |
120 if (mInterest != null | |
121 && TextUtils.equals(interest.getName(), mInterest.getName()) | |
122 && TextUtils.equals(interest.getImageUrl(), mInterest.getImageUr l())) { | |
123 return; | |
Marc Treib
2015/11/19 12:42:18
Hm, I'd still set mInterest = interest here
PEConn
2015/11/20 11:23:40
Done.
| |
124 } | |
125 | |
126 mInterest = interest; | |
127 | |
128 setText(mInterest.getName()); | |
129 | |
130 Drawable image = mImageCache.get(mInterest.getImageUrl()); | |
131 if (image == null) { | |
132 // Cache miss, download the image and use a temporary in the meantim e. | |
133 mDownloadTask = new DownloadImageTask(mInterest.getImageUrl()); | |
134 mDownloadTask.execute(); | |
135 | |
136 mIconGenerator.setBackgroundColor(getTileColor(mInterest.getName())) ; | |
137 image = new BitmapDrawable(mContext.getResources(), | |
138 mIconGenerator.generateIconForText(mInter est.getName())); | |
139 } | |
140 setImage(image); | |
141 } | |
142 | |
143 /** | |
144 * @return The image URL for the interest. | |
145 */ | |
146 public String getImageUrl() { | |
147 return mInterest.getImageUrl(); | |
148 } | |
149 | |
150 /** | |
151 * @return The name of the interest. | |
152 */ | |
153 public String getName() { | |
154 return mInterest.getName(); | |
155 } | |
156 | |
157 private void setImage(Drawable image) { | |
158 image.setBounds(new Rect(0, 0, mDrawingData.mImageSize, mDrawingData.mIm ageSize)); | |
159 setCompoundDrawables(null, image, null, null); | |
160 } | |
161 | |
162 private int getTileColor(String str) { | |
163 // Rough copy of LetterTileDrawable.pickColor. | |
164 // TODO(peconn): Move this to a more general class. | |
165 TypedArray colors = mContext.getResources().obtainTypedArray(R.array.let ter_tile_colors); | |
166 return colors.getColor(Math.abs(str.hashCode() % colors.length()), Color .DKGRAY); | |
167 } | |
168 | |
169 @Override | |
170 public void onClick(View v) { | |
171 mListener.onInterestClicked(getName()); | |
172 } | |
173 | |
174 private void onImageDownloaded(Drawable image, String url) { | |
175 if (image == null) { | |
176 return; | |
177 } | |
178 | |
179 mImageCache.put(url, image); | |
180 | |
181 // If the Interest this View is displaying has changed while downloading , do not update | |
182 // the image. | |
Marc Treib
2015/11/19 12:42:18
Do we still want to cache the image? If not, this
PEConn
2015/11/20 11:23:40
We've gone to the effort of loading it, so we may
| |
183 if (url == mInterest.getImageUrl()) { | |
184 setImage(image); | |
185 } | |
186 } | |
187 | |
188 private class DownloadImageTask extends AsyncTask<Void, Void, Drawable> { | |
189 | |
190 private final String mUrl; | |
191 | |
192 public DownloadImageTask(String url) { | |
193 mUrl = url; | |
194 } | |
195 | |
196 @Override | |
197 protected Drawable doInBackground(Void... voids) { | |
198 // This is run on a background thread. | |
199 try { | |
200 // TODO(peconn): Replace this with something from the C++ Chrome stack. | |
201 URL imageUrl = new URL(mUrl); | |
202 InputStream in = imageUrl.openStream(); | |
203 RoundedBitmapDrawable img = | |
204 RoundedBitmapDrawableFactory.create(getResources(), in); | |
205 | |
206 // TODO(peconn): Modify image so it does not get stretched. | |
207 img.setCircular(true); | |
208 return img; | |
209 } catch (IOException e) { | |
210 Log.e(TAG, "Error downloading image: " + e.toString()); | |
211 } | |
212 return null; | |
213 } | |
214 | |
215 @Override | |
216 protected void onPostExecute(Drawable image) { | |
217 // This is run on the main thread. | |
218 onImageDownloaded(image, mUrl); | |
219 } | |
220 } | |
221 } | |
OLD | NEW |