Skip to content

Commit 0e89b6e

Browse files
author
Tibor Kaputa
committed
fix the flashlight on Android 7
1 parent 5a61ad7 commit 0e89b6e

File tree

3 files changed

+56
-33
lines changed

3 files changed

+56
-33
lines changed

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

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,15 @@ public boolean toggleStroboscope() {
5050
if (!mIsStroboscopeRunning)
5151
disableFlashlight();
5252

53-
if (mCamera == null) {
54-
initCamera();
55-
}
53+
if (!Utils.isNougat()) {
54+
if (mCamera == null) {
55+
initCamera();
56+
}
5657

57-
if (mCamera == null) {
58-
Utils.showToast(mContext, R.string.camera_error);
59-
return false;
58+
if (mCamera == null) {
59+
Utils.showToast(mContext, R.string.camera_error);
60+
return false;
61+
}
6062
}
6163

6264
if (mIsStroboscopeRunning) {
@@ -181,6 +183,7 @@ public void releaseCamera() {
181183
mBus.unregister(this);
182184
}
183185
mIsFlashlightOn = false;
186+
mShouldStroboscopeStop = true;
184187
}
185188

186189
private boolean isMarshmallow() {
@@ -197,38 +200,53 @@ public void run() {
197200
mShouldStroboscopeStop = false;
198201
mIsStroboscopeRunning = true;
199202

200-
if (mCamera == null) {
201-
initCamera();
202-
}
203+
if (Utils.isNougat()) {
204+
while (!mShouldStroboscopeStop) {
205+
try {
206+
mMarshmallowCamera.toggleMarshmallowFlashlight(mBus, true);
207+
Thread.sleep(mStroboFrequency);
208+
mMarshmallowCamera.toggleMarshmallowFlashlight(mBus, false);
209+
Thread.sleep(mStroboFrequency);
210+
} catch (InterruptedException ignored) {
211+
mShouldStroboscopeStop = true;
212+
} catch (RuntimeException ignored) {
213+
mShouldStroboscopeStop = true;
214+
}
215+
}
216+
} else {
217+
if (mCamera == null) {
218+
initCamera();
219+
}
203220

204-
Camera.Parameters torchOn = mCamera.getParameters();
205-
Camera.Parameters torchOff = mCamera.getParameters();
206-
torchOn.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
207-
torchOff.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
221+
Camera.Parameters torchOn = mCamera.getParameters();
222+
Camera.Parameters torchOff = mCamera.getParameters();
223+
torchOn.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
224+
torchOff.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
225+
226+
while (!mShouldStroboscopeStop) {
227+
try {
228+
mCamera.setParameters(torchOn);
229+
Thread.sleep(mStroboFrequency);
230+
mCamera.setParameters(torchOff);
231+
Thread.sleep(mStroboFrequency);
232+
} catch (InterruptedException ignored) {
233+
mShouldStroboscopeStop = true;
234+
} catch (RuntimeException ignored) {
235+
mShouldStroboscopeStop = true;
236+
}
237+
}
208238

209-
while (!mShouldStroboscopeStop) {
210239
try {
211-
mCamera.setParameters(torchOn);
212-
Thread.sleep(mStroboFrequency);
213-
mCamera.setParameters(torchOff);
214-
Thread.sleep(mStroboFrequency);
215-
} catch (InterruptedException ignored) {
216-
mShouldStroboscopeStop = true;
240+
if (mCamera != null) {
241+
mCamera.setParameters(torchOff);
242+
if (!mShouldEnableFlashlight || mIsMarshmallow) {
243+
mCamera.release();
244+
mCamera = null;
245+
}
246+
}
217247
} catch (RuntimeException ignored) {
218-
mShouldStroboscopeStop = true;
219-
}
220-
}
221248

222-
try {
223-
if (mCamera != null) {
224-
mCamera.setParameters(torchOff);
225-
if (!mShouldEnableFlashlight || mIsMarshmallow) {
226-
mCamera.release();
227-
mCamera = null;
228-
}
229249
}
230-
} catch (RuntimeException ignored) {
231-
232250
}
233251

234252
mIsStroboscopeRunning = false;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.graphics.Bitmap;
55
import android.graphics.Canvas;
66
import android.graphics.drawable.Drawable;
7+
import android.os.Build;
78
import android.widget.Toast;
89

910
public class Utils {
@@ -20,4 +21,8 @@ public static Bitmap drawableToBitmap(Drawable drawable) {
2021
public static void showToast(Context context, int resId) {
2122
Toast.makeText(context, context.getResources().getString(resId), Toast.LENGTH_SHORT).show();
2223
}
24+
25+
public static boolean isNougat() {
26+
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N;
27+
}
2328
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public void tryToggleStroboscope() {
115115

116116
private void toggleStroboscope() {
117117
// use the old Camera API for stroboscope, the new Camera Manager is way too slow
118-
if (isCameraPermissionGranted()) {
118+
if (isCameraPermissionGranted() || Utils.isNougat()) {
119119
if (mCameraImpl.toggleStroboscope()) {
120120
mStroboscopeBar.setVisibility(mStroboscopeBar.getVisibility() == View.VISIBLE ? View.INVISIBLE : View.VISIBLE);
121121
changeIconColor(mStroboscopeBar.getVisibility() == View.VISIBLE ? R.color.colorPrimary : R.color.translucent_white, mStroboscopeBtn);

0 commit comments

Comments
 (0)