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