Skip to content

Commit 6a5b7d8

Browse files
committed
fix flashlight for Android 4
1 parent c7e321f commit 6a5b7d8

3 files changed

Lines changed: 56 additions & 20 deletions

File tree

app/src/main/java/com/simplemobiletools/flashlight/MainActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ public void toggleFlashlight() {
5959
@Override
6060
protected void onStart() {
6161
super.onStart();
62-
cameraImpl.setupCamera();
62+
cameraImpl.handleCameraSetup();
6363
}
6464

6565
@Override
6666
protected void onResume() {
6767
super.onResume();
68-
cameraImpl.setupCamera();
68+
cameraImpl.handleCameraSetup();
6969
}
7070

7171
@Override
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.simplemobiletools.flashlight;
2+
3+
import android.annotation.TargetApi;
4+
import android.content.Context;
5+
import android.hardware.camera2.CameraAccessException;
6+
import android.hardware.camera2.CameraManager;
7+
import android.os.Build;
8+
import android.util.Log;
9+
10+
public class MarshmallowCamera {
11+
private static final String TAG = MyCameraImpl.class.getSimpleName();
12+
private MyCamera mCallback;
13+
private Context mContext;
14+
15+
public MarshmallowCamera(MyCamera camera, Context cxt) {
16+
mCallback = camera;
17+
mContext = cxt;
18+
}
19+
20+
@TargetApi(Build.VERSION_CODES.M)
21+
public void toggleMarshmallowFlashlight(boolean enable) {
22+
try {
23+
final CameraManager manager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
24+
final String[] list = manager.getCameraIdList();
25+
manager.setTorchMode(list[0], enable);
26+
} catch (CameraAccessException e) {
27+
Log.e(TAG, "toggle marshmallow flashlight " + e.getMessage());
28+
mCallback.cameraUnavailable();
29+
}
30+
}
31+
}

app/src/main/java/com/simplemobiletools/flashlight/MyCameraImpl.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
package com.simplemobiletools.flashlight;
22

3-
import android.annotation.TargetApi;
43
import android.content.Context;
54
import android.hardware.Camera;
6-
import android.hardware.camera2.CameraAccessException;
7-
import android.hardware.camera2.CameraManager;
8-
import android.os.Build;
95
import android.util.Log;
106

117
public class MyCameraImpl {
@@ -15,15 +11,18 @@ public class MyCameraImpl {
1511
private boolean isFlashlightOn;
1612
private MyCamera callback;
1713
private Context context;
14+
private boolean isMarshmallow;
15+
private MarshmallowCamera marshmallowCamera;
1816

1917
public MyCameraImpl(MyCamera camera, Context cxt) {
2018
callback = camera;
2119
context = cxt;
22-
setupCamera();
20+
isMarshmallow = isMarshmallow();
21+
handleCameraSetup();
2322
}
2423

2524
public void toggleFlashlight() {
26-
setupCamera();
25+
handleCameraSetup();
2726
isFlashlightOn = !isFlashlightOn;
2827

2928
if (isFlashlightOn) {
@@ -33,8 +32,22 @@ public void toggleFlashlight() {
3332
}
3433
}
3534

36-
public void setupCamera() {
37-
if (isMarshmallow())
35+
public void handleCameraSetup() {
36+
if (isMarshmallow) {
37+
setupMarshmallowCamera();
38+
} else {
39+
setupCamera();
40+
}
41+
}
42+
43+
private void setupMarshmallowCamera() {
44+
if (marshmallowCamera == null) {
45+
marshmallowCamera = new MarshmallowCamera(callback, context);
46+
}
47+
}
48+
49+
private void setupCamera() {
50+
if (isMarshmallow)
3851
return;
3952

4053
if (camera == null) {
@@ -54,7 +67,7 @@ public void setupCamera() {
5467
}
5568

5669
private void enableFlashlight() {
57-
if (isMarshmallow()) {
70+
if (isMarshmallow) {
5871
toggleMarshmallowFlashlight(true);
5972
} else {
6073
if (camera == null || params == null)
@@ -79,16 +92,8 @@ private void disableFlashlight() {
7992
callback.disableFlashlight();
8093
}
8194

82-
@TargetApi(Build.VERSION_CODES.M)
8395
private void toggleMarshmallowFlashlight(boolean enable) {
84-
try {
85-
final CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
86-
final String[] list = manager.getCameraIdList();
87-
manager.setTorchMode(list[0], enable);
88-
} catch (CameraAccessException e) {
89-
Log.e(TAG, "toggle marshmallow flashlight " + e.getMessage());
90-
callback.cameraUnavailable();
91-
}
96+
marshmallowCamera.toggleMarshmallowFlashlight(enable);
9297
}
9398

9499
public void releaseCamera() {

0 commit comments

Comments
 (0)