0

I am trying to override the download Listener for a WebView so it gets to open websites not belonging to us in an external browser instead of loading them in the webview. Thing is, I'd like to simplify the code into something like this

webView?.setDownloadListener { url, userAgent, contentDisposition, mimetype, contentLength ->
        if (!url.contains("mydomain")) {
            CLog.v("InternalWebviewFragment.configWebView().setDownloadListener() isNOTmydomain url: $url")
            val i = Intent(Intent.ACTION_VIEW, Uri.parse(url))
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            activity?.startActivity(i)

        }else{
            super(url, userAgent, contentDisposition, mimetype, contentLength)
        }
    }

However, that call to super there, which I intend to have for urls that do belong to our domain, says that can only be used on the left hand of a dot. When I try to type

super.onDownloadStart

it gives an unresolved reference error.

How could I get the webview Listener to go on business as usual when the url is part of our domain? Is the super call only available in non-lambda methods?

  • "...open websites not belonging to us in an external browser instead of loading them in the webview..." Why not just use `shouldOverrideUrlLoading` ? – Demigod Feb 13 '20 at 08:15
  • I thought so, but API 21 (our app supports 21-29) goes through onDownload instead :\ – Francisco García Feb 13 '20 at 08:19
  • why do you need that, `onDownloadStart` has no default implementation – IR42 Feb 13 '20 at 08:27
  • But `shouldOverrideUrlLoading` is also available for lower APIs, isn't it? – Demigod Feb 13 '20 at 08:27
  • @Demigod it is available, but when I follow the Logcat, it never goes through ```shouldOverrideUrlLoading ``` – Francisco García Feb 13 '20 at 08:33
  • @DrawnRaccoon well, I guess that's why there's no super. – Francisco García Feb 13 '20 at 08:34
  • @FranciscoGarcía, I still think, this is the right approach to do this (using `shouldOverrideUrlLoading `) - https://stackoverflow.com/a/36484720/3569545 – Demigod Feb 13 '20 at 08:36
  • Maybe I didn't explain myself correctly. If so, I'm sorry. We do have a ```shouldOverrideUrlLoading``` method dealing with this. Checking the LogCat I see that API 29 does take that route, but API 21, by some reason, doesn't. API 21 goes to ```onDownloadStart```. And if I take out the piece of code I pasted on the question (while keeping the ```shouldOverrideUrlLoading```, of course) it just won't do a thing in API 21, you click on an external link and nothing happens. – Francisco García Feb 13 '20 at 08:42

1 Answers1

0

After many tests, @Demigod's approach turned out to be the right one. It just needed the bit of having both deprecated and current methods of shouldOverrideUrlLoading as found here

        // APIs up to 23 need this method
        override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
            if (!url.contains("mydomain")) {
                // InternalWebviewFragment external (non mydomain) urls will be dispatched to an external browser
                CLog.v("InternalWebviewFragment.getSimpleWebViewClientUrlLoading().shouldOverrideUrlLoadingOLD() isNotmydomain url: $url")
                val i = Intent(Intent.ACTION_VIEW, Uri.parse(url))
                activity?.startActivity(i)
                CLog.v("InternalWebviewFragment.getSimpleWebViewClientUrlLoading().shouldOverrideUrlLoadingOLD() isNotmydomain  After Intent")
                return true //InternalWebviewFragment this avoids the webview to load the url we've just sent to the browser
            }
            CLog.v("InternalWebviewFragment.getSimpleWebViewClientUrlLoading().shouldOverrideUrlLoadingOLD() ismydomain url: $url")
            // mydomain urls should load fine in the webview
            view?.loadUrl(url)
            return super.shouldOverrideUrlLoading(view, url)

        }
        // for APIs 24+
        override fun shouldOverrideUrlLoading(
            view: WebView?,
            request: WebResourceRequest?
        ): Boolean {
            if (!request?.url.toString().contains("mydomain")) {
                // InternalWebviewFragment external (non mydomain) urls will be dispatched to an external browser
                CLog.v("InternalWebviewFragment.getSimpleWebViewClientUrlLoading().shouldOverrideUrlLoading2() isNotmydomain url: ${request?.url.toString()}")
                val i = Intent(Intent.ACTION_VIEW, Uri.parse(request?.url.toString()))
                activity?.startActivity(i)
                CLog.v("InternalWebviewFragment.getSimpleWebViewClientUrlLoading().shouldOverrideUrlLoading2() isNotmydomain After Intent")
                return true //InternalWebviewFragment this avoids the webview to load the url we've just sent to the browser
            }
            CLog.v("InternalWebviewFragment.getSimpleWebViewClientUrlLoading().shouldOverrideUrlLoading() ismydomain url: $url")
            // mydomain urls should load fine in the webview
            view?.loadUrl(request?.url.toString())
            return super.shouldOverrideUrlLoading(view, request)
        }