minor refactoring of the MarshmallowCamera class

This commit is contained in:
tibbi 2016-09-25 11:41:04 +02:00
parent 78384662b6
commit 4ea83ba0da

View File

@ -9,22 +9,28 @@ import android.util.Log;
import com.squareup.otto.Bus; import com.squareup.otto.Bus;
public class MarshmallowCamera { class MarshmallowCamera {
private static final String TAG = MyCameraImpl.class.getSimpleName(); private static final String TAG = MyCameraImpl.class.getSimpleName();
private Context mContext; private Context mContext;
private CameraManager manager;
private String cameraId;
public MarshmallowCamera(Context cxt) { @TargetApi(Build.VERSION_CODES.M)
MarshmallowCamera(Context cxt) {
mContext = cxt; mContext = cxt;
manager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
try {
final String[] list = manager.getCameraIdList();
cameraId = list[0];
} catch (CameraAccessException ignored) {
}
} }
@TargetApi(Build.VERSION_CODES.M) @TargetApi(Build.VERSION_CODES.M)
public void toggleMarshmallowFlashlight(final Bus bus, boolean enable) { void toggleMarshmallowFlashlight(final Bus bus, boolean enable) {
try { try {
final CameraManager manager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE); manager.setTorchMode(cameraId, enable);
final String[] list = manager.getCameraIdList();
if (list.length > 0)
manager.setTorchMode(list[0], enable);
} catch (CameraAccessException e) { } catch (CameraAccessException e) {
Log.e(TAG, "toggle marshmallow flashlight " + e.getMessage()); Log.e(TAG, "toggle marshmallow flashlight " + e.getMessage());
bus.post(new Events.CameraUnavailable()); bus.post(new Events.CameraUnavailable());