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.graphics.drawable.Icon
22 import android.view.ContextThemeWrapper
23 import android.view.LayoutInflater
24 import android.view.ViewGroup
25 import android.widget.TextView
26 import com.android.systemui.res.R
27 import com.android.systemui.plugins.qs.QSTile
28 import com.android.systemui.plugins.qs.QSTileView
29 import com.android.systemui.qs.tileimpl.QSTileImpl
30 import com.android.systemui.qs.tileimpl.QSTileImpl.ResourceIcon
31 import com.android.systemui.qs.tileimpl.QSTileViewImpl
32 import com.android.systemui.statusbar.phone.SystemUIDialog
33 
34 /**
35  * Dialog to present to the user to ask for authorization to add a [TileService].
36  */
37 class TileRequestDialog(
38     context: Context,
39 ) : SystemUIDialog(context) {
40 
41     companion object {
42         internal val CONTENT_ID = R.id.content
43     }
44 
45     /**
46      * Set the data of the tile to add, to show the user.
47      */
setTileDatanull48     fun setTileData(tileData: TileData, iUriGrantsManager: IUriGrantsManager) {
49         val ll = (LayoutInflater
50                         .from(context)
51                         .inflate(R.layout.tile_service_request_dialog, null)
52                         as ViewGroup).apply {
53                     requireViewById<TextView>(R.id.text).apply {
54                         text = context
55                                 .getString(R.string.qs_tile_request_dialog_text, tileData.appName)
56                     }
57                     addView(
58                             createTileView(tileData, iUriGrantsManager),
59                             context.resources.getDimensionPixelSize(
60                                     R.dimen.qs_tile_service_request_tile_width),
61                             context.resources.getDimensionPixelSize(R.dimen.qs_quick_tile_size)
62                     )
63                     isSelected = true
64         }
65         val spacing = 0
66         setView(ll, spacing, spacing, spacing, spacing / 2)
67     }
68 
createTileViewnull69     private fun createTileView(
70             tileData: TileData,
71             iUriGrantsManager: IUriGrantsManager,
72     ): QSTileView {
73         val themedContext = ContextThemeWrapper(context, R.style.Theme_SystemUI_QuickSettings)
74         val tile = QSTileViewImpl(themedContext, true)
75         val state = QSTile.BooleanState().apply {
76             label = tileData.label
77             handlesLongClick = false
78             icon = tileData.icon?.loadDrawableCheckingUriGrant(
79                     context,
80                     iUriGrantsManager,
81                     tileData.callingUid,
82                     tileData.packageName,
83             )?.let {
84                 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     /**
98      * Data bundle of information to show the user.
99      *
100      * @property appName Name of the app requesting their [TileService] to be added.
101      * @property label Label of the tile.
102      * @property icon Icon for the tile.
103      */
104     data class TileData(
105         val callingUid: Int,
106         val appName: CharSequence,
107         val label: CharSequence,
108         val icon: Icon?,
109         val packageName: String,
110     )
111 }
112