[Android] Streams: paste clipboard content when relevant

Geoffrey Métais git at videolan.org
Thu Nov 28 07:31:06 CET 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Jun  7 17:43:04 2019 +0200| [802061bf19c6a3eb29a389c4fb0d57869ca387ae] | committer: Nicolas Pomepuy

Streams: paste clipboard content when relevant

If a valid URL is currently in clipboard, paste it in edittext.

Fix #884

> https://code.videolan.org/videolan/vlc-android/commit/802061bf19c6a3eb29a389c4fb0d57869ca387ae
---

 .../src/main/java/org/videolan/tools/KotlinExtensions.kt  | 12 +++---------
 .../src/org/videolan/vlc/gui/network/MRLPanelFragment.kt  | 15 ++++++++++++---
 2 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/tools/src/main/java/org/videolan/tools/KotlinExtensions.kt b/tools/src/main/java/org/videolan/tools/KotlinExtensions.kt
index b4f4d04ab..3fae1f721 100644
--- a/tools/src/main/java/org/videolan/tools/KotlinExtensions.kt
+++ b/tools/src/main/java/org/videolan/tools/KotlinExtensions.kt
@@ -1,30 +1,24 @@
 package org.videolan.tools
 
-import android.util.Patterns
 import android.app.ActivityManager
 import android.app.ActivityManager.RunningAppProcessInfo
 import android.content.ClipData
 import android.content.ClipboardManager
 import android.content.Context
 import android.content.res.Resources
+import android.util.Patterns
 import android.util.TypedValue
 import android.view.View
 import androidx.annotation.AttrRes
 import androidx.lifecycle.Lifecycle
 import androidx.lifecycle.LifecycleOwner
-import androidx.lifecycle.OnLifecycleEvent
-import kotlinx.coroutines.CoroutineScope
-import kotlinx.coroutines.Dispatchers
-import kotlinx.coroutines.ExperimentalCoroutinesApi
-import kotlinx.coroutines.Job
-import kotlin.contracts.ExperimentalContracts
-import kotlin.contracts.contract
-import kotlinx.coroutines.*
 import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.channels.Channel
 import kotlinx.coroutines.channels.actor
 import kotlinx.coroutines.delay
 import kotlinx.coroutines.yield
+import kotlin.contracts.ExperimentalContracts
+import kotlin.contracts.contract
 
 fun <T> List<T>.getposition(target: T): Int {
     for ((index, item) in withIndex()) if (item == target) return index
diff --git a/vlc-android/src/org/videolan/vlc/gui/network/MRLPanelFragment.kt b/vlc-android/src/org/videolan/vlc/gui/network/MRLPanelFragment.kt
index 705357448..55706cdce 100644
--- a/vlc-android/src/org/videolan/vlc/gui/network/MRLPanelFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/network/MRLPanelFragment.kt
@@ -20,6 +20,8 @@
  */
 package org.videolan.vlc.gui.network
 
+import android.content.ClipboardManager
+import android.content.Context
 import android.graphics.Rect
 import android.net.Uri
 import android.os.Bundle
@@ -48,6 +50,7 @@ import kotlinx.coroutines.channels.actor
 import org.videolan.medialibrary.MLServiceLocator
 import org.videolan.medialibrary.interfaces.media.AbstractMediaWrapper
 import org.videolan.tools.copy
+import org.videolan.tools.isValidUrl
 import org.videolan.vlc.R
 import org.videolan.vlc.databinding.MrlPanelBinding
 import org.videolan.vlc.gui.ContentActivity
@@ -65,7 +68,8 @@ const val TAG = "VLC/MrlPanelFragment"
 
 @ExperimentalCoroutinesApi
 @ObsoleteCoroutinesApi
-class MRLPanelFragment : Fragment(), View.OnKeyListener, TextView.OnEditorActionListener, View.OnClickListener, CtxActionReceiver, BrowserFragmentInterface {
+class MRLPanelFragment : Fragment(), View.OnKeyListener, TextView.OnEditorActionListener,
+        View.OnClickListener, CtxActionReceiver, BrowserFragmentInterface {
 
     private lateinit var adapter: MRLAdapter
     private lateinit var editText: com.google.android.material.textfield.TextInputLayout
@@ -103,7 +107,6 @@ class MRLPanelFragment : Fragment(), View.OnKeyListener, TextView.OnEditorAction
                 }
             })
             recyclerView.layoutManager = gridLayoutManager
-
         } else {
             recyclerView.layoutManager = LinearLayoutManager(activity)
             recyclerView.addItemDecoration(DividerItemDecoration(activity, DividerItemDecoration.VERTICAL))
@@ -121,6 +124,13 @@ class MRLPanelFragment : Fragment(), View.OnKeyListener, TextView.OnEditorAction
         viewModel.loading.observe(requireActivity(), Observer { (activity as? MainActivity)?.refreshing = it })
     }
 
+    override fun onResume() {
+        super.onResume()
+        val clipBoardManager = requireContext().getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
+        val text = clipBoardManager.primaryClip?.getItemAt(0)?.text?.toString()
+        if (text.isValidUrl()) viewModel.observableSearchText.set(text)
+    }
+
     override fun onStart() {
         super.onStart()
         viewModel.refresh()
@@ -128,7 +138,6 @@ class MRLPanelFragment : Fragment(), View.OnKeyListener, TextView.OnEditorAction
         (activity as? AppCompatActivity)?.supportActionBar?.setTitle(R.string.open_mrl)
     }
 
-
     override fun onKey(v: View, keyCode: Int, event: KeyEvent) = (keyCode == EditorInfo.IME_ACTION_DONE ||
             keyCode == EditorInfo.IME_ACTION_GO ||
             event.action == KeyEvent.ACTION_DOWN && event.keyCode == KeyEvent.KEYCODE_ENTER) && processUri()



More information about the Android mailing list