From ad48d0be76b719315fa56ba44c7863bd23168dfb Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Fri, 2 Mar 2018 22:07:18 +0100 Subject: [PATCH] Refactor Mapper class --- MIGRATION.md | 1 + .../cameraview/CameraOptions1Test.java | 2 +- .../otaliastudios/cameraview/Mapper1Test.java | 2 +- .../com/otaliastudios/cameraview/Camera1.java | 2 +- .../cameraview/CameraOptions.java | 3 +- .../com/otaliastudios/cameraview/Mapper.java | 122 ------------------ .../com/otaliastudios/cameraview/Mapper1.java | 85 ++++++++++++ .../otaliastudios/cameraview/CameraUtils.java | 2 +- 8 files changed, 91 insertions(+), 128 deletions(-) create mode 100644 MIGRATION.md create mode 100644 cameraview/src/main/java/com/otaliastudios/cameraview/Mapper1.java diff --git a/MIGRATION.md b/MIGRATION.md new file mode 100644 index 00000000..7c46ca93 --- /dev/null +++ b/MIGRATION.md @@ -0,0 +1 @@ +# Migrating to v2 diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraOptions1Test.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraOptions1Test.java index b240c7aa..54dad130 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraOptions1Test.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraOptions1Test.java @@ -153,7 +153,7 @@ public class CameraOptions1Test extends BaseTest { } CameraOptions o = new CameraOptions(mock(Camera.Parameters.class), false); - Mapper m = new Mapper.Mapper1(); + Mapper m = new Mapper1(); Collection s = o.getSupportedControls(Facing.class); assertEquals(s.size(), supported.size()); for (Facing facing : s) { diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/Mapper1Test.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/Mapper1Test.java index 4a04c12d..37fd449e 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/Mapper1Test.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/Mapper1Test.java @@ -15,7 +15,7 @@ import static org.junit.Assert.*; @SmallTest public class Mapper1Test extends BaseTest { - private Mapper mapper = new Mapper.Mapper1(); + private Mapper mapper = new Mapper1(); @Test public void testMap() { diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java index b8af16b2..3b250da5 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java @@ -50,7 +50,7 @@ class Camera1 extends CameraController implements Camera.PreviewCallback, Camera Camera1(CameraView.CameraCallbacks callback) { super(callback); - mMapper = new Mapper.Mapper1(); + mMapper = new Mapper1(); } private void schedule(@Nullable final Task task, final boolean ensureAvailable, final Runnable action) { diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java index e072eec0..97f1d20d 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java @@ -6,7 +6,6 @@ import android.hardware.Camera; import android.hardware.camera2.CameraCharacteristics; import android.support.annotation.NonNull; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -38,7 +37,7 @@ public class CameraOptions { @SuppressWarnings("deprecation") CameraOptions(Camera.Parameters params, boolean flipSizes) { List strings; - Mapper mapper = new Mapper.Mapper1(); + Mapper mapper = new Mapper1(); // Facing Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/Mapper.java b/cameraview/src/main/java/com/otaliastudios/cameraview/Mapper.java index 1b507016..1c964960 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/Mapper.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/Mapper.java @@ -25,126 +25,4 @@ abstract class Mapper { default: return MediaRecorder.VideoEncoder.DEFAULT; } } - - - static class Mapper1 extends Mapper { - - private static final HashMap FLASH = new HashMap<>(); - private static final HashMap WB = new HashMap<>(); - private static final HashMap FACING = new HashMap<>(); - private static final HashMap HDR = new HashMap<>(); - - static { - 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(WhiteBalance.AUTO, Camera.Parameters.WHITE_BALANCE_AUTO); - WB.put(WhiteBalance.INCANDESCENT, Camera.Parameters.WHITE_BALANCE_INCANDESCENT); - WB.put(WhiteBalance.FLUORESCENT, Camera.Parameters.WHITE_BALANCE_FLUORESCENT); - WB.put(WhiteBalance.DAYLIGHT, Camera.Parameters.WHITE_BALANCE_DAYLIGHT); - WB.put(WhiteBalance.CLOUDY, Camera.Parameters.WHITE_BALANCE_CLOUDY_DAYLIGHT); - HDR.put(Hdr.OFF, Camera.Parameters.SCENE_MODE_AUTO); - if (Build.VERSION.SDK_INT >= 17) { - HDR.put(Hdr.ON, Camera.Parameters.SCENE_MODE_HDR); - } else { - HDR.put(Hdr.ON, "hdr"); - } - } - - @Override - T map(Flash flash) { - return (T) FLASH.get(flash); - } - - @Override - T map(Facing facing) { - return (T) FACING.get(facing); - } - - @Override - T map(WhiteBalance whiteBalance) { - return (T) WB.get(whiteBalance); - } - - @Override - T map(Hdr hdr) { - return (T) HDR.get(hdr); - } - - private T reverseLookup(HashMap map, Object object) { - for (T value : map.keySet()) { - if (map.get(value).equals(object)) { - return value; - } - } - return null; - } - - @Override - Flash unmapFlash(T cameraConstant) { - return reverseLookup(FLASH, cameraConstant); - } - - @Override - Facing unmapFacing(T cameraConstant) { - return reverseLookup(FACING, cameraConstant); - } - - @Override - WhiteBalance unmapWhiteBalance(T cameraConstant) { - return reverseLookup(WB, cameraConstant); - } - - @Override - Hdr unmapHdr(T cameraConstant) { - return reverseLookup(HDR, cameraConstant); - } - } - - static class Mapper2 extends Mapper { - - @Override - T map(WhiteBalance whiteBalance) { - return null; - } - - @Override - T map(Flash flash) { - return null; - } - - @Override - Flash unmapFlash(T cameraConstant) { - return null; - } - - @Override - WhiteBalance unmapWhiteBalance(T cameraConstant) { - return null; - } - - @Override - T map(Facing facing) { - return null; - } - - @Override - Facing unmapFacing(T cameraConstant) { - return null; - } - - @Override - T map(Hdr hdr) { - return null; - } - - @Override - Hdr unmapHdr(T cameraConstant) { - return null; - } - } - } diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/Mapper1.java b/cameraview/src/main/java/com/otaliastudios/cameraview/Mapper1.java new file mode 100644 index 00000000..76c9d981 --- /dev/null +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/Mapper1.java @@ -0,0 +1,85 @@ +package com.otaliastudios.cameraview; + +import android.hardware.Camera; +import android.os.Build; + +import java.util.HashMap; + + +@SuppressWarnings("unchecked") +class Mapper1 extends Mapper { + + private static final HashMap FLASH = new HashMap<>(); + private static final HashMap WB = new HashMap<>(); + private static final HashMap FACING = new HashMap<>(); + private static final HashMap HDR = new HashMap<>(); + + static { + 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(WhiteBalance.AUTO, Camera.Parameters.WHITE_BALANCE_AUTO); + WB.put(WhiteBalance.INCANDESCENT, Camera.Parameters.WHITE_BALANCE_INCANDESCENT); + WB.put(WhiteBalance.FLUORESCENT, Camera.Parameters.WHITE_BALANCE_FLUORESCENT); + WB.put(WhiteBalance.DAYLIGHT, Camera.Parameters.WHITE_BALANCE_DAYLIGHT); + WB.put(WhiteBalance.CLOUDY, Camera.Parameters.WHITE_BALANCE_CLOUDY_DAYLIGHT); + HDR.put(Hdr.OFF, Camera.Parameters.SCENE_MODE_AUTO); + if (Build.VERSION.SDK_INT >= 17) { + HDR.put(Hdr.ON, Camera.Parameters.SCENE_MODE_HDR); + } else { + HDR.put(Hdr.ON, "hdr"); + } + } + + @Override + T map(Flash flash) { + return (T) FLASH.get(flash); + } + + @Override + T map(Facing facing) { + return (T) FACING.get(facing); + } + + @Override + T map(WhiteBalance whiteBalance) { + return (T) WB.get(whiteBalance); + } + + @Override + T map(Hdr hdr) { + return (T) HDR.get(hdr); + } + + private T reverseLookup(HashMap map, Object object) { + for (T value : map.keySet()) { + if (map.get(value).equals(object)) { + return value; + } + } + return null; + } + + @Override + Flash unmapFlash(T cameraConstant) { + return reverseLookup(FLASH, cameraConstant); + } + + @Override + Facing unmapFacing(T cameraConstant) { + return reverseLookup(FACING, cameraConstant); + } + + @Override + WhiteBalance unmapWhiteBalance(T cameraConstant) { + return reverseLookup(WB, cameraConstant); + } + + @Override + Hdr unmapHdr(T cameraConstant) { + return reverseLookup(HDR, cameraConstant); + } +} diff --git a/cameraview/src/main/utils/com/otaliastudios/cameraview/CameraUtils.java b/cameraview/src/main/utils/com/otaliastudios/cameraview/CameraUtils.java index c6f9ff19..cef7dadb 100644 --- a/cameraview/src/main/utils/com/otaliastudios/cameraview/CameraUtils.java +++ b/cameraview/src/main/utils/com/otaliastudios/cameraview/CameraUtils.java @@ -46,7 +46,7 @@ public class CameraUtils { * @return true if such sensor exists */ public static boolean hasCameraFacing(Context context, Facing facing) { - int internal = new Mapper.Mapper1().map(facing); + int internal = new Mapper1().map(facing); Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); for (int i = 0, count = Camera.getNumberOfCameras(); i < count; i++) { Camera.getCameraInfo(i, cameraInfo);