From ebca7d5dafc99d7f5c081789caa13736a414e31d Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 31 Jul 2023 14:56:35 +0200 Subject: [PATCH] feat: search intent support --- app/src/main/AndroidManifest.xml | 2 ++ .../libretube/ui/activities/MainActivity.kt | 13 +++++++ .../libretube/ui/activities/RouterActivity.kt | 34 +++++++++++-------- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 8d4ab66e9..b8fd816ac 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -291,6 +291,8 @@ + + diff --git a/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt b/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt index 3ece11d55..9af6cd3bf 100644 --- a/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt @@ -58,6 +58,8 @@ class MainActivity : BaseActivity() { private val searchViewModel: SearchViewModel by viewModels() private val subscriptionsViewModel: SubscriptionsViewModel by viewModels() + private var savedSearchQuery: String? = null + val autoRotationEnabled: Boolean by lazy { PreferenceHelper.getBoolean( PreferenceKeys.AUTO_ROTATION, @@ -374,6 +376,14 @@ class MainActivity : BaseActivity() { return !isSearchInProgress() } }) + + // handle search queries passed by the intent + if (savedSearchQuery != null) { + searchItem.expandActionView() + searchView.setQuery(savedSearchQuery, true) + savedSearchQuery = null + } + return super.onCreateOptionsMenu(menu) } @@ -442,6 +452,9 @@ class MainActivity : BaseActivity() { timestamp = intent.getLongExtra(IntentData.timeStamp, 0L) ) } + intent?.getStringExtra(IntentData.query)?.let { + savedSearchQuery = it + } intent?.getStringExtra("fragmentToOpen")?.let { if (it != "downloads") { // Not a shortcut diff --git a/app/src/main/java/com/github/libretube/ui/activities/RouterActivity.kt b/app/src/main/java/com/github/libretube/ui/activities/RouterActivity.kt index 10103cac1..3709172ad 100644 --- a/app/src/main/java/com/github/libretube/ui/activities/RouterActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/activities/RouterActivity.kt @@ -30,15 +30,21 @@ class RouterActivity : BaseActivity() { /** * Resolve the uri and return a bundle with the arguments */ - private fun resolveType(intent: Intent, uri: Uri): Intent { + private fun Intent.resolveType(uri: Uri) = apply { val channelNamePaths = listOf("/c/", "/user/") val videoPaths = listOf("/shorts/", "/embed/", "/v/", "/live/") when { + uri.path!!.contains("/results") -> { + val searchQuery = uri.getQueryParameter("search_query") + + putExtra(IntentData.query, searchQuery) + } + uri.path!!.contains("/channel/") -> { val channelId = uri.path!! .replace("/channel/", "") - intent.putExtra(IntentData.channelId, channelId) + putExtra(IntentData.channelId, channelId) } channelNamePaths.any { uri.path!!.contains(it) } -> { @@ -48,13 +54,13 @@ class RouterActivity : BaseActivity() { channelName = channelName.replace(it, "") } - intent.putExtra(IntentData.channelName, channelName) + putExtra(IntentData.channelName, channelName) } uri.path!!.contains("/playlist") -> { val playlistId = uri.getQueryParameter("list") - intent.putExtra(IntentData.playlistId, playlistId) + putExtra(IntentData.playlistId, playlistId) } videoPaths.any { uri.path!!.contains(it) } -> { @@ -64,38 +70,36 @@ class RouterActivity : BaseActivity() { videoId = videoId.replace(it, "") } - intent.putExtra(IntentData.videoId, videoId) + putExtra(IntentData.videoId, videoId) uri.getQueryParameter("t") - ?.let { intent.putExtra(IntentData.timeStamp, it.toTimeInSeconds()) } + ?.let { putExtra(IntentData.timeStamp, it.toTimeInSeconds()) } } uri.path!!.contains("/watch") && uri.query != null -> { val videoId = uri.getQueryParameter("v") - intent.putExtra(IntentData.videoId, videoId) + putExtra(IntentData.videoId, videoId) uri.getQueryParameter("t") - ?.let { intent.putExtra(IntentData.timeStamp, it.toTimeInSeconds()) } + ?.let { putExtra(IntentData.timeStamp, it.toTimeInSeconds()) } } else -> { val videoId = uri.path!!.replace("/", "") - intent.putExtra(IntentData.videoId, videoId) + putExtra(IntentData.videoId, videoId) uri.getQueryParameter("t") - ?.let { intent.putExtra(IntentData.timeStamp, it.toTimeInSeconds()) } + ?.let { putExtra(IntentData.timeStamp, it.toTimeInSeconds()) } } } - return intent } private fun handleSendText(uri: Uri) { Log.i(TAG(), uri.toString()) - val pm: PackageManager = this.packageManager - val intent = pm.getLaunchIntentForPackage(this.packageName) - intent?.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK - startActivity(resolveType(intent!!, uri)) + val intent = this.packageManager.getLaunchIntentForPackage(this.packageName)!!.resolveType(uri) + intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK + startActivity(intent) finishAndRemoveTask() } }