aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/web/MessageWebView.kt
blob: 0f3a12b6e8d8f15e0a7e0bf7c49c946f0f01a110 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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.FbCookie
import com.pitchedapps.frost.facebook.FbTab
import com.pitchedapps.frost.facebook.USER_AGENT_BASIC
import com.pitchedapps.frost.injectors.JsActions
import com.pitchedapps.frost.services.NotificationService
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.utils.frostAnswersCustom
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.runOnUiThread

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

    init {
        gone()
        setupWebview()
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun setupWebview() {
        settings.javaScriptEnabled = true
        settings.userAgentString = USER_AGENT_BASIC
        webViewClient = HeadlessWebViewClient("MessageNotifs", JsActions.GET_MESSAGES)
        webChromeClient = QuietChromeClient()
        addJavascriptInterface(MessageJSI(), "Frost")
    }

    private val startTime = System.currentTimeMillis()
    private val endTime: Long by lazy { System.currentTimeMillis() }
    private var inProgress = false
    private val pendingRequests: MutableList<CookieModel> = mutableListOf()
    private lateinit var data: CookieModel

    fun request(data: CookieModel) {
        pendingRequests.add(data)
        if (inProgress) return
        inProgress = true
        load(data)
    }

    private fun load(data: CookieModel) {
        L.d("Notif retrieving messages", data.toString())
        this.data = data
        FbCookie.setWebCookie(data.cookie) { context.runOnUiThread { L.d("Notif messages load"); loadUrl(FbTab.MESSAGES.url) } }
    }

    inner class MessageJSI {
        @JavascriptInterface
        fun handleHtml(html: String) {
            L.d("Notif messages received", data.toString())
            doAsync { service.fetchMessageNotifications(data, html) }
            pendingRequests.remove(data)
            if (pendingRequests.isEmpty()) {
                val time = endTime - startTime
                L.d("Notif messages finished $time")
                frostAnswersCustom("Notifications") {
                    putCustomAttribute("Message retrieval duration", time)
                }
                post { destroy() }
                service.jobFinished(params, false)
                service.future = null
            } else {
                load(pendingRequests.first())
            }
        }
    }

}