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,6 +50,7 @@ public class MyCameraImpl {
if (!mIsStroboscopeRunning)
disableFlashlight();
if (!Utils.isNougat()) {
if (mCamera == null) {
initCamera();
}
@ -58,6 +59,7 @@ public class MyCameraImpl {
Utils.showToast(mContext, R.string.camera_error);
return false;
}
}
if (mIsStroboscopeRunning) {
stopStroboscope();
@ -181,6 +183,7 @@ public class MyCameraImpl {
mBus.unregister(this);
}
mIsFlashlightOn = false;
mShouldStroboscopeStop = true;
}
private boolean isMarshmallow() {
@ -197,6 +200,20 @@ public class MyCameraImpl {
mShouldStroboscopeStop = false;
mIsStroboscopeRunning = true;
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;
}
}
} else {
if (mCamera == null) {
initCamera();
}
@ -230,6 +247,7 @@ public class MyCameraImpl {
} catch (RuntimeException ignored) {
}
}
mIsStroboscopeRunning = false;
mShouldStroboscopeStop = 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);