Fabian Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 15 | package com.android.systemui.privacy |
| 16 | |
| 17 | import android.app.AlertDialog |
| 18 | import android.app.Dialog |
| 19 | import android.content.Context |
| 20 | import android.content.DialogInterface |
Fabian Kozynski | ef12449 | 2018-11-02 11:02:11 -0400 | [diff] [blame] | 21 | import android.content.Intent |
Fabian Kozynski | 873e193 | 2019-01-29 18:33:07 -0500 | [diff] [blame] | 22 | import android.content.pm.PackageManager |
Fabian Kozynski | ef12449 | 2018-11-02 11:02:11 -0400 | [diff] [blame] | 23 | import android.content.res.ColorStateList |
Fabian Kozynski | 0d03da3 | 2019-01-28 10:35:28 -0500 | [diff] [blame] | 24 | import android.os.UserHandle |
Fabian Kozynski | eac70d8 | 2019-01-31 15:04:56 -0500 | [diff] [blame] | 25 | import android.provider.Settings |
Fabian Kozynski | ba21972 | 2019-01-11 16:30:18 -0500 | [diff] [blame] | 26 | import android.util.IconDrawableFactory |
Fabian Kozynski | bfd6d85 | 2019-02-15 15:08:16 -0500 | [diff] [blame] | 27 | import android.util.StatsLog |
Fabian Kozynski | bb68be0 | 2018-11-19 12:58:01 -0500 | [diff] [blame] | 28 | import android.view.Gravity |
Fabian Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 29 | import android.view.LayoutInflater |
| 30 | import android.view.View |
Fabian Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 31 | import android.widget.ImageView |
| 32 | import android.widget.LinearLayout |
| 33 | import android.widget.TextView |
| 34 | import com.android.systemui.Dependency |
| 35 | import com.android.systemui.R |
| 36 | import com.android.systemui.plugins.ActivityStarter |
Joel Galenson | 4bed492 | 2019-01-22 10:56:10 -0800 | [diff] [blame] | 37 | import java.util.concurrent.TimeUnit |
Fabian Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 38 | |
| 39 | class OngoingPrivacyDialog constructor( |
Fabian Kozynski | 20b609c | 2019-02-11 12:56:27 -0500 | [diff] [blame] | 40 | private val context: Context, |
| 41 | private val dialogBuilder: PrivacyDialogBuilder |
Fabian Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 42 | ) { |
| 43 | |
Fabian Kozynski | bb68be0 | 2018-11-19 12:58:01 -0500 | [diff] [blame] | 44 | private val iconSize = context.resources.getDimensionPixelSize( |
| 45 | R.dimen.ongoing_appops_dialog_icon_size) |
| 46 | private val iconColor = context.resources.getColor( |
Fabian Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 47 | com.android.internal.R.color.text_color_primary, context.theme) |
Fabian Kozynski | bb68be0 | 2018-11-19 12:58:01 -0500 | [diff] [blame] | 48 | private val iconMargin = context.resources.getDimensionPixelSize( |
| 49 | R.dimen.ongoing_appops_dialog_icon_margin) |
Fabian Kozynski | ba21972 | 2019-01-11 16:30:18 -0500 | [diff] [blame] | 50 | private val iconFactory = IconDrawableFactory.newInstance(context, true) |
Fabian Kozynski | 0d03da3 | 2019-01-28 10:35:28 -0500 | [diff] [blame] | 51 | private var dismissDialog: (() -> Unit)? = null |
Fabian Kozynski | a37d523 | 2019-02-01 10:14:26 -0500 | [diff] [blame] | 52 | private val appsAndTypes = dialogBuilder.appsAndTypes |
| 53 | .sortedWith(compareBy({ -it.second.size }, // Sort by number of AppOps |
| 54 | { it.second.min() }, |
| 55 | { it.first })) |
Fabian Kozynski | ba21972 | 2019-01-11 16:30:18 -0500 | [diff] [blame] | 56 | |
Fabian Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 57 | fun createDialog(): Dialog { |
Fabian Kozynski | ef12449 | 2018-11-02 11:02:11 -0400 | [diff] [blame] | 58 | val builder = AlertDialog.Builder(context).apply { |
Fabian Kozynski | bfd6d85 | 2019-02-15 15:08:16 -0500 | [diff] [blame] | 59 | setPositiveButton(R.string.ongoing_privacy_dialog_ok, |
| 60 | object : DialogInterface.OnClickListener { |
| 61 | override fun onClick(dialog: DialogInterface?, which: Int) { |
| 62 | StatsLog.write(StatsLog.PRIVACY_INDICATORS_INTERACTED, |
| 63 | StatsLog.PRIVACY_INDICATORS_INTERACTED__TYPE__DIALOG_DISMISS) |
| 64 | } |
| 65 | }) |
Fabian Kozynski | 0d03da3 | 2019-01-28 10:35:28 -0500 | [diff] [blame] | 66 | setNeutralButton(R.string.ongoing_privacy_dialog_open_settings, |
Fabian Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 67 | object : DialogInterface.OnClickListener { |
Fabian Kozynski | 2d6c125 | 2019-02-05 12:57:34 -0500 | [diff] [blame] | 68 | val intent = Intent(Settings.ACTION_PRIVACY_SETTINGS).putExtra( |
Joel Galenson | 4bed492 | 2019-01-22 10:56:10 -0800 | [diff] [blame] | 69 | Intent.EXTRA_DURATION_MILLIS, TimeUnit.MINUTES.toMillis(1)) |
Fabian Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 70 | |
Aurimas Liutikas | f1f63f6 | 2019-01-03 13:00:58 -0800 | [diff] [blame] | 71 | @Suppress("DEPRECATION") |
Fabian Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 72 | override fun onClick(dialog: DialogInterface?, which: Int) { |
Fabian Kozynski | bfd6d85 | 2019-02-15 15:08:16 -0500 | [diff] [blame] | 73 | StatsLog.write(StatsLog.PRIVACY_INDICATORS_INTERACTED, StatsLog |
| 74 | .PRIVACY_INDICATORS_INTERACTED__TYPE__DIALOG_PRIVACY_SETTINGS) |
Fabian Kozynski | 554fb77 | 2019-01-09 17:04:56 -0500 | [diff] [blame] | 75 | Dependency.get(ActivityStarter::class.java) |
| 76 | .postStartActivityDismissingKeyguard(intent, 0) |
Fabian Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 77 | } |
| 78 | }) |
Fabian Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 79 | } |
| 80 | builder.setView(getContentView()) |
Fabian Kozynski | 0d03da3 | 2019-01-28 10:35:28 -0500 | [diff] [blame] | 81 | val dialog = builder.create() |
| 82 | dismissDialog = dialog::dismiss |
| 83 | return dialog |
Fabian Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | fun getContentView(): View { |
| 87 | val layoutInflater = LayoutInflater.from(context) |
| 88 | val contentView = layoutInflater.inflate(R.layout.ongoing_privacy_dialog_content, null) |
| 89 | |
Fabian Kozynski | ef12449 | 2018-11-02 11:02:11 -0400 | [diff] [blame] | 90 | val title = contentView.findViewById(R.id.title) as TextView |
| 91 | val appsList = contentView.findViewById(R.id.items_container) as LinearLayout |
Fabian Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 92 | |
Fabian Kozynski | ef12449 | 2018-11-02 11:02:11 -0400 | [diff] [blame] | 93 | title.setText(dialogBuilder.getDialogTitle()) |
| 94 | |
Fabian Kozynski | a37d523 | 2019-02-01 10:14:26 -0500 | [diff] [blame] | 95 | val numItems = appsAndTypes.size |
Fabian Kozynski | ef12449 | 2018-11-02 11:02:11 -0400 | [diff] [blame] | 96 | for (i in 0..(numItems - 1)) { |
Fabian Kozynski | a37d523 | 2019-02-01 10:14:26 -0500 | [diff] [blame] | 97 | val item = appsAndTypes[i] |
Fabian Kozynski | ef12449 | 2018-11-02 11:02:11 -0400 | [diff] [blame] | 98 | addAppItem(appsList, item.first, item.second, dialogBuilder.types.size > 1) |
Fabian Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 99 | } |
| 100 | return contentView |
| 101 | } |
| 102 | |
Fabian Kozynski | 0d03da3 | 2019-01-28 10:35:28 -0500 | [diff] [blame] | 103 | @Suppress("DEPRECATION") |
Fabian Kozynski | ef12449 | 2018-11-02 11:02:11 -0400 | [diff] [blame] | 104 | private fun addAppItem( |
| 105 | itemList: LinearLayout, |
| 106 | app: PrivacyApplication, |
| 107 | types: List<PrivacyType>, |
| 108 | showIcons: Boolean = true |
| 109 | ) { |
| 110 | val layoutInflater = LayoutInflater.from(context) |
| 111 | val item = layoutInflater.inflate(R.layout.ongoing_privacy_dialog_item, itemList, false) |
| 112 | val appIcon = item.findViewById(R.id.app_icon) as ImageView |
| 113 | val appName = item.findViewById(R.id.app_name) as TextView |
| 114 | val icons = item.findViewById(R.id.icons) as LinearLayout |
Fabian Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 115 | |
Fabian Kozynski | 508422b | 2018-12-20 10:58:17 -0500 | [diff] [blame] | 116 | val lp = LinearLayout.LayoutParams(iconSize, iconSize).apply { |
Fabian Kozynski | bb68be0 | 2018-11-19 12:58:01 -0500 | [diff] [blame] | 117 | gravity = Gravity.CENTER_VERTICAL |
| 118 | marginStart = iconMargin |
| 119 | } |
| 120 | |
Fabian Kozynski | 508422b | 2018-12-20 10:58:17 -0500 | [diff] [blame] | 121 | app.icon.let { |
Fabian Kozynski | ba21972 | 2019-01-11 16:30:18 -0500 | [diff] [blame] | 122 | appIcon.setImageDrawable(iconFactory.getShadowedIcon(it)) |
Fabian Kozynski | ef12449 | 2018-11-02 11:02:11 -0400 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | appName.text = app.applicationName |
| 126 | if (showIcons) { |
Fabian Kozynski | 6024e86 | 2019-01-11 13:13:40 -0500 | [diff] [blame] | 127 | dialogBuilder.generateIconsForApp(types).forEachIndexed { index, it -> |
Fabian Kozynski | ef12449 | 2018-11-02 11:02:11 -0400 | [diff] [blame] | 128 | it.setBounds(0, 0, iconSize, iconSize) |
| 129 | val image = ImageView(context).apply { |
| 130 | imageTintList = ColorStateList.valueOf(iconColor) |
| 131 | setImageDrawable(it) |
| 132 | } |
Fabian Kozynski | 6024e86 | 2019-01-11 13:13:40 -0500 | [diff] [blame] | 133 | image.contentDescription = types[index].getName(context) |
Fabian Kozynski | bb68be0 | 2018-11-19 12:58:01 -0500 | [diff] [blame] | 134 | icons.addView(image, lp) |
Fabian Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 135 | } |
Fabian Kozynski | ef12449 | 2018-11-02 11:02:11 -0400 | [diff] [blame] | 136 | icons.visibility = View.VISIBLE |
| 137 | } else { |
| 138 | icons.visibility = View.GONE |
Fabian Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 139 | } |
Fabian Kozynski | 873e193 | 2019-01-29 18:33:07 -0500 | [diff] [blame] | 140 | try { |
| 141 | // Check if package exists |
| 142 | context.packageManager.getPackageInfo(app.packageName, 0) |
| 143 | item.setOnClickListener(object : View.OnClickListener { |
Fabian Kozynski | 2d6c125 | 2019-02-05 12:57:34 -0500 | [diff] [blame] | 144 | val intent = Intent(Intent.ACTION_MANAGE_APP_PERMISSIONS) |
Fabian Kozynski | 873e193 | 2019-01-29 18:33:07 -0500 | [diff] [blame] | 145 | .putExtra(Intent.EXTRA_PACKAGE_NAME, app.packageName) |
| 146 | .putExtra(Intent.EXTRA_USER, UserHandle.getUserHandleForUid(app.uid)) |
| 147 | override fun onClick(v: View?) { |
Fabian Kozynski | bfd6d85 | 2019-02-15 15:08:16 -0500 | [diff] [blame] | 148 | StatsLog.write(StatsLog.PRIVACY_INDICATORS_INTERACTED, |
| 149 | StatsLog.PRIVACY_INDICATORS_INTERACTED__TYPE__DIALOG_LINE_ITEM, |
| 150 | app.packageName) |
Fabian Kozynski | 873e193 | 2019-01-29 18:33:07 -0500 | [diff] [blame] | 151 | Dependency.get(ActivityStarter::class.java) |
| 152 | .postStartActivityDismissingKeyguard(intent, 0) |
| 153 | dismissDialog?.invoke() |
| 154 | } |
| 155 | }) |
| 156 | } catch (e: PackageManager.NameNotFoundException) {} |
| 157 | |
Fabian Kozynski | ef12449 | 2018-11-02 11:02:11 -0400 | [diff] [blame] | 158 | itemList.addView(item) |
Fabian Kozynski | 1263824 | 2018-10-12 15:33:41 -0400 | [diff] [blame] | 159 | } |
| 160 | } |