Skip to content

Commit

Permalink
fix: open external links in external browser
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Sep 26, 2023
1 parent 82085ff commit 61a8cd9
Showing 1 changed file with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import android.webkit.WebView

import android.content.Intent.ACTION_VIEW
import android.util.Log
import android.webkit.URLUtil
import android.webkit.WebResourceRequest
import android.webkit.WebViewClient
import net.activitywatch.android.R
import java.lang.Thread.sleep
Expand Down Expand Up @@ -66,6 +68,24 @@ class WebUIFragment : Fragment() {
it.getString(ARG_URL)?.let { it1 -> myWebView.loadUrl(it1) }
}
}

// Open external links in external browser
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
val url = request?.url.toString()
if (URLUtil.isNetworkUrl(url)) {
if (url.startsWith("http://") || url.startsWith("https://")) {
if (!url.contains("//localhost:")) {
// Open the URL in an external browser
val i = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(i)
return true
}
}
// For all other URLs, load them inside the WebView
return false
}
return true
}
}
myWebView.webViewClient = MyWebViewClient()

Expand Down

0 comments on commit 61a8cd9

Please sign in to comment.