summaryrefslogtreecommitdiff
path: root/PermissionController/src/com/android/permissioncontroller/permission/ui/model/grantPermissions/StorageGrantBehavior.kt
blob: a690f5f598175ba6a01ce1180082934bc6365bad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Copyright (C) 2023 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.permissioncontroller.permission.ui.model.grantPermissions

import android.Manifest.permission.ACCESS_MEDIA_LOCATION
import android.Manifest.permission.READ_MEDIA_VISUAL_USER_SELECTED
import android.Manifest.permission_group.READ_MEDIA_VISUAL
import android.Manifest.permission_group.STORAGE
import android.os.Build
import com.android.modules.utils.build.SdkLevel
import com.android.permissioncontroller.permission.model.livedatatypes.LightAppPermGroup
import com.android.permissioncontroller.permission.ui.model.DenyButton
import com.android.permissioncontroller.permission.ui.model.Prompt
import com.android.permissioncontroller.permission.utils.KotlinUtils.isPhotoPickerPromptEnabled
import com.android.permissioncontroller.permission.utils.KotlinUtils.isPhotoPickerPromptSupported

/**
 * Storage split from one group (STORAGE) into two (READ_MEDIA_VISUAL and READ_MEDIA_AURAL) in T.
 * There are special dialogs to deal with a pre-T app requesting STORAGE on a post-T device. In U,
 * the READ_MEDIA_VISUAL group was augmented with the option to select photos, which led to several
 * new dialogs to handle that.
 */
object StorageGrantBehavior : GrantBehavior() {
    override fun getPrompt(
        group: LightAppPermGroup,
        requestedPerms: Set<String>,
        isSystemTriggeredPrompt: Boolean
    ): Prompt {
        val appSupportsSplitStoragePermissions = appSupportsSplitStoragePermissions(group)
        if (!SdkLevel.isAtLeastT()) {
            return Prompt.BASIC
        } else if (appSupportsSplitStoragePermissions && group.permGroupName == STORAGE) {
            return Prompt.NO_UI_REJECT_THIS_GROUP
        }

        if (appSupportsSplitStoragePermissions && !shouldShowPhotoPickerPromptForApp(group)) {
            return Prompt.BASIC
        }

        if (!appSupportsSplitStoragePermissions) {
            if (group.packageInfo.targetSdkVersion < Build.VERSION_CODES.Q) {
                return Prompt.STORAGE_SUPERGROUP_PRE_Q
            } else {
                return Prompt.STORAGE_SUPERGROUP_Q_TO_S
            }
        }

        // Else, app supports the new photo picker dialog
        if (requestedPerms.all { it in getPartialGrantPermissions(group) }) {
            // Do not allow apps to request only READ_MEDIA_VISUAL_USER_SELECTED
            return Prompt.NO_UI_REJECT_THIS_GROUP
        }

        val userSelectedPerm = group.permissions[READ_MEDIA_VISUAL_USER_SELECTED]
        if (userSelectedPerm?.isUserFixed == true && userSelectedPerm.isGrantedIncludingAppOp) {
            return Prompt.NO_UI_PHOTO_PICKER_REDIRECT
        }

        if (userSelectedPerm?.isGrantedIncludingAppOp == true) {
            return Prompt.SELECT_MORE_PHOTOS
        } else {
            return Prompt.SELECT_PHOTOS
        }
    }

    override fun getDenyButton(
        group: LightAppPermGroup,
        requestedPerms: Set<String>,
        prompt: Prompt
    ): DenyButton {
        if (prompt == Prompt.SELECT_MORE_PHOTOS) {
            return DenyButton.DONT_SELECT_MORE
        }
        return BasicGrantBehavior.getDenyButton(group, requestedPerms, prompt)
    }

    override fun isGroupFullyGranted(
        group: LightAppPermGroup,
        requestedPerms: Set<String>
    ): Boolean {
        if (!isPhotoPickerPromptSupported() || group.permGroupName != READ_MEDIA_VISUAL) {
            return super.isGroupFullyGranted(group, requestedPerms)
        }

        return group.permissions.values.any {
            it.name !in getPartialGrantPermissions(group) && it.isGrantedIncludingAppOp
        }
    }

    override fun isPermissionFixed(group: LightAppPermGroup, perm: String): Boolean {
        val userSelectedPerm = group.permissions[READ_MEDIA_VISUAL_USER_SELECTED]
        if (
            userSelectedPerm != null &&
                userSelectedPerm.isGrantedIncludingAppOp &&
                userSelectedPerm.isUserFixed
        ) {
            // If the user selected permission is fixed and granted, we immediately show the
            // photo picker, rather than filtering
            return false
        }
        return super.isPermissionFixed(group, perm)
    }

    private fun appSupportsSplitStoragePermissions(group: LightAppPermGroup) =
        SdkLevel.isAtLeastT() && group.packageInfo.targetSdkVersion >= Build.VERSION_CODES.TIRAMISU

    private fun shouldShowPhotoPickerPromptForApp(group: LightAppPermGroup) =
        isPhotoPickerPromptEnabled() &&
            group.permGroupName == READ_MEDIA_VISUAL &&
            (group.packageInfo.targetSdkVersion >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE ||
                appSupportsPhotoPicker(group))

    private fun appSupportsPhotoPicker(group: LightAppPermGroup) =
        group.packageInfo.targetSdkVersion >= Build.VERSION_CODES.TIRAMISU &&
            group.permissions[READ_MEDIA_VISUAL_USER_SELECTED]?.isImplicit == false

    private fun getPartialGrantPermissions(group: LightAppPermGroup): Set<String> {
        return if (appSupportsPhotoPicker(group) && shouldShowPhotoPickerPromptForApp(group)) {
            setOf(READ_MEDIA_VISUAL_USER_SELECTED, ACCESS_MEDIA_LOCATION)
        } else {
            setOf(READ_MEDIA_VISUAL_USER_SELECTED)
        }
    }
}