fix the flashlight on Android 7

This commit is contained in:
Tibor Kaputa 2016-11-24 15:08:50 +01:00
parent 5a61ad7957
commit 0e89b6e3c6
3 changed files with 59 additions and 36 deletions

View File

@ -50,13 +50,15 @@ public class MyCameraImpl {
if (!mIsStroboscopeRunning)
disableFlashlight();
if (mCamera == null) {
initCamera();
}
if (!Utils.isNougat()) {
if (mCamera == null) {
initCamera();
}
if (mCamera == null) {
Utils.showToast(mContext, R.string.camera_error);
return false;
if (mCamera == null) {
Utils.showToast(mContext, R.string.camera_error);
return false;
}
}
if (mIsStroboscopeRunning) {
@ -181,6 +183,7 @@ public class MyCameraImpl {
mBus.unregister(this);
}
mIsFlashlightOn = false;
mShouldStroboscopeStop = true;
}
private boolean isMarshmallow() {
@ -197,38 +200,53 @@ public class MyCameraImpl {
mShouldStroboscopeStop = false;
mIsStroboscopeRunning = true;
if (mCamera == null) {
initCamera();
}
Camera.Parameters torchOn = mCamera.getParameters();
Camera.Parameters torchOff = mCamera.getParameters();
torchOn.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
torchOff.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
while (!mShouldStroboscopeStop) {
try {
mCamera.setParameters(torchOn);
Thread.sleep(mStroboFrequency);
mCamera.setParameters(torchOff);
Thread.sleep(mStroboFrequency);
} catch (InterruptedException ignored) {
mShouldStroboscopeStop = true;
} catch (RuntimeException ignored) {
mShouldStroboscopeStop = true;
}
}
try {
if (mCamera != null) {
mCamera.setParameters(torchOff);
if (!mShouldEnableFlashlight || mIsMarshmallow) {
mCamera.release();
mCamera = null;
if (Utils.isNougat()) {
while (!mShouldStroboscopeStop) {
try {
mMarshmallowCamera.toggleMarshmallowFlashlight(mBus, true);
Thread.sleep(mStroboFrequency);
mMarshmallowCamera.toggleMarshmallowFlashlight(mBus, false);
Thread.sleep(mStroboFrequency);
} catch (InterruptedException ignored) {
mShouldStroboscopeStop = true;
} catch (RuntimeException ignored) {
mShouldStroboscopeStop = true;
}
}
} catch (RuntimeException ignored) {
} else {
if (mCamera == null) {
initCamera();
}
Camera.Parameters torchOn = mCamera.getParameters();
Camera.Parameters torchOff = mCamera.getParameters();
torchOn.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
torchOff.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
while (!mShouldStroboscopeStop) {
try {
mCamera.setParameters(torchOn);
Thread.sleep(mStroboFrequency);
mCamera.setParameters(torchOff);
Thread.sleep(mStroboFrequency);
} catch (InterruptedException ignored) {
mShouldStroboscopeStop = true;
} catch (RuntimeException ignored) {
mShouldStroboscopeStop = true;
}
}
try {
if (mCamera != null) {
mCamera.setParameters(torchOff);
if (!mShouldEnableFlashlight || mIsMarshmallow) {
mCamera.release();
mCamera = null;
}
}
} catch (RuntimeException ignored) {
}
}
mIsStroboscopeRunning = false;

View File

@ -4,6 +4,7 @@ import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.widget.Toast;
public class Utils {
@ -20,4 +21,8 @@ public class Utils {
public static void showToast(Context context, int resId) {
Toast.makeText(context, context.getResources().getString(resId), Toast.LENGTH_SHORT).show();
}
public static boolean isNougat() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N;
}
}

View File

@ -115,7 +115,7 @@ public class MainActivity extends SimpleActivity {
private void toggleStroboscope() {
// use the old Camera API for stroboscope, the new Camera Manager is way too slow
if (isCameraPermissionGranted()) {
if (isCameraPermissionGranted() || Utils.isNougat()) {
if (mCameraImpl.toggleStroboscope()) {
mStroboscopeBar.setVisibility(mStroboscopeBar.getVisibility() == View.VISIBLE ? View.INVISIBLE : View.VISIBLE);
changeIconColor(mStroboscopeBar.getVisibility() == View.VISIBLE ? R.color.colorPrimary : R.color.translucent_white, mStroboscopeBtn);