• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * 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.systemui.utils
18 
19 import android.app.IUriGrantsManager
20 import android.content.Context
21 import android.graphics.drawable.Drawable
22 import android.graphics.drawable.Icon
23 import android.os.UserHandle
24 import com.android.systemui.dagger.qualifiers.Application
25 import dagger.assisted.Assisted
26 import dagger.assisted.AssistedFactory
27 import dagger.assisted.AssistedInject
28 
29 /**
30  * Use to load an icon (from another app) safely. It will prevent cross user icon loading if there
31  * are no permissions.
32  */
33 class SafeIconLoader
34 @AssistedInject
35 constructor(
36     @Assisted("serviceUid") private val serviceUid: Int,
37     @Assisted private val packageName: String,
38     @Assisted("userId") private val userId: Int,
39     @Application private val applicationContext: Context,
40     private val iUriGrantsManager: IUriGrantsManager,
41 ) {
42 
43     private val serviceContext =
44         applicationContext.createPackageContextAsUser(packageName, 0, UserHandle.of(userId))
45 
46     /**
47      * Tries to load the icon. If it fails in any way (for example, cross user permissions), it will
48      * return `null`. Prefer calling this in a background thread.
49      */
loadnull50     fun load(icon: Icon): Drawable? {
51         return icon.loadDrawableCheckingUriGrant(
52             serviceContext,
53             iUriGrantsManager,
54             serviceUid,
55             packageName,
56         )
57     }
58 
59     @AssistedFactory
60     interface Factory {
61 
createnull62         fun create(
63             @Assisted("serviceUid") serviceUid: Int,
64             packageName: String,
65             @Assisted("userId") userId: Int,
66         ): SafeIconLoader
67     }
68 }
69