restore flashlight/stroboscope state on device rotation

This commit is contained in:
tibbi 2018-04-05 12:12:25 +02:00
parent 71c9a038cd
commit 687dbe5e56
4 changed files with 33 additions and 9 deletions

View File

@ -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?) {

View File

@ -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)
}
}

View File

@ -13,7 +13,7 @@
android:layout_height="wrap_content">
<ImageView
android:id="@+id/toggle_btn"
android:id="@+id/flashlight_btn"
android:layout_width="@dimen/main_button_size"
android:layout_height="@dimen/main_button_size"
android:layout_marginBottom="@dimen/activity_margin"
@ -37,7 +37,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toggle_btn"/>
app:layout_constraintTop_toBottomOf="@+id/flashlight_btn"/>
<ImageView
android:id="@+id/stroboscope_btn"

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toggle_btn"
android:id="@+id/flashlight_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"/>