feat: search intent support

This commit is contained in:
Bnyro 2023-07-31 14:56:35 +02:00
parent 84bcd28c2a
commit ebca7d5daf
3 changed files with 34 additions and 15 deletions

View File

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

View File

@ -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

View File

@ -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()
}
}