Skip to content

Commit 9943c53

Browse files
committed
Browser : Default to welcome page when starting page is unavalable [#799]
1 parent 7cf43ae commit 9943c53

File tree

2 files changed

+55
-48
lines changed

2 files changed

+55
-48
lines changed

app/src/main/java/me/devsaki/hentoid/activities/sources/BaseBrowserActivity.kt

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ import me.devsaki.hentoid.util.openReader
105105
import me.devsaki.hentoid.util.parseDownloadParams
106106
import me.devsaki.hentoid.util.setMargins
107107
import me.devsaki.hentoid.util.showTooltip
108+
import me.devsaki.hentoid.util.snack
108109
import me.devsaki.hentoid.util.toast
109110
import me.devsaki.hentoid.util.tryShowMenuIcons
110111
import me.devsaki.hentoid.util.useLegacyInsets
@@ -285,7 +286,7 @@ abstract class BaseBrowserActivity : BaseActivity(), CustomWebViewClient.Browser
285286
tryShowMenuIcons(this, toolbar.menu)
286287
toolbar.setOnMenuItemClickListener { this.onMenuItemSelected(it) }
287288
toolbar.title = getStartSite().description
288-
toolbar.setOnClickListener { loadUrl(getStartUrl(true)) }
289+
toolbar.setOnClickListener { getStartUrl(true) { loadUrl(it) } }
289290
addCustomBackControl()
290291

291292
refreshStopMenu = toolbar.menu.findItem(R.id.web_menu_refresh_stop)
@@ -308,7 +309,7 @@ abstract class BaseBrowserActivity : BaseActivity(), CustomWebViewClient.Browser
308309
// Webview
309310
initWebview()
310311
initSwipeLayout()
311-
webView.loadUrl(getStartUrl())
312+
getStartUrl { webView.loadUrl(it) }
312313
if (!Settings.recentVisibility) {
313314
window.setFlags(
314315
WindowManager.LayoutParams.FLAG_SECURE,
@@ -471,39 +472,66 @@ abstract class BaseBrowserActivity : BaseActivity(), CustomWebViewClient.Browser
471472

472473
/**
473474
* Determine the URL the browser will load at startup
474-
* - Either an URL specifically given to the activity (e.g. "view source" action)
475+
* - Either a URL specifically given to the activity (e.g. "view source" action)
475476
* - Or the last viewed page, if the setting is enabled
476477
* - If neither of the previous cases, the default URL of the site
477478
*
478479
* @param forceHomepage Force the URL to be the current site's homepage
479480
*
480481
* @return URL to load at startup
481482
*/
482-
private fun getStartUrl(forceHomepage: Boolean = false): String {
483-
val dao: CollectionDAO = ObjectBoxDAO()
484-
try {
485-
if (!forceHomepage) {
486-
// Priority 1 : URL specifically given to the activity (e.g. "view source" action)
487-
if (intent.extras != null) {
488-
val bundle = BaseBrowserActivityBundle(intent.extras!!)
489-
val intentUrl = bundle.url
490-
if (intentUrl.isNotEmpty()) return intentUrl
491-
}
483+
private fun getStartUrl(
484+
forceHomepage: Boolean = false,
485+
onFound: Consumer<String>
486+
) {
487+
lifecycleScope.launch(Dispatchers.IO) {
488+
val dao: CollectionDAO = ObjectBoxDAO()
489+
val site = getStartSite()
490+
var result = ""
491+
try {
492+
if (!forceHomepage) {
493+
// Priority 1 : URL specifically given to the activity (e.g. "view source" action)
494+
if (intent.extras != null) {
495+
val bundle = BaseBrowserActivityBundle(intent.extras!!)
496+
result = bundle.url
497+
}
492498

493-
// Priority 2 : Last viewed position, if setting enabled
494-
if (Settings.isBrowserResumeLast) {
495-
val siteHistory = dao.selectLastHistory(getStartSite())
496-
if (siteHistory.url.isNotEmpty()) return siteHistory.url
499+
// Priority 2 : Last viewed position, if setting enabled
500+
if (result.isBlank() && Settings.isBrowserResumeLast) {
501+
val siteHistory = dao.selectLastHistory(getStartSite())
502+
result = siteHistory.url
503+
}
504+
var reason = ""
505+
if (result.isNotBlank()) {
506+
try {
507+
val headers: MutableList<Pair<String, String>> = ArrayList()
508+
headers.add(Pair(HEADER_REFERER_KEY, site.url))
509+
val response = getOnlineResourceFast(
510+
result,
511+
headers,
512+
site.useMobileAgent,
513+
site.useHentoidAgent,
514+
site.useWebviewAgent
515+
)
516+
if (response.code < 300) {
517+
withContext(Dispatchers.Main) { onFound(result) }
518+
return@launch
519+
}
520+
else reason = "HTTP ${response.code}"
521+
} catch (e: Exception) {
522+
Timber.i(e, "Unavailable resource ($reason) : $result")
523+
reason = e.javaClass.name
524+
}
525+
}
526+
snack("Webpage not available ($reason); loading the welcome page instead") // TODO make a resource
497527
}
498-
}
499528

500-
// Priority 3 : Homepage (manually set through bookmarks or default)
501-
val welcomePage = dao.selectHomepage(getStartSite())
502-
return welcomePage?.url ?: getStartSite().url
503-
504-
// Default site URL
505-
} finally {
506-
dao.cleanup()
529+
// Priority 3 : Homepage (manually set through bookmarks or default)
530+
val welcomePage = dao.selectHomepage(getStartSite())
531+
withContext(Dispatchers.Main) { onFound(welcomePage?.url ?: getStartSite().url) }
532+
} finally {
533+
dao.cleanup()
534+
}
507535
}
508536
}
509537

@@ -1328,10 +1356,7 @@ abstract class BaseBrowserActivity : BaseActivity(), CustomWebViewClient.Browser
13281356
getCookies(onlineContent.coverImageUrl)
13291357
downloadParams[HEADER_REFERER_KEY] = onlineContent.site.url
13301358
getOnlineResourceFast(
1331-
fixUrl(
1332-
onlineContent.coverImageUrl,
1333-
getStartUrl() // TODO is that the URL we need?!
1334-
),
1359+
fixUrl(onlineContent.coverImageUrl, onlineContent.site.url),
13351360
requestHeadersList,
13361361
getStartSite().useMobileAgent,
13371362
getStartSite().useHentoidAgent,

app/src/main/java/me/devsaki/hentoid/util/network/HttpHelper.kt

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -129,32 +129,14 @@ fun getOnlineResource(
129129
return OkHttpClientManager.getInstance().newCall(request).execute()
130130
}
131131

132-
@Throws(IOException::class)
133-
fun getOnlineResourceFast(
134-
url: String,
135-
headers: List<Pair<String, String>>?,
136-
useMobileAgent: Boolean,
137-
useHentoidAgent: Boolean,
138-
useWebviewAgent: Boolean
139-
): Response {
140-
return getOnlineResourceFast(
141-
url,
142-
headers,
143-
useMobileAgent,
144-
useHentoidAgent,
145-
useWebviewAgent,
146-
true
147-
)
148-
}
149-
150132
@Throws(IOException::class)
151133
fun getOnlineResourceFast(
152134
url: String,
153135
headers: List<Pair<String, String>>?,
154136
useMobileAgent: Boolean,
155137
useHentoidAgent: Boolean,
156138
useWebviewAgent: Boolean,
157-
followRedirects: Boolean
139+
followRedirects: Boolean = true
158140
): Response {
159141
val requestBuilder: Request.Builder =
160142
buildRequest(url, headers, useMobileAgent, useHentoidAgent, useWebviewAgent)

0 commit comments

Comments
 (0)