| 
									
										
										
										
											2021-02-19 20:41:49 +01:00
										 |  |  | package com.mrousavy.camera | 
					
						
							| 
									
										
										
										
											2021-02-19 16:28:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | import com.facebook.react.bridge.ReadableMap | 
					
						
							|  |  |  | import com.facebook.react.common.MapBuilder | 
					
						
							|  |  |  | import com.facebook.react.uimanager.ThemedReactContext | 
					
						
							| 
									
										
										
										
											2023-08-21 12:50:14 +02:00
										 |  |  | import com.facebook.react.uimanager.ViewGroupManager | 
					
						
							| 
									
										
										
										
											2021-02-19 16:28:14 +01:00
										 |  |  | import com.facebook.react.uimanager.annotations.ReactProp | 
					
						
							| 
									
										
										
										
											2023-08-21 12:50:14 +02:00
										 |  |  | import com.mrousavy.camera.parsers.Orientation | 
					
						
							| 
									
										
										
										
											2023-08-21 14:43:12 +02:00
										 |  |  | import com.mrousavy.camera.parsers.PixelFormat | 
					
						
							| 
									
										
										
										
											2023-08-21 12:50:14 +02:00
										 |  |  | import com.mrousavy.camera.parsers.Torch | 
					
						
							|  |  |  | import com.mrousavy.camera.parsers.VideoStabilizationMode | 
					
						
							| 
									
										
										
										
											2021-08-25 11:33:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | @Suppress("unused") | 
					
						
							| 
									
										
										
										
											2023-08-21 14:43:12 +02:00
										 |  |  | class CameraViewManager : ViewGroupManager<CameraView>() { | 
					
						
							| 
									
										
										
										
											2021-08-25 11:33:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   public override fun createViewInstance(context: ThemedReactContext): CameraView { | 
					
						
							| 
									
										
										
										
											2023-08-21 12:50:14 +02:00
										 |  |  |     return CameraView(context) | 
					
						
							| 
									
										
										
										
											2021-08-25 11:33:57 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   override fun onAfterUpdateTransaction(view: CameraView) { | 
					
						
							|  |  |  |     super.onAfterUpdateTransaction(view) | 
					
						
							|  |  |  |     val changedProps = cameraViewTransactions[view] ?: ArrayList() | 
					
						
							|  |  |  |     view.update(changedProps) | 
					
						
							|  |  |  |     cameraViewTransactions.remove(view) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   override fun getExportedCustomDirectEventTypeConstants(): MutableMap<String, Any>? { | 
					
						
							|  |  |  |     return MapBuilder.builder<String, Any>() | 
					
						
							| 
									
										
										
										
											2021-10-11 18:27:23 +02:00
										 |  |  |       .put("cameraViewReady", MapBuilder.of("registrationName", "onViewReady")) | 
					
						
							| 
									
										
										
										
											2021-08-25 11:33:57 +02:00
										 |  |  |       .put("cameraInitialized", MapBuilder.of("registrationName", "onInitialized")) | 
					
						
							|  |  |  |       .put("cameraError", MapBuilder.of("registrationName", "onError")) | 
					
						
							|  |  |  |       .build() | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   override fun getName(): String { | 
					
						
							|  |  |  |     return TAG | 
					
						
							| 
									
										
										
										
											2021-02-26 10:56:20 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @ReactProp(name = "cameraId") | 
					
						
							|  |  |  |   fun setCameraId(view: CameraView, cameraId: String) { | 
					
						
							|  |  |  |     if (view.cameraId != cameraId) | 
					
						
							|  |  |  |       addChangedPropToTransaction(view, "cameraId") | 
					
						
							|  |  |  |     view.cameraId = cameraId | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-07 13:08:40 +02:00
										 |  |  |   @ReactProp(name = "photo") | 
					
						
							|  |  |  |   fun setPhoto(view: CameraView, photo: Boolean?) { | 
					
						
							|  |  |  |     if (view.photo != photo) | 
					
						
							|  |  |  |       addChangedPropToTransaction(view, "photo") | 
					
						
							|  |  |  |     view.photo = photo | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @ReactProp(name = "video") | 
					
						
							|  |  |  |   fun setVideo(view: CameraView, video: Boolean?) { | 
					
						
							|  |  |  |     if (view.video != video) | 
					
						
							|  |  |  |       addChangedPropToTransaction(view, "video") | 
					
						
							|  |  |  |     view.video = video | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @ReactProp(name = "audio") | 
					
						
							|  |  |  |   fun setAudio(view: CameraView, audio: Boolean?) { | 
					
						
							|  |  |  |     if (view.audio != audio) | 
					
						
							|  |  |  |       addChangedPropToTransaction(view, "audio") | 
					
						
							|  |  |  |     view.audio = audio | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-12 15:16:03 +02:00
										 |  |  |   @ReactProp(name = "enableFrameProcessor") | 
					
						
							|  |  |  |   fun setEnableFrameProcessor(view: CameraView, enableFrameProcessor: Boolean) { | 
					
						
							|  |  |  |     if (view.enableFrameProcessor != enableFrameProcessor) | 
					
						
							|  |  |  |       addChangedPropToTransaction(view, "enableFrameProcessor") | 
					
						
							|  |  |  |     view.enableFrameProcessor = enableFrameProcessor | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-21 12:50:14 +02:00
										 |  |  |   @ReactProp(name = "pixelFormat") | 
					
						
							|  |  |  |   fun setPixelFormat(view: CameraView, pixelFormat: String?) { | 
					
						
							|  |  |  |     val newPixelFormat = PixelFormat.fromUnionValue(pixelFormat) | 
					
						
							|  |  |  |     if (view.pixelFormat != newPixelFormat) | 
					
						
							|  |  |  |       addChangedPropToTransaction(view, "pixelFormat") | 
					
						
							|  |  |  |     view.pixelFormat = newPixelFormat ?: PixelFormat.NATIVE | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-26 10:56:20 +01:00
										 |  |  |   @ReactProp(name = "enableDepthData") | 
					
						
							|  |  |  |   fun setEnableDepthData(view: CameraView, enableDepthData: Boolean) { | 
					
						
							|  |  |  |     if (view.enableDepthData != enableDepthData) | 
					
						
							|  |  |  |       addChangedPropToTransaction(view, "enableDepthData") | 
					
						
							|  |  |  |     view.enableDepthData = enableDepthData | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-23 15:39:24 +02:00
										 |  |  |   @ReactProp(name = "enableZoomGesture") | 
					
						
							|  |  |  |   fun setEnableZoomGesture(view: CameraView, enableZoomGesture: Boolean) { | 
					
						
							|  |  |  |     if (view.enableZoomGesture != enableZoomGesture) | 
					
						
							|  |  |  |       addChangedPropToTransaction(view, "enableZoomGesture") | 
					
						
							|  |  |  |     view.enableZoomGesture = enableZoomGesture | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-21 12:50:14 +02:00
										 |  |  |   @ReactProp(name = "videoStabilizationMode") | 
					
						
							|  |  |  |   fun setVideoStabilizationMode(view: CameraView, videoStabilizationMode: String?) { | 
					
						
							|  |  |  |     val newMode = VideoStabilizationMode.fromUnionValue(videoStabilizationMode) | 
					
						
							|  |  |  |     if (view.videoStabilizationMode != newMode) | 
					
						
							|  |  |  |       addChangedPropToTransaction(view, "videoStabilizationMode") | 
					
						
							|  |  |  |     view.videoStabilizationMode = newMode | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-10 13:49:34 +02:00
										 |  |  |   @ReactProp(name = "enableHighQualityPhotos") | 
					
						
							|  |  |  |   fun setEnableHighQualityPhotos(view: CameraView, enableHighQualityPhotos: Boolean?) { | 
					
						
							|  |  |  |     if (view.enableHighQualityPhotos != enableHighQualityPhotos) | 
					
						
							|  |  |  |       addChangedPropToTransaction(view, "enableHighQualityPhotos") | 
					
						
							|  |  |  |     view.enableHighQualityPhotos = enableHighQualityPhotos | 
					
						
							| 
									
										
										
										
											2021-02-26 10:56:20 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @ReactProp(name = "enablePortraitEffectsMatteDelivery") | 
					
						
							|  |  |  |   fun setEnablePortraitEffectsMatteDelivery(view: CameraView, enablePortraitEffectsMatteDelivery: Boolean) { | 
					
						
							|  |  |  |     if (view.enablePortraitEffectsMatteDelivery != enablePortraitEffectsMatteDelivery) | 
					
						
							|  |  |  |       addChangedPropToTransaction(view, "enablePortraitEffectsMatteDelivery") | 
					
						
							|  |  |  |     view.enablePortraitEffectsMatteDelivery = enablePortraitEffectsMatteDelivery | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @ReactProp(name = "format") | 
					
						
							|  |  |  |   fun setFormat(view: CameraView, format: ReadableMap?) { | 
					
						
							|  |  |  |     if (view.format != format) | 
					
						
							|  |  |  |       addChangedPropToTransaction(view, "format") | 
					
						
							|  |  |  |     view.format = format | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-25 11:33:57 +02:00
										 |  |  |   // TODO: Change when TurboModules release.
 | 
					
						
							| 
									
										
										
										
											2021-02-26 10:56:20 +01:00
										 |  |  |   // We're treating -1 as "null" here, because when I make the fps parameter
 | 
					
						
							|  |  |  |   // of type "Int?" the react bridge throws an error.
 | 
					
						
							|  |  |  |   @ReactProp(name = "fps", defaultInt = -1) | 
					
						
							|  |  |  |   fun setFps(view: CameraView, fps: Int) { | 
					
						
							|  |  |  |     if (view.fps != fps) | 
					
						
							|  |  |  |       addChangedPropToTransaction(view, "fps") | 
					
						
							|  |  |  |     view.fps = if (fps > 0) fps else null | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @ReactProp(name = "hdr") | 
					
						
							|  |  |  |   fun setHdr(view: CameraView, hdr: Boolean?) { | 
					
						
							|  |  |  |     if (view.hdr != hdr) | 
					
						
							|  |  |  |       addChangedPropToTransaction(view, "hdr") | 
					
						
							|  |  |  |     view.hdr = hdr | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @ReactProp(name = "lowLightBoost") | 
					
						
							|  |  |  |   fun setLowLightBoost(view: CameraView, lowLightBoost: Boolean?) { | 
					
						
							|  |  |  |     if (view.lowLightBoost != lowLightBoost) | 
					
						
							|  |  |  |       addChangedPropToTransaction(view, "lowLightBoost") | 
					
						
							|  |  |  |     view.lowLightBoost = lowLightBoost | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @ReactProp(name = "isActive") | 
					
						
							|  |  |  |   fun setIsActive(view: CameraView, isActive: Boolean) { | 
					
						
							|  |  |  |     if (view.isActive != isActive) | 
					
						
							|  |  |  |       addChangedPropToTransaction(view, "isActive") | 
					
						
							|  |  |  |     view.isActive = isActive | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @ReactProp(name = "torch") | 
					
						
							|  |  |  |   fun setTorch(view: CameraView, torch: String) { | 
					
						
							| 
									
										
										
										
											2023-08-21 12:50:14 +02:00
										 |  |  |     val newMode = Torch.fromUnionValue(torch) | 
					
						
							|  |  |  |     if (view.torch != newMode) | 
					
						
							| 
									
										
										
										
											2021-02-26 10:56:20 +01:00
										 |  |  |       addChangedPropToTransaction(view, "torch") | 
					
						
							| 
									
										
										
										
											2023-08-21 12:50:14 +02:00
										 |  |  |     view.torch = newMode | 
					
						
							| 
									
										
										
										
											2021-02-26 10:56:20 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @ReactProp(name = "zoom") | 
					
						
							|  |  |  |   fun setZoom(view: CameraView, zoom: Double) { | 
					
						
							| 
									
										
										
										
											2021-07-29 11:44:22 +02:00
										 |  |  |     val zoomFloat = zoom.toFloat() | 
					
						
							|  |  |  |     if (view.zoom != zoomFloat) | 
					
						
							| 
									
										
										
										
											2021-02-26 10:56:20 +01:00
										 |  |  |       addChangedPropToTransaction(view, "zoom") | 
					
						
							| 
									
										
										
										
											2021-07-29 11:44:22 +02:00
										 |  |  |     view.zoom = zoomFloat | 
					
						
							| 
									
										
										
										
											2021-02-26 10:56:20 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 16:57:40 +01:00
										 |  |  |   @ReactProp(name = "orientation") | 
					
						
							| 
									
										
										
										
											2023-08-21 12:50:14 +02:00
										 |  |  |   fun setOrientation(view: CameraView, orientation: String?) { | 
					
						
							|  |  |  |     val newMode = Orientation.fromUnionValue(orientation) | 
					
						
							|  |  |  |     if (view.orientation != newMode) | 
					
						
							| 
									
										
										
										
											2022-01-04 16:57:40 +01:00
										 |  |  |       addChangedPropToTransaction(view, "orientation") | 
					
						
							| 
									
										
										
										
											2023-08-21 12:50:14 +02:00
										 |  |  |     view.orientation = newMode | 
					
						
							| 
									
										
										
										
											2022-01-04 16:57:40 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-26 10:56:20 +01:00
										 |  |  |   companion object { | 
					
						
							| 
									
										
										
										
											2021-07-07 15:00:32 +02:00
										 |  |  |     const val TAG = "CameraView" | 
					
						
							| 
									
										
										
										
											2021-02-26 10:56:20 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     val cameraViewTransactions: HashMap<CameraView, ArrayList<String>> = HashMap() | 
					
						
							| 
									
										
										
										
											2021-08-25 11:33:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private fun addChangedPropToTransaction(view: CameraView, changedProp: String) { | 
					
						
							|  |  |  |       if (cameraViewTransactions[view] == null) { | 
					
						
							|  |  |  |         cameraViewTransactions[view] = ArrayList() | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       cameraViewTransactions[view]!!.add(changedProp) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-02-26 10:56:20 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-02-19 16:28:14 +01:00
										 |  |  | } |