From 8c7cbd7f3ad78a2a456a83f737d3ac0cb058c5dd Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 5 Apr 2018 13:31:33 +0200 Subject: [PATCH] save last used stroboscope frequency --- .../flashlight/activities/MainActivity.kt | 10 ++++++---- .../simplemobiletools/flashlight/helpers/Config.kt | 8 ++++++++ .../flashlight/helpers/Constants.kt | 2 ++ .../flashlight/helpers/MyCameraImpl.kt | 12 +++++++----- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/flashlight/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/flashlight/activities/MainActivity.kt index ec88de9..e645073 100644 --- a/app/src/main/kotlin/com/simplemobiletools/flashlight/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/flashlight/activities/MainActivity.kt @@ -23,8 +23,8 @@ import com.squareup.otto.Subscribe import kotlinx.android.synthetic.main.activity_main.* class MainActivity : SimpleActivity() { - private val MAX_STROBO_DELAY = 2000 - private val MIN_STROBO_DELAY = 30 + private val MAX_STROBO_DELAY = 2000L + private val MIN_STROBO_DELAY = 30L private val FLASHLIGHT_STATE = "flashlight_state" private val STROBOSCOPE_STATE = "stroboscope_state" @@ -148,12 +148,14 @@ class MainActivity : SimpleActivity() { toggleStroboscope() } - stroboscope_bar.max = MAX_STROBO_DELAY - MIN_STROBO_DELAY - stroboscope_bar.progress = stroboscope_bar.max / 2 + stroboscope_bar.max = (MAX_STROBO_DELAY - MIN_STROBO_DELAY).toInt() + stroboscope_bar.progress = config.stroboscopeProgress stroboscope_bar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener { override fun onProgressChanged(seekBar: SeekBar, progress: Int, b: Boolean) { val frequency = stroboscope_bar.max - progress + MIN_STROBO_DELAY mCameraImpl?.stroboFrequency = frequency + config.stroboscopeFrequency = frequency + config.stroboscopeProgress = progress } override fun onStartTrackingTouch(seekBar: SeekBar) { diff --git a/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/Config.kt index 6667e84..27409a9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/Config.kt @@ -19,4 +19,12 @@ class Config(context: Context) : BaseConfig(context) { var turnFlashlightOn: Boolean get() = prefs.getBoolean(TURN_FLASHLIGHT_ON, false) set(turnFlashlightOn) = prefs.edit().putBoolean(TURN_FLASHLIGHT_ON, turnFlashlightOn).apply() + + var stroboscopeProgress: Int + get() = prefs.getInt(STROBOSCOPE_PROGRESS, 1000) + set(stroboscopeProgress) = prefs.edit().putInt(STROBOSCOPE_PROGRESS, stroboscopeProgress).apply() + + var stroboscopeFrequency: Long + get() = prefs.getLong(STROBOSCOPE_FREQUENCY, 1000L) + set(stroboscopeFrequency) = prefs.edit().putLong(STROBOSCOPE_FREQUENCY, stroboscopeFrequency).apply() } diff --git a/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/Constants.kt index 5a98abd..9341270 100644 --- a/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/Constants.kt @@ -6,3 +6,5 @@ const val TURN_FLASHLIGHT_ON = "turn_flashlight_on" const val IS_ENABLED = "is_enabled" const val TOGGLE = "toggle" const val TOGGLE_WIDGET_UI = "toggle_widget_ui" +const val STROBOSCOPE_FREQUENCY = "stroboscope_frequency" +const val STROBOSCOPE_PROGRESS = "stroboscope_progress" diff --git a/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/MyCameraImpl.kt b/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/MyCameraImpl.kt index f93898e..025e9a2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/MyCameraImpl.kt +++ b/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/MyCameraImpl.kt @@ -8,13 +8,14 @@ import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.helpers.isMarshmallowPlus import com.simplemobiletools.commons.helpers.isNougatPlus import com.simplemobiletools.flashlight.R +import com.simplemobiletools.flashlight.extensions.config import com.simplemobiletools.flashlight.extensions.updateWidgets import com.simplemobiletools.flashlight.models.Events import com.squareup.otto.Bus import java.io.IOException class MyCameraImpl(val context: Context) { - var stroboFrequency = 1000 + var stroboFrequency = 1000L companion object { var isFlashlightOn = false @@ -43,6 +44,7 @@ class MyCameraImpl(val context: Context) { } handleCameraSetup() + stroboFrequency = context.config.stroboscopeFrequency } fun toggleFlashlight() { @@ -202,9 +204,9 @@ class MyCameraImpl(val context: Context) { while (!shouldStroboscopeStop) { try { marshmallowCamera!!.toggleMarshmallowFlashlight(bus!!, true) - Thread.sleep(stroboFrequency.toLong()) + Thread.sleep(stroboFrequency) marshmallowCamera!!.toggleMarshmallowFlashlight(bus!!, false) - Thread.sleep(stroboFrequency.toLong()) + Thread.sleep(stroboFrequency) } catch (e: Exception) { shouldStroboscopeStop = true } @@ -231,9 +233,9 @@ class MyCameraImpl(val context: Context) { while (!shouldStroboscopeStop) { try { camera!!.parameters = torchOn - Thread.sleep(stroboFrequency.toLong()) + Thread.sleep(stroboFrequency) camera!!.parameters = torchOff - Thread.sleep(stroboFrequency.toLong()) + Thread.sleep(stroboFrequency) } catch (e: Exception) { } }