Merge pull request #4338 from Bnyro/search-intents

feat: search intent support
This commit is contained in:
Bnyro 2023-07-31 14:58:31 +02:00 committed by GitHub
commit bd6b9eb052
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 15 deletions

View File

@ -291,6 +291,8 @@
<data android:pathPrefix="/c/" /> <data android:pathPrefix="/c/" />
<!-- playlist prefix --> <!-- playlist prefix -->
<data android:pathPrefix="/playlist" /> <data android:pathPrefix="/playlist" />
<!-- search prefix -->
<data android:pathPrefix="/results" />
</intent-filter> </intent-filter>
<!-- Support being detected as media player --> <!-- Support being detected as media player -->

View File

@ -58,6 +58,8 @@ class MainActivity : BaseActivity() {
private val searchViewModel: SearchViewModel by viewModels() private val searchViewModel: SearchViewModel by viewModels()
private val subscriptionsViewModel: SubscriptionsViewModel by viewModels() private val subscriptionsViewModel: SubscriptionsViewModel by viewModels()
private var savedSearchQuery: String? = null
val autoRotationEnabled: Boolean by lazy { val autoRotationEnabled: Boolean by lazy {
PreferenceHelper.getBoolean( PreferenceHelper.getBoolean(
PreferenceKeys.AUTO_ROTATION, PreferenceKeys.AUTO_ROTATION,
@ -374,6 +376,14 @@ class MainActivity : BaseActivity() {
return !isSearchInProgress() return !isSearchInProgress()
} }
}) })
// handle search queries passed by the intent
if (savedSearchQuery != null) {
searchItem.expandActionView()
searchView.setQuery(savedSearchQuery, true)
savedSearchQuery = null
}
return super.onCreateOptionsMenu(menu) return super.onCreateOptionsMenu(menu)
} }
@ -442,6 +452,9 @@ class MainActivity : BaseActivity() {
timestamp = intent.getLongExtra(IntentData.timeStamp, 0L) timestamp = intent.getLongExtra(IntentData.timeStamp, 0L)
) )
} }
intent?.getStringExtra(IntentData.query)?.let {
savedSearchQuery = it
}
intent?.getStringExtra("fragmentToOpen")?.let { intent?.getStringExtra("fragmentToOpen")?.let {
if (it != "downloads") { // Not a shortcut if (it != "downloads") { // Not a shortcut

View File

@ -30,15 +30,21 @@ class RouterActivity : BaseActivity() {
/** /**
* Resolve the uri and return a bundle with the arguments * 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 channelNamePaths = listOf("/c/", "/user/")
val videoPaths = listOf("/shorts/", "/embed/", "/v/", "/live/") val videoPaths = listOf("/shorts/", "/embed/", "/v/", "/live/")
when { when {
uri.path!!.contains("/results") -> {
val searchQuery = uri.getQueryParameter("search_query")
putExtra(IntentData.query, searchQuery)
}
uri.path!!.contains("/channel/") -> { uri.path!!.contains("/channel/") -> {
val channelId = uri.path!! val channelId = uri.path!!
.replace("/channel/", "") .replace("/channel/", "")
intent.putExtra(IntentData.channelId, channelId) putExtra(IntentData.channelId, channelId)
} }
channelNamePaths.any { uri.path!!.contains(it) } -> { channelNamePaths.any { uri.path!!.contains(it) } -> {
@ -48,13 +54,13 @@ class RouterActivity : BaseActivity() {
channelName = channelName.replace(it, "") channelName = channelName.replace(it, "")
} }
intent.putExtra(IntentData.channelName, channelName) putExtra(IntentData.channelName, channelName)
} }
uri.path!!.contains("/playlist") -> { uri.path!!.contains("/playlist") -> {
val playlistId = uri.getQueryParameter("list") val playlistId = uri.getQueryParameter("list")
intent.putExtra(IntentData.playlistId, playlistId) putExtra(IntentData.playlistId, playlistId)
} }
videoPaths.any { uri.path!!.contains(it) } -> { videoPaths.any { uri.path!!.contains(it) } -> {
@ -64,38 +70,36 @@ class RouterActivity : BaseActivity() {
videoId = videoId.replace(it, "") videoId = videoId.replace(it, "")
} }
intent.putExtra(IntentData.videoId, videoId) putExtra(IntentData.videoId, videoId)
uri.getQueryParameter("t") uri.getQueryParameter("t")
?.let { intent.putExtra(IntentData.timeStamp, it.toTimeInSeconds()) } ?.let { putExtra(IntentData.timeStamp, it.toTimeInSeconds()) }
} }
uri.path!!.contains("/watch") && uri.query != null -> { uri.path!!.contains("/watch") && uri.query != null -> {
val videoId = uri.getQueryParameter("v") val videoId = uri.getQueryParameter("v")
intent.putExtra(IntentData.videoId, videoId) putExtra(IntentData.videoId, videoId)
uri.getQueryParameter("t") uri.getQueryParameter("t")
?.let { intent.putExtra(IntentData.timeStamp, it.toTimeInSeconds()) } ?.let { putExtra(IntentData.timeStamp, it.toTimeInSeconds()) }
} }
else -> { else -> {
val videoId = uri.path!!.replace("/", "") val videoId = uri.path!!.replace("/", "")
intent.putExtra(IntentData.videoId, videoId) putExtra(IntentData.videoId, videoId)
uri.getQueryParameter("t") uri.getQueryParameter("t")
?.let { intent.putExtra(IntentData.timeStamp, it.toTimeInSeconds()) } ?.let { putExtra(IntentData.timeStamp, it.toTimeInSeconds()) }
} }
} }
return intent
} }
private fun handleSendText(uri: Uri) { private fun handleSendText(uri: Uri) {
Log.i(TAG(), uri.toString()) Log.i(TAG(), uri.toString())
val pm: PackageManager = this.packageManager val intent = this.packageManager.getLaunchIntentForPackage(this.packageName)!!.resolveType(uri)
val intent = pm.getLaunchIntentForPackage(this.packageName) intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK
intent?.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK startActivity(intent)
startActivity(resolveType(intent!!, uri))
finishAndRemoveTask() finishAndRemoveTask()
} }
} }