• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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.systemui.clipboardoverlay
17 
18 import android.content.Context
19 import android.graphics.Bitmap
20 import android.net.Uri
21 import android.util.Log
22 import android.util.Size
23 import com.android.app.tracing.coroutines.launchTraced as launch
24 import com.android.systemui.Flags.clipboardOverlayMultiuser
25 import com.android.systemui.dagger.qualifiers.Application
26 import com.android.systemui.dagger.qualifiers.Background
27 import com.android.systemui.res.R
28 import com.android.systemui.settings.UserTracker
29 import java.io.IOException
30 import java.util.function.Consumer
31 import javax.inject.Inject
32 import kotlinx.coroutines.CoroutineDispatcher
33 import kotlinx.coroutines.CoroutineScope
34 import kotlinx.coroutines.withContext
35 import kotlinx.coroutines.withTimeoutOrNull
36 
37 class ClipboardImageLoader
38 @Inject
39 constructor(
40     private val context: Context,
41     private val userTracker: UserTracker,
42     @Background private val bgDispatcher: CoroutineDispatcher,
43     @Application private val mainScope: CoroutineScope,
44 ) {
45     private val TAG: String = "ClipboardImageLoader"
46 
loadnull47     suspend fun load(uri: Uri, timeoutMs: Long = 300) =
48         withTimeoutOrNull(timeoutMs) {
49             withContext(bgDispatcher) {
50                 try {
51                     val size = context.resources.getDimensionPixelSize(R.dimen.overlay_x_scale)
52                     if (clipboardOverlayMultiuser()) {
53                         userTracker.userContentResolver.loadThumbnail(
54                             uri,
55                             Size(size, size * 4),
56                             null,
57                         )
58                     } else {
59                         context.contentResolver.loadThumbnail(uri, Size(size, size * 4), null)
60                     }
61                 } catch (e: IOException) {
62                     Log.e(TAG, "Thumbnail loading failed!", e)
63                     null
64                 }
65             }
66         }
67 
loadAsyncnull68     fun loadAsync(uri: Uri, callback: Consumer<Bitmap?>) {
69         mainScope.launch { callback.accept(load(uri)) }
70     }
71 }
72