Austin Kolander | 4143e37 | 2017-07-05 14:31:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.documentsui; |
| 17 | |
Jeff Sharkey | 2c0b485 | 2019-02-15 15:53:47 -0700 | [diff] [blame] | 18 | import static android.content.ContentResolver.wrap; |
| 19 | |
Felipe Leme | 9de5807 | 2018-01-19 16:40:04 -0800 | [diff] [blame] | 20 | import static com.android.documentsui.base.SharedMinimal.VERBOSE; |
Austin Kolander | 4143e37 | 2017-07-05 14:31:37 -0700 | [diff] [blame] | 21 | |
| 22 | import android.content.ContentProviderClient; |
| 23 | import android.content.ContentResolver; |
| 24 | import android.content.Context; |
| 25 | import android.graphics.Bitmap; |
| 26 | import android.graphics.Point; |
| 27 | import android.net.Uri; |
| 28 | import android.os.AsyncTask; |
| 29 | import android.os.CancellationSignal; |
Jeff Sharkey | bb68a65 | 2019-02-19 11:17:30 -0700 | [diff] [blame] | 30 | import android.os.FileUtils; |
Austin Kolander | 4143e37 | 2017-07-05 14:31:37 -0700 | [diff] [blame] | 31 | import android.os.OperationCanceledException; |
| 32 | import android.provider.DocumentsContract; |
| 33 | import android.util.Log; |
| 34 | import android.view.View; |
| 35 | import android.widget.ImageView; |
| 36 | import com.android.documentsui.ProviderExecutor.Preemptable; |
| 37 | import java.util.function.BiConsumer; |
Austin Kolander | 04efd4a | 2017-07-14 11:23:17 -0700 | [diff] [blame] | 38 | import java.util.function.Consumer; |
Austin Kolander | 4143e37 | 2017-07-05 14:31:37 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Loads a Thumbnails asynchronously then animates from the mime icon to the thumbnail |
| 42 | */ |
| 43 | public final class ThumbnailLoader extends AsyncTask<Uri, Void, Bitmap> implements Preemptable { |
| 44 | |
| 45 | private static final String TAG = ThumbnailLoader.class.getCanonicalName(); |
| 46 | |
| 47 | /** |
| 48 | * Two animations applied to image views. The first is used to switch mime icon and thumbnail. |
| 49 | * The second is used when we need to update thumbnail. |
| 50 | */ |
| 51 | public static final BiConsumer<View, View> ANIM_FADE_IN = (mime, thumb) -> { |
| 52 | float alpha = mime.getAlpha(); |
| 53 | mime.animate().alpha(0f).start(); |
| 54 | thumb.setAlpha(0f); |
| 55 | thumb.animate().alpha(alpha).start(); |
| 56 | }; |
| 57 | public static final BiConsumer<View, View> ANIM_NO_OP = (mime, thumb) -> {}; |
| 58 | |
| 59 | private final ImageView mIconThumb; |
| 60 | private final Point mThumbSize; |
Austin Kolander | 4143e37 | 2017-07-05 14:31:37 -0700 | [diff] [blame] | 61 | private final Uri mUri; |
Austin Kolander | 4143e37 | 2017-07-05 14:31:37 -0700 | [diff] [blame] | 62 | private final long mLastModified; |
Austin Kolander | 04efd4a | 2017-07-14 11:23:17 -0700 | [diff] [blame] | 63 | private final Consumer<Bitmap> mCallback; |
Austin Kolander | 4143e37 | 2017-07-05 14:31:37 -0700 | [diff] [blame] | 64 | private final boolean mAddToCache; |
| 65 | private final CancellationSignal mSignal; |
| 66 | |
| 67 | /** |
| 68 | * @param uri - to a thumbnail. |
Austin Kolander | 4143e37 | 2017-07-05 14:31:37 -0700 | [diff] [blame] | 69 | * @param iconThumb - ImageView to display the thumbnail. |
| 70 | * @param thumbSize - size of the thumbnail. |
| 71 | * @param lastModified - used for updating thumbnail caches. |
Austin Kolander | 4143e37 | 2017-07-05 14:31:37 -0700 | [diff] [blame] | 72 | * @param addToCache - flag that determines if the loader saves the thumbnail to the cache. |
| 73 | */ |
Austin Kolander | 04efd4a | 2017-07-14 11:23:17 -0700 | [diff] [blame] | 74 | public ThumbnailLoader(Uri uri, ImageView iconThumb, Point thumbSize, long lastModified, |
| 75 | Consumer<Bitmap> callback, boolean addToCache) { |
| 76 | |
Austin Kolander | 4143e37 | 2017-07-05 14:31:37 -0700 | [diff] [blame] | 77 | mUri = uri; |
Austin Kolander | 4143e37 | 2017-07-05 14:31:37 -0700 | [diff] [blame] | 78 | mIconThumb = iconThumb; |
| 79 | mThumbSize = thumbSize; |
Austin Kolander | 4143e37 | 2017-07-05 14:31:37 -0700 | [diff] [blame] | 80 | mLastModified = lastModified; |
Austin Kolander | 04efd4a | 2017-07-14 11:23:17 -0700 | [diff] [blame] | 81 | mCallback = callback; |
Austin Kolander | 4143e37 | 2017-07-05 14:31:37 -0700 | [diff] [blame] | 82 | mAddToCache = addToCache; |
| 83 | mSignal = new CancellationSignal(); |
| 84 | mIconThumb.setTag(this); |
Austin Kolander | 04efd4a | 2017-07-14 11:23:17 -0700 | [diff] [blame] | 85 | |
Austin Kolander | 4143e37 | 2017-07-05 14:31:37 -0700 | [diff] [blame] | 86 | if (VERBOSE) Log.v(TAG, "Starting icon loader task for " + mUri); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void preempt() { |
| 91 | if (VERBOSE) Log.v(TAG, "Icon loader task for " + mUri + " was cancelled."); |
| 92 | cancel(false); |
| 93 | mSignal.cancel(); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | protected Bitmap doInBackground(Uri... params) { |
| 98 | if (isCancelled()) { |
| 99 | return null; |
| 100 | } |
| 101 | |
| 102 | final Context context = mIconThumb.getContext(); |
| 103 | final ContentResolver resolver = context.getContentResolver(); |
| 104 | |
| 105 | ContentProviderClient client = null; |
| 106 | Bitmap result = null; |
| 107 | try { |
| 108 | client = DocumentsApplication.acquireUnstableProviderOrThrow( |
| 109 | resolver, mUri.getAuthority()); |
Jeff Sharkey | 2c0b485 | 2019-02-15 15:53:47 -0700 | [diff] [blame] | 110 | result = DocumentsContract.getDocumentThumbnail(wrap(client), |
| 111 | mUri, mThumbSize, mSignal); |
Austin Kolander | 4143e37 | 2017-07-05 14:31:37 -0700 | [diff] [blame] | 112 | if (result != null && mAddToCache) { |
| 113 | final ThumbnailCache cache = DocumentsApplication.getThumbnailCache(context); |
| 114 | cache.putThumbnail(mUri, mThumbSize, result, mLastModified); |
| 115 | } |
| 116 | } catch (Exception e) { |
| 117 | if (!(e instanceof OperationCanceledException)) { |
| 118 | Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e); |
| 119 | } |
| 120 | } finally { |
Jeff Sharkey | bb68a65 | 2019-02-19 11:17:30 -0700 | [diff] [blame] | 121 | FileUtils.closeQuietly(client); |
Austin Kolander | 4143e37 | 2017-07-05 14:31:37 -0700 | [diff] [blame] | 122 | } |
| 123 | return result; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | protected void onPostExecute(Bitmap result) { |
| 128 | if (VERBOSE) Log.v(TAG, "Loader task for " + mUri + " completed"); |
| 129 | |
Austin Kolander | 04efd4a | 2017-07-14 11:23:17 -0700 | [diff] [blame] | 130 | if (mIconThumb.getTag() == this) { |
| 131 | mIconThumb.setTag(null); |
| 132 | mCallback.accept(result); |
| 133 | } |
Austin Kolander | 4143e37 | 2017-07-05 14:31:37 -0700 | [diff] [blame] | 134 | } |
Austin Kolander | 04efd4a | 2017-07-14 11:23:17 -0700 | [diff] [blame] | 135 | } |