blob: 3991c19e4d0558f716d23c4e447b88292ce46143 [file] [log] [blame]
Fabian Kozynski12638242018-10-12 15:33:41 -04001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15package com.android.systemui.privacy
16
17import android.app.AlertDialog
18import android.app.Dialog
19import android.content.Context
20import android.content.DialogInterface
Fabian Kozynskief124492018-11-02 11:02:11 -040021import android.content.Intent
22import android.content.res.ColorStateList
Fabian Kozynskibb68be02018-11-19 12:58:01 -050023import android.view.Gravity
Fabian Kozynski12638242018-10-12 15:33:41 -040024import android.view.LayoutInflater
25import android.view.View
Fabian Kozynski12638242018-10-12 15:33:41 -040026import android.widget.ImageView
27import android.widget.LinearLayout
28import android.widget.TextView
29import com.android.systemui.Dependency
30import com.android.systemui.R
31import com.android.systemui.plugins.ActivityStarter
32
33class OngoingPrivacyDialog constructor(
34 val context: Context,
35 val dialogBuilder: PrivacyDialogBuilder
36) {
37
Fabian Kozynskibb68be02018-11-19 12:58:01 -050038 private val iconSize = context.resources.getDimensionPixelSize(
39 R.dimen.ongoing_appops_dialog_icon_size)
40 private val iconColor = context.resources.getColor(
Fabian Kozynski12638242018-10-12 15:33:41 -040041 com.android.internal.R.color.text_color_primary, context.theme)
Fabian Kozynskibb68be02018-11-19 12:58:01 -050042 private val iconMargin = context.resources.getDimensionPixelSize(
43 R.dimen.ongoing_appops_dialog_icon_margin)
44 private val MAX_ITEMS = context.resources.getInteger(R.integer.ongoing_appops_dialog_max_apps)
Fabian Kozynski12638242018-10-12 15:33:41 -040045
46 fun createDialog(): Dialog {
Fabian Kozynskief124492018-11-02 11:02:11 -040047 val builder = AlertDialog.Builder(context).apply {
48 setNegativeButton(R.string.ongoing_privacy_dialog_cancel, null)
49 setPositiveButton(R.string.ongoing_privacy_dialog_open_settings,
Fabian Kozynski12638242018-10-12 15:33:41 -040050 object : DialogInterface.OnClickListener {
Fabian Kozynskief124492018-11-02 11:02:11 -040051 val intent = Intent(Intent.ACTION_REVIEW_PERMISSION_USAGE)
Fabian Kozynski12638242018-10-12 15:33:41 -040052
53 override fun onClick(dialog: DialogInterface?, which: Int) {
54 Dependency.get(ActivityStarter::class.java).startActivity(intent, false)
55 }
56 })
Fabian Kozynski12638242018-10-12 15:33:41 -040057 }
58 builder.setView(getContentView())
59 return builder.create()
60 }
61
62 fun getContentView(): View {
63 val layoutInflater = LayoutInflater.from(context)
64 val contentView = layoutInflater.inflate(R.layout.ongoing_privacy_dialog_content, null)
65
Fabian Kozynskief124492018-11-02 11:02:11 -040066 val title = contentView.findViewById(R.id.title) as TextView
67 val appsList = contentView.findViewById(R.id.items_container) as LinearLayout
Fabian Kozynski12638242018-10-12 15:33:41 -040068
Fabian Kozynskief124492018-11-02 11:02:11 -040069 title.setText(dialogBuilder.getDialogTitle())
70
71 val numItems = dialogBuilder.appsAndTypes.size
72 for (i in 0..(numItems - 1)) {
73 if (i >= MAX_ITEMS) break
74 val item = dialogBuilder.appsAndTypes[i]
75 addAppItem(appsList, item.first, item.second, dialogBuilder.types.size > 1)
Fabian Kozynski12638242018-10-12 15:33:41 -040076 }
Fabian Kozynskief124492018-11-02 11:02:11 -040077
78 if (numItems > MAX_ITEMS) {
79 val overflow = contentView.findViewById(R.id.overflow) as LinearLayout
80 overflow.visibility = View.VISIBLE
81 val overflowText = overflow.findViewById(R.id.app_name) as TextView
82 overflowText.text = context.resources.getQuantityString(
83 R.plurals.ongoing_privacy_dialog_overflow_text,
84 numItems - MAX_ITEMS,
85 numItems - MAX_ITEMS
86 )
87 val overflowPlus = overflow.findViewById(R.id.app_icon) as ImageView
88 overflowPlus.apply {
89 imageTintList = ColorStateList.valueOf(iconColor)
90 setImageDrawable(context.getDrawable(R.drawable.plus))
91 }
92 }
93
Fabian Kozynski12638242018-10-12 15:33:41 -040094 return contentView
95 }
96
Fabian Kozynskief124492018-11-02 11:02:11 -040097 private fun addAppItem(
98 itemList: LinearLayout,
99 app: PrivacyApplication,
100 types: List<PrivacyType>,
101 showIcons: Boolean = true
102 ) {
103 val layoutInflater = LayoutInflater.from(context)
104 val item = layoutInflater.inflate(R.layout.ongoing_privacy_dialog_item, itemList, false)
105 val appIcon = item.findViewById(R.id.app_icon) as ImageView
106 val appName = item.findViewById(R.id.app_name) as TextView
107 val icons = item.findViewById(R.id.icons) as LinearLayout
Fabian Kozynski12638242018-10-12 15:33:41 -0400108
Fabian Kozynski508422b2018-12-20 10:58:17 -0500109 val lp = LinearLayout.LayoutParams(iconSize, iconSize).apply {
Fabian Kozynskibb68be02018-11-19 12:58:01 -0500110 gravity = Gravity.CENTER_VERTICAL
111 marginStart = iconMargin
112 }
113
Fabian Kozynski508422b2018-12-20 10:58:17 -0500114 app.icon.let {
Fabian Kozynskief124492018-11-02 11:02:11 -0400115 appIcon.setImageDrawable(it)
116 }
117
118 appName.text = app.applicationName
119 if (showIcons) {
120 dialogBuilder.generateIconsForApp(types).forEach {
121 it.setBounds(0, 0, iconSize, iconSize)
122 val image = ImageView(context).apply {
123 imageTintList = ColorStateList.valueOf(iconColor)
124 setImageDrawable(it)
125 }
Fabian Kozynskibb68be02018-11-19 12:58:01 -0500126 icons.addView(image, lp)
Fabian Kozynski12638242018-10-12 15:33:41 -0400127 }
Fabian Kozynskief124492018-11-02 11:02:11 -0400128 icons.visibility = View.VISIBLE
129 } else {
130 icons.visibility = View.GONE
Fabian Kozynski12638242018-10-12 15:33:41 -0400131 }
Fabian Kozynskief124492018-11-02 11:02:11 -0400132 itemList.addView(item)
Fabian Kozynski12638242018-10-12 15:33:41 -0400133 }
134}