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

View File

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

View File

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