public class BlockingCameraManager extends Object
CameraManager
functionality with blocking functions.
Safe to use at the same time as the regular CameraManager, so this does not duplicate any functionality that is already blocking.
Be careful when using this from UI thread! This function will typically block for about 500ms when successful, and as long as ms when timing out.
Modifier and Type | Class and Description |
---|---|
static class |
BlockingCameraManager.BlockingOpenException
Exception thrown by
openCamera(java.lang.String, android.hardware.camera2.CameraDevice.StateCallback, android.os.Handler) if the open fails asynchronously. |
Constructor and Description |
---|
BlockingCameraManager(CameraManager manager)
Create a new blocking camera manager.
|
Modifier and Type | Method and Description |
---|---|
CameraDevice |
openCamera(String cameraId,
CameraDevice.StateCallback listener,
Handler handler)
Open the camera, blocking it until it succeeds or fails.
|
public BlockingCameraManager(CameraManager manager)
manager
- CameraManager returned by
Context.getSystemService(Context.CAMERA_SERVICE)
public CameraDevice openCamera(String cameraId, CameraDevice.StateCallback listener, Handler handler) throws CameraAccessException, BlockingCameraManager.BlockingOpenException
Note that the Handler provided must not be null. Furthermore, if there is a handler, its Looper must not be the current thread's Looper. Otherwise we'd never receive the callbacks from the CameraDevice since this function would prevent them from being processed.
Throws CameraAccessException
for the same reason CameraManager.openCamera(java.lang.String, android.hardware.camera2.CameraDevice.StateCallback, android.os.Handler)
does.
Throws BlockingCameraManager.BlockingOpenException
when the open fails asynchronously (due to
CameraDevice.StateCallback#onDisconnected(CameraDevice)
or
(CameraDevice.StateCallback#onError(CameraDevice)
.
Throws TimeoutRuntimeException
if opening times out. This is usually
highly unrecoverable, and all future calls to opening that camera will fail since the
service will think it's busy. This class will do its best to clean up eventually.
cameraId
- Id of the cameralistener
- Listener to the camera. onOpened, onDisconnected, onError need not be implemented.handler
- Handler which to run the listener on. Must not be null.IllegalArgumentException
- If the handler is null, or if the handler's looper is current.CameraAccessException
- If open fails immediately.BlockingCameraManager.BlockingOpenException
- If open fails after blocking for some amount of time.TimeoutRuntimeException
- If opening times out. Typically unrecoverable.