• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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.domain.camera
17 
18 import android.content.ContentResolver
19 import android.net.Uri
20 import android.view.Display
21 import androidx.camera.core.ImageCapture
22 import androidx.camera.core.SurfaceRequest
23 import com.google.jetpackcamera.settings.model.AspectRatio
24 import com.google.jetpackcamera.settings.model.CameraAppSettings
25 import com.google.jetpackcamera.settings.model.CaptureMode
26 import com.google.jetpackcamera.settings.model.DynamicRange
27 import com.google.jetpackcamera.settings.model.FlashMode
28 import com.google.jetpackcamera.settings.model.LensFacing
29 import kotlinx.coroutines.flow.SharedFlow
30 import kotlinx.coroutines.flow.StateFlow
31 
32 /**
33  * Data layer for camera.
34  */
35 interface CameraUseCase {
36     /**
37      * Initializes the camera.
38      *
39      * @return list of available lenses.
40      */
initializenull41     suspend fun initialize(disableVideoCapture: Boolean)
42 
43     /**
44      * Starts the camera.
45      *
46      * This will start to configure the camera, but frames won't stream until a [SurfaceRequest]
47      * from [getSurfaceRequest] has been fulfilled.
48      *
49      * The camera will run until the calling coroutine is cancelled.
50      */
51     suspend fun runCamera()
52 
53     suspend fun takePicture(onCaptureStarted: (() -> Unit) = {})
54 
55     /**
56      * Takes a picture with the camera. If ignoreUri is set to true, the picture taken will be saved
57      * at the default directory for pictures on device. Otherwise, it will be saved at the uri
58      * location if the uri is not null. If it is null, an error will be thrown.
59      */
takePicturenull60     suspend fun takePicture(
61         onCaptureStarted: (() -> Unit) = {},
62         contentResolver: ContentResolver,
63         imageCaptureUri: Uri?,
64         ignoreUri: Boolean = false
65     ): ImageCapture.OutputFileResults
66 
startVideoRecordingnull67     suspend fun startVideoRecording(onVideoRecord: (OnVideoRecordEvent) -> Unit)
68 
69     fun stopVideoRecording()
70 
71     fun setZoomScale(scale: Float)
72 
73     fun getZoomScale(): StateFlow<Float>
74 
75     fun getSurfaceRequest(): StateFlow<SurfaceRequest?>
76 
77     fun getScreenFlashEvents(): SharedFlow<ScreenFlashEvent>
78 
79     fun getCurrentSettings(): StateFlow<CameraAppSettings?>
80 
81     fun setFlashMode(flashMode: FlashMode)
82 
83     fun isScreenFlashEnabled(): Boolean
84 
85     suspend fun setAspectRatio(aspectRatio: AspectRatio)
86 
87     suspend fun setLensFacing(lensFacing: LensFacing)
88 
89     fun tapToFocus(display: Display, surfaceWidth: Int, surfaceHeight: Int, x: Float, y: Float)
90 
91     suspend fun setCaptureMode(captureMode: CaptureMode)
92 
93     suspend fun setDynamicRange(dynamicRange: DynamicRange)
94 
95     /**
96      * Represents the events required for screen flash.
97      */
98     data class ScreenFlashEvent(val type: Type, val onComplete: () -> Unit) {
99         enum class Type {
100             APPLY_UI,
101             CLEAR_UI
102         }
103     }
104 
105     /**
106      * Represents the events for video recording.
107      */
108     sealed interface OnVideoRecordEvent {
109         object OnVideoRecorded : OnVideoRecordEvent
110 
111         data class OnVideoRecordStatus(val audioAmplitude: Double) : OnVideoRecordEvent
112 
113         object OnVideoRecordError : OnVideoRecordEvent
114     }
115 }
116