From c122fbe80e680bc2d873604b642242914b82ba6b Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Fri, 11 Aug 2017 21:19:11 +0200 Subject: [PATCH] Moved Flash to enums --- .../com/otaliastudios/cameraview/Camera1.java | 8 +-- .../com/otaliastudios/cameraview/Camera2.java | 2 +- .../cameraview/CameraConstants.java | 20 ------ .../cameraview/CameraController.java | 8 +-- .../cameraview/CameraOptions.java | 14 ++-- .../otaliastudios/cameraview/CameraView.java | 57 +++++++-------- .../com/otaliastudios/cameraview/Mapper.java | 26 +++---- .../com/otaliastudios/cameraview/Facing.java | 6 +- .../com/otaliastudios/cameraview/Flash.java | 70 ++++++++++++++++--- .../cameraview/GestureAction.java | 6 +- .../cameraview/demo/MainActivity.java | 6 +- 11 files changed, 124 insertions(+), 99 deletions(-) diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java index 5827fea5..9146615b 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java @@ -143,7 +143,7 @@ class Camera1 extends CameraController { mExtraProperties = new ExtraProperties(params); mOptions = new CameraOptions(params); applyDefaultFocus(params); - mergeFlash(params, CameraConstants.Defaults.DEFAULT_FLASH); + mergeFlash(params, Flash.DEFAULT); mergeLocation(params, 0d, 0d); mergeWhiteBalance(params, CameraConstants.Defaults.DEFAULT_WHITE_BALANCE); params.setRecordingHint(mSessionType == SESSION_TYPE_VIDEO); @@ -271,8 +271,8 @@ class Camera1 extends CameraController { } @Override - void setFlash(@Flash int flash) { - int old = mFlash; + void setFlash(Flash flash) { + Flash old = mFlash; mFlash = flash; if (isCameraOpened()) { synchronized (mLock) { @@ -283,7 +283,7 @@ class Camera1 extends CameraController { } - private boolean mergeFlash(Camera.Parameters params, @Flash int oldFlash) { + private boolean mergeFlash(Camera.Parameters params, Flash oldFlash) { if (mOptions.getSupportedFlash().contains(mFlash)) { params.setFlashMode((String) mMapper.mapFlash(mFlash)); return true; diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java index a30afbb3..1b7f7887 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java @@ -137,7 +137,7 @@ class Camera2 extends CameraController { } @Override - void setFlash(@Flash int flash) { + void setFlash(Flash flash) { } diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraConstants.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraConstants.java index 87cd9b86..848c5e72 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraConstants.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraConstants.java @@ -5,16 +5,6 @@ public class CameraConstants { public static final int PERMISSION_REQUEST_CODE = 16; - @Deprecated public static final int GESTURE_ACTION_NONE = 0; - @Deprecated public static final int FACING_BACK = 0; - @Deprecated public static final int FOCUS_CONTINUOUS = 1; - @Deprecated public static final int ZOOM_OFF = 0; - - public static final int FLASH_OFF = 0; - public static final int FLASH_ON = 1; - public static final int FLASH_AUTO = 2; - public static final int FLASH_TORCH = 3; - public static final int GRID_OFF = 0; public static final int GRID_3X3 = 1; public static final int GRID_4X4 = 2; @@ -41,17 +31,7 @@ public class CameraConstants { static class Defaults { // TODO: Move to enum classes - static final int DEFAULT_GESTURE_ACTION_TAP = GESTURE_ACTION_NONE; - static final int DEFAULT_GESTURE_ACTION_LONG_TAP = GESTURE_ACTION_NONE; - static final int DEFAULT_GESTURE_ACTION_PINCH = GESTURE_ACTION_NONE; - static final int DEFAULT_FACING = FACING_BACK; - - // Deprecated - @Deprecated static final int DEFAULT_FOCUS = FOCUS_CONTINUOUS; - @Deprecated static final int DEFAULT_ZOOM = ZOOM_OFF; - // OK - static final int DEFAULT_FLASH = FLASH_OFF; static final int DEFAULT_VIDEO_QUALITY = VIDEO_QUALITY_480P; static final int DEFAULT_WHITE_BALANCE = WHITE_BALANCE_AUTO; static final int DEFAULT_SESSION_TYPE = SESSION_TYPE_PICTURE; diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java index 3336adb0..350e7c49 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java @@ -12,7 +12,7 @@ abstract class CameraController implements Preview.SurfaceCallback { protected final Preview mPreview; protected Facing mFacing; - @Flash protected int mFlash; + protected Flash mFlash; @VideoQuality protected int mVideoQuality; @WhiteBalance protected int mWhiteBalance; @SessionType protected int mSessionType; @@ -32,7 +32,7 @@ abstract class CameraController implements Preview.SurfaceCallback { abstract boolean setZoom(float zoom); abstract boolean setExposureCorrection(float EVvalue); abstract void setFacing(Facing facing); - abstract void setFlash(@Flash int flash); + abstract void setFlash(Flash flash); abstract void setVideoQuality(@VideoQuality int videoQuality); abstract void setWhiteBalance(@WhiteBalance int whiteBalance); abstract void setSessionType(@SessionType int sessionType); @@ -56,9 +56,7 @@ abstract class CameraController implements Preview.SurfaceCallback { final Facing getFacing() { return mFacing; } - - @Flash - final int getFlash() { + final Flash getFlash() { return mFlash; } diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java index cda43d97..e80d1ddd 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java @@ -17,7 +17,7 @@ public class CameraOptions { private Set supportedWhiteBalance = new HashSet<>(5); private Set supportedFacing = new HashSet<>(2); - private Set supportedFlash = new HashSet<>(4); + private Set supportedFlash = new HashSet<>(4); private boolean zoomSupported; private boolean videoSnapshotSupported; @@ -54,7 +54,7 @@ public class CameraOptions { strings = params.getSupportedFlashModes(); if (strings != null) { for (String string : strings) { - Integer value = mapper.unmapFlash(string); + Flash value = mapper.unmapFlash(string); if (value != null) supportedFlash.add(value); } } @@ -93,14 +93,14 @@ public class CameraOptions { /** * Set of supported flash values. * - * @see CameraConstants#FLASH_AUTO - * @see CameraConstants#FLASH_OFF - * @see CameraConstants#FLASH_ON - * @see CameraConstants#FLASH_TORCH + * @see Flash#AUTO + * @see Flash#OFF + * @see Flash#ON + * @see Flash#TORCH * @return a set of supported values. */ @NonNull - public Set getSupportedFlash() { + public Set getSupportedFlash() { return supportedFlash; } diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java index a52b8546..31994183 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java @@ -38,11 +38,7 @@ import static android.view.View.MeasureSpec.UNSPECIFIED; import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; -/** - * TODO: README for gestures - * TODO: deprecate setFocus, CONTINUOUS should be the default - * - */ + public class CameraView extends FrameLayout { private final static String TAG = CameraView.class.getSimpleName(); @@ -96,18 +92,17 @@ public class CameraView extends FrameLayout { @SuppressWarnings("WrongConstant") private void init(@NonNull Context context, @Nullable AttributeSet attrs) { TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CameraView, 0, 0); - Facing facing = Facing.fromValue(a.getInteger(R.styleable.CameraView_cameraFacing, Defaults.DEFAULT_FACING)); - int flash = a.getInteger(R.styleable.CameraView_cameraFlash, Defaults.DEFAULT_FLASH); + Facing facing = Facing.fromValue(a.getInteger(R.styleable.CameraView_cameraFacing, Facing.DEFAULT.value())); + Flash flash = Flash.fromValue(a.getInteger(R.styleable.CameraView_cameraFlash, Flash.DEFAULT.value())); int sessionType = a.getInteger(R.styleable.CameraView_cameraSessionType, Defaults.DEFAULT_SESSION_TYPE); int whiteBalance = a.getInteger(R.styleable.CameraView_cameraWhiteBalance, Defaults.DEFAULT_WHITE_BALANCE); int videoQuality = a.getInteger(R.styleable.CameraView_cameraVideoQuality, Defaults.DEFAULT_VIDEO_QUALITY); int grid = a.getInteger(R.styleable.CameraView_cameraGrid, Defaults.DEFAULT_GRID); mJpegQuality = a.getInteger(R.styleable.CameraView_cameraJpegQuality, Defaults.DEFAULT_JPEG_QUALITY); mCropOutput = a.getBoolean(R.styleable.CameraView_cameraCropOutput, Defaults.DEFAULT_CROP_OUTPUT); - GestureAction tapGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGestureTap, Defaults.DEFAULT_GESTURE_ACTION_TAP)); - // int doubleTapGesture = a.getInteger(R.styleable.CameraView_cameraGestureDoubleTap, Defaults.DEFAULT_GESTURE_ACTION_DOUBLE_TAP); - GestureAction longTapGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGestureLongTap, Defaults.DEFAULT_GESTURE_ACTION_LONG_TAP)); - GestureAction pinchGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGesturePinch, Defaults.DEFAULT_GESTURE_ACTION_PINCH)); + GestureAction tapGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGestureTap, GestureAction.DEFAULT_TAP.value())); + GestureAction longTapGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGestureLongTap, GestureAction.DEFAULT_LONG_TAP.value())); + GestureAction pinchGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGesturePinch, GestureAction.DEFAULT_PINCH.value())); a.recycle(); mCameraCallbacks = new CameraCallbacks(); @@ -736,14 +731,14 @@ public class CameraView extends FrameLayout { /** * Sets the flash mode. * - * @see CameraConstants#FLASH_OFF - * @see CameraConstants#FLASH_ON - * @see CameraConstants#FLASH_AUTO - * @see CameraConstants#FLASH_TORCH + * @see Flash#OFF + * @see Flash#ON + * @see Flash#AUTO + * @see Flash#TORCH * @param flash desired flash mode. */ - public void setFlash(@Flash int flash) { + public void setFlash(Flash flash) { mCameraController.setFlash(flash); } @@ -752,33 +747,31 @@ public class CameraView extends FrameLayout { * Gets the current flash mode. * @return a flash mode */ - @Flash - public int getFlash() { + public Flash getFlash() { return mCameraController.getFlash(); } /** - * Toggles the flash mode between {@link CameraConstants#FLASH_OFF}, - * {@link CameraConstants#FLASH_ON} and {@link CameraConstants#FLASH_AUTO}, in this order. + * Toggles the flash mode between {@link Flash#OFF}, + * {@link Flash#ON} and {@link Flash#AUTO}, in this order. * * @return the new flash value */ - @Flash - public int toggleFlash() { - int flash = mCameraController.getFlash(); + public Flash toggleFlash() { + Flash flash = mCameraController.getFlash(); switch (flash) { - case FLASH_OFF: - setFlash(FLASH_ON); + case OFF: + setFlash(Flash.ON); break; - case FLASH_ON: - setFlash(FLASH_AUTO); + case ON: + setFlash(Flash.AUTO); break; - case FLASH_AUTO: - case FLASH_TORCH: - setFlash(FLASH_OFF); + case AUTO: + case TORCH: + setFlash(Flash.OFF); break; } @@ -1325,7 +1318,7 @@ public class CameraView extends FrameLayout { */ @Deprecated public int getFocus() { - return Defaults.DEFAULT_FOCUS; + return 0; } @@ -1365,7 +1358,7 @@ public class CameraView extends FrameLayout { */ @Deprecated public int getZoomMode() { - return Defaults.DEFAULT_ZOOM; + return 0; } } diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/Mapper.java b/cameraview/src/main/java/com/otaliastudios/cameraview/Mapper.java index a44b433b..9796995e 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/Mapper.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/Mapper.java @@ -6,23 +6,23 @@ import java.util.HashMap; abstract class Mapper { - abstract T mapFlash(@Flash int internalConstant); + abstract T mapFlash(Flash internalConstant); abstract T mapFacing(Facing internalConstant); abstract T mapWhiteBalance(@WhiteBalance int internalConstant); - @Flash abstract Integer unmapFlash(T cameraConstant); + abstract Flash unmapFlash(T cameraConstant); abstract Facing unmapFacing(T cameraConstant); @WhiteBalance abstract Integer unmapWhiteBalance(T cameraConstant); static class Mapper1 extends Mapper { - private static final HashMap FLASH = new HashMap<>(); + private static final HashMap FLASH = new HashMap<>(); private static final HashMap WB = new HashMap<>(); private static final HashMap FACING = new HashMap<>(); static { - FLASH.put(CameraConstants.FLASH_OFF, Camera.Parameters.FLASH_MODE_OFF); - FLASH.put(CameraConstants.FLASH_ON, Camera.Parameters.FLASH_MODE_ON); - FLASH.put(CameraConstants.FLASH_AUTO, Camera.Parameters.FLASH_MODE_AUTO); - FLASH.put(CameraConstants.FLASH_TORCH, Camera.Parameters.FLASH_MODE_TORCH); + FLASH.put(Flash.OFF, Camera.Parameters.FLASH_MODE_OFF); + FLASH.put(Flash.ON, Camera.Parameters.FLASH_MODE_ON); + FLASH.put(Flash.AUTO, Camera.Parameters.FLASH_MODE_AUTO); + FLASH.put(Flash.TORCH, Camera.Parameters.FLASH_MODE_TORCH); FACING.put(Facing.BACK, Camera.CameraInfo.CAMERA_FACING_BACK); FACING.put(Facing.FRONT, Camera.CameraInfo.CAMERA_FACING_FRONT); WB.put(CameraConstants.WHITE_BALANCE_AUTO, Camera.Parameters.WHITE_BALANCE_AUTO); @@ -33,7 +33,7 @@ abstract class Mapper { } @Override - T mapFlash(int internalConstant) { + T mapFlash(Flash internalConstant) { return (T) FLASH.get(internalConstant); } @@ -57,7 +57,7 @@ abstract class Mapper { } @Override - Integer unmapFlash(T cameraConstant) { + Flash unmapFlash(T cameraConstant) { return reverseLookup(FLASH, cameraConstant); } @@ -80,18 +80,18 @@ abstract class Mapper { } @Override - T mapFlash(@Flash int internalConstant) { + T mapFlash(Flash internalConstant) { return null; } @Override - Integer unmapFlash(T cameraConstant) { - return 0; + Flash unmapFlash(T cameraConstant) { + return null; } @Override Integer unmapWhiteBalance(T cameraConstant) { - return 0; + return null; } @Override diff --git a/cameraview/src/main/options/com/otaliastudios/cameraview/Facing.java b/cameraview/src/main/options/com/otaliastudios/cameraview/Facing.java index 71b4967b..d20c9912 100644 --- a/cameraview/src/main/options/com/otaliastudios/cameraview/Facing.java +++ b/cameraview/src/main/options/com/otaliastudios/cameraview/Facing.java @@ -2,7 +2,7 @@ package com.otaliastudios.cameraview; /** - * Facing values indicates which camera sensor should be used for the current session. + * Facing value indicates which camera sensor should be used for the current session. * * @see CameraView#setFacing(int) */ @@ -18,13 +18,15 @@ public enum Facing { */ FRONT(1); + final static Facing DEFAULT = BACK; + private int value; Facing(int value) { this.value = value; } - private int value() { + int value() { return value; } diff --git a/cameraview/src/main/options/com/otaliastudios/cameraview/Flash.java b/cameraview/src/main/options/com/otaliastudios/cameraview/Flash.java index 71733ffa..28d2a27f 100644 --- a/cameraview/src/main/options/com/otaliastudios/cameraview/Flash.java +++ b/cameraview/src/main/options/com/otaliastudios/cameraview/Flash.java @@ -1,16 +1,64 @@ package com.otaliastudios.cameraview; -import android.support.annotation.IntDef; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; +/** + * Flash value indicates the flash mode to be used. + * + * @see CameraView#setFlash(int) + */ +public enum Flash { -import static com.otaliastudios.cameraview.CameraConstants.FLASH_AUTO; -import static com.otaliastudios.cameraview.CameraConstants.FLASH_OFF; -import static com.otaliastudios.cameraview.CameraConstants.FLASH_ON; -import static com.otaliastudios.cameraview.CameraConstants.FLASH_TORCH; + /** + * Flash is always off. + */ + OFF(0), -@Retention(RetentionPolicy.SOURCE) -@IntDef({FLASH_OFF, FLASH_ON, FLASH_AUTO, FLASH_TORCH}) -public @interface Flash { -} \ No newline at end of file + /** + * Flash will be on when capturing. + * This is not guaranteed to be supported. + * + * @see CameraOptions#getSupportedFlash() + */ + ON(1), + + + /** + * Flash mode is chosen by the camera. + * This is not guaranteed to be supported. + * + * @see CameraOptions#getSupportedFlash() + */ + AUTO(2), + + + /** + * TODO: is this true? + * Flash is always on, working as a torch. + * This is not guaranteed to be supported. + * + * @see CameraOptions#getSupportedFlash() + */ + TORCH(3); + + static final Flash DEFAULT = OFF; + + private int value; + + Flash(int value) { + this.value = value; + } + + int value() { + return value; + } + + static Flash fromValue(int value) { + Flash[] list = Flash.values(); + for (Flash action : list) { + if (action.value() == value) { + return action; + } + } + return null; + } +} diff --git a/cameraview/src/main/options/com/otaliastudios/cameraview/GestureAction.java b/cameraview/src/main/options/com/otaliastudios/cameraview/GestureAction.java index c61b5592..1cc8b5a7 100644 --- a/cameraview/src/main/options/com/otaliastudios/cameraview/GestureAction.java +++ b/cameraview/src/main/options/com/otaliastudios/cameraview/GestureAction.java @@ -61,13 +61,17 @@ public enum GestureAction { EXPOSURE_CORRECTION(5); + final static GestureAction DEFAULT_PINCH = NONE; + final static GestureAction DEFAULT_TAP = NONE; + final static GestureAction DEFAULT_LONG_TAP = NONE; + private int value; GestureAction(int value) { this.value = value; } - private int value() { + int value() { return value; } diff --git a/demo/src/main/java/com/otaliastudios/cameraview/demo/MainActivity.java b/demo/src/main/java/com/otaliastudios/cameraview/demo/MainActivity.java index a059e145..3eea3845 100644 --- a/demo/src/main/java/com/otaliastudios/cameraview/demo/MainActivity.java +++ b/demo/src/main/java/com/otaliastudios/cameraview/demo/MainActivity.java @@ -197,15 +197,15 @@ public class MainActivity extends AppCompatActivity implements View.OnLayoutChan void toggleFlash() { if (mCapturingPicture) return; switch (camera.toggleFlash()) { - case CameraConstants.FLASH_ON: + case ON: message("Flash on!", false); break; - case CameraConstants.FLASH_OFF: + case OFF: message("Flash off!", false); break; - case CameraConstants.FLASH_AUTO: + case AUTO: message("Flash auto!", false); break; }