| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.ntp.snippets; | 5 package org.chromium.chrome.browser.ntp.snippets; |
| 6 | 6 |
| 7 import android.annotation.SuppressLint; | 7 import android.annotation.SuppressLint; |
| 8 import android.content.res.Resources; | 8 import android.content.res.Resources; |
| 9 import android.graphics.Bitmap; | 9 import android.graphics.Bitmap; |
| 10 import android.graphics.drawable.BitmapDrawable; | 10 import android.graphics.drawable.BitmapDrawable; |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 : R.dimen.snippets_publisher_margin_top_with_article
_snippet); | 178 : R.dimen.snippets_publisher_margin_top_with_article
_snippet); |
| 179 | 179 |
| 180 params.setMargins(params.leftMargin, | 180 params.setMargins(params.leftMargin, |
| 181 topMargin, | 181 topMargin, |
| 182 params.rightMargin, | 182 params.rightMargin, |
| 183 params.bottomMargin); | 183 params.bottomMargin); |
| 184 | 184 |
| 185 mPublisherBar.setLayoutParams(params); | 185 mPublisherBar.setLayoutParams(params); |
| 186 } | 186 } |
| 187 | 187 |
| 188 private static String getAttributionString(SnippetArticle article) { |
| 189 if (article.mPublishTimestampMilliseconds == 0) return article.mPublishe
r; |
| 190 |
| 191 // DateUtils.getRelativeTimeSpanString(...) calls through to TimeZone.ge
tDefault(). If this |
| 192 // has never been called before it loads the current time zone from disk
. In most likelihood |
| 193 // this will have been called previously and the current time zone will
have been cached, |
| 194 // but in some cases (eg instrumentation tests) it will cause a strict m
ode violation. |
| 195 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); |
| 196 CharSequence relativeTimeSpan; |
| 197 try { |
| 198 long time = SystemClock.elapsedRealtime(); |
| 199 relativeTimeSpan = |
| 200 DateUtils.getRelativeTimeSpanString(article.mPublishTimestam
pMilliseconds, |
| 201 System.currentTimeMillis(), DateUtils.MINUTE_IN_MILL
IS); |
| 202 RecordHistogram.recordTimesHistogram("Android.StrictMode.SnippetUIBu
ildTime", |
| 203 SystemClock.elapsedRealtime() - time, TimeUnit.MILLISECONDS)
; |
| 204 } finally { |
| 205 StrictMode.setThreadPolicy(oldPolicy); |
| 206 } |
| 207 // We format the publisher here so that having a publisher name in an RT
L language |
| 208 // doesn't mess up the formatting on an LTR device and vice versa. |
| 209 return String.format(PUBLISHER_FORMAT_STRING, |
| 210 BidiFormatter.getInstance().unicodeWrap(article.mPublisher), rel
ativeTimeSpan); |
| 211 } |
| 212 |
| 188 public void onBindViewHolder(SnippetArticle article, SuggestionsCategoryInfo
categoryInfo) { | 213 public void onBindViewHolder(SnippetArticle article, SuggestionsCategoryInfo
categoryInfo) { |
| 189 super.onBindViewHolder(); | 214 super.onBindViewHolder(); |
| 190 | 215 |
| 191 // No longer listen for offline status changes to the old article. | 216 // No longer listen for offline status changes to the old article. |
| 192 if (mArticle != null) mArticle.setOfflineStatusChangeRunnable(null); | 217 if (mArticle != null) mArticle.setOfflineStatusChangeRunnable(null); |
| 193 | 218 |
| 194 mArticle = article; | 219 mArticle = article; |
| 195 mCategoryInfo = categoryInfo; | 220 mCategoryInfo = categoryInfo; |
| 196 updateLayout(); | 221 updateLayout(); |
| 197 | 222 |
| 198 mHeadlineTextView.setText(mArticle.mTitle); | 223 mHeadlineTextView.setText(mArticle.mTitle); |
| 199 | 224 mPublisherTextView.setText(getAttributionString(mArticle)); |
| 200 // DateUtils.getRelativeTimeSpanString(...) calls through to TimeZone.ge
tDefault(). If this | |
| 201 // has never been called before it loads the current time zone from disk
. In most likelihood | |
| 202 // this will have been called previously and the current time zone will
have been cached, | |
| 203 // but in some cases (eg instrumentation tests) it will cause a strict m
ode violation. | |
| 204 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); | |
| 205 try { | |
| 206 long time = SystemClock.elapsedRealtime(); | |
| 207 CharSequence relativeTimeSpan = DateUtils.getRelativeTimeSpanString( | |
| 208 mArticle.mPublishTimestampMilliseconds, System.currentTimeMi
llis(), | |
| 209 DateUtils.MINUTE_IN_MILLIS); | |
| 210 RecordHistogram.recordTimesHistogram("Android.StrictMode.SnippetUIBu
ildTime", | |
| 211 SystemClock.elapsedRealtime() - time, TimeUnit.MILLISECONDS)
; | |
| 212 | |
| 213 // We format the publisher here so that having a publisher name in a
n RTL language | |
| 214 // doesn't mess up the formatting on an LTR device and vice versa. | |
| 215 String publisherAttribution = String.format(PUBLISHER_FORMAT_STRING, | |
| 216 BidiFormatter.getInstance().unicodeWrap(mArticle.mPublisher)
, relativeTimeSpan); | |
| 217 mPublisherTextView.setText(publisherAttribution); | |
| 218 } finally { | |
| 219 StrictMode.setThreadPolicy(oldPolicy); | |
| 220 } | |
| 221 | 225 |
| 222 // The favicon of the publisher should match the TextView height. | 226 // The favicon of the publisher should match the TextView height. |
| 223 int widthSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); | 227 int widthSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 224 int heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
; | 228 int heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
; |
| 225 mPublisherTextView.measure(widthSpec, heightSpec); | 229 mPublisherTextView.measure(widthSpec, heightSpec); |
| 226 mPublisherFaviconSizePx = mPublisherTextView.getMeasuredHeight(); | 230 mPublisherFaviconSizePx = mPublisherTextView.getMeasuredHeight(); |
| 227 | 231 |
| 228 mArticleSnippetTextView.setText(mArticle.mPreviewText); | 232 mArticleSnippetTextView.setText(mArticle.mPreviewText); |
| 229 | 233 |
| 230 // If there's still a pending thumbnail fetch, cancel it. | 234 // If there's still a pending thumbnail fetch, cancel it. |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 ApiCompatibilityUtils.setCompoundDrawablesRelative( | 391 ApiCompatibilityUtils.setCompoundDrawablesRelative( |
| 388 mPublisherTextView, drawable, null, null, null); | 392 mPublisherTextView, drawable, null, null, null); |
| 389 mPublisherTextView.setVisibility(View.VISIBLE); | 393 mPublisherTextView.setVisibility(View.VISIBLE); |
| 390 } | 394 } |
| 391 | 395 |
| 392 @Override | 396 @Override |
| 393 public boolean isDismissable() { | 397 public boolean isDismissable() { |
| 394 return !isPeeking(); | 398 return !isPeeking(); |
| 395 } | 399 } |
| 396 } | 400 } |
| OLD | NEW |