[Android] Refactor current playlist contextmenu

Geoffrey Métais git at videolan.org
Thu Aug 9 15:29:53 CEST 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Aug  9 15:28:03 2018 +0200| [70bb613523b55131fe4717c0c7429f675a4385a6] | committer: Geoffrey Métais

Refactor current playlist contextmenu

Fix #629

> https://code.videolan.org/videolan/vlc-android/commit/70bb613523b55131fe4717c0c7429f675a4385a6
---

 .../src/org/videolan/vlc/gui/audio/AudioPlayer.kt  | 62 +++++++++++-----------
 .../videolan/vlc/gui/audio/PlaylistAdapter.java    |  3 ++
 .../org/videolan/vlc/gui/dialogs/ContextSheet.kt   |  1 +
 .../src/org/videolan/vlc/util/Constants.java       |  1 +
 4 files changed, 35 insertions(+), 32 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt b/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
index a59c10004..d21f3a1ce 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
@@ -36,7 +36,6 @@ import android.support.design.widget.BottomSheetBehavior
 import android.support.design.widget.Snackbar
 import android.support.v4.app.Fragment
 import android.support.v7.widget.LinearLayoutManager
-import android.support.v7.widget.PopupMenu
 import android.support.v7.widget.helper.ItemTouchHelper
 import android.text.Editable
 import android.text.TextUtils
@@ -63,13 +62,14 @@ import org.videolan.vlc.databinding.AudioPlayerBinding
 import org.videolan.vlc.gui.AudioPlayerContainerActivity
 import org.videolan.vlc.gui.PlaybackServiceActivity
 import org.videolan.vlc.gui.dialogs.AdvOptionsDialog
+import org.videolan.vlc.gui.dialogs.CtxActionReceiver
+import org.videolan.vlc.gui.dialogs.showContext
 import org.videolan.vlc.gui.helpers.AudioUtil
 import org.videolan.vlc.gui.helpers.SwipeDragItemTouchHelperCallback
 import org.videolan.vlc.gui.helpers.UiTools
 import org.videolan.vlc.gui.preferences.PreferencesActivity
 import org.videolan.vlc.gui.video.VideoPlayerActivity
 import org.videolan.vlc.gui.view.AudioMediaSwitcher.AudioMediaSwitcherListener
-import org.videolan.vlc.util.AndroidDevices
 import org.videolan.vlc.util.Constants
 import org.videolan.vlc.util.VLCIO
 import org.videolan.vlc.viewmodels.PlaybackProgress
@@ -164,32 +164,30 @@ class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher, PlaybackSe
         outState.putInt("player_state", playerState)
     }
 
+    private val ctxReceiver : CtxActionReceiver = object : CtxActionReceiver {
+        override fun onCtxAction(position: Int, option: Int) {
+            when(option) {
+                Constants.CTX_SET_RINGTONE -> AudioUtil.setRingtone(playlistAdapter.getItem(position), activity)
+                Constants.CTX_ADD_TO_PLAYLIST -> {
+                    val mw = playlistAdapter.getItem(position)
+                    UiTools.addToPlaylist(requireActivity(), listOf(mw))
+                }
+                Constants.CTX_REMOVE -> view?.let {
+                    val mw = playlistAdapter.getItem(position)
+                    val cancelAction = Runnable { service?.insertItem(position, mw) }
+                    val message = String.format(VLCApplication.getAppResources().getString(R.string.remove_playlist_item), mw.title)
+                    UiTools.snackerWithCancel(it, message, null, cancelAction)
+                    service?.remove(position)
+                }
+            }
+        }
+    }
+
     override fun onPopupMenu(anchor: View, position: Int, media: MediaWrapper) {
         val activity = activity
         if (activity === null || position >= playlistAdapter.itemCount) return
-        val pos = playlistModel.getItemPosition(position, media)
-        if (pos == -1) return
-        val mw = playlistAdapter.getItem(pos)
-        val popupMenu = PopupMenu(activity, anchor)
-        popupMenu.menuInflater.inflate(R.menu.audio_player, popupMenu.menu)
-
-        popupMenu.menu.setGroupVisible(R.id.phone_only, mw!!.type != MediaWrapper.TYPE_VIDEO
-                && TextUtils.equals(mw.uri.scheme, "file")
-                && AndroidDevices.isPhone)
-
-        popupMenu.setOnMenuItemClickListener(PopupMenu.OnMenuItemClickListener { item ->
-            if (item.itemId == R.id.audio_player_mini_remove) {
-                service?.apply {
-                    remove(pos)
-                    return at OnMenuItemClickListener true
-                }
-            } else if (item.itemId == R.id.audio_player_set_song) {
-                AudioUtil.setRingtone(mw, activity)
-                return at OnMenuItemClickListener true
-            }
-            false
-        })
-        popupMenu.show()
+        val flags = Constants.CTX_REMOVE or Constants.CTX_SET_RINGTONE or Constants.CTX_ADD_TO_PLAYLIST
+        showContext(activity, ctxReceiver, position, media.title, flags)
     }
 
     private fun doUpdate() {
@@ -306,7 +304,7 @@ class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher, PlaybackSe
     }
 
     override fun playItem(position: Int, item: MediaWrapper) {
-         service?.playIndex(playlistModel.getItemPosition(position, item))
+        service?.playIndex(playlistModel.getItemPosition(position, item))
     }
 
     fun onTimeLabelClick(view: View) {
@@ -582,12 +580,12 @@ class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher, PlaybackSe
         override fun onMediaSwitching() {}
 
         override fun onMediaSwitched(position: Int) {
-           service?.apply {
-               when (position) {
-                   AudioMediaSwitcherListener.PREVIOUS_MEDIA -> previous(true)
-                   AudioMediaSwitcherListener.NEXT_MEDIA ->  next()
-               }
-           }
+            service?.apply {
+                when (position) {
+                    AudioMediaSwitcherListener.PREVIOUS_MEDIA -> previous(true)
+                    AudioMediaSwitcherListener.NEXT_MEDIA ->  next()
+                }
+            }
         }
 
         override fun onTouchDown() = hideHeaderButtons()
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/PlaylistAdapter.java b/vlc-android/src/org/videolan/vlc/gui/audio/PlaylistAdapter.java
index b24fa4541..c1bb33025 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/PlaylistAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/PlaylistAdapter.java
@@ -23,8 +23,10 @@
 
 package org.videolan.vlc.gui.audio;
 
+import android.annotation.TargetApi;
 import android.content.Context;
 import android.databinding.DataBindingUtil;
+import android.os.Build;
 import android.os.Message;
 import android.support.annotation.MainThread;
 import android.support.v4.app.Fragment;
@@ -166,6 +168,7 @@ public class PlaylistAdapter extends DiffUtilAdapter<MediaWrapper, PlaylistAdapt
     public class ViewHolder extends RecyclerView.ViewHolder {
         PlaylistItemBinding binding;
 
+        @TargetApi(Build.VERSION_CODES.M)
         public ViewHolder(View v) {
             super(v);
             binding = DataBindingUtil.bind(v);
diff --git a/vlc-android/src/org/videolan/vlc/gui/dialogs/ContextSheet.kt b/vlc-android/src/org/videolan/vlc/gui/dialogs/ContextSheet.kt
index 6415df9ee..8e53d13f4 100644
--- a/vlc-android/src/org/videolan/vlc/gui/dialogs/ContextSheet.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/dialogs/ContextSheet.kt
@@ -95,6 +95,7 @@ class ContextSheet : BottomSheetDialogFragment() {
         if (flags and Constants.CTX_FAV_ADD != 0) add(Simple(Constants.CTX_FAV_ADD, getString(R.string.favorites_add), R.drawable.ic_menu_network))
         if (flags and Constants.CTX_FAV_EDIT != 0) add(Simple(Constants.CTX_FAV_EDIT, getString(R.string.favorites_edit), R.drawable.ic_menu_network))
         if (flags and Constants.CTX_FAV_REMOVE != 0) add(Simple(Constants.CTX_FAV_REMOVE, getString(R.string.favorites_remove), R.drawable.ic_menu_network))
+        if (flags and Constants.CTX_REMOVE != 0) add(Simple(Constants.CTX_REMOVE, getString(R.string.remove), R.drawable.ic_ctx_delete_normal))
     }
 
     inner class ContextAdapter : RecyclerView.Adapter<ContextAdapter.ViewHolder>() {
diff --git a/vlc-android/src/org/videolan/vlc/util/Constants.java b/vlc-android/src/org/videolan/vlc/util/Constants.java
index 066d51eef..9ec165c7d 100644
--- a/vlc-android/src/org/videolan/vlc/util/Constants.java
+++ b/vlc-android/src/org/videolan/vlc/util/Constants.java
@@ -152,6 +152,7 @@ public class Constants {
     public final static int CTX_FAV_REMOVE         = 1 << 14;
     public final static int CTX_CUSTOM_REMOVE      = 1 << 15;
     public final static int CTX_ITEM_DL            = 1 << 16;
+    public final static int CTX_REMOVE             = 1 << 17;
 
     public final static int CTX_VIDEO_FLAGS = Constants.CTX_APPEND|Constants.CTX_DELETE|Constants.CTX_DOWNLOAD_SUBTITLES|Constants.CTX_INFORMATION|Constants.CTX_PLAY_ALL|Constants.CTX_PLAY_AS_AUDIO;
     public final static int CTX_TRACK_FLAGS = Constants.CTX_APPEND|Constants.CTX_PLAY_NEXT|Constants.CTX_DELETE|Constants.CTX_INFORMATION|Constants.CTX_PLAY_ALL|Constants.CTX_ADD_TO_PLAYLIST|Constants.CTX_SET_RINGTONE;



More information about the Android mailing list