public class

TorchStateReset

extends java.lang.Object

 java.lang.Object

↳androidx.camera.camera2.internal.compat.workaround.TorchStateReset

Gradle dependencies

compile group: 'androidx.camera', name: 'camera-camera2', version: '1.5.0-alpha01'

  • groupId: androidx.camera
  • artifactId: camera-camera2
  • version: 1.5.0-alpha01

Artifact androidx.camera:camera-camera2:1.5.0-alpha01 it located at Google repository (https://maven.google.com/)

Overview

This is a workaround for b/228272227 where the Torch is unexpectedly closed after a single capturing.

Summary

Constructors
publicTorchStateReset()

Methods
public CaptureConfigcreateTorchResetRequest(CaptureConfig repeatingConfig)

Create a single request from the CaptureConfig.

public booleanisTorchResetRequired(java.util.List<CaptureRequest> captureRequestList, boolean isSingleCapture)

The method indicates if the Torch would be unexpectedly closed after the capture.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public TorchStateReset()

Methods

public boolean isTorchResetRequired(java.util.List<CaptureRequest> captureRequestList, boolean isSingleCapture)

The method indicates if the Torch would be unexpectedly closed after the capture. Return true means the Torch will be unexpectedly closed, and it requires turning on the Torch again after the capturing.

public CaptureConfig createTorchResetRequest(CaptureConfig repeatingConfig)

Create a single request from the CaptureConfig. And turns off the Torch by overriding the FLASH_MODE to FLASH_MODE_OFF.

Parameters:

repeatingConfig: the CaptureConfig that is used for the repeating request of the current session.

Returns:

a CaptureConfig that can be used in a single request to reset the FLASH_MODE to FLASH_MODE_OFF. The returned CaptureConfig will not set the CameraCaptureCallbacks and Tags since it is only used for Torch reset.

Source

/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.camera.camera2.internal.compat.workaround;

import android.hardware.camera2.CaptureRequest;

import androidx.annotation.NonNull;
import androidx.annotation.OptIn;
import androidx.camera.camera2.impl.Camera2ImplConfig;
import androidx.camera.camera2.internal.compat.quirk.DeviceQuirks;
import androidx.camera.camera2.internal.compat.quirk.TorchIsClosedAfterImageCapturingQuirk;
import androidx.camera.camera2.interop.ExperimentalCamera2Interop;
import androidx.camera.core.impl.CaptureConfig;
import androidx.camera.core.impl.DeferrableSurface;

import java.util.List;

/**
 * This is a workaround for b/228272227 where the Torch is unexpectedly closed after a single
 * capturing.
 */
public class TorchStateReset {
    private final boolean mIsImageCaptureTorchIsClosedQuirkEnabled;

    public TorchStateReset() {
        mIsImageCaptureTorchIsClosedQuirkEnabled =
                DeviceQuirks.get(TorchIsClosedAfterImageCapturingQuirk.class) != null;
    }

    /**
     * The method indicates if the Torch would be unexpectedly closed after the capture. Return
     * true means the Torch will be unexpectedly closed, and it requires turning on the Torch
     * again after the capturing.
     */
    public boolean isTorchResetRequired(@NonNull List<CaptureRequest> captureRequestList,
            boolean isSingleCapture) {
        if (mIsImageCaptureTorchIsClosedQuirkEnabled && isSingleCapture) {
            for (CaptureRequest captureRequest: captureRequestList) {
                Integer flashMode = captureRequest.get(CaptureRequest.FLASH_MODE);
                if (flashMode != null && flashMode == CaptureRequest.FLASH_MODE_TORCH) {
                    return true;
                }
            }
        }

        return false;
    }

    /**
     * Create a single request from the {@link CaptureConfig}. And turns off the Torch by
     * overriding the FLASH_MODE to FLASH_MODE_OFF.
     *
     * @param repeatingConfig the CaptureConfig that is used for the repeating request of the
     *                        current session.
     * @return a CaptureConfig that can be used in a single request to reset the FLASH_MODE to
     * FLASH_MODE_OFF. The returned CaptureConfig will not set the CameraCaptureCallbacks and
     * Tags since it is only used for Torch reset.
     */
    @OptIn(markerClass = ExperimentalCamera2Interop.class)
    @NonNull
    public CaptureConfig createTorchResetRequest(@NonNull CaptureConfig repeatingConfig) {
        CaptureConfig.Builder captureConfigBuilder = new CaptureConfig.Builder();
        captureConfigBuilder.setTemplateType(repeatingConfig.getTemplateType());

        // The single request only applies to the repeating surfaces.
        for (DeferrableSurface deferrableSurface : repeatingConfig.getSurfaces()) {
            captureConfigBuilder.addSurface(deferrableSurface);
        }

        captureConfigBuilder.addImplementationOptions(repeatingConfig.getImplementationOptions());
        Camera2ImplConfig.Builder builder = new Camera2ImplConfig.Builder();
        builder.setCaptureRequestOption(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);

        captureConfigBuilder.addImplementationOptions(builder.build());

        return captureConfigBuilder.build();
    }
}