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.content.Context
20 import android.graphics.drawable.Icon
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.R
26 import com.android.systemui.plugins.qs.QSTile
27 import com.android.systemui.plugins.qs.QSTileView
28 import com.android.systemui.qs.tileimpl.QSIconViewImpl
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      */
48     fun setTileData(tileData: TileData) {
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),
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 
69     private fun createTileView(tileData: TileData): QSTileView {
70         val themedContext = ContextThemeWrapper(context, R.style.Theme_SystemUI_QuickSettings)
71         val tile = QSTileViewImpl(themedContext, QSIconViewImpl(themedContext), true)
72         val state = QSTile.BooleanState().apply {
73             label = tileData.label
74             handlesLongClick = false
75             icon = tileData.icon?.loadDrawable(context)?.let {
76                 QSTileImpl.DrawableIcon(it)
77             } ?: ResourceIcon.get(R.drawable.android)
78             contentDescription = label
79         }
80         tile.onStateChanged(state)
81         tile.post {
82             tile.stateDescription = ""
83             tile.isClickable = false
84             tile.isSelected = true
85         }
86         return tile
87     }
88 
89     /**
90      * Data bundle of information to show the user.
91      *
92      * @property appName Name of the app requesting their [TileService] to be added.
93      * @property label Label of the tile.
94      * @property icon Icon for the tile.
95      */
96     data class TileData(
97         val appName: CharSequence,
98         val label: CharSequence,
99         val icon: Icon?
100     )
101 }
102