<commit_before>package jp.toastkid.jitte.browser.tab

import android.app.DownloadManager
import android.content.Context
import android.graphics.Bitmap
import android.net.Uri
import android.support.v7.app.AlertDialog
import android.text.TextUtils
import android.view.View
import android.webkit.*
import android.widget.FrameLayout
import android.widget.ProgressBar
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.functions.Consumer
import jp.toastkid.jitte.R
import jp.toastkid.jitte.TitlePair
import jp.toastkid.jitte.browser.UserAgent
import jp.toastkid.jitte.browser.WebViewFactory
import jp.toastkid.jitte.browser.archive.Archive
import jp.toastkid.jitte.browser.history.ViewHistoryInsertion
import jp.toastkid.jitte.browser.screenshots.Screenshot
import jp.toastkid.jitte.libs.Bitmaps
import jp.toastkid.jitte.libs.Toaster
import jp.toastkid.jitte.libs.clip.Clipboard
import jp.toastkid.jitte.libs.preference.ColorPair
import jp.toastkid.jitte.libs.preference.PreferenceApplier
import jp.toastkid.jitte.libs.storage.Storeroom
import jp.toastkid.jitte.search.SiteSearch
import okio.Okio
import java.io.File
import java.io.IOException


/**
 * ModuleAdapter of [Tab].
 *
 * @author toastkidjp
 */
class TabAdapter(
        progress: ProgressBar,
        webViewContainer: FrameLayout,
        titleCallback: Consumer<TitlePair>,
        touchCallback: () -> Unit,
        private val tabEmptyCallback: () -> Unit
) {

    private val tabList: TabList

    private val colorPair: ColorPair

    private val webView: WebView

    /** Loading flag.  */
    private var isLoadFinished: Boolean = false

    private var backOrForwardProgress: Boolean = false

    private val favicons: Storeroom

    private val tabsScreenshots: Storeroom

    private val preferenceApplier: PreferenceApplier

    init {
        tabList = TabList.loadOrInit(progress.context)

        webView = makeWebView(progress, titleCallback, touchCallback)
        webViewContainer.addView(this.webView)

        favicons = Storeroom(webView.context, "favicons")

        tabsScreenshots = Storeroom(webView.context, "tabs/screenshots")
        preferenceApplier = PreferenceApplier(webView.context)
        colorPair = preferenceApplier.colorPair()
    }

    private fun makeWebView(
            progress: ProgressBar,
            titleCallback: Consumer<TitlePair>,
            touchCallback: () -> Unit
    ): WebView {
        val webViewClient = object : WebViewClient() {

            override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {
                super.onPageStarted(view, url, favicon)
                progress.visibility = View.VISIBLE
                val file = favicons.assignNewFile(Uri.parse(url).host)
                if (favicon != null) {
                    favicon.compress(
                            Bitmap.CompressFormat.PNG,
                            100,
                            Okio.buffer(Okio.sink(file)).outputStream()
                    )
                }
                isLoadFinished = false
            }

            override fun onPageFinished(view: WebView, url: String) {
                super.onPageFinished(view, url)
                isLoadFinished = true
                progress.visibility = View.GONE
                try {
                    titleCallback.accept(TitlePair.make(view.title, view.url))
                } catch (e: Exception) {
                    e.printStackTrace()
                }

                deleteThumbnail(tabList.currentTab().thumbnailPath)

                saveNewThumbnail()

                if (!backOrForwardProgress) {
                    addHistory(view.title, view.url)

                    if (preferenceApplier.saveViewHistory) {
                        ViewHistoryInsertion
                                .make(
                                        view.context,
                                        view.title,
                                        view.url,
                                        favicons.assignNewFile(Uri.parse(url).host).absolutePath
                                )
                                .insert()
                    }
                }
                backOrForwardProgress = false
            }

            override fun onReceivedError(
                    view: WebView, request: WebResourceRequest, error: WebResourceError) {
                super.onReceivedError(view, request, error)
                backOrForwardProgress = false
            }
        }
        val webChromeClient = object : WebChromeClient() {
            override fun onProgressChanged(view: WebView, newProgress: Int) {
                super.onProgressChanged(view, newProgress)
                if (!isLoadFinished) {
                    progress.progress = newProgress
                    try {
                        titleCallback.accept(TitlePair.make(
                                view.context.getString(R.string.prefix_loading) + newProgress + "%",
                                view.url
                        )
                        )
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }<commit_msg>Fix non-null bug on Yahoo! JAPAN.<commit_after>package jp.toastkid.jitte.browser.tab

import android.app.DownloadManager
import android.content.Context
import android.graphics.Bitmap
import android.net.Uri
import android.support.v7.app.AlertDialog
import android.text.TextUtils
import android.view.View
import android.webkit.*
import android.widget.FrameLayout
import android.widget.ProgressBar
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.functions.Consumer
import jp.toastkid.jitte.R
import jp.toastkid.jitte.TitlePair
import jp.toastkid.jitte.browser.UserAgent
import jp.toastkid.jitte.browser.WebViewFactory
import jp.toastkid.jitte.browser.archive.Archive
import jp.toastkid.jitte.browser.history.ViewHistoryInsertion
import jp.toastkid.jitte.browser.screenshots.Screenshot
import jp.toastkid.jitte.libs.Bitmaps
import jp.toastkid.jitte.libs.Toaster
import jp.toastkid.jitte.libs.clip.Clipboard
import jp.toastkid.jitte.libs.preference.ColorPair
import jp.toastkid.jitte.libs.preference.PreferenceApplier
import jp.toastkid.jitte.libs.storage.Storeroom
import jp.toastkid.jitte.search.SiteSearch
import okio.Okio
import java.io.File
import java.io.IOException


/**
 * ModuleAdapter of [Tab].
 *
 * @author toastkidjp
 */
class TabAdapter(
        progress: ProgressBar,
        webViewContainer: FrameLayout,
        titleCallback: Consumer<TitlePair>,
        touchCallback: () -> Unit,
        private val tabEmptyCallback: () -> Unit
) {

    private val tabList: TabList

    private val colorPair: ColorPair

    private val webView: WebView

    /** Loading flag.  */
    private var isLoadFinished: Boolean = false

    private var backOrForwardProgress: Boolean = false

    private val favicons: Storeroom

    private val tabsScreenshots: Storeroom

    private val preferenceApplier: PreferenceApplier

    init {
        tabList = TabList.loadOrInit(progress.context)

        webView = makeWebView(progress, titleCallback, touchCallback)
        webViewContainer.addView(this.webView)

        favicons = Storeroom(webView.context, "favicons")

        tabsScreenshots = Storeroom(webView.context, "tabs/screenshots")
        preferenceApplier = PreferenceApplier(webView.context)
        colorPair = preferenceApplier.colorPair()
    }

    private fun makeWebView(
            progress: ProgressBar,
            titleCallback: Consumer<TitlePair>,
            touchCallback: () -> Unit
    ): WebView {
        val webViewClient = object : WebViewClient() {

            override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {
                super.onPageStarted(view, url, favicon)
                progress.visibility = View.VISIBLE
                val file = favicons.assignNewFile(Uri.parse(url).host)
                if (favicon != null) {
                    favicon.compress(
                            Bitmap.CompressFormat.PNG,
                            100,
                            Okio.buffer(Okio.sink(file)).outputStream()
                    )
                }
                isLoadFinished = false
            }

            override fun onPageFinished(view: WebView, url: String) {
                super.onPageFinished(view, url)
                isLoadFinished = true
                progress.visibility = View.GONE

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

                try {
                    titleCallback.accept(TitlePair.make(title, urlstr))
                } catch (e: Exception) {
                    e.printStackTrace()
                }

                deleteThumbnail(tabList.currentTab().thumbnailPath)

                saveNewThumbnail()

                if (!backOrForwardProgress) {
                    addHistory(title, urlstr)

                    if (preferenceApplier.saveViewHistory
                            && title.isNotEmpty()
                            && urlstr.isNotEmpty()
                            ) {
                        ViewHistoryInsertion
                                .make(
                                        view.context,
                                        title,
                                        urlstr,
                                        favicons.assignNewFile(Uri.parse(urlstr).host).absolutePath
                                )
                                .insert()
                    }
                }
                backOrForwardProgress = false
            }

            override fun onReceivedError(
                    view: WebView, request: WebResourceRequest, error: WebResourceError) {
                super.onReceivedError(view, request, error)
                backOrForwardProgress = false
            }
        }
        val webChromeClient = object : WebChromeClient() {
            override fun onProgressChanged(view: WebView, newProgress: Int) {
                super.onProgressChanged(view, newProgress)
                if (!isLoadFinished) {
                    progress.progress = newProgress
                    try {
                        titleCallback.accept(TitlePair.make(
                                view.context.getString(R.string.prefix_loading) + newProgress + "%",
                                view.url
                        )
                        )
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
{'fraction_non_alphanumeric': 0.07768610364327432, 'fraction_numerical': 0.0007416334476684898, 'mean_word_length': 1.7249305380146502, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3226065', 'n_tokens_mistral': 2930, 'n_tokens_neox': 2675, 'n_words': 515}