move the functionality to an interface
This commit is contained in:
parent
599137903d
commit
901a2c6861
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
package flashlight.simplemobiletools.com;
|
||||
|
||||
public interface MyCamera {
|
||||
void enableFlashlight();
|
||||
|
||||
void disableFlashlight();
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue