fix flashlight for Android 4
This commit is contained in:
parent
c7e321fc65
commit
6a5b7d84e9
|
@ -59,13 +59,13 @@ public class MainActivity extends AppCompatActivity implements MyCamera {
|
||||||
@Override
|
@Override
|
||||||
protected void onStart() {
|
protected void onStart() {
|
||||||
super.onStart();
|
super.onStart();
|
||||||
cameraImpl.setupCamera();
|
cameraImpl.handleCameraSetup();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
cameraImpl.setupCamera();
|
cameraImpl.handleCameraSetup();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
package com.simplemobiletools.flashlight;
|
||||||
|
|
||||||
|
import android.annotation.TargetApi;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.hardware.camera2.CameraAccessException;
|
||||||
|
import android.hardware.camera2.CameraManager;
|
||||||
|
import android.os.Build;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
public class MarshmallowCamera {
|
||||||
|
private static final String TAG = MyCameraImpl.class.getSimpleName();
|
||||||
|
private MyCamera mCallback;
|
||||||
|
private Context mContext;
|
||||||
|
|
||||||
|
public MarshmallowCamera(MyCamera camera, Context cxt) {
|
||||||
|
mCallback = camera;
|
||||||
|
mContext = cxt;
|
||||||
|
}
|
||||||
|
|
||||||
|
@TargetApi(Build.VERSION_CODES.M)
|
||||||
|
public void toggleMarshmallowFlashlight(boolean enable) {
|
||||||
|
try {
|
||||||
|
final CameraManager manager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
|
||||||
|
final String[] list = manager.getCameraIdList();
|
||||||
|
manager.setTorchMode(list[0], enable);
|
||||||
|
} catch (CameraAccessException e) {
|
||||||
|
Log.e(TAG, "toggle marshmallow flashlight " + e.getMessage());
|
||||||
|
mCallback.cameraUnavailable();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,11 +1,7 @@
|
||||||
package com.simplemobiletools.flashlight;
|
package com.simplemobiletools.flashlight;
|
||||||
|
|
||||||
import android.annotation.TargetApi;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.hardware.Camera;
|
import android.hardware.Camera;
|
||||||
import android.hardware.camera2.CameraAccessException;
|
|
||||||
import android.hardware.camera2.CameraManager;
|
|
||||||
import android.os.Build;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
public class MyCameraImpl {
|
public class MyCameraImpl {
|
||||||
|
@ -15,15 +11,18 @@ public class MyCameraImpl {
|
||||||
private boolean isFlashlightOn;
|
private boolean isFlashlightOn;
|
||||||
private MyCamera callback;
|
private MyCamera callback;
|
||||||
private Context context;
|
private Context context;
|
||||||
|
private boolean isMarshmallow;
|
||||||
|
private MarshmallowCamera marshmallowCamera;
|
||||||
|
|
||||||
public MyCameraImpl(MyCamera camera, Context cxt) {
|
public MyCameraImpl(MyCamera camera, Context cxt) {
|
||||||
callback = camera;
|
callback = camera;
|
||||||
context = cxt;
|
context = cxt;
|
||||||
setupCamera();
|
isMarshmallow = isMarshmallow();
|
||||||
|
handleCameraSetup();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void toggleFlashlight() {
|
public void toggleFlashlight() {
|
||||||
setupCamera();
|
handleCameraSetup();
|
||||||
isFlashlightOn = !isFlashlightOn;
|
isFlashlightOn = !isFlashlightOn;
|
||||||
|
|
||||||
if (isFlashlightOn) {
|
if (isFlashlightOn) {
|
||||||
|
@ -33,8 +32,22 @@ public class MyCameraImpl {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setupCamera() {
|
public void handleCameraSetup() {
|
||||||
if (isMarshmallow())
|
if (isMarshmallow) {
|
||||||
|
setupMarshmallowCamera();
|
||||||
|
} else {
|
||||||
|
setupCamera();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupMarshmallowCamera() {
|
||||||
|
if (marshmallowCamera == null) {
|
||||||
|
marshmallowCamera = new MarshmallowCamera(callback, context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupCamera() {
|
||||||
|
if (isMarshmallow)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (camera == null) {
|
if (camera == null) {
|
||||||
|
@ -54,7 +67,7 @@ public class MyCameraImpl {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void enableFlashlight() {
|
private void enableFlashlight() {
|
||||||
if (isMarshmallow()) {
|
if (isMarshmallow) {
|
||||||
toggleMarshmallowFlashlight(true);
|
toggleMarshmallowFlashlight(true);
|
||||||
} else {
|
} else {
|
||||||
if (camera == null || params == null)
|
if (camera == null || params == null)
|
||||||
|
@ -79,16 +92,8 @@ public class MyCameraImpl {
|
||||||
callback.disableFlashlight();
|
callback.disableFlashlight();
|
||||||
}
|
}
|
||||||
|
|
||||||
@TargetApi(Build.VERSION_CODES.M)
|
|
||||||
private void toggleMarshmallowFlashlight(boolean enable) {
|
private void toggleMarshmallowFlashlight(boolean enable) {
|
||||||
try {
|
marshmallowCamera.toggleMarshmallowFlashlight(enable);
|
||||||
final CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
|
|
||||||
final String[] list = manager.getCameraIdList();
|
|
||||||
manager.setTorchMode(list[0], enable);
|
|
||||||
} catch (CameraAccessException e) {
|
|
||||||
Log.e(TAG, "toggle marshmallow flashlight " + e.getMessage());
|
|
||||||
callback.cameraUnavailable();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void releaseCamera() {
|
public void releaseCamera() {
|
||||||
|
|
Loading…
Reference in New Issue