1 /*
<lambda>null2  * Copyright (C) 2024 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 
17 package com.android.intentresolver.contentpreview
18 
19 import android.content.ContentResolver
20 import android.graphics.Bitmap
21 import android.net.Uri
22 import android.util.Size
23 import com.android.intentresolver.util.withCancellationSignal
24 import javax.inject.Inject
25 
26 /** Interface for objects that can attempt load a [Bitmap] from a [Uri]. */
27 interface ThumbnailLoader {
28     /**
29      * Loads a thumbnail for the given [uri].
30      *
31      * The size of the thumbnail is determined by the implementation.
32      */
33     suspend fun loadThumbnail(uri: Uri): Bitmap?
34 
35     /**
36      * Loads a thumbnail for the given [uri] and [size].
37      *
38      * The [size] is the size of the thumbnail in pixels.
39      */
40     suspend fun loadThumbnail(uri: Uri, size: Size): Bitmap?
41 }
42 
43 /** Default implementation of [ThumbnailLoader]. */
44 class ThumbnailLoaderImpl
45 @Inject
46 constructor(
47     private val contentResolver: ContentResolver,
48     @ThumbnailSize thumbnailSize: Int,
49 ) : ThumbnailLoader {
50 
51     private val size = Size(thumbnailSize, thumbnailSize)
52 
loadThumbnailnull53     override suspend fun loadThumbnail(uri: Uri): Bitmap =
54         contentResolver.loadThumbnail(uri, size, /* signal= */ null)
55 
56     override suspend fun loadThumbnail(uri: Uri, size: Size): Bitmap =
57         withCancellationSignal { signal ->
58             contentResolver.loadThumbnail(uri, size, signal)
59         }
60 }
61