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 d89aa44..ec88de9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/flashlight/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/flashlight/activities/MainActivity.kt @@ -25,9 +25,12 @@ import kotlinx.android.synthetic.main.activity_main.* class MainActivity : SimpleActivity() { private val MAX_STROBO_DELAY = 2000 private val MIN_STROBO_DELAY = 30 + private val FLASHLIGHT_STATE = "flashlight_state" + private val STROBOSCOPE_STATE = "stroboscope_state" private var mBus: Bus? = null private var mCameraImpl: MyCameraImpl? = null + private var mIsFlashlightOn = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -41,7 +44,7 @@ class MainActivity : SimpleActivity() { startActivity(Intent(applicationContext, BrightDisplayActivity::class.java)) } - toggle_btn.setOnClickListener { + flashlight_btn.setOnClickListener { mCameraImpl!!.toggleFlashlight() } @@ -100,6 +103,25 @@ class MainActivity : SimpleActivity() { return true } + override fun onSaveInstanceState(outState: Bundle) { + outState.putBoolean(FLASHLIGHT_STATE, mIsFlashlightOn) + outState.putBoolean(STROBOSCOPE_STATE, stroboscope_bar.isVisible()) + super.onSaveInstanceState(outState) + } + + override fun onRestoreInstanceState(savedInstanceState: Bundle) { + super.onRestoreInstanceState(savedInstanceState) + val isFlashlightOn = savedInstanceState.getBoolean(FLASHLIGHT_STATE, false) + if (isFlashlightOn) { + mCameraImpl!!.toggleFlashlight() + } + + val isStroboscopeOn = savedInstanceState.getBoolean(STROBOSCOPE_STATE, false) + if (isStroboscopeOn) { + toggleStroboscope() + } + } + private fun launchSettings() { startActivity(Intent(applicationContext, SettingsActivity::class.java)) } @@ -185,16 +207,18 @@ class MainActivity : SimpleActivity() { } private fun enableFlashlight() { - changeIconColor(getAdjustedPrimaryColor(), toggle_btn) + changeIconColor(getAdjustedPrimaryColor(), flashlight_btn) window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) + mIsFlashlightOn = true changeIconColor(config.backgroundColor.getContrastColor(), stroboscope_btn) stroboscope_bar.beInvisible() } private fun disableFlashlight() { - changeIconColor(config.backgroundColor.getContrastColor(), toggle_btn) + changeIconColor(config.backgroundColor.getContrastColor(), flashlight_btn) window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) + mIsFlashlightOn = false } private fun changeIconColor(color: Int, imageView: ImageView?) { diff --git a/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/MyWidgetProvider.kt b/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/MyWidgetProvider.kt index 1562bb7..75ecb67 100644 --- a/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/MyWidgetProvider.kt +++ b/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/MyWidgetProvider.kt @@ -31,8 +31,8 @@ class MyWidgetProvider : AppWidgetProvider() { val views = RemoteViews(context.packageName, R.layout.widget) val pendingIntent = PendingIntent.getBroadcast(context, it, intent, 0) - views.setOnClickPendingIntent(R.id.toggle_btn, pendingIntent) - views.setImageViewBitmap(R.id.toggle_btn, bmp) + views.setOnClickPendingIntent(R.id.flashlight_btn, pendingIntent) + views.setImageViewBitmap(R.id.flashlight_btn, bmp) appWidgetManager.updateAppWidget(it, views) } } @@ -62,7 +62,7 @@ class MyWidgetProvider : AppWidgetProvider() { val appWidgetManager = AppWidgetManager.getInstance(context) appWidgetManager.getAppWidgetIds(getComponentName(context)).forEach { val views = RemoteViews(context.packageName, R.layout.widget) - views.setImageViewBitmap(R.id.toggle_btn, bmp) + views.setImageViewBitmap(R.id.flashlight_btn, bmp) appWidgetManager.updateAppWidget(it, views) } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 4202685..4bfc8d7 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -13,7 +13,7 @@ android:layout_height="wrap_content"> + app:layout_constraintTop_toBottomOf="@+id/flashlight_btn"/>