add a stroboscope effect

This commit is contained in:
tibbi 2016-09-25 13:16:51 +02:00
parent b45fb35b0f
commit ac96e9b2e5
7 changed files with 146 additions and 12 deletions

View File

@ -2,6 +2,7 @@ package com.simplemobiletools.flashlight;
import android.content.Context;
import android.hardware.Camera;
import android.os.Handler;
import android.util.Log;
import com.squareup.otto.Bus;
@ -13,9 +14,12 @@ public class MyCameraImpl {
private static Bus mBus;
private Context mContext;
private MarshmallowCamera mMarshmallowCamera;
private volatile boolean mShouldStroboscopeStop;
private volatile boolean mIsStroboscopeRunning;
private static boolean mIsFlashlightOn;
private static boolean mIsMarshmallow;
private static boolean mShouldEnableFlashlight;
public MyCameraImpl(Context cxt) {
mContext = cxt;
@ -35,6 +39,31 @@ public class MyCameraImpl {
handleCameraSetup();
}
public boolean toggleStroboscope() {
if (!mIsStroboscopeRunning)
disableFlashlight();
if (mCamera == null) {
initCamera();
}
if (mCamera == null) {
Utils.showToast(mContext, R.string.camera_error);
return false;
}
if (mIsStroboscopeRunning) {
stopStroboscope();
} else {
new Thread(stroboscope).start();
}
return true;
}
private void stopStroboscope() {
mShouldStroboscopeStop = true;
}
public void handleCameraSetup() {
if (mIsMarshmallow) {
setupMarshmallowCamera();
@ -55,15 +84,19 @@ public class MyCameraImpl {
return;
if (mCamera == null) {
try {
mCamera = Camera.open();
mParams = mCamera.getParameters();
mParams.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
mCamera.setParameters(mParams);
} catch (Exception e) {
Log.e(TAG, "setup mCamera " + e.getMessage());
mBus.post(new Events.CameraUnavailable());
}
initCamera();
}
}
private void initCamera() {
try {
mCamera = Camera.open();
mParams = mCamera.getParameters();
mParams.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
mCamera.setParameters(mParams);
} catch (Exception e) {
Log.e(TAG, "setup mCamera " + e.getMessage());
mBus.post(new Events.CameraUnavailable());
}
}
@ -76,6 +109,12 @@ public class MyCameraImpl {
}
public void enableFlashlight() {
mShouldStroboscopeStop = true;
if (mIsStroboscopeRunning) {
mShouldEnableFlashlight = true;
return;
}
mIsFlashlightOn = true;
if (mIsMarshmallow) {
toggleMarshmallowFlashlight(true);
@ -88,12 +127,19 @@ public class MyCameraImpl {
mCamera.setParameters(mParams);
mCamera.startPreview();
}
mBus.post(new Events.StateChanged(true));
Runnable mainRunnable = new Runnable() {
@Override
public void run() {
mBus.post(new Events.StateChanged(true));
}
};
new Handler(mContext.getMainLooper()).post(mainRunnable);
}
private void disableFlashlight() {
mIsFlashlightOn = false;
if (isMarshmallow()) {
if (mIsMarshmallow) {
toggleMarshmallowFlashlight(false);
} else {
if (mCamera == null || mParams == null) {
@ -129,4 +175,51 @@ public class MyCameraImpl {
private boolean isMarshmallow() {
return android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M;
}
private Runnable stroboscope = new Runnable() {
@Override
public void run() {
if (mIsStroboscopeRunning) {
return;
}
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(500);
mCamera.setParameters(torchOff);
Thread.sleep(500);
} catch (InterruptedException ignored) {
mShouldStroboscopeStop = true;
} catch (RuntimeException ignored) {
mShouldStroboscopeStop = true;
}
}
if (mCamera != null) {
mCamera.setParameters(torchOff);
mCamera.release();
mCamera = null;
}
mIsStroboscopeRunning = false;
mShouldStroboscopeStop = false;
if (mShouldEnableFlashlight) {
enableFlashlight();
mShouldEnableFlashlight = false;
}
}
};
}

View File

@ -1,8 +1,12 @@
package com.simplemobiletools.flashlight.activities;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
@ -24,6 +28,8 @@ import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends SimpleActivity {
private static final int CAMERA_PERMISSION = 1;
@BindView(R.id.toggle_btn) ImageView mToggleBtn;
@BindView(R.id.bright_display_btn) ImageView mBrightDisplayBtn;
@BindView(R.id.stroboscope_btn) ImageView mStroboscopeBtn;
@ -70,6 +76,7 @@ public class MainActivity extends SimpleActivity {
@OnClick(R.id.toggle_btn)
public void toggleFlashlight() {
mStroboscopeBar.setVisibility(View.INVISIBLE);
mCameraImpl.toggleFlashlight();
}
@ -79,8 +86,33 @@ public class MainActivity extends SimpleActivity {
}
@OnClick(R.id.stroboscope_btn)
public void launchStroboscope() {
public void tryToggleStroboscope() {
toggleStroboscope();
}
private void toggleStroboscope() {
// use the old Camera API for stroboscope, the new Camera Manager is way too slow
if (isCameraPermissionGranted()) {
if (mCameraImpl.toggleStroboscope()) {
mStroboscopeBar.setVisibility(mStroboscopeBar.getVisibility() == View.VISIBLE ? View.INVISIBLE : View.VISIBLE);
}
} else {
final String[] permissions = {Manifest.permission.CAMERA};
ActivityCompat.requestPermissions(this, permissions, CAMERA_PERMISSION);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == CAMERA_PERMISSION) {
if (isCameraPermissionGranted()) {
toggleStroboscope();
} else {
Utils.showToast(getApplicationContext(), R.string.camera_permission);
}
}
}
@Override
@ -123,6 +155,10 @@ public class MainActivity extends SimpleActivity {
}
}
private boolean isCameraPermissionGranted() {
return ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
}
@Subscribe
public void stateChangedEvent(Events.StateChanged event) {
if (event.getIsEnabled()) {

View File

@ -3,6 +3,7 @@
<string name="app_launcher_name">Flashlight</string>
<string name="camera_error">Beanspruchen der Kamera fehlgeschlagen</string>
<string name="ok">OK</string>
<string name="camera_permission">Camera permission is necessary for proper stroboscope effect</string>
<!-- Settings -->
<string name="settings">Einstellungen</string>

View File

@ -3,6 +3,7 @@
<string name="app_launcher_name">Flashlight</string>
<string name="camera_error">Rilevamento fotocamera fallito</string>
<string name="ok">OK</string>
<string name="camera_permission">Camera permission is necessary for proper stroboscope effect</string>
<!-- Settings -->
<string name="settings">Impostazioni</string>

View File

@ -3,6 +3,7 @@
<string name="app_launcher_name">Flashlight</string>
<string name="camera_error">カメラの取得に失敗しました</string>
<string name="ok">OK</string>
<string name="camera_permission">Camera permission is necessary for proper stroboscope effect</string>
<!-- Settings -->
<string name="settings">設定</string>

View File

@ -3,6 +3,7 @@
<string name="app_launcher_name">Flashlight</string>
<string name="camera_error">Det gick inte att komma åt kameran</string>
<string name="ok">OK</string>
<string name="camera_permission">Camera permission is necessary for proper stroboscope effect</string>
<!-- Settings -->
<string name="settings">Inställningar</string>

View File

@ -3,6 +3,7 @@
<string name="app_launcher_name">Flashlight</string>
<string name="camera_error">Obtaining the camera failed</string>
<string name="ok">OK</string>
<string name="camera_permission">Camera permission is necessary for proper stroboscope effect</string>
<!-- Settings -->
<string name="settings">Settings</string>