I have some code for a camera app that allows the user to toggle some flash options: OFF, ON, TORCH, AUTO, and RED_EYE. When the user swaps the mode I have the following switch statement:
when (flash) {
Flash.OFF -> {
builder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON)
builder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF)
}
Flash.ON -> {
builder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH)
builder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF)
}
Flash.TORCH -> {
builder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON)
builder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_TORCH)
}
Flash.AUTO -> {
builder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH)
builder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF)
}
Flash.RED_EYE -> {
builder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE)
builder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF)
}
}
Clicking the button just iterates through these, so everything is working great until the user gets to TORCH. The flashlight turns on and all is fine, then the user clicks one more time to switch to AUTO, but the light doesn't go off. Only when the user clicks enough times to get to OFF will the light go off.
I got this from https://github.com/google/cameraview/blob/master/library/src/main/api21/com/google/android/cameraview/Camera2.java#L544 which by the way also breaks in the same manner.
I've also creating an issue at https://github.com/google/cameraview/issues/259 to help gain visibility for this issue.
Do I have to start a new capture session with flash off before coming off TORCH? Is there a better way?
UPDATE:
As suggested I've added a TorchCallback and got the following results though I can't make much sense of them. These come it when I start the first capture session and never change as I toggle the flash mode.
onTorchModeChanged(0, false)
onTorchModeChanged(0, false)
onTorchModeUnavailable(0)
onTorchModeUnavailable(0)