Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ val androidxTestVersion = "1.5.0"
val media3Version = "1.11.0"
val coroutinesVersion = "1.11.0"
val mockitoKotlinVersion = "6.3.0"
val cameraXVersion = "1.6.1"

android {
compileSdk = 37
Expand Down Expand Up @@ -244,6 +245,12 @@ dependencies {
implementation("androidx.lifecycle:lifecycle-common:$lifecycleVersion")
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycleVersion")

implementation("androidx.camera:camera-core:$cameraXVersion")
implementation("androidx.camera:camera-camera2:$cameraXVersion")
implementation("androidx.camera:camera-lifecycle:$cameraXVersion")
implementation("androidx.camera:camera-video:$cameraXVersion")
implementation("androidx.camera:camera-view:$cameraXVersion")

implementation("androidx.biometric:biometric:1.1.0")

implementation("io.reactivex.rxjava2:rxandroid:2.1.1")
Expand Down
60 changes: 60 additions & 0 deletions app/src/main/java/com/nextcloud/talk/chat/CameraLens.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Nextcloud Talk - Android Client
*
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package com.nextcloud.talk.chat

import android.content.Context
import android.util.Log
import androidx.camera.core.Camera
import androidx.camera.core.CameraSelector
import androidx.camera.core.UseCase
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.core.content.ContextCompat
import androidx.lifecycle.LifecycleOwner
import java.util.concurrent.ExecutionException

private const val TAG = "CameraLens"

internal fun cameraSelectorFor(lens: Int): CameraSelector = CameraSelector.Builder().requireLensFacing(lens).build()

internal fun oppositeLens(lens: Int): Int =
if (lens == CameraSelector.LENS_FACING_FRONT) CameraSelector.LENS_FACING_BACK else CameraSelector.LENS_FACING_FRONT

/**
* The lens to bind: [preferred] when the device has it, otherwise the other one.
*/
internal fun resolveLens(provider: ProcessCameraProvider, preferred: Int): Int =
if (provider.hasCamera(cameraSelectorFor(preferred))) preferred else oppositeLens(preferred)

/**
* Calls [onResult] on the main thread with the camera provider, or with null when it is not available.
*/
internal fun whenCameraProviderReady(context: Context, onResult: (ProcessCameraProvider?) -> Unit) {
val providerFuture = ProcessCameraProvider.getInstance(context)
providerFuture.addListener({
val provider = try {
providerFuture.get()
} catch (e: ExecutionException) {
Log.w(TAG, "camera provider is not available", e)
null
}
onResult(provider)
}, ContextCompat.getMainExecutor(context))
}

/**
* Binds [useCases] to the camera with [lens]. Returns null when the camera cannot be bound.
*/
internal fun ProcessCameraProvider.bindSafely(owner: LifecycleOwner, lens: Int, vararg useCases: UseCase): Camera? =
try {
bindToLifecycle(owner, cameraSelectorFor(lens), *useCases)
} catch (e: IllegalArgumentException) {
Log.w(TAG, "cannot bind camera use cases", e)
null
} catch (e: IllegalStateException) {
Log.w(TAG, "cannot bind camera use cases", e)
null
}
Loading