add some handling for flashlight, sos and stroboscope cancelling each other

This commit is contained in:
tibbi 2019-08-20 23:05:44 +02:00
parent af58c36d67
commit 551b3350b2
3 changed files with 53 additions and 7 deletions

View File

@ -39,7 +39,7 @@ class MainActivity : SimpleActivity() {
appLaunched(BuildConfig.APPLICATION_ID)
mBus = BusProvider.instance
changeIconColor(config.backgroundColor.getContrastColor(), stroboscope_btn)
changeIconColor(getContrastColor(), stroboscope_btn)
bright_display_btn.setOnClickListener {
startActivity(Intent(applicationContext, BrightDisplayActivity::class.java))
@ -66,7 +66,7 @@ class MainActivity : SimpleActivity() {
mCameraImpl!!.handleCameraSetup()
checkState(MyCameraImpl.isFlashlightOn)
val contrastColor = config.backgroundColor.getContrastColor()
val contrastColor = getContrastColor()
changeIconColor(contrastColor, bright_display_btn)
bright_display_btn.beVisibleIf(config.brightDisplay)
sos_btn.beVisibleIf(config.sos)
@ -202,14 +202,18 @@ class MainActivity : SimpleActivity() {
private fun cameraPermissionGranted(isSOS: Boolean) {
if (isSOS) {
val isSOSRunning = mCameraImpl!!.toggleSOS()
sos_btn.setTextColor(if (isSOSRunning) getAdjustedPrimaryColor() else getContrastColor())
} else {
if (mCameraImpl!!.toggleStroboscope()) {
stroboscope_bar.beInvisibleIf(stroboscope_bar.isVisible())
changeIconColor(if (stroboscope_bar.isVisible()) getAdjustedPrimaryColor() else config.backgroundColor.getContrastColor(), stroboscope_btn)
changeIconColor(if (stroboscope_bar.isVisible()) getAdjustedPrimaryColor() else getContrastColor(), stroboscope_btn)
}
}
}
private fun getContrastColor() = config.backgroundColor.getContrastColor()
private fun releaseCamera() {
mCameraImpl?.releaseCamera()
mCameraImpl = null
@ -220,6 +224,17 @@ class MainActivity : SimpleActivity() {
checkState(event.isEnabled)
}
@Subscribe
fun stopStroboscope(event: Events.StopStroboscope) {
stroboscope_bar.beInvisible()
changeIconColor(getContrastColor(), stroboscope_btn)
}
@Subscribe
fun stopSOS(event: Events.StopSOS) {
sos_btn.setTextColor(getContrastColor())
}
private fun checkState(isEnabled: Boolean) {
if (isEnabled) {
enableFlashlight()
@ -233,12 +248,12 @@ class MainActivity : SimpleActivity() {
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
mIsFlashlightOn = true
changeIconColor(config.backgroundColor.getContrastColor(), stroboscope_btn)
changeIconColor(getContrastColor(), stroboscope_btn)
stroboscope_bar.beInvisible()
}
private fun disableFlashlight() {
changeIconColor(config.backgroundColor.getContrastColor(), flashlight_btn)
changeIconColor(getContrastColor(), flashlight_btn)
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
mIsFlashlightOn = false
}

View File

@ -31,6 +31,8 @@ class MyCameraImpl(val context: Context) {
private var shouldStroboscopeStop = false
@Volatile
private var isStroboscopeRunning = false
@Volatile
private var isSOSRunning = false
fun newInstance(context: Context) = MyCameraImpl(context)
}
@ -78,6 +80,25 @@ class MyCameraImpl(val context: Context) {
fun stopStroboscope() {
shouldStroboscopeStop = true
bus!!.post(Events.StopStroboscope())
}
fun toggleSOS(): Boolean {
if (isStroboscopeRunning) {
stopStroboscope()
}
if (isFlashlightOn) {
disableFlashlight()
}
isSOSRunning = !isSOSRunning
return isSOSRunning
}
fun stopSOS() {
isSOSRunning = false
bus!!.post(Events.StopSOS())
}
fun handleCameraSetup() {
@ -95,8 +116,9 @@ class MyCameraImpl(val context: Context) {
}
private fun setupCamera() {
if (isMarshmallow)
if (isMarshmallow) {
return
}
if (camera == null) {
initCamera()
@ -112,7 +134,6 @@ class MyCameraImpl(val context: Context) {
} catch (e: Exception) {
bus!!.post(Events.CameraUnavailable())
}
}
private fun checkFlashlight() {
@ -128,6 +149,12 @@ class MyCameraImpl(val context: Context) {
}
fun enableFlashlight() {
if (isSOSRunning) {
shouldEnableFlashlight = true
stopSOS()
return
}
shouldStroboscopeStop = true
if (isStroboscopeRunning) {
shouldEnableFlashlight = true

View File

@ -4,4 +4,8 @@ class Events {
class StateChanged(val isEnabled: Boolean)
class CameraUnavailable
class StopStroboscope
class StopSOS
}