aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/web/MessageWebView.kt
blob: 53fa0657b9ecc26dbe15c5916588c1dac7711cc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.pitchedapps.frost.web

import android.annotation.SuppressLint
import android.app.job.JobParameters
import android.webkit.JavascriptInterface
import android.webkit.WebView
import ca.allanwang.kau.utils.gone
import com.pitchedapps.frost.dbflow.CookieModel
import com.pitchedapps.frost.facebook.FbTab
import com.pitchedapps.frost.facebook.USER_AGENT_BASIC
import com.pitchedapps.frost.injectors.JsAssets
import com.pitchedapps.frost.services.NotificationService
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.utils.frostAnswersCustom
import org.jetbrains.anko.doAsync

/**
 * Created by Allan Wang on 2017-07-17.
 *
 * Bare boned headless view made solely to extract conversation info
 */
@SuppressLint("ViewConstructor")
class MessageWebView(val service: NotificationService, val params: JobParameters?, val cookie: CookieModel) : WebView(service) {

    private val startTime = System.currentTimeMillis()
    private var isCancelled = false

    init {
        gone()
        setupWebview()
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun setupWebview() {
        settings.javaScriptEnabled = true
        settings.userAgentString = USER_AGENT_BASIC
        webViewClient = HeadlessWebViewClient("MessageNotifs", JsAssets.NOTIF_MSG)
        webChromeClient = QuietChromeClient()
        addJavascriptInterface(MessageJSI(), "Frost")
        loadUrl(FbTab.MESSAGES.url)
    }

    fun finish() {
        if (isCancelled) return
        isCancelled = true
        post { destroy() }
        service.finish(params)
    }

    override fun destroy() {
        L.d("MessageWebView destroyed")
        super.destroy()
    }

    inner class MessageJSI {
        @JavascriptInterface
        fun handleHtml(html: String) {
            if (isCancelled) return
            if (html.length < 10) return finish()
            val time = System.currentTimeMillis() - startTime
            L.d("Notif messages fetched in $time ms")
            frostAnswersCustom("NotificationTime", "Type" to "IM Headless", "Duration" to time)
            doAsync { service.fetchMessageNotifications(cookie, html); finish() }
        }
    }

}