move the functionality to an interface

This commit is contained in:
tibbi 2016-01-06 19:13:19 +01:00
parent 599137903d
commit 901a2c6861
3 changed files with 84 additions and 55 deletions

View File

@ -1,18 +1,13 @@
package flashlight.simplemobiletools.com; package flashlight.simplemobiletools.com;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.hardware.Camera;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.view.View; import android.view.View;
import android.widget.ImageView; import android.widget.ImageView;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity implements MyCamera {
private Camera camera;
private Camera.Parameters params;
private boolean isFlashlightOn;
private ImageView toggleBtn; private ImageView toggleBtn;
private MyCameraImpl cameraImpl;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -20,8 +15,13 @@ public class MainActivity extends AppCompatActivity {
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
setupToggleButton(); setupToggleButton();
setupCamera(); setupCameraImpl();
toggleFlashlight(); }
private void setupCameraImpl() {
cameraImpl = new MyCameraImpl(this);
cameraImpl.setupCamera();
cameraImpl.toggleFlashlight();
} }
private void setupToggleButton() { private void setupToggleButton() {
@ -29,73 +29,42 @@ public class MainActivity extends AppCompatActivity {
toggleBtn.setOnClickListener(new View.OnClickListener() { toggleBtn.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { 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 @Override
protected void onStart() { protected void onStart() {
super.onStart(); super.onStart();
setupCamera(); cameraImpl.setupCamera();
} }
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
setupCamera(); cameraImpl.setupCamera();
} }
@Override @Override
protected void onPause() { protected void onPause() {
super.onPause(); super.onPause();
releaseCamera(); cameraImpl.releaseCamera();
} }
@Override @Override
protected void onStop() { protected void onStop() {
super.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));
} }
} }

View File

@ -0,0 +1,7 @@
package flashlight.simplemobiletools.com;
public interface MyCamera {
void enableFlashlight();
void disableFlashlight();
}

View File

@ -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;
}
}
}