Skip to content

Commit

Permalink
Fix intent issues related to supported links
Browse files Browse the repository at this point in the history
- Fix issue where original media notification intent crashes app
- Allow supported links to open app from closed state
  • Loading branch information
MarmadileManteater committed Feb 8, 2024
1 parent ec2588b commit 1f14e66
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
37 changes: 27 additions & 10 deletions android/app/src/main/java/io/freetubeapp/freetube/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import io.freetubeapp.freetube.databinding.ActivityMainBinding
import java.net.URLEncoder


class MainActivity : AppCompatActivity(), OnRequestPermissionsResultCallback {
Expand Down Expand Up @@ -170,9 +171,19 @@ class MainActivity : AppCompatActivity(), OnRequestPermissionsResultCallback {
super.onPageFinished(view, url)
}
}

webView.loadUrl("file:///android_asset/index.html")

if (intent!!.data !== null) {
val url = intent!!.data.toString()
val host = intent!!.data!!.host.toString()
val intentPath = if (host != "youtube.com" && host != "youtu.be" && host != "m.youtube.com" && host != "www.youtube.com") {
url.replace("${intent!!.data!!.host}", "youtube.com")
} else {
url
}
val intentEncoded = URLEncoder.encode(intentPath)
webView.loadUrl("file:///android_asset/index.html?intent=${intentEncoded}")
} else {
webView.loadUrl("file:///android_asset/index.html")
}
}

fun listenForPermissionsCallbacks(listener: (Int, Array<String?>, IntArray) -> Unit) {
Expand All @@ -193,16 +204,22 @@ class MainActivity : AppCompatActivity(), OnRequestPermissionsResultCallback {
permissionsListeners.clear()
}

/**
* handles new intents which involve deep links (aka supported links)
*/
@SuppressLint("MissingSuperCall")
override fun onNewIntent(intent: Intent?) {
val uri = intent!!.data
val isYT = uri!!.host!! == "www.youtube.com" || uri!!.host!! == "youtube.com" || uri!!.host!! == "m.youtube.com" || uri!!.host!! == "youtu.be"
val url = if (!isYT) {
uri.toString().replace(uri.host.toString(), "www.youtube.com")
} else {
uri
if (intent!!.data !== null) {
val uri = intent!!.data
val isYT =
uri!!.host!! == "www.youtube.com" || uri!!.host!! == "youtube.com" || uri!!.host!! == "m.youtube.com" || uri!!.host!! == "youtu.be"
val url = if (!isYT) {
uri.toString().replace(uri.host.toString(), "www.youtube.com")
} else {
uri
}
webView.loadUrl("javascript: window.notifyYoutubeLinkHandlers(\"${url}\")")
}
webView.loadUrl("javascript: window.notifyYoutubeLinkHandlers(\"${url}\")")
}

override fun onDestroy() {
Expand Down
5 changes: 5 additions & 0 deletions src/renderer/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ export default defineComponent({
window.addYoutubeLinkHandler((link) => {
this.handleYoutubeLink(link)
})
if (location.search.indexOf('?intent=') !== -1) {
const intent = location.search.split('?intent=')[1]
const uri = decodeURIComponent(intent)
this.handleYoutubeLink(uri)
}
}

this.dataReady = true
Expand Down

0 comments on commit 1f14e66

Please sign in to comment.