Chromium Code Reviews| 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.InterestsClickListener; | |
| 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"; | |
|
knn
2015/12/02 14:18:59
CamelCase
PEConn
2015/12/08 17:27:49
Done.
| |
| 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 InterestsClickListener mListener; | |
| 75 private final RoundedIconGenerator mIconGenerator; | |
| 76 | |
| 77 /** | |
| 78 * @param context The view context in which this item will be shown. | |
| 79 * @param interest The interest to display. | |
| 80 * @param listener Callback object for when a view is pressed. | |
| 81 * @param imageCache A cache to store downloaded images. | |
| 82 * @param drawingData Information about the view size. | |
| 83 */ | |
| 84 InterestsItemView(Context context, Interest interest, InterestsClickListener listener, | |
| 85 LruCache<String, Drawable> imageCache, DrawingData drawingData) { | |
| 86 super(context); | |
| 87 | |
| 88 mContext = context; | |
| 89 mListener = listener; | |
| 90 mImageCache = imageCache; | |
| 91 mDrawingData = drawingData; | |
| 92 | |
| 93 setTextSize(TypedValue.COMPLEX_UNIT_PX, mDrawingData.mTextSize); | |
| 94 setMinimumHeight(mDrawingData.mMinHeight); | |
| 95 setGravity(Gravity.CENTER); | |
| 96 setTextAlignment(View.TEXT_ALIGNMENT_CENTER); | |
| 97 | |
| 98 mIconGenerator = new RoundedIconGenerator( | |
| 99 mDrawingData.mImageSize, | |
| 100 mDrawingData.mImageSize, | |
| 101 mDrawingData.mImageSize / 2, | |
| 102 Color.GRAY, | |
| 103 mDrawingData.mImageTextSize); | |
| 104 | |
| 105 setOnClickListener(this); | |
| 106 | |
| 107 reset(interest); | |
| 108 } | |
| 109 | |
| 110 /** | |
| 111 * Resets the view contents so that it can be reused in the grid view. | |
| 112 * | |
| 113 * @param interest The interest to display. | |
| 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 mInterest = interest; | |
| 124 return; | |
| 125 } | |
| 126 | |
| 127 mInterest = interest; | |
| 128 | |
| 129 setText(mInterest.getName()); | |
| 130 | |
| 131 Drawable image = mImageCache.get(mInterest.getImageUrl()); | |
| 132 if (image == null) { | |
| 133 // Cache miss, download the image and use a temporary in the meantim e. | |
|
Bernhard Bauer
2015/12/02 14:09:47
Hm... if we are called for the same interest sever
Bernhard Bauer
2015/12/02 14:10:39
(I suspect this will eventually boil down to somet
PEConn
2015/12/08 17:27:49
Done.
| |
| 134 mDownloadTask = new DownloadImageTask(mInterest.getImageUrl()); | |
| 135 mDownloadTask.execute(); | |
| 136 | |
| 137 mIconGenerator.setBackgroundColor(getTileColor(mInterest.getName())) ; | |
| 138 image = new BitmapDrawable(mContext.getResources(), | |
| 139 mIconGenerator.generateIconForText(mInter est.getName())); | |
| 140 } | |
| 141 setImage(image); | |
| 142 } | |
| 143 | |
| 144 /** | |
| 145 * @return The image URL for the interest. | |
| 146 */ | |
| 147 public String getImageUrl() { | |
| 148 return mInterest.getImageUrl(); | |
| 149 } | |
| 150 | |
| 151 /** | |
| 152 * @return The name of the interest. | |
| 153 */ | |
| 154 public String getName() { | |
| 155 return mInterest.getName(); | |
| 156 } | |
| 157 | |
| 158 private void setImage(Drawable image) { | |
| 159 image.setBounds(new Rect(0, 0, mDrawingData.mImageSize, mDrawingData.mIm ageSize)); | |
| 160 setCompoundDrawables(null, image, null, null); | |
| 161 } | |
| 162 | |
| 163 private int getTileColor(String str) { | |
| 164 // Rough copy of LetterTileDrawable.pickColor. | |
| 165 // TODO(peconn): Move this to a more general class. | |
| 166 TypedArray colors = mContext.getResources().obtainTypedArray(R.array.let ter_tile_colors); | |
| 167 return colors.getColor(Math.abs(str.hashCode() % colors.length()), Color .DKGRAY); | |
| 168 } | |
| 169 | |
| 170 @Override | |
| 171 public void onClick(View v) { | |
| 172 mListener.onInterestClicked(getName()); | |
| 173 } | |
| 174 | |
| 175 private void onImageDownloaded(Drawable image, String url) { | |
| 176 if (image == null) { | |
| 177 return; | |
| 178 } | |
| 179 | |
| 180 mImageCache.put(url, image); | |
| 181 | |
| 182 // If the Interest this View is displaying has changed while downloading , do not update | |
| 183 // the image. | |
| 184 if (url == mInterest.getImageUrl()) { | |
| 185 setImage(image); | |
| 186 } | |
| 187 } | |
| 188 | |
| 189 private class DownloadImageTask extends AsyncTask<Void, Void, Drawable> { | |
| 190 | |
| 191 private final String mUrl; | |
| 192 | |
| 193 public DownloadImageTask(String url) { | |
| 194 mUrl = url; | |
| 195 } | |
| 196 | |
| 197 @Override | |
| 198 protected Drawable doInBackground(Void... voids) { | |
| 199 // This is run on a background thread. | |
| 200 try { | |
| 201 // TODO(peconn): Replace this with something from the C++ Chrome stack. | |
| 202 URL imageUrl = new URL(mUrl); | |
| 203 InputStream in = imageUrl.openStream(); | |
| 204 RoundedBitmapDrawable img = | |
| 205 RoundedBitmapDrawableFactory.create(getResources(), in); | |
| 206 | |
| 207 // TODO(peconn): Modify image so it does not get stretched. | |
| 208 img.setCircular(true); | |
| 209 return img; | |
| 210 } catch (IOException e) { | |
| 211 Log.e(TAG, "Error downloading image: " + e.toString()); | |
| 212 } | |
| 213 return null; | |
| 214 } | |
| 215 | |
| 216 @Override | |
| 217 protected void onPostExecute(Drawable image) { | |
| 218 // This is run on the main thread. | |
| 219 onImageDownloaded(image, mUrl); | |
| 220 } | |
| 221 } | |
| 222 } | |
| OLD | NEW |