Skip to content
This repository has been archived by the owner on Mar 8, 2024. It is now read-only.

Commit

Permalink
#4. Changed the selectCamera() method to return optional. Null indica…
Browse files Browse the repository at this point in the history
…ting that there were no cameras in the id list. This is then handled within the start method, by calling the listener with an error. Added a test to accompany this change
  • Loading branch information
alistairsykes committed Mar 21, 2019
1 parent 5dd029b commit e606295
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Expand Up @@ -42,6 +42,12 @@ internal class Camera2Source(
if (cameraDevice != null) return

val cameraId = selectCamera()
if (cameraId == null) {
val exception = CameraServiceException()
Timber.e(exception, "Error opening camera - No cameraId available")
listener?.onCameraFailure(exception)
return
}
cameraManager.openCamera(cameraId, object : CameraDevice.StateCallback() {
override fun onOpened(camera: CameraDevice) {
cameraDevice = camera
Expand Down Expand Up @@ -72,7 +78,8 @@ internal class Camera2Source(
val characteristics = if (cameraDevice != null) {
cameraManager.getCameraCharacteristics(cameraDevice.id)
} else {
cameraManager.getCameraCharacteristics(selectCamera())
val cameraId = selectCamera() ?: return null
cameraManager.getCameraCharacteristics(cameraId)
}
val configs = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
?: throw IllegalStateException() // This key is available on all devices
Expand All @@ -92,8 +99,11 @@ internal class Camera2Source(
return characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION)
}

@Suppress("ReturnCount") // Better readability
@VisibleForTesting
internal fun selectCamera(): String {
internal fun selectCamera(): String? {
if (cameraManager.cameraIdList.isEmpty()) return null

for (cameraId in cameraManager.cameraIdList) {
val characteristics = cameraManager.getCameraCharacteristics(cameraId)
if (characteristics.get(CameraCharacteristics.LENS_FACING) == requestedFacing) {
Expand Down
Expand Up @@ -13,6 +13,7 @@ import android.view.Surface
import androidx.test.filters.SmallTest
import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.anyOrNull
import com.nhaarman.mockitokotlin2.argumentCaptor
import com.nhaarman.mockitokotlin2.doNothing
import com.nhaarman.mockitokotlin2.doReturn
import com.nhaarman.mockitokotlin2.eq
Expand Down Expand Up @@ -97,6 +98,21 @@ internal class Camera2SourceTest {
)
}

@Test
fun noCameraId__start__error() {
// GIVEN
doReturn(null).whenever(cameraSource).selectCamera()

// WHEN
val listener = mock<OnCameraReadyListener>()
cameraSource.start(mock(), listener)

// THEN
val captor = argumentCaptor<CameraException>()
verify(listener).onCameraFailure(captor.capture())
assertTrue(captor.firstValue is CameraServiceException)
}

@Test
fun cameraId__start__opensCamera() {
// GIVEN
Expand Down

0 comments on commit e606295

Please sign in to comment.