Skip to content

Commit 57642aa

Browse files
author
farfromrefug
committed
chore(android): refactor
1 parent 3c7406c commit 57642aa

File tree

1 file changed

+33
-23
lines changed
  • packages/ui-cameraview/platforms/android/java/com/nativescript/cameraview

1 file changed

+33
-23
lines changed

packages/ui-cameraview/platforms/android/java/com/nativescript/cameraview/CameraView.kt

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
7575

7676
// private var aspectRatioStrategy: AspectRatioStrategy? =
7777
// AspectRatioStrategy(AspectRatio.RATIO_4_3, AspectRatioStrategy.FALLBACK_RULE_AUTO)
78-
private var cameraProviderFuture: ListenableFuture<ProcessCameraProvider>
7978
private var cameraProvider: ProcessCameraProvider? = null
8079
private var extensionsManager: ExtensionsManager? = null
8180
private var imageCapture: ImageCapture? = null
@@ -88,6 +87,7 @@ constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
8887
private var preview: Preview? = null
8988
private var previewView: PreviewView = PreviewView(context, attrs, defStyleAttr)
9089
private var isStarted = false
90+
private var bindindProvider = false
9191
private var isRecording = false
9292
private var file: File? = null
9393
private var isForceStopping = false
@@ -416,26 +416,6 @@ constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
416416
}
417417
addView(previewView)
418418
// previewView.controller = cameraController
419-
420-
// TODO: Bind this to the view's onCreate method
421-
cameraProviderFuture = ProcessCameraProvider.getInstance(context)
422-
cameraProviderFuture.addListener(
423-
{
424-
try {
425-
cameraProvider?.unbindAll()
426-
cameraProvider = cameraProviderFuture.get()
427-
extensionsManager =
428-
ExtensionsManager.getInstanceAsync(context, cameraProvider!!).get()
429-
safeUnbindAll()
430-
refreshCamera() // or just initPreview() ?
431-
} catch (e: Exception) {
432-
e.printStackTrace()
433-
listener?.onCameraError("Failed to get camera", e)
434-
isStarted = false
435-
}
436-
},
437-
ContextCompat.getMainExecutor(context)
438-
)
439419
}
440420

441421
fun focusAtPoint(x: Float, y: Float) {
@@ -937,10 +917,40 @@ constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
937917

938918
@SuppressLint("RestrictedApi", "UnsafeOptInUsageError")
939919
fun refreshCamera() {
940-
if (pause || !hasCameraPermission() || cameraProvider == null) {
920+
if (pause || !hasCameraPermission() || bindindProvider) {
921+
return
922+
}
923+
924+
if (cameraProvider == null) {
925+
bindindProvider = true
926+
val cameraProviderFuture = ProcessCameraProvider.getInstance(context)
927+
cameraProviderFuture.addListener(
928+
{
929+
try {
930+
cameraProvider = cameraProviderFuture.get()
931+
val extensionsManagerFuture =
932+
ExtensionsManager.getInstanceAsync(context, cameraProvider!!)
933+
extensionsManagerFuture.addListener({
934+
try {
935+
extensionsManager = extensionsManagerFuture.get()
936+
bindindProvider = false
937+
refreshCamera()
938+
} catch (e: Exception) {
939+
e.printStackTrace()
940+
listener?.onCameraError("Failed to get camera", e)
941+
isStarted = false
942+
}
943+
}, ContextCompat.getMainExecutor(context))
944+
} catch (e: Exception) {
945+
e.printStackTrace()
946+
listener?.onCameraError("Failed to get camera", e)
947+
isStarted = false
948+
}
949+
},
950+
ContextCompat.getMainExecutor(context)
951+
)
941952
return
942953
}
943-
if (!hasCameraPermission()) return
944954
cachedPictureRatioSizeMap.clear()
945955
// cachedPreviewRatioSizeMap.clear()
946956

0 commit comments

Comments
 (0)