[Android] Simplify windows insets

Nicolas Pomepuy git at videolan.org
Tue Mar 29 11:54:28 UTC 2022


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Tue Mar 29 09:29:05 2022 +0200| [4df5f2285b09165bcacafdda2a25cb00908eeea4] | committer: Nicolas Pomepuy

Simplify windows insets

> https://code.videolan.org/videolan/vlc-android/commit/4df5f2285b09165bcacafdda2a25cb00908eeea4
---

 .../vlc-android/res/layout/playlist_activity.xml   |  2 +-
 .../vlc/gui/AudioPlayerContainerActivity.kt        | 30 ++++++++++++----------
 .../src/org/videolan/vlc/gui/InfoActivity.kt       |  2 +-
 .../src/org/videolan/vlc/gui/MainActivity.kt       |  1 -
 .../src/org/videolan/vlc/gui/PlaylistActivity.kt   |  2 +-
 .../src/org/videolan/vlc/gui/SecondaryActivity.kt  |  1 -
 .../src/org/videolan/vlc/gui/audio/AudioPlayer.kt  |  3 ++-
 7 files changed, 22 insertions(+), 19 deletions(-)

diff --git a/application/vlc-android/res/layout/playlist_activity.xml b/application/vlc-android/res/layout/playlist_activity.xml
index cad94f33f..efe575b13 100644
--- a/application/vlc-android/res/layout/playlist_activity.xml
+++ b/application/vlc-android/res/layout/playlist_activity.xml
@@ -54,7 +54,7 @@
                 <androidx.appcompat.widget.Toolbar
                         android:id="@+id/main_toolbar"
                         android:layout_width="match_parent"
-                        android:layout_height="48dp"
+                        android:layout_height="?attr/actionBarSize"
                         vlc:navigationContentDescription="@string/abc_action_bar_up_description"
                         vlc:popupTheme="?attr/toolbar_popup_style"
                         vlc:theme="@style/Toolbar.VLC"
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.kt b/application/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.kt
index 7aa763e81..23d71e6b3 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.kt
@@ -109,7 +109,7 @@ open class AudioPlayerContainerActivity : BaseActivity(), KeycodeListener {
     val menu: Menu
         get() = toolbar.menu
 
-    open fun needsTopInset(): Boolean = true
+    open fun isTransparent(): Boolean = false
 
     open val insetListener: (Insets) -> Unit = {}
 
@@ -141,7 +141,7 @@ open class AudioPlayerContainerActivity : BaseActivity(), KeycodeListener {
             savedInstanceState.getIntegerArrayList(SHOWN_TIPS)?.let { shownTips.addAll(it) }
         }
         super.onCreate(savedInstanceState)
-        if (AndroidUtil.isLolliPopOrLater) WindowCompat.setDecorFitsSystemWindows(window, false)
+        if (AndroidUtil.isLolliPopOrLater && this is MainActivity) WindowCompat.setDecorFitsSystemWindows(window, false)
 
         volumeControlStream = AudioManager.STREAM_MUSIC
         registerLiveData()
@@ -149,18 +149,22 @@ open class AudioPlayerContainerActivity : BaseActivity(), KeycodeListener {
         ViewCompat.setOnApplyWindowInsetsListener(findViewById(android.R.id.content)) { view, windowInsets ->
             val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
             view.updateLayoutParams<ViewGroup.MarginLayoutParams> {
-                leftMargin = insets.left
-                rightMargin = insets.right
-                if (!needsTopInset()) bottomMargin = insets.bottom
-                if (needsTopInset()) topMargin = insets.top else {
+                if (isTransparent()) {
                     val toolbarLayoutParams = findViewById<Toolbar>(R.id.main_toolbar).layoutParams as ViewGroup.MarginLayoutParams
                     toolbarLayoutParams.topMargin = insets.top
                 }
-                val bottomNavigationView = findViewById<BottomNavigationView?>(R.id.navigation)
-                bottomNavigationView?.setPadding(bottomNavigationView.paddingLeft, bottomNavigationView.paddingTop, bottomNavigationView.paddingRight, insets.bottom)
-                bottomInset = insets.bottom
-                setContentBottomPadding()
-                insetListener.invoke(insets)
+                if (this at AudioPlayerContainerActivity is MainActivity) {
+                    leftMargin = insets.left
+                    rightMargin = insets.right
+                    if (isTablet()) bottomMargin = insets.bottom
+                    topMargin = insets.top
+                    topInset = insets.top
+                    val bottomNavigationView = findViewById<BottomNavigationView?>(R.id.navigation)
+                    bottomNavigationView?.setPadding(bottomNavigationView.paddingLeft, bottomNavigationView.paddingTop, bottomNavigationView.paddingRight, insets.bottom)
+                    bottomInset = insets.bottom
+                    setContentBottomPadding()
+                    insetListener.invoke(insets)
+                }
             }
 
             WindowInsetsCompat.CONSUMED
@@ -228,7 +232,7 @@ open class AudioPlayerContainerActivity : BaseActivity(), KeycodeListener {
                 val translationpercent = min(1f, max(0f, slideOffset))
                 bottomBehavior?.let { bottomBehavior ->
                     bottomBar?.let { bottomBar ->
-                        val translation = min((translationpercent * audioPlayerContainer.height / 2), bottomBar.height.toFloat()) - if (!needsTopInset()) topInset else 0
+                        val translation = min((translationpercent * audioPlayerContainer.height / 2), bottomBar.height.toFloat()) - if (isTablet()) topInset else 0
                         bottomBehavior.translate(bottomBar, translation)
                     }
                 }
@@ -249,7 +253,7 @@ open class AudioPlayerContainerActivity : BaseActivity(), KeycodeListener {
     }
 
     private fun hideStatusIfNeeded(newState: Int) {
-        if (!needsTopInset()) {
+        if (isTransparent()) {
             WindowInsetsControllerCompat(window, window.decorView).apply {
                 systemBarsBehavior = if (newState == STATE_EXPANDED) WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE else WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_TOUCH
                 if (newState == STATE_EXPANDED) hide(WindowInsetsCompat.Type.statusBars()) else show(WindowInsetsCompat.Type.statusBars())
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/InfoActivity.kt b/application/vlc-android/src/org/videolan/vlc/gui/InfoActivity.kt
index c8638bc4e..2bd95c019 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/InfoActivity.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/InfoActivity.kt
@@ -66,7 +66,7 @@ class InfoActivity : AudioPlayerContainerActivity(), View.OnClickListener, PathA
     private lateinit var model: InfoModel
 
     internal lateinit var binding: InfoActivityBinding
-    override fun needsTopInset() = false
+    override fun isTransparent() = true
     override val insetListener = {insets:Insets ->
         (binding.mlItemResolution.layoutParams as ConstraintLayout.LayoutParams).topMargin = insets.top + 16.dp
     }
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/MainActivity.kt b/application/vlc-android/src/org/videolan/vlc/gui/MainActivity.kt
index 7d756c43d..d56615e5e 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/MainActivity.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/MainActivity.kt
@@ -77,7 +77,6 @@ class MainActivity : ContentActivity(),
         }
     private lateinit var mediaLibrary: Medialibrary
     private var scanNeeded = false
-    override fun needsTopInset() = !isTablet()
 
     override fun getSnackAnchorView(overAudioPlayer:Boolean): View? {
         val view = super.getSnackAnchorView(overAudioPlayer)
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.kt b/application/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.kt
index 50489e661..c7aba4ba1 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.kt
@@ -86,7 +86,7 @@ open class PlaylistActivity : AudioPlayerContainerActivity(), IEventsHandler<Med
     private var isPlaylist: Boolean = false
     private lateinit var viewModel: PlaylistViewModel
     private var itemTouchHelper: ItemTouchHelper? = null
-    override fun needsTopInset(): Boolean = false
+    override fun isTransparent() = true
 
     public override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/SecondaryActivity.kt b/application/vlc-android/src/org/videolan/vlc/gui/SecondaryActivity.kt
index a4b2fe6b9..1b50fb498 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/SecondaryActivity.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/SecondaryActivity.kt
@@ -72,7 +72,6 @@ class SecondaryActivity : ContentActivity(), IDialogManager {
         return intent.getStringExtra(KEY_FRAGMENT) == STORAGE_BROWSER_ONBOARDING
     }
 
-    override fun needsTopInset() = true
 
     override fun forcedTheme() =
         if (intent.getStringExtra(KEY_FRAGMENT) == STORAGE_BROWSER_ONBOARDING) R.style.Theme_VLC_Black
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt b/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
index 895a719bd..293fefea2 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
@@ -61,6 +61,7 @@ import org.videolan.vlc.R
 import org.videolan.vlc.databinding.AudioPlayerBinding
 import org.videolan.vlc.gui.AudioPlayerContainerActivity
 import org.videolan.vlc.gui.InfoActivity
+import org.videolan.vlc.gui.MainActivity
 import org.videolan.vlc.gui.dialogs.CtxActionReceiver
 import org.videolan.vlc.gui.dialogs.PlaybackSpeedDialog
 import org.videolan.vlc.gui.dialogs.SleepTimerDialog
@@ -240,7 +241,7 @@ class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher, IAudioPlay
     fun setBottomMargin() {
         (binding.playPause.layoutParams as? ConstraintLayout.LayoutParams)?.let {
             val audioPlayerContainerActivity = (requireActivity() as AudioPlayerContainerActivity)
-            if (audioPlayerContainerActivity.needsTopInset()) it.bottomMargin = it.bottomMargin + audioPlayerContainerActivity.bottomInset
+            if (audioPlayerContainerActivity is MainActivity && !audioPlayerContainerActivity.isTablet()) it.bottomMargin = it.bottomMargin + audioPlayerContainerActivity.bottomInset
         }
     }
 



More information about the Android mailing list