@@ -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 ;
0 commit comments