From 901a2c6861b90da4bfead0647eaa7edc76ada72a Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 6 Jan 2016 19:13:19 +0100 Subject: [PATCH] move the functionality to an interface --- .../simplemobiletools/com/MainActivity.java | 79 ++++++------------- .../simplemobiletools/com/MyCamera.java | 7 ++ .../simplemobiletools/com/MyCameraImpl.java | 53 +++++++++++++ 3 files changed, 84 insertions(+), 55 deletions(-) create mode 100644 app/src/main/java/flashlight/simplemobiletools/com/MyCamera.java create mode 100644 app/src/main/java/flashlight/simplemobiletools/com/MyCameraImpl.java diff --git a/app/src/main/java/flashlight/simplemobiletools/com/MainActivity.java b/app/src/main/java/flashlight/simplemobiletools/com/MainActivity.java index 8581422..5d50096 100644 --- a/app/src/main/java/flashlight/simplemobiletools/com/MainActivity.java +++ b/app/src/main/java/flashlight/simplemobiletools/com/MainActivity.java @@ -1,18 +1,13 @@ package flashlight.simplemobiletools.com; -import android.graphics.Color; -import android.graphics.PorterDuff; -import android.hardware.Camera; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.ImageView; -public class MainActivity extends AppCompatActivity { - private Camera camera; - private Camera.Parameters params; - private boolean isFlashlightOn; +public class MainActivity extends AppCompatActivity implements MyCamera { private ImageView toggleBtn; + private MyCameraImpl cameraImpl; @Override protected void onCreate(Bundle savedInstanceState) { @@ -20,8 +15,13 @@ public class MainActivity extends AppCompatActivity { setContentView(R.layout.activity_main); setupToggleButton(); - setupCamera(); - toggleFlashlight(); + setupCameraImpl(); + } + + private void setupCameraImpl() { + cameraImpl = new MyCameraImpl(this); + cameraImpl.setupCamera(); + cameraImpl.toggleFlashlight(); } private void setupToggleButton() { @@ -29,73 +29,42 @@ public class MainActivity extends AppCompatActivity { toggleBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - toggleFlashlight(); + cameraImpl.toggleFlashlight(); } }); } - private void toggleFlashlight() { - isFlashlightOn = !isFlashlightOn; - - if (isFlashlightOn) { - enableFlashlight(); - } else { - disableFlashlight(); - } - } - - private void setupCamera() { - if (camera == null) { - camera = Camera.open(); - params = camera.getParameters(); - params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); - camera.setParameters(params); - - if (isFlashlightOn) - enableFlashlight(); - } - } - - private void enableFlashlight() { - params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); - camera.setParameters(params); - toggleBtn.setBackground(getResources().getDrawable(R.mipmap.flashlight_big_on)); - } - - private void disableFlashlight() { - params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); - camera.setParameters(params); - toggleBtn.setBackground(getResources().getDrawable(R.mipmap.flashlight_big_off)); - } - - private void releaseCamera() { - if (camera != null) { - camera.release(); - camera = null; - } - } - @Override protected void onStart() { super.onStart(); - setupCamera(); + cameraImpl.setupCamera(); } @Override protected void onResume() { super.onResume(); - setupCamera(); + cameraImpl.setupCamera(); } @Override protected void onPause() { super.onPause(); - releaseCamera(); + cameraImpl.releaseCamera(); } @Override protected void onStop() { super.onStop(); - releaseCamera(); + cameraImpl.releaseCamera(); + } + + @Override + public void enableFlashlight() { + toggleBtn.setBackground(getResources().getDrawable(R.mipmap.flashlight_big_on)); + } + + @Override + public void disableFlashlight() { + toggleBtn.setBackground(getResources().getDrawable(R.mipmap.flashlight_big_off)); } } diff --git a/app/src/main/java/flashlight/simplemobiletools/com/MyCamera.java b/app/src/main/java/flashlight/simplemobiletools/com/MyCamera.java new file mode 100644 index 0000000..5ec4152 --- /dev/null +++ b/app/src/main/java/flashlight/simplemobiletools/com/MyCamera.java @@ -0,0 +1,7 @@ +package flashlight.simplemobiletools.com; + +public interface MyCamera { + void enableFlashlight(); + + void disableFlashlight(); +} diff --git a/app/src/main/java/flashlight/simplemobiletools/com/MyCameraImpl.java b/app/src/main/java/flashlight/simplemobiletools/com/MyCameraImpl.java new file mode 100644 index 0000000..b559a24 --- /dev/null +++ b/app/src/main/java/flashlight/simplemobiletools/com/MyCameraImpl.java @@ -0,0 +1,53 @@ +package flashlight.simplemobiletools.com; + +public class MyCameraImpl { + private android.hardware.Camera camera; + private android.hardware.Camera.Parameters params; + private boolean isFlashlightOn; + private MyCamera callback; + + public MyCameraImpl(MyCamera camera) { + callback = camera; + } + + public void toggleFlashlight() { + isFlashlightOn = !isFlashlightOn; + + if (isFlashlightOn) { + enableFlashlight(); + } else { + disableFlashlight(); + } + } + + public void setupCamera() { + if (camera == null) { + camera = android.hardware.Camera.open(); + params = camera.getParameters(); + params.setFlashMode(android.hardware.Camera.Parameters.FLASH_MODE_OFF); + camera.setParameters(params); + + if (isFlashlightOn) + enableFlashlight(); + } + } + + private void enableFlashlight() { + params.setFlashMode(android.hardware.Camera.Parameters.FLASH_MODE_TORCH); + camera.setParameters(params); + callback.enableFlashlight(); + } + + private void disableFlashlight() { + params.setFlashMode(android.hardware.Camera.Parameters.FLASH_MODE_OFF); + camera.setParameters(params); + callback.disableFlashlight(); + } + + public void releaseCamera() { + if (camera != null) { + camera.release(); + camera = null; + } + } +}