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.ColorStateList; | 8 import android.content.res.ColorStateList; |
9 import android.content.res.Resources; | 9 import android.content.res.Resources; |
10 import android.graphics.Bitmap; | 10 import android.graphics.Bitmap; |
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 | 347 |
348 if (fileType != DownloadFilter.FILTER_IMAGE) return; | 348 if (fileType != DownloadFilter.FILTER_IMAGE) return; |
349 if (mImageCallback != null) { | 349 if (mImageCallback != null) { |
350 mThumbnailProvider.cancelRetrieval(mImageCallback); | 350 mThumbnailProvider.cancelRetrieval(mImageCallback); |
351 mImageCallback = null; | 351 mImageCallback = null; |
352 } | 352 } |
353 mImageCallback = new FetchImageCallback(this, mArticle); | 353 mImageCallback = new FetchImageCallback(this, mArticle); |
354 mArticle.setThumbnailBitmap(null); | 354 mArticle.setThumbnailBitmap(null); |
355 Bitmap thumbnail = mThumbnailProvider.getThumbnail(mImageCallback); | 355 Bitmap thumbnail = mThumbnailProvider.getThumbnail(mImageCallback); |
356 if (thumbnail == null || thumbnail.isRecycled()) return; | 356 if (thumbnail == null || thumbnail.isRecycled()) return; |
357 mArticle.setThumbnailBitmap(thumbnail); | 357 mArticle.setThumbnailBitmap(mUiDelegate.getReferencePool().put(thumb
nail)); |
358 setThumbnailFromBitmap(thumbnail); | 358 setThumbnailFromBitmap(thumbnail); |
359 | 359 |
360 return; | 360 return; |
361 } | 361 } |
362 | 362 |
363 setThumbnailFromFileType(DownloadFilter.FILTER_PAGE); | 363 setThumbnailFromFileType(DownloadFilter.FILTER_PAGE); |
364 } | 364 } |
365 | 365 |
366 private void setThumbnail() { | 366 private void setThumbnail() { |
367 // If there's still a pending thumbnail fetch, cancel it. | 367 // If there's still a pending thumbnail fetch, cancel it. |
368 cancelImageFetch(); | 368 cancelImageFetch(); |
369 | 369 |
370 // mThumbnailView's visibility is modified in updateLayout(). | 370 // mThumbnailView's visibility is modified in updateLayout(). |
371 if (mThumbnailView.getVisibility() != View.VISIBLE) return; | 371 if (mThumbnailView.getVisibility() != View.VISIBLE) return; |
372 if (mArticle.getThumbnailBitmap() != null && !mArticle.getThumbnailBitma
p().isRecycled()) { | 372 Bitmap thumbnail = mArticle.getThumbnailBitmap(); |
373 setThumbnailFromBitmap(mArticle.getThumbnailBitmap()); | 373 if (thumbnail != null && !thumbnail.isRecycled()) { |
| 374 setThumbnailFromBitmap(thumbnail); |
374 return; | 375 return; |
375 } | 376 } |
376 | 377 |
377 if (mArticle.isDownload()) { | 378 if (mArticle.isDownload()) { |
378 setDownloadThumbnail(); | 379 setDownloadThumbnail(); |
379 return; | 380 return; |
380 } | 381 } |
381 | 382 |
382 // Temporarily set placeholder and then fetch the thumbnail from a provi
der. | 383 // Temporarily set placeholder and then fetch the thumbnail from a provi
der. |
383 mThumbnailView.setBackground(null); | 384 mThumbnailView.setBackground(null); |
(...skipping 25 matching lines...) Expand all Loading... |
409 | 410 |
410 // We need to crop and scale the downloaded bitmap, as the ImageView we
set it on won't be | 411 // We need to crop and scale the downloaded bitmap, as the ImageView we
set it on won't be |
411 // able to do so when using a TransitionDrawable (as opposed to the stra
ight bitmap). | 412 // able to do so when using a TransitionDrawable (as opposed to the stra
ight bitmap). |
412 // That's a limitation of TransitionDrawable, which doesn't handle layer
s of varying sizes. | 413 // That's a limitation of TransitionDrawable, which doesn't handle layer
s of varying sizes. |
413 Resources res = mThumbnailView.getResources(); | 414 Resources res = mThumbnailView.getResources(); |
414 int targetSize = res.getDimensionPixelSize(R.dimen.snippets_thumbnail_si
ze); | 415 int targetSize = res.getDimensionPixelSize(R.dimen.snippets_thumbnail_si
ze); |
415 Bitmap scaledThumbnail = ThumbnailUtils.extractThumbnail( | 416 Bitmap scaledThumbnail = ThumbnailUtils.extractThumbnail( |
416 thumbnail, targetSize, targetSize, ThumbnailUtils.OPTIONS_RECYCL
E_INPUT); | 417 thumbnail, targetSize, targetSize, ThumbnailUtils.OPTIONS_RECYCL
E_INPUT); |
417 | 418 |
418 // Store the bitmap to skip the download task next time we display this
snippet. | 419 // Store the bitmap to skip the download task next time we display this
snippet. |
419 snippet.setThumbnailBitmap(scaledThumbnail); | 420 snippet.setThumbnailBitmap(mUiDelegate.getReferencePool().put(scaledThum
bnail)); |
420 | 421 |
421 // Cross-fade between the placeholder and the thumbnail. We cross-fade b
ecause the incoming | 422 // Cross-fade between the placeholder and the thumbnail. We cross-fade b
ecause the incoming |
422 // image may have transparency and we don't want the previous image show
ing up behind. | 423 // image may have transparency and we don't want the previous image show
ing up behind. |
423 Drawable[] layers = {mThumbnailView.getDrawable(), | 424 Drawable[] layers = {mThumbnailView.getDrawable(), |
424 new BitmapDrawable(mThumbnailView.getResources(), scaledThumbnai
l)}; | 425 new BitmapDrawable(mThumbnailView.getResources(), scaledThumbnai
l)}; |
425 TransitionDrawable transitionDrawable = new TransitionDrawable(layers); | 426 TransitionDrawable transitionDrawable = new TransitionDrawable(layers); |
426 mThumbnailView.setScaleType(ImageView.ScaleType.CENTER_CROP); | 427 mThumbnailView.setScaleType(ImageView.ScaleType.CENTER_CROP); |
427 mThumbnailView.setBackground(null); | 428 mThumbnailView.setBackground(null); |
428 mThumbnailView.setImageDrawable(transitionDrawable); | 429 mThumbnailView.setImageDrawable(transitionDrawable); |
429 mThumbnailView.setTint(null); | 430 mThumbnailView.setTint(null); |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
583 * Callback to refresh the offline badge visibility. | 584 * Callback to refresh the offline badge visibility. |
584 */ | 585 */ |
585 public static class RefreshOfflineBadgeVisibilityCallback extends PartialBin
dCallback { | 586 public static class RefreshOfflineBadgeVisibilityCallback extends PartialBin
dCallback { |
586 @Override | 587 @Override |
587 public void onResult(NewTabPageViewHolder holder) { | 588 public void onResult(NewTabPageViewHolder holder) { |
588 assert holder instanceof SnippetArticleViewHolder; | 589 assert holder instanceof SnippetArticleViewHolder; |
589 ((SnippetArticleViewHolder) holder).refreshOfflineBadgeVisibility(); | 590 ((SnippetArticleViewHolder) holder).refreshOfflineBadgeVisibility(); |
590 } | 591 } |
591 } | 592 } |
592 } | 593 } |
OLD | NEW |