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 17 package com.android.systemui 18 19 import android.content.Context 20 import android.graphics.Path 21 import android.graphics.Rect 22 import android.graphics.RectF 23 import android.util.PathParser 24 import com.android.systemui.res.R 25 import dagger.assisted.Assisted 26 import dagger.assisted.AssistedFactory 27 import dagger.assisted.AssistedInject 28 import kotlin.math.roundToInt 29 30 interface CameraProtectionLoader { loadCameraProtectionInfoListnull31 fun loadCameraProtectionInfoList(): List<CameraProtectionInfo> 32 } 33 34 class CameraProtectionLoaderImpl 35 @AssistedInject 36 constructor(@Assisted private val context: Context) : CameraProtectionLoader { 37 38 override fun loadCameraProtectionInfoList(): List<CameraProtectionInfo> { 39 val list = mutableListOf<CameraProtectionInfo>() 40 val front = 41 loadCameraProtectionInfo( 42 R.string.config_protectedCameraId, 43 R.string.config_protectedPhysicalCameraId, 44 R.string.config_frontBuiltInDisplayCutoutProtection, 45 R.string.config_protectedScreenUniqueId, 46 ) 47 if (front != null) { 48 list.add(front) 49 } 50 val inner = 51 loadCameraProtectionInfo( 52 R.string.config_protectedInnerCameraId, 53 R.string.config_protectedInnerPhysicalCameraId, 54 R.string.config_innerBuiltInDisplayCutoutProtection, 55 R.string.config_protectedInnerScreenUniqueId, 56 ) 57 if (inner != null) { 58 list.add(inner) 59 } 60 return list 61 } 62 63 private fun loadCameraProtectionInfo( 64 cameraIdRes: Int, 65 physicalCameraIdRes: Int, 66 pathRes: Int, 67 displayUniqueIdRes: Int, 68 ): CameraProtectionInfo? { 69 val logicalCameraId = context.getString(cameraIdRes) 70 if (logicalCameraId.isNullOrEmpty()) { 71 return null 72 } 73 val physicalCameraId = context.getString(physicalCameraIdRes) 74 val protectionPath = pathFromString(context.getString(pathRes)) 75 val computed = RectF() 76 protectionPath.computeBounds(computed) 77 val protectionBounds = 78 Rect( 79 computed.left.roundToInt(), 80 computed.top.roundToInt(), 81 computed.right.roundToInt(), 82 computed.bottom.roundToInt(), 83 ) 84 val displayUniqueId = context.getString(displayUniqueIdRes) 85 return CameraProtectionInfo( 86 logicalCameraId, 87 physicalCameraId, 88 protectionPath, 89 protectionBounds, 90 displayUniqueId, 91 ) 92 } 93 94 private fun pathFromString(pathString: String): Path { 95 return try { 96 PathParser.createPathFromPathData(pathString.trim()) 97 } catch (e: Throwable) { 98 throw IllegalArgumentException("Invalid protection path", e) 99 } 100 } 101 102 @AssistedFactory 103 interface Factory { 104 fun create(context: Context): CameraProtectionLoaderImpl 105 } 106 } 107