1 /** 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * ``` 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * ``` 10 * 11 * Unless required by applicable law or agreed to in writing, software distributed under the License 12 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 13 * or implied. See the License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 package com.android.healthconnect.controller.permissions.data 17 18 /** Pair of {@link HealthPermission} and {@link PermissionsAccessType}. */ 19 data class HealthPermission( 20 val healthPermissionType: HealthPermissionType, 21 val permissionsAccessType: PermissionsAccessType 22 ) { 23 companion object { 24 private const val READ_PERMISSION_PREFIX = "android.permission.health.READ_" 25 private const val WRITE_PERMISSION_PREFIX = "android.permission.health.WRITE_" 26 fromPermissionStringnull27 fun fromPermissionString(permission: String): HealthPermission { 28 return if (permission.startsWith(READ_PERMISSION_PREFIX)) { 29 val type = 30 getHealthPermissionType(permission.substring(READ_PERMISSION_PREFIX.length)) 31 HealthPermission(type, PermissionsAccessType.READ) 32 } else if (permission.startsWith(WRITE_PERMISSION_PREFIX)) { 33 val type = 34 getHealthPermissionType(permission.substring(WRITE_PERMISSION_PREFIX.length)) 35 HealthPermission(type, PermissionsAccessType.WRITE) 36 } else { 37 throw IllegalArgumentException("Permission not supported! $permission") 38 } 39 } 40 getHealthPermissionTypenull41 private fun getHealthPermissionType(value: String): HealthPermissionType { 42 return HealthPermissionType.valueOf(value) 43 } 44 } 45 toStringnull46 override fun toString(): String { 47 return if (permissionsAccessType == PermissionsAccessType.READ) { 48 "$READ_PERMISSION_PREFIX${healthPermissionType.name}" 49 } else { 50 "$WRITE_PERMISSION_PREFIX${healthPermissionType.name}" 51 } 52 } 53 } 54