@@ -105,6 +105,7 @@ import me.devsaki.hentoid.util.openReader
105105import me.devsaki.hentoid.util.parseDownloadParams
106106import me.devsaki.hentoid.util.setMargins
107107import me.devsaki.hentoid.util.showTooltip
108+ import me.devsaki.hentoid.util.snack
108109import me.devsaki.hentoid.util.toast
109110import me.devsaki.hentoid.util.tryShowMenuIcons
110111import 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,
0 commit comments