adding some stroboscope improvements

This commit is contained in:
tibbi 2019-08-23 20:24:41 +02:00
parent 9ac8d07cf1
commit 353fb14af3

View File

@ -25,6 +25,7 @@ class MyCameraImpl(val context: Context) {
private var bus: Bus? = null private var bus: Bus? = null
private var isMarshmallow = false private var isMarshmallow = false
private var shouldEnableFlashlight = false private var shouldEnableFlashlight = false
private var isStroboSOS = false // are we sending SOS, or casual stroboscope?
private var marshmallowCamera: MarshmallowCamera? = null private var marshmallowCamera: MarshmallowCamera? = null
@Volatile @Volatile
@ -32,6 +33,8 @@ class MyCameraImpl(val context: Context) {
@Volatile @Volatile
private var isStroboscopeRunning = false private var isStroboscopeRunning = false
@Volatile @Volatile
private var shouldSOSStop = false
@Volatile
private var isSOSRunning = false private var isSOSRunning = false
fun newInstance(context: Context) = MyCameraImpl(context) fun newInstance(context: Context) = MyCameraImpl(context)
@ -55,6 +58,7 @@ class MyCameraImpl(val context: Context) {
} }
fun toggleStroboscope(): Boolean { fun toggleStroboscope(): Boolean {
isStroboSOS = false
if (!isStroboscopeRunning) { if (!isStroboscopeRunning) {
disableFlashlight() disableFlashlight()
} }
@ -63,12 +67,13 @@ class MyCameraImpl(val context: Context) {
return false return false
} }
if (isStroboscopeRunning) { return if (isStroboscopeRunning) {
stopStroboscope() stopStroboscope()
false
} else { } else {
Thread(stroboscope).start() Thread(stroboscope).start()
true
} }
return true
} }
fun stopStroboscope() { fun stopStroboscope() {
@ -77,6 +82,7 @@ class MyCameraImpl(val context: Context) {
} }
fun toggleSOS(): Boolean { fun toggleSOS(): Boolean {
isStroboSOS = true
if (isStroboscopeRunning) { if (isStroboscopeRunning) {
stopStroboscope() stopStroboscope()
} }
@ -89,12 +95,17 @@ class MyCameraImpl(val context: Context) {
disableFlashlight() disableFlashlight()
} }
isSOSRunning = !isSOSRunning if (isSOSRunning) {
stopSOS()
} else {
isSOSRunning = true
Thread(stroboscope).start()
}
return isSOSRunning return isSOSRunning
} }
fun stopSOS() { fun stopSOS() {
isSOSRunning = false shouldSOSStop = true
bus!!.post(Events.StopSOS()) bus!!.post(Events.StopSOS())
} }
@ -160,14 +171,9 @@ class MyCameraImpl(val context: Context) {
} }
fun enableFlashlight() { fun enableFlashlight() {
if (isSOSRunning) {
shouldEnableFlashlight = true
stopSOS()
return
}
shouldStroboscopeStop = true shouldStroboscopeStop = true
if (isStroboscopeRunning) { shouldSOSStop = true
if (isStroboscopeRunning || isSOSRunning) {
shouldEnableFlashlight = true shouldEnableFlashlight = true
return return
} }
@ -189,7 +195,7 @@ class MyCameraImpl(val context: Context) {
} }
private fun disableFlashlight() { private fun disableFlashlight() {
if (isStroboscopeRunning) { if (isStroboscopeRunning || isSOSRunning) {
return return
} }
@ -228,15 +234,21 @@ class MyCameraImpl(val context: Context) {
bus?.unregister(this) bus?.unregister(this)
isFlashlightOn = false isFlashlightOn = false
shouldStroboscopeStop = true shouldStroboscopeStop = true
shouldSOSStop = true
} }
private val stroboscope = Runnable { private val stroboscope = Runnable {
if (isStroboscopeRunning) { if (isStroboscopeRunning || isSOSRunning) {
return@Runnable return@Runnable
} }
if (isStroboSOS) {
shouldSOSStop = false
isSOSRunning = true
} else {
shouldStroboscopeStop = false shouldStroboscopeStop = false
isStroboscopeRunning = true isStroboscopeRunning = true
}
if (isNougatPlus()) { if (isNougatPlus()) {
while (!shouldStroboscopeStop) { while (!shouldStroboscopeStop) {
@ -247,6 +259,7 @@ class MyCameraImpl(val context: Context) {
Thread.sleep(stroboFrequency) Thread.sleep(stroboFrequency)
} catch (e: Exception) { } catch (e: Exception) {
shouldStroboscopeStop = true shouldStroboscopeStop = true
shouldSOSStop = true
} }
} }
} else { } else {
@ -289,8 +302,13 @@ class MyCameraImpl(val context: Context) {
} }
} }
isStroboscopeRunning = false if (isStroboSOS) {
shouldSOSStop = false
isSOSRunning = false
} else {
shouldStroboscopeStop = false shouldStroboscopeStop = false
isStroboscopeRunning = false
}
if (shouldEnableFlashlight) { if (shouldEnableFlashlight) {
enableFlashlight() enableFlashlight()