xref: /aosp_15_r20/frameworks/base/packages/SystemUI/src/com/android/systemui/qs/external/TileRequestDialog.kt (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1 /*
2  * Copyright (C) 2021 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.qs.external
18 
19 import android.app.IUriGrantsManager
20 import android.content.Context
21 import android.view.ContextThemeWrapper
22 import android.view.LayoutInflater
23 import android.view.ViewGroup
24 import android.widget.TextView
25 import com.android.systemui.plugins.qs.QSTile
26 import com.android.systemui.plugins.qs.QSTileView
27 import com.android.systemui.qs.tileimpl.QSTileImpl
28 import com.android.systemui.qs.tileimpl.QSTileImpl.ResourceIcon
29 import com.android.systemui.qs.tileimpl.QSTileViewImpl
30 import com.android.systemui.res.R
31 import com.android.systemui.statusbar.phone.SystemUIDialog
32 
33 /** Dialog to present to the user to ask for authorization to add a [TileService]. */
34 class TileRequestDialog(context: Context) : SystemUIDialog(context) {
35 
36     companion object {
37         internal val CONTENT_ID = R.id.content
38     }
39 
40     /** Set the data of the tile to add, to show the user. */
setTileDatanull41     fun setTileData(tileData: TileData, iUriGrantsManager: IUriGrantsManager) {
42         val ll =
43             (LayoutInflater.from(context).inflate(R.layout.tile_service_request_dialog, null)
44                     as ViewGroup)
45                 .apply {
46                     requireViewById<TextView>(R.id.text).apply {
47                         text =
48                             context.getString(
49                                 R.string.qs_tile_request_dialog_text,
50                                 tileData.appName,
51                             )
52                     }
53                     addView(
54                         createTileView(tileData, iUriGrantsManager),
55                         context.resources.getDimensionPixelSize(
56                             R.dimen.qs_tile_service_request_tile_width
57                         ),
58                         context.resources.getDimensionPixelSize(R.dimen.qs_quick_tile_size),
59                     )
60                     isSelected = true
61                 }
62         val spacing = 0
63         setView(ll, spacing, spacing, spacing, spacing / 2)
64     }
65 
createTileViewnull66     private fun createTileView(
67         tileData: TileData,
68         iUriGrantsManager: IUriGrantsManager,
69     ): QSTileView {
70         val themedContext = ContextThemeWrapper(context, R.style.Theme_SystemUI_QuickSettings)
71         val tile = QSTileViewImpl(themedContext, true)
72         val state =
73             QSTile.BooleanState().apply {
74                 label = tileData.label
75                 handlesLongClick = false
76                 icon =
77                     tileData.icon
78                         ?.loadDrawableCheckingUriGrant(
79                             context,
80                             iUriGrantsManager,
81                             tileData.callingUid,
82                             tileData.packageName,
83                         )
84                         ?.let { QSTileImpl.DrawableIcon(it) }
85                         ?: ResourceIcon.get(R.drawable.android)
86                 contentDescription = label
87             }
88         tile.onStateChanged(state)
89         tile.post {
90             tile.stateDescription = ""
91             tile.isClickable = false
92             tile.isSelected = true
93         }
94         return tile
95     }
96 }
97