Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Toggle ambient support in Android Wear 2.0 based on Drawer state (open or closed/peek)
private var mAmbientMode: AmbientMode? = null
private var mAmbientAttached = false
private val mDrawerStateCallback = object : WearableDrawerLayout.DrawerStateCallback() {
override fun onDrawerStateChanged(layout: WearableDrawerLayout?, newState: Int) {
super.onDrawerStateChanged(layout, newState)
if (newState == 0) {
if ((playback_drawer.isClosed || playback_drawer.isPeeking)) removeAmbientSupport()
else if (playback_drawer.isOpened) addAmbientSupport()
}
}
}
private fun addAmbientSupport() {
if (!mAmbientAttached) {
mAmbientAttached = true
if (mAmbientMode == null) AmbientMode.attachAmbientSupport(this@LibraryAltActivity)
else fragmentManager
.beginTransaction()
.add(mAmbientMode, AmbientMode.FRAGMENT_TAG)
.commit()
}
}
private fun removeAmbientSupport() {
if (mAmbientAttached) {
mAmbientAttached = false
if (mAmbientMode == null) {
mAmbientMode = fragmentManager.findFragmentByTag(AmbientMode.FRAGMENT_TAG) as AmbientMode?
}
if (mAmbientMode != null) {
fragmentManager
.beginTransaction()
.remove(mAmbientMode)
.commit()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment