xref: /aosp_15_r20/cts/tests/tests/security/CameraPermissionTestApp/src/Utils.kt (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
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 android.security.cts.camera.open
18 
19 import android.content.Intent
20 import android.security.cts.camera.open.lib.IntentKeys
21 import android.util.Log
22 import kotlin.coroutines.resume
23 import kotlinx.coroutines.CancellableContinuation
24 
25 private val TAG = OpenCameraActivity::class.java.simpleName
26 
tryOrResumenull27 fun CancellableContinuation<Intent>.tryOrResume(
28     keys: IntentKeys,
29     result: Intent,
30     method: String,
31     callback: () -> Unit
32 ) {
33   try {
34     callback()
35   } catch (e: Exception) {
36     Log.e(TAG, "${method}: Received exception: ${e.exceptionString}")
37     if (isActive) {
38       resume(result.apply { putException(keys, e) })
39     }
40   }
41 }
42 
putExceptionnull43 fun Intent.putException(keys: IntentKeys, e: Exception) {
44   if (!hasExtra(keys.exception)) {
45     putExtra(keys.exception, e.exceptionString)
46   }
47 }
48 
49 val Exception.exceptionString: String
50   get() = "${this::class.java.simpleName}/${message}"
51