1 /* 2 * Copyright (C) 2024 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 package com.google.jetpackcamera.permissions 17 18 import androidx.annotation.DrawableRes 19 import androidx.annotation.StringRes 20 import androidx.compose.material.icons.Icons 21 import androidx.compose.material.icons.outlined.CameraAlt 22 import androidx.compose.material.icons.outlined.Mic 23 import androidx.compose.runtime.Composable 24 import androidx.compose.ui.graphics.painter.Painter 25 import androidx.compose.ui.graphics.vector.ImageVector 26 import androidx.compose.ui.graphics.vector.rememberVectorPainter 27 import androidx.compose.ui.res.painterResource 28 import com.google.jetpackcamera.permissions.ui.CAMERA_PERMISSION_BUTTON 29 import com.google.jetpackcamera.permissions.ui.RECORD_AUDIO_PERMISSION_BUTTON 30 31 const val CAMERA_PERMISSION = "android.permission.CAMERA" 32 const val AUDIO_RECORD_PERMISSION = "android.permission.RECORD_AUDIO" 33 34 /** 35 * Helper class storing a permission's relevant UI information 36 */ 37 sealed interface PermissionInfoProvider { 38 @Composable getPainternull39 fun getPainter(): Painter { 40 val iconResId = getDrawableResId() 41 val iconVector = getImageVector() 42 require((iconResId == null).xor(iconVector == null)) { 43 "UI Item should have exactly one of iconResId or iconVector set." 44 } 45 return iconResId?.let { painterResource(it) } 46 ?: iconVector?.let { 47 rememberVectorPainter( 48 it 49 ) 50 }!! // !! allowed because we've checked null 51 } 52 53 /** 54 * @return the String reference for the permission 55 */ getPermissionnull56 fun getPermission(): String 57 58 fun isOptional(): Boolean 59 60 fun getTestTag(): String 61 62 @DrawableRes 63 fun getDrawableResId(): Int? 64 65 fun getImageVector(): ImageVector? 66 67 @StringRes 68 fun getPermissionTitleResId(): Int 69 70 @StringRes 71 fun getPermissionBodyTextResId(): Int 72 73 @StringRes 74 fun getRationaleBodyTextResId(): Int? 75 76 @StringRes 77 fun getIconAccessibilityTextResId(): Int 78 } 79 80 /** 81 * Implementation of [PermissionInfoProvider] 82 * Supplies the information needed for a permission's UI screen 83 */ 84 enum class PermissionEnum : PermissionInfoProvider { 85 86 CAMERA { 87 override fun getPermission(): String = CAMERA_PERMISSION 88 89 override fun isOptional(): Boolean = false 90 91 override fun getTestTag(): String = CAMERA_PERMISSION_BUTTON 92 93 override fun getDrawableResId(): Int? = null 94 95 override fun getImageVector(): ImageVector = Icons.Outlined.CameraAlt 96 97 override fun getPermissionTitleResId(): Int = R.string.camera_permission_screen_title 98 99 override fun getPermissionBodyTextResId(): Int = 100 R.string.camera_permission_required_rationale 101 102 override fun getRationaleBodyTextResId(): Int = 103 R.string.camera_permission_declined_rationale 104 105 override fun getIconAccessibilityTextResId(): Int = 106 R.string.camera_permission_accessibility_text 107 }, 108 109 RECORD_AUDIO { 110 override fun getPermission(): String = AUDIO_RECORD_PERMISSION 111 112 override fun isOptional(): Boolean = true 113 114 override fun getTestTag(): String = RECORD_AUDIO_PERMISSION_BUTTON 115 116 override fun getDrawableResId(): Int? = null 117 118 override fun getImageVector(): ImageVector = Icons.Outlined.Mic 119 120 override fun getPermissionTitleResId(): Int = R.string.microphone_permission_screen_title 121 122 override fun getPermissionBodyTextResId(): Int = 123 R.string.microphone_permission_required_rationale 124 125 override fun getRationaleBodyTextResId(): Int? = null 126 127 override fun getIconAccessibilityTextResId(): Int = 128 R.string.microphone_permission_accessibility_text 129 } 130 } 131