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