Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 54649e34 authored by Giulio Fiscella's avatar Giulio Fiscella
Browse files

Clone and flag enable old privacy dialog

This is to aid the review of follow up changes.
Only class names were changed.
And we start using the ENABLE_NEW_PRIVACY_DIALOG flag to force the
old dialog only when Safety Center is on.

Bug: 283456173
Test: atest SystemUITests
Change-Id: I4ec29afc3d60a4d393e6b214154ea0b2e92be2e2
parent 00f62acc
Loading
Loading
Loading
Loading
+341 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.privacy

import android.Manifest
import android.app.ActivityManager
import android.app.Dialog
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.UserHandle
import android.permission.PermissionGroupUsage
import android.permission.PermissionManager
import android.util.Log
import androidx.annotation.MainThread
import androidx.annotation.VisibleForTesting
import androidx.annotation.WorkerThread
import com.android.internal.logging.UiEventLogger
import com.android.systemui.appops.AppOpsController
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.dagger.qualifiers.Main
import com.android.systemui.plugins.ActivityStarter
import com.android.systemui.privacy.logging.PrivacyLogger
import com.android.systemui.settings.UserTracker
import com.android.systemui.statusbar.policy.KeyguardStateController
import java.util.concurrent.Executor
import javax.inject.Inject

private val defaultDialogProvider = object : PrivacyDialogControllerV2.DialogProvider {
    override fun makeDialog(
        context: Context,
        list: List<PrivacyDialogV2.PrivacyElement>,
        starter: (String, Int, CharSequence?, Intent?) -> Unit
    ): PrivacyDialogV2 {
        return PrivacyDialogV2(context, list, starter)
    }
}

/**
 * Controller for [PrivacyDialogV2].
 *
 * This controller shows and dismissed the dialog, as well as determining the information to show in
 * it.
 */
@SysUISingleton
class PrivacyDialogControllerV2(
    private val permissionManager: PermissionManager,
    private val packageManager: PackageManager,
    private val privacyItemController: PrivacyItemController,
    private val userTracker: UserTracker,
    private val activityStarter: ActivityStarter,
    private val backgroundExecutor: Executor,
    private val uiExecutor: Executor,
    private val privacyLogger: PrivacyLogger,
    private val keyguardStateController: KeyguardStateController,
    private val appOpsController: AppOpsController,
    private val uiEventLogger: UiEventLogger,
    @VisibleForTesting private val dialogProvider: DialogProvider
) {

    @Inject
    constructor(
        permissionManager: PermissionManager,
        packageManager: PackageManager,
        privacyItemController: PrivacyItemController,
        userTracker: UserTracker,
        activityStarter: ActivityStarter,
        @Background backgroundExecutor: Executor,
        @Main uiExecutor: Executor,
        privacyLogger: PrivacyLogger,
        keyguardStateController: KeyguardStateController,
        appOpsController: AppOpsController,
        uiEventLogger: UiEventLogger
    ) : this(
            permissionManager,
            packageManager,
            privacyItemController,
            userTracker,
            activityStarter,
            backgroundExecutor,
            uiExecutor,
            privacyLogger,
            keyguardStateController,
            appOpsController,
            uiEventLogger,
            defaultDialogProvider
    )

    companion object {
        private const val TAG = "PrivacyDialogController"
    }

    private var dialog: Dialog? = null

    private val onDialogDismissed = object : PrivacyDialogV2.OnDialogDismissed {
        override fun onDialogDismissed() {
            privacyLogger.logPrivacyDialogDismissed()
            uiEventLogger.log(PrivacyDialogEvent.PRIVACY_DIALOG_DISMISSED)
            dialog = null
        }
    }

    @MainThread
    private fun startActivity(
        packageName: String,
        userId: Int,
        attributionTag: CharSequence?,
        navigationIntent: Intent?
    ) {
        val intent = if (navigationIntent == null) {
            getDefaultManageAppPermissionsIntent(packageName, userId)
        } else {
            navigationIntent
        }
        uiEventLogger.log(PrivacyDialogEvent.PRIVACY_DIALOG_ITEM_CLICKED_TO_APP_SETTINGS,
            userId, packageName)
        privacyLogger.logStartSettingsActivityFromDialog(packageName, userId)
        if (!keyguardStateController.isUnlocked) {
            // If we are locked, hide the dialog so the user can unlock
            dialog?.hide()
        }
        // startActivity calls internally startActivityDismissingKeyguard
        activityStarter.startActivity(intent, true) {
            if (ActivityManager.isStartResultSuccessful(it)) {
                dismissDialog()
            } else {
                dialog?.show()
            }
        }
    }

    @WorkerThread
    private fun getManagePermissionIntent(
        packageName: String,
        userId: Int,
        permGroupName: CharSequence,
        attributionTag: CharSequence?,
        isAttributionSupported: Boolean
    ): Intent
    {
        lateinit var intent: Intent
        if (attributionTag != null && isAttributionSupported) {
            intent = Intent(Intent.ACTION_MANAGE_PERMISSION_USAGE)
            intent.setPackage(packageName)
            intent.putExtra(Intent.EXTRA_PERMISSION_GROUP_NAME, permGroupName.toString())
            intent.putExtra(Intent.EXTRA_ATTRIBUTION_TAGS, arrayOf(attributionTag.toString()))
            intent.putExtra(Intent.EXTRA_SHOWING_ATTRIBUTION, true)
            val resolveInfo = packageManager.resolveActivity(
                    intent, PackageManager.ResolveInfoFlags.of(0))
            if (resolveInfo != null && resolveInfo.activityInfo != null &&
                    resolveInfo.activityInfo.permission ==
                    android.Manifest.permission.START_VIEW_PERMISSION_USAGE) {
                intent.component = ComponentName(packageName, resolveInfo.activityInfo.name)
                return intent
            }
        }
        return getDefaultManageAppPermissionsIntent(packageName, userId)
    }

    fun getDefaultManageAppPermissionsIntent(packageName: String, userId: Int): Intent {
        val intent = Intent(Intent.ACTION_MANAGE_APP_PERMISSIONS)
        intent.putExtra(Intent.EXTRA_PACKAGE_NAME, packageName)
        intent.putExtra(Intent.EXTRA_USER, UserHandle.of(userId))
        return intent
    }

    @WorkerThread
    private fun permGroupUsage(): List<PermissionGroupUsage> {
        return permissionManager.getIndicatorAppOpUsageData(appOpsController.isMicMuted)
    }

    /**
     * Show the [PrivacyDialogV2]
     *
     * This retrieves the permission usage from [PermissionManager] and creates a new
     * [PrivacyDialogV2] with a list of [PrivacyDialogV2.PrivacyElement] to show.
     *
     * This list will be filtered by [filterAndSelect]. Only types available by
     * [PrivacyItemController] will be shown.
     *
     * @param context A context to use to create the dialog.
     * @see filterAndSelect
     */
    fun showDialog(context: Context) {
        dismissDialog()
        backgroundExecutor.execute {
            val usage = permGroupUsage()
            val userInfos = userTracker.userProfiles
            privacyLogger.logUnfilteredPermGroupUsage(usage)
            val items = usage.mapNotNull {
                val type = filterType(permGroupToPrivacyType(it.permissionGroupName))
                val userInfo = userInfos.firstOrNull { ui -> ui.id == UserHandle.getUserId(it.uid) }
                if (userInfo != null || it.isPhoneCall) {
                    type?.let { t ->
                        // Only try to get the app name if we actually need it
                        val appName = if (it.isPhoneCall) {
                            ""
                        } else {
                            getLabelForPackage(it.packageName, it.uid)
                        }
                        val userId = UserHandle.getUserId(it.uid)
                        PrivacyDialogV2.PrivacyElement(
                                t,
                                it.packageName,
                                userId,
                                appName,
                                it.attributionTag,
                                it.attributionLabel,
                                it.proxyLabel,
                                it.lastAccessTimeMillis,
                                it.isActive,
                                // If there's no user info, we're in a phoneCall in secondary user
                                userInfo?.isManagedProfile ?: false,
                                it.isPhoneCall,
                                it.permissionGroupName,
                                getManagePermissionIntent(
                                        it.packageName,
                                        userId,
                                        it.permissionGroupName,
                                        it.attributionTag,
                                        // attributionLabel is set only when subattribution policies
                                        // are supported and satisfied
                                        it.attributionLabel != null
                                )
                        )
                    }
                } else {
                    // No matching user or phone call
                    null
                }
            }
            uiExecutor.execute {
                val elements = filterAndSelect(items)
                if (elements.isNotEmpty()) {
                    val d = dialogProvider.makeDialog(context, elements, this::startActivity)
                    d.setShowForAllUsers(true)
                    d.addOnDismissListener(onDialogDismissed)
                    d.show()
                    privacyLogger.logShowDialogV2Contents(elements)
                    dialog = d
                } else {
                    Log.w(TAG, "Trying to show empty dialog")
                }
            }
        }
    }

    /**
     * Dismisses the dialog
     */
    fun dismissDialog() {
        dialog?.dismiss()
    }

    @WorkerThread
    private fun getLabelForPackage(packageName: String, uid: Int): CharSequence {
        return try {
            packageManager
                .getApplicationInfoAsUser(packageName, 0, UserHandle.getUserId(uid))
                .loadLabel(packageManager)
        } catch (_: PackageManager.NameNotFoundException) {
            Log.w(TAG, "Label not found for: $packageName")
            packageName
        }
    }

    private fun permGroupToPrivacyType(group: String): PrivacyType? {
        return when (group) {
            Manifest.permission_group.CAMERA -> PrivacyType.TYPE_CAMERA
            Manifest.permission_group.MICROPHONE -> PrivacyType.TYPE_MICROPHONE
            Manifest.permission_group.LOCATION -> PrivacyType.TYPE_LOCATION
            else -> null
        }
    }

    private fun filterType(type: PrivacyType?): PrivacyType? {
        return type?.let {
            if ((it == PrivacyType.TYPE_CAMERA || it == PrivacyType.TYPE_MICROPHONE) &&
                privacyItemController.micCameraAvailable) {
                it
            } else if (it == PrivacyType.TYPE_LOCATION && privacyItemController.locationAvailable) {
                it
            } else {
                null
            }
        }
    }

    /**
     * Filters the list of elements to show.
     *
     * For each privacy type, it'll return all active elements. If there are no active elements,
     * it'll return the most recent access
     */
    private fun filterAndSelect(
        list: List<PrivacyDialogV2.PrivacyElement>
    ): List<PrivacyDialogV2.PrivacyElement> {
        return list.groupBy { it.type }.toSortedMap().flatMap { (_, elements) ->
            val actives = elements.filter { it.active }
            if (actives.isNotEmpty()) {
                actives.sortedByDescending { it.lastActiveTimestamp }
            } else {
                elements.maxByOrNull { it.lastActiveTimestamp }?.let {
                    listOf(it)
                } ?: emptyList()
            }
        }
    }

    /**
     * Interface to create a [PrivacyDialogV2].
     *
     * Can be used to inject a mock creator.
     */
    interface DialogProvider {
        /**
         * Create a [PrivacyDialogV2].
         */
        fun makeDialog(
            context: Context,
            list: List<PrivacyDialogV2.PrivacyElement>,
            starter: (String, Int, CharSequence?, Intent?) -> Unit
        ): PrivacyDialogV2
    }
}
+234 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.privacy

import android.content.Context
import android.content.Intent
import android.graphics.drawable.LayerDrawable
import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowInsets
import android.widget.ImageView
import android.widget.TextView
import com.android.settingslib.Utils
import com.android.systemui.R
import com.android.systemui.statusbar.phone.SystemUIDialog
import java.lang.ref.WeakReference
import java.util.concurrent.atomic.AtomicBoolean

/**
 * Dialog to show ongoing and recent app ops usage.
 *
 * @see PrivacyDialogV2Controller
 * @param context A context to create the dialog
 * @param list list of elements to show in the dialog. The elements will show in the same order they
 *             appear in the list
 * @param activityStarter a callback to start an activity for a given package name, user id, attributionTag and intent
 */
class PrivacyDialogV2(
    context: Context,
    private val list: List<PrivacyElement>,
    activityStarter: (String, Int, CharSequence?, Intent?) -> Unit
) : SystemUIDialog(context, R.style.PrivacyDialog) {

    private val dismissListeners = mutableListOf<WeakReference<OnDialogDismissed>>()
    private val dismissed = AtomicBoolean(false)

    private val iconColorSolid = Utils.getColorAttrDefaultColor(
            this.context, com.android.internal.R.attr.colorPrimary
    )
    private val enterpriseText = " ${context.getString(R.string.ongoing_privacy_dialog_enterprise)}"
    private val phonecall = context.getString(R.string.ongoing_privacy_dialog_phonecall)

    private lateinit var rootView: ViewGroup

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window?.apply {
            attributes.fitInsetsTypes = attributes.fitInsetsTypes or WindowInsets.Type.statusBars()
            attributes.receiveInsetsIgnoringZOrder = true
            setGravity(Gravity.TOP or Gravity.CENTER_HORIZONTAL)
        }
        setTitle(R.string.ongoing_privacy_dialog_a11y_title)
        setContentView(R.layout.privacy_dialog)
        rootView = requireViewById<ViewGroup>(R.id.root)

        list.forEach {
            rootView.addView(createView(it))
        }
    }

    /**
     * Add a listener that will be called when the dialog is dismissed.
     *
     * If the dialog has already been dismissed, the listener will be called immediately, in the
     * same thread.
     */
    fun addOnDismissListener(listener: OnDialogDismissed) {
        if (dismissed.get()) {
            listener.onDialogDismissed()
        } else {
            dismissListeners.add(WeakReference(listener))
        }
    }

    override fun stop() {
        dismissed.set(true)
        val iterator = dismissListeners.iterator()
        while (iterator.hasNext()) {
            val el = iterator.next()
            iterator.remove()
            el.get()?.onDialogDismissed()
        }
    }

    private fun createView(element: PrivacyElement): View {
        val newView = LayoutInflater.from(context).inflate(
                R.layout.privacy_dialog_item, rootView, false
        ) as ViewGroup
        val d = getDrawableForType(element.type)
        d.findDrawableByLayerId(R.id.icon).setTint(iconColorSolid)
        newView.requireViewById<ImageView>(R.id.icon).apply {
            setImageDrawable(d)
            contentDescription = element.type.getName(context)
        }
        val stringId = getStringIdForState(element.active)
        val app = if (element.phoneCall) phonecall else element.applicationName
        val appName = if (element.enterprise) {
            TextUtils.concat(app, enterpriseText)
        } else {
            app
        }
        val firstLine = context.getString(stringId, appName)
        val finalText = getFinalText(firstLine, element.attributionLabel, element.proxyLabel)
        newView.requireViewById<TextView>(R.id.text).text = finalText
        if (element.phoneCall) {
            newView.requireViewById<View>(R.id.chevron).visibility = View.GONE
        }
        newView.apply {
            setTag(element)
            if (!element.phoneCall) {
                setOnClickListener(clickListener)
            }
        }
        return newView
    }

    private fun getFinalText(
        firstLine: CharSequence,
        attributionLabel: CharSequence?,
        proxyLabel: CharSequence?
    ): CharSequence {
        var dialogText: CharSequence? = null
        if (attributionLabel != null && proxyLabel != null) {
            dialogText = context.getString(R.string.ongoing_privacy_dialog_attribution_proxy_label,
                attributionLabel, proxyLabel)
        } else if (attributionLabel != null) {
            dialogText = context.getString(R.string.ongoing_privacy_dialog_attribution_label,
                attributionLabel)
        } else if (proxyLabel != null) {
            dialogText = context.getString(R.string.ongoing_privacy_dialog_attribution_text,
                proxyLabel)
        }
        return if (dialogText != null) TextUtils.concat(firstLine, " ", dialogText) else firstLine
    }

    private fun getStringIdForState(active: Boolean): Int {
        return if (active) {
            R.string.ongoing_privacy_dialog_using_op
        } else {
            R.string.ongoing_privacy_dialog_recent_op
        }
    }

    private fun getDrawableForType(type: PrivacyType): LayerDrawable {
        return context.getDrawable(when (type) {
            PrivacyType.TYPE_LOCATION -> R.drawable.privacy_item_circle_location
            PrivacyType.TYPE_CAMERA -> R.drawable.privacy_item_circle_camera
            PrivacyType.TYPE_MICROPHONE -> R.drawable.privacy_item_circle_microphone
            PrivacyType.TYPE_MEDIA_PROJECTION -> R.drawable.privacy_item_circle_media_projection
        }) as LayerDrawable
    }

    private val clickListener = View.OnClickListener { v ->
        v.tag?.let {
            val element = it as PrivacyElement
            activityStarter(element.packageName, element.userId,
                element.attributionTag, element.navigationIntent)
        }
    }

    /** */
    data class PrivacyElement(
        val type: PrivacyType,
        val packageName: String,
        val userId: Int,
        val applicationName: CharSequence,
        val attributionTag: CharSequence?,
        val attributionLabel: CharSequence?,
        val proxyLabel: CharSequence?,
        val lastActiveTimestamp: Long,
        val active: Boolean,
        val enterprise: Boolean,
        val phoneCall: Boolean,
        val permGroupName: CharSequence,
        val navigationIntent: Intent?
    ) {
        private val builder = StringBuilder("PrivacyElement(")

        init {
            builder.append("type=${type.logName}")
            builder.append(", packageName=$packageName")
            builder.append(", userId=$userId")
            builder.append(", appName=$applicationName")
            if (attributionTag != null) {
                builder.append(", attributionTag=$attributionTag")
            }
            if (attributionLabel != null) {
                builder.append(", attributionLabel=$attributionLabel")
            }
            if (proxyLabel != null) {
                builder.append(", proxyLabel=$proxyLabel")
            }
            builder.append(", lastActive=$lastActiveTimestamp")
            if (active) {
                builder.append(", active")
            }
            if (enterprise) {
                builder.append(", enterprise")
            }
            if (phoneCall) {
                builder.append(", phoneCall")
            }
            builder.append(", permGroupName=$permGroupName)")
            if (navigationIntent != null) {
                builder.append(", navigationIntent=$navigationIntent")
            }
        }

        override fun toString(): String = builder.toString()
    }

    /** */
    interface OnDialogDismissed {
        fun onDialogDismissed()
    }
}
 No newline at end of file
+9 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import com.android.systemui.log.LogBuffer
import com.android.systemui.log.core.LogLevel
import com.android.systemui.log.core.LogMessage
import com.android.systemui.privacy.PrivacyDialog
import com.android.systemui.privacy.PrivacyDialogV2
import com.android.systemui.privacy.PrivacyItem
import java.util.Locale
import javax.inject.Inject
@@ -126,6 +127,14 @@ class PrivacyLogger @Inject constructor(
        })
    }

    fun logShowDialogV2Contents(contents: List<PrivacyDialogV2.PrivacyElement>) {
        log(LogLevel.INFO, {
            str1 = contents.toString()
        }, {
            "Privacy dialog shown. Contents: $str1"
        })
    }

    fun logEmptyDialog() {
        log(LogLevel.WARNING, {}, {
            "Trying to show an empty dialog"
+11 −2
Original line number Diff line number Diff line
@@ -14,10 +14,13 @@ import com.android.internal.logging.UiEventLogger
import com.android.systemui.animation.ActivityLaunchAnimator
import com.android.systemui.appops.AppOpsController
import com.android.systemui.broadcast.BroadcastDispatcher
import com.android.systemui.flags.FeatureFlags
import com.android.systemui.flags.Flags
import com.android.systemui.plugins.ActivityStarter
import com.android.systemui.privacy.OngoingPrivacyChip
import com.android.systemui.privacy.PrivacyChipEvent
import com.android.systemui.privacy.PrivacyDialogController
import com.android.systemui.privacy.PrivacyDialogControllerV2
import com.android.systemui.privacy.PrivacyItem
import com.android.systemui.privacy.PrivacyItemController
import com.android.systemui.privacy.logging.PrivacyLogger
@@ -49,6 +52,7 @@ class HeaderPrivacyIconsController @Inject constructor(
    private val uiEventLogger: UiEventLogger,
    @Named(SHADE_HEADER) private val privacyChip: OngoingPrivacyChip,
    private val privacyDialogController: PrivacyDialogController,
    private val privacyDialogControllerV2: PrivacyDialogControllerV2,
    private val privacyLogger: PrivacyLogger,
    @Named(SHADE_HEADER) private val iconContainer: StatusIconContainer,
    private val permissionManager: PermissionManager,
@@ -58,7 +62,8 @@ class HeaderPrivacyIconsController @Inject constructor(
    private val appOpsController: AppOpsController,
    private val broadcastDispatcher: BroadcastDispatcher,
    private val safetyCenterManager: SafetyCenterManager,
    private val deviceProvisionedController: DeviceProvisionedController
    private val deviceProvisionedController: DeviceProvisionedController,
    private val featureFlags: FeatureFlags
) {

    var chipVisibilityListener: ChipVisibilityListener? = null
@@ -143,7 +148,11 @@ class HeaderPrivacyIconsController @Inject constructor(
            // If the privacy chip is visible, it means there were some indicators
            uiEventLogger.log(PrivacyChipEvent.ONGOING_INDICATORS_CHIP_CLICK)
            if (safetyCenterEnabled) {
                if (featureFlags.isEnabled(Flags.ENABLE_NEW_PRIVACY_DIALOG)) {
                    privacyDialogControllerV2.showDialog(privacyChip.context)
                } else {
                    showSafetyCenter()
                }
            } else {
                privacyDialogController.showDialog(privacyChip.context)
            }
+798 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading