[Android] Fix video track option focus and avoid using a custom view

Nicolas Pomepuy git at videolan.org
Wed Jan 20 09:54:28 UTC 2021


vlc-android | branch: 3.3.x | Nicolas Pomepuy <nicolas at videolabs.io> | Tue Jan 12 11:06:28 2021 +0100| [776d1646512037495f920fde6fde745dac9a3904] | committer: Nicolas Pomepuy

Fix video track option focus and avoid using a custom view

Fixes #1608

(cherry picked from commit 8621e3d0bc1ad5a16ba0b23f4abd87f17c269d20)

> https://code.videolan.org/videolan/vlc-android/commit/776d1646512037495f920fde6fde745dac9a3904
---

 .../layout/player_overlay_track_option_item.xml    |  2 +-
 .../videolan/vlc/gui/dialogs/VideoTracksDialog.kt  | 39 +++++++-----
 .../org/videolan/vlc/gui/view/VideoTrackOption.kt  | 72 ----------------------
 3 files changed, 24 insertions(+), 89 deletions(-)

diff --git a/application/vlc-android/res/layout/player_overlay_track_option_item.xml b/application/vlc-android/res/layout/player_overlay_track_option_item.xml
index a4832e2ae..9e841d653 100644
--- a/application/vlc-android/res/layout/player_overlay_track_option_item.xml
+++ b/application/vlc-android/res/layout/player_overlay_track_option_item.xml
@@ -26,7 +26,7 @@
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:background="?android:attr/selectableItemBackground"
-        android:orientation="horizontal">
+        android:focusable="true">
 
     <ImageView
             android:id="@+id/option_icon"
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/dialogs/VideoTracksDialog.kt b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/VideoTracksDialog.kt
index 4345b6582..c196125b2 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/dialogs/VideoTracksDialog.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/VideoTracksDialog.kt
@@ -28,7 +28,9 @@ import android.os.Bundle
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
+import android.widget.ImageView
 import android.widget.LinearLayout
+import android.widget.TextView
 import androidx.annotation.DrawableRes
 import androidx.core.content.ContextCompat
 import androidx.recyclerview.widget.LinearLayoutManager
@@ -44,7 +46,7 @@ import org.videolan.vlc.PlaybackService
 import org.videolan.vlc.R
 import org.videolan.vlc.databinding.PlayerOverlayTracksBinding
 import org.videolan.vlc.gui.dialogs.adapters.TrackAdapter
-import org.videolan.vlc.gui.view.VideoTrackOption
+import org.videolan.vlc.gui.helpers.getBitmapFromDrawable
 
 @ObsoleteCoroutinesApi
 @ExperimentalCoroutinesApi
@@ -130,19 +132,20 @@ class VideoTracksDialog : VLCBottomSheetDialogFragment() {
         binding.tracksSeparator2.isEnabled = false
 
 
-        binding.audioTracks.options.addView(generateSeparator())
-        binding.audioTracks.options.addView(generateOptionItem(getString(R.string.audio_delay), R.drawable.ic_delay, VideoTrackOption.AUDIO_DELAY))
-        binding.audioTracks.options.addView(generateSeparator(true))
+
+        generateSeparator(binding.audioTracks.options)
+        generateOptionItem(binding.audioTracks.options, getString(R.string.audio_delay), R.drawable.ic_delay, VideoTrackOption.AUDIO_DELAY)
+        generateSeparator(binding.audioTracks.options, true)
         binding.audioTracks.options.setAnimationUpdateListener {
             binding.audioTracks.trackMore.rotation = if (binding.audioTracks.options.isCollapsed) 180F - (180F * it) else 180F * it
         }
 
 
-        binding.subtitleTracks.options.addView(generateSeparator())
-        binding.subtitleTracks.options.addView(generateOptionItem(getString(R.string.spu_delay), R.drawable.ic_delay, VideoTrackOption.SUB_DELAY))
-        binding.subtitleTracks.options.addView(generateOptionItem(getString(R.string.subtitle_select), R.drawable.ic_subtitles_file, VideoTrackOption.SUB_PICK))
-        binding.subtitleTracks.options.addView(generateOptionItem(getString(R.string.download_subtitles), R.drawable.ic_download, VideoTrackOption.SUB_DOWNLOAD))
-        binding.subtitleTracks.options.addView(generateSeparator(true))
+        generateSeparator(binding.subtitleTracks.options)
+        generateOptionItem(binding.subtitleTracks.options, getString(R.string.spu_delay), R.drawable.ic_delay, VideoTrackOption.SUB_DELAY)
+        generateOptionItem(binding.subtitleTracks.options, getString(R.string.subtitle_select), R.drawable.ic_subtitles_file, VideoTrackOption.SUB_PICK)
+        generateOptionItem(binding.subtitleTracks.options, getString(R.string.download_subtitles), R.drawable.ic_download, VideoTrackOption.SUB_DOWNLOAD)
+        generateSeparator(binding.subtitleTracks.options, true)
         binding.subtitleTracks.options.setAnimationUpdateListener {
             binding.subtitleTracks.trackMore.rotation = if (binding.subtitleTracks.options.isCollapsed) 180F - (180F * it) else 180F * it
         }
@@ -159,24 +162,28 @@ class VideoTracksDialog : VLCBottomSheetDialogFragment() {
         super.onViewCreated(view, savedInstanceState)
     }
 
-    private fun generateSeparator(margin: Boolean = false) = View(context).apply {
-        setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.white_transparent_50))
+    private fun generateSeparator(parent: ViewGroup, margin: Boolean = false) {
+        val view = View(context)
+        view.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.white_transparent_50))
         val lp = LinearLayout.LayoutParams(-1, 1.dp)
 
         lp.marginStart = if (margin) 56.dp else 16.dp
         lp.marginEnd = 16.dp
         lp.topMargin = 8.dp
         lp.bottomMargin = 8.dp
-        layoutParams = lp
+        view.layoutParams = lp
+        parent.addView(view)
     }
 
-    private fun generateOptionItem(title: String, @DrawableRes icon: Int, optionId: VideoTrackOption) = VideoTrackOption(requireContext()).apply {
-        setIcon(icon)
-        setTitle(title)
-        setOnClickListener {
+    private fun generateOptionItem(parent: ViewGroup, title: String, @DrawableRes icon: Int, optionId: VideoTrackOption) {
+        val view = layoutInflater.inflate(R.layout.player_overlay_track_option_item, null)
+        view.findViewById<TextView>(R.id.option_title).text = title
+        view.findViewById<ImageView>(R.id.option_icon).setImageBitmap(requireContext().getBitmapFromDrawable(icon))
+        view.setOnClickListener {
             menuItemListener.invoke(optionId)
             dismiss()
         }
+        parent.addView(view)
     }
 
     companion object : DependencyProvider<Any>() {
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/view/VideoTrackOption.kt b/application/vlc-android/src/org/videolan/vlc/gui/view/VideoTrackOption.kt
deleted file mode 100644
index ad37cfd84..000000000
--- a/application/vlc-android/src/org/videolan/vlc/gui/view/VideoTrackOption.kt
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * ************************************************************************
- *  TitleListView.kt
- * *************************************************************************
- * Copyright © 2020 VLC authors and VideoLAN
- * Author: Nicolas POMEPUY
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
- * **************************************************************************
- *
- *
- */
-
-package org.videolan.vlc.gui.view
-
-import android.content.Context
-import android.util.AttributeSet
-import android.view.LayoutInflater
-import android.widget.ImageView
-import android.widget.TextView
-import androidx.annotation.DrawableRes
-import androidx.constraintlayout.widget.ConstraintLayout
-import org.videolan.vlc.R
-import org.videolan.vlc.gui.helpers.getBitmapFromDrawable
-
-class VideoTrackOption : ConstraintLayout {
-
-    private val titleView: TextView by lazy {
-        findViewById(R.id.option_title)
-    }
-    private val iconView: ImageView by lazy {
-        findViewById(R.id.option_icon)
-    }
-
-
-
-    fun setIcon(@DrawableRes icon:Int) {
-        iconView.setImageBitmap(context.getBitmapFromDrawable(icon))
-    }
-
-    fun setTitle(title:String) {
-        titleView.text = title
-    }
-
-    constructor(context: Context) : super(context) {
-        initialize()
-    }
-
-    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
-        initialize()
-    }
-
-    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {
-        initialize()
-    }
-
-
-    private fun initialize() {
-        LayoutInflater.from(context).inflate(R.layout.player_overlay_track_option_item, this, true)
-    }
-}



More information about the Android mailing list