<commit_before>/*
 * Copyright (c) 2020 toastkidjp.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompany this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
 */

package jp.toastkid.yobidashi.browser.webview.factory

import android.annotation.TargetApi
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.net.http.SslError
import android.os.Build
import android.webkit.SslErrorHandler
import android.webkit.WebResourceError
import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.fragment.app.FragmentActivity
import jp.toastkid.lib.ContentViewModel
import jp.toastkid.lib.preference.PreferenceApplier
import jp.toastkid.yobidashi.R
import jp.toastkid.yobidashi.browser.BrowserHeaderViewModel
import jp.toastkid.yobidashi.browser.FaviconApplier
import jp.toastkid.yobidashi.browser.LoadingViewModel
import jp.toastkid.yobidashi.browser.archive.auto.AutoArchive
import jp.toastkid.yobidashi.browser.block.AdRemover
import jp.toastkid.yobidashi.browser.history.ViewHistoryInsertion
import jp.toastkid.yobidashi.browser.tls.TlsErrorDialogFragment
import jp.toastkid.yobidashi.browser.tls.TlsErrorMessageGenerator
import jp.toastkid.yobidashi.libs.intent.IntentFactory
import jp.toastkid.yobidashi.rss.suggestion.RssAddingSuggestion
import jp.toastkid.yobidashi.tab.History
import timber.log.Timber

class WebViewClientFactory(
        private val contentViewModel: ContentViewModel?,
        private val adRemover: AdRemover,
        private val faviconApplier: FaviconApplier,
        private val preferenceApplier: PreferenceApplier,
        private val browserHeaderViewModel: BrowserHeaderViewModel? = null,
        private val rssAddingSuggestion: RssAddingSuggestion? = null,
        private val loadingViewModel: LoadingViewModel? = null,
        private val currentView: () -> WebView? = { null },
        private val lastId: () -> String = { "" }
) {

    /**
     * Add onPageFinished and onPageStarted.
     */
    operator fun invoke(): WebViewClient = object : WebViewClient() {

        override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {
            super.onPageStarted(view, url, favicon)

            browserHeaderViewModel?.updateProgress(0)
            browserHeaderViewModel?.nextUrl(url)

            rssAddingSuggestion?.invoke(view, url)
            browserHeaderViewModel?.setBackButtonEnability(view.canGoBack())
        }

        override fun onPageFinished(view: WebView, url: String?) {
            super.onPageFinished(view, url)

            val title = view.title ?: ""
            val urlStr = url ?: ""

            if (!AutoArchive.shouldNotUpdateTab(urlStr)) {
                loadingViewModel?.finished(lastId(), History.make(title, urlStr))
            }

            browserHeaderViewModel?.updateProgress(100)
            browserHeaderViewModel?.stopProgress(true)

            try {
                if (view == currentView()) {
                    browserHeaderViewModel?.nextTitle(title)
                    browserHeaderViewModel?.nextUrl(urlStr)
                }
            } catch (e: Exception) {
                Timber.e(e)
            }

            if (preferenceApplier.saveViewHistory<commit_msg>bugfix #489 Fix confusion when tab refresh.<commit_after>/*
 * Copyright (c) 2020 toastkidjp.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompany this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
 */

package jp.toastkid.yobidashi.browser.webview.factory

import android.annotation.TargetApi
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.net.http.SslError
import android.os.Build
import android.util.SparseArray
import android.webkit.SslErrorHandler
import android.webkit.WebResourceError
import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.fragment.app.FragmentActivity
import jp.toastkid.lib.ContentViewModel
import jp.toastkid.lib.preference.PreferenceApplier
import jp.toastkid.yobidashi.R
import jp.toastkid.yobidashi.browser.BrowserHeaderViewModel
import jp.toastkid.yobidashi.browser.FaviconApplier
import jp.toastkid.yobidashi.browser.LoadingViewModel
import jp.toastkid.yobidashi.browser.block.AdRemover
import jp.toastkid.yobidashi.browser.history.ViewHistoryInsertion
import jp.toastkid.yobidashi.browser.tls.TlsErrorDialogFragment
import jp.toastkid.yobidashi.browser.tls.TlsErrorMessageGenerator
import jp.toastkid.yobidashi.libs.intent.IntentFactory
import jp.toastkid.yobidashi.rss.suggestion.RssAddingSuggestion
import jp.toastkid.yobidashi.tab.History
import timber.log.Timber

class WebViewClientFactory(
        private val contentViewModel: ContentViewModel?,
        private val adRemover: AdRemover,
        private val faviconApplier: FaviconApplier,
        private val preferenceApplier: PreferenceApplier,
        private val browserHeaderViewModel: BrowserHeaderViewModel? = null,
        private val rssAddingSuggestion: RssAddingSuggestion? = null,
        private val loadingViewModel: LoadingViewModel? = null,
        private val currentView: () -> WebView? = { null },
        private val lastId: () -> String = { "" }
) {

    private val tabIds = SparseArray<String>()

    /**
     * Add onPageFinished and onPageStarted.
     */
    operator fun invoke(): WebViewClient = object : WebViewClient() {

        override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {
            super.onPageStarted(view, url, favicon)

            tabIds.put(view.hashCode(), lastId())

            browserHeaderViewModel?.updateProgress(0)
            browserHeaderViewModel?.nextUrl(url)

            rssAddingSuggestion?.invoke(view, url)
            browserHeaderViewModel?.setBackButtonEnability(view.canGoBack())
        }

        override fun onPageFinished(view: WebView, url: String?) {
            super.onPageFinished(view, url)

            val title = view.title ?: ""
            val urlStr = url ?: ""

            val tabId = tabIds.get(view.hashCode())
            if (tabId?.isNotBlank() == true) {
                loadingViewModel?.finished(tabId, History.make(title, urlStr))
            }

            browserHeaderViewModel?.updateProgress(100)
            browserHeaderViewModel?.stopProgress(true)

            try {
                if (view == currentView()) {
                    browserHeaderViewModel?.nextTitle(title)
                    browserHeaderViewModel?.nextUrl(urlStr)
                }
            } catch (e: Exception) {
                Timber.e(e)
            }

            if (preferenceApplier.saveViewHistory
{'fraction_non_alphanumeric': 0.08512101210121012, 'fraction_numerical': 0.0037128712871287127, 'mean_word_length': 3.336911150864639, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4268312', 'n_tokens_mistral': 2159, 'n_tokens_neox': 1993, 'n_words': 417}