Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add callback for handling internal links #28

Merged
merged 3 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ All notable changes to this project will be documented in this file. Take a look
#### Navigator

* `EpubNavigatorFragment`'s `goForward()` and `goBackward()` are now jumping to the previous or next pages instead of resources.
* [#20](https://github.com/readium/kotlin-toolkit/issues/20) EPUB navigator stuck between two pages with vertical swipes
* [#20](https://github.com/readium/kotlin-toolkit/issues/20) EPUB navigator stuck between two pages with vertical swipes.
* [#27](https://github.com/readium/kotlin-toolkit/issues/27) Internal links break the EPUB navigator (contributed by [@mihai-wolfpack](https://github.com/readium/kotlin-toolkit/pull/28)).


## [2.1.0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
* Returns the custom [ActionMode.Callback] to be used with the text selection menu.
*/
val selectionActionModeCallback: ActionMode.Callback? get() = null

/**
* Offers an opportunity to override a request loaded by the given web view.
*/
fun shouldOverrideUrlLoading(webView: WebView, request: WebResourceRequest): Boolean = false
}

lateinit var listener: Listener
Expand Down Expand Up @@ -471,31 +476,10 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
}
}

/**
* Prevents opening external links in the web view.
* To be called from the WebViewClient implementation attached to the web view.
*/
internal fun shouldOverrideUrlLoading(request: WebResourceRequest): Boolean {
val resourceUrl = url ?: return false

// List of hosts that are allowed to be loaded in the web view.
// The host of the page already loaded in the web view is always allowed.
val passthroughHosts = listOfNotNull(
"localhost", "127.0.0.1",
tryOrNull { Uri.parse(resourceUrl) }?.host
)
if (!passthroughHosts.contains(request.url.host)) {
openExternalLink(request.url)
return true
}

return false
}
if (resourceUrl == request.url?.toString()) return false

private fun openExternalLink(url: Uri) {
CustomTabsIntent.Builder()
.build()
.launchUrl(context, url)
return listener.shouldOverrideUrlLoading(this, request)
}

// Text selection ActionMode overrides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ import android.content.Context
import android.content.SharedPreferences
import android.graphics.PointF
import android.graphics.RectF
import android.net.Uri
import android.os.Bundle
import android.view.ActionMode
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebResourceRequest
import android.webkit.WebView
import androidx.browser.customtabs.CustomTabsIntent
import androidx.collection.forEach
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
Expand Down Expand Up @@ -46,7 +50,6 @@ import org.readium.r2.shared.publication.*
import org.readium.r2.shared.publication.epub.EpubLayout
import org.readium.r2.shared.publication.presentation.presentation
import org.readium.r2.shared.publication.services.isRestricted
import org.readium.r2.shared.publication.services.positions
import org.readium.r2.shared.publication.services.positionsByReadingOrder
import kotlin.math.ceil
import kotlin.reflect.KClass
Expand Down Expand Up @@ -262,7 +265,6 @@ class EpubNavigatorFragment private constructor(
internal var pendingLocator: Locator? = null

override fun go(locator: Locator, animated: Boolean, completion: () -> Unit): Boolean {

val href = locator.href
// Remove anchor
.substringBefore("#")
Expand Down Expand Up @@ -470,6 +472,36 @@ class EpubNavigatorFragment private constructor(

override val selectionActionModeCallback: ActionMode.Callback?
get() = config.selectionActionModeCallback

/**
* Prevents opening external links in the web view and handles internal links.
*/
override fun shouldOverrideUrlLoading(webView: WebView, request: WebResourceRequest): Boolean {
val url = request.url?.toString()
?: return false

val baseUrl = baseUrl.takeIf { it.isNotBlank() }
?: publication.linkWithRel("self")?.href
?: return false

if (!url.startsWith(baseUrl)) {
openExternalLink(request.url)
} else {
// Navigate to an internal link
go(Link(href = url.removePrefix(baseUrl).addPrefix("/")))
}

return true
}

private fun openExternalLink(url: Uri) {
val context = context ?: return
tryOrLog {
CustomTabsIntent.Builder()
.build()
.launchUrl(context, url)
}
}
}

override fun goForward(animated: Boolean, completion: () -> Unit): Boolean {
Expand Down