Skip to content

Instantly share code, notes, and snippets.

@seahorsepip
Last active February 1, 2018 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seahorsepip/20276d67af20df093d2ce8b5910be1b4 to your computer and use it in GitHub Desktop.
Save seahorsepip/20276d67af20df093d2ce8b5910be1b4 to your computer and use it in GitHub Desktop.
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