diff --git a/app/src/main/java/de/westnordost/streetcomplete/screens/main/controls/LocationStateButton.kt b/app/src/main/java/de/westnordost/streetcomplete/screens/main/controls/LocationStateButton.kt index 27a6140218..ed4aaf0a2f 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/screens/main/controls/LocationStateButton.kt +++ b/app/src/main/java/de/westnordost/streetcomplete/screens/main/controls/LocationStateButton.kt @@ -4,13 +4,15 @@ import android.content.Context import android.content.res.ColorStateList import android.content.res.TypedArray import android.graphics.drawable.Animatable -import android.os.Parcel +import android.os.Bundle import android.os.Parcelable import android.util.AttributeSet import android.view.View -import androidx.annotation.Keep import androidx.appcompat.widget.AppCompatImageButton +import androidx.core.os.bundleOf import de.westnordost.streetcomplete.R +import de.westnordost.streetcomplete.util.ktx.parcelable +import de.westnordost.streetcomplete.util.ktx.serializable /** * An image button which shows the current location state @@ -84,49 +86,20 @@ class LocationStateButton @JvmOverloads constructor( return drawableState } - public override fun onSaveInstanceState(): Parcelable { - val superState = super.onSaveInstanceState() - val ss = SavedState(superState) - ss.state = state - ss.activated = isActivated - ss.navigation = isNavigation - return ss - } - - public override fun onRestoreInstanceState(s: Parcelable) { - val ss = s as SavedState - super.onRestoreInstanceState(ss.superState) - state = ss.state - isActivated = ss.activated - isNavigation = ss.navigation - requestLayout() - } - - internal class SavedState : BaseSavedState { - var state: State = State.DENIED - var activated = false - var navigation = false - - constructor(superState: Parcelable?) : super(superState) - constructor(parcel: Parcel) : super(parcel) { - state = State.valueOf(parcel.readString()!!) - activated = parcel.readInt() == 1 - navigation = parcel.readInt() == 1 - } - - override fun writeToParcel(out: Parcel, flags: Int) { - super.writeToParcel(out, flags) - out.writeString(state.name) - out.writeInt(if (activated) 1 else 0) - out.writeInt(if (navigation) 1 else 0) - } - - companion object { - @JvmField @Keep - val CREATOR = object : Parcelable.Creator { - override fun createFromParcel(parcel: Parcel) = SavedState(parcel) - override fun newArray(size: Int) = arrayOfNulls(size) - } + override fun onSaveInstanceState() = bundleOf( + KEY_SUPER_STATE to super.onSaveInstanceState(), + KEY_STATE to state, + KEY_IS_ACTIVATED to isActivated, + KEY_IS_NAVIGATION to isNavigation, + ) + + override fun onRestoreInstanceState(state: Parcelable?) { + if (state is Bundle) { + super.onRestoreInstanceState(state.parcelable(KEY_SUPER_STATE)) + this.state = state.serializable(KEY_STATE)!! + isActivated = state.getBoolean(KEY_IS_ACTIVATED) + isNavigation = state.getBoolean(KEY_IS_NAVIGATION) + requestLayout() } } @@ -148,4 +121,11 @@ class LocationStateButton @JvmOverloads constructor( R.attr.state_searching, R.attr.state_updating ).subList(0, ordinal) + + companion object { + private const val KEY_SUPER_STATE = "superState" + private const val KEY_STATE = "state" + private const val KEY_IS_ACTIVATED = "isActivated" + private const val KEY_IS_NAVIGATION = "isNavigation" + } } diff --git a/app/src/main/java/de/westnordost/streetcomplete/util/ktx/Bundle.kt b/app/src/main/java/de/westnordost/streetcomplete/util/ktx/Bundle.kt new file mode 100644 index 0000000000..0ecbebfd06 --- /dev/null +++ b/app/src/main/java/de/westnordost/streetcomplete/util/ktx/Bundle.kt @@ -0,0 +1,18 @@ +package de.westnordost.streetcomplete.util.ktx + +import android.os.Build +import android.os.Bundle +import android.os.Parcelable +import androidx.core.os.BundleCompat +import java.io.Serializable + +inline fun Bundle.serializable(key: String?): T? = + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { + getSerializable(key, T::class.java) + } else { + @Suppress("DEPRECATION") + getSerializable(key) as? T + } + +inline fun Bundle.parcelable(key: String?): T? = + BundleCompat.getParcelable(this, key, T::class.java)