-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle youtube url scheme from web view.
- Loading branch information
1 parent
c0041f2
commit 72b481c
Showing
2 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...hub/reactivecircus/kstreamlined/android/feature/contentviewer/SchemeAwareWebViewClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.github.reactivecircus.kstreamlined.android.feature.contentviewer | ||
|
||
import android.content.ActivityNotFoundException | ||
import android.content.Intent | ||
import android.net.Uri | ||
import android.webkit.WebResourceRequest | ||
import android.webkit.WebView | ||
import co.touchlab.kermit.Logger | ||
|
||
internal class SchemeAwareWebViewClient : AccompanistWebViewClient() { | ||
override fun shouldOverrideUrlLoading( | ||
view: WebView?, | ||
request: WebResourceRequest? | ||
): Boolean { | ||
request?.let { | ||
var url = it.url.toString() | ||
if (url.startsWith("http://") || url.startsWith("https://")) { | ||
return false | ||
} | ||
if (url.contains("youtube.com")) { | ||
url = url.replaceFirst("intent://", "vnd.youtube:") | ||
} | ||
try { | ||
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)) | ||
view?.context?.startActivity(intent) | ||
} catch (e: ActivityNotFoundException) { | ||
Logger.e(e) { "No Activity was found to handle the Intent." } | ||
} | ||
} | ||
return true | ||
} | ||
} |