Skip to content

Commit

Permalink
Add error handling for CameraManager#setTorch.
Browse files Browse the repository at this point in the history
  • Loading branch information
rkistner committed Mar 20, 2017
1 parent 09571d3 commit e3aed41
Showing 1 changed file with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -446,26 +446,36 @@ public void setDisplayConfiguration(DisplayConfiguration displayConfiguration) {

public void setTorch(boolean on) {
if (camera != null) {
boolean isOn = isTorchOn();
if (on != isOn) {
if (autoFocusManager != null) {
autoFocusManager.stop();
}

Camera.Parameters parameters = camera.getParameters();
CameraConfigurationUtils.setTorch(parameters, on);
if (settings.isExposureEnabled()) {
CameraConfigurationUtils.setBestExposure(parameters, on);
}
camera.setParameters(parameters);

if (autoFocusManager != null) {
autoFocusManager.start();
try {
boolean isOn = isTorchOn();
if (on != isOn) {
if (autoFocusManager != null) {
autoFocusManager.stop();
}

Camera.Parameters parameters = camera.getParameters();
CameraConfigurationUtils.setTorch(parameters, on);
if (settings.isExposureEnabled()) {
CameraConfigurationUtils.setBestExposure(parameters, on);
}
camera.setParameters(parameters);

if (autoFocusManager != null) {
autoFocusManager.start();
}
}
} catch(RuntimeException e) {
// Camera error. Could happen if the camera is being closed.
Log.e(TAG, "Failed to set torch", e);
}
}
}

/**
*
* @return true if the torch is on
* @throws RuntimeException if there is a camera error
*/
public boolean isTorchOn() {
Camera.Parameters parameters = camera.getParameters();
if (parameters != null) {
Expand Down

0 comments on commit e3aed41

Please sign in to comment.