diff options
author | Allan Wang <me@allanwang.ca> | 2017-07-17 12:38:42 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-17 12:38:42 -0700 |
commit | e4679b1663fa78a99c6c8225e454595c6c6f4e38 (patch) | |
tree | a777ee782207c89d6e9243399471b9ad936736d0 /app/src/main/kotlin/com/pitchedapps/frost/web/MessageWebView.kt | |
parent | 7bf93ea74b00ab7e8ceedac2aac6ad08fdf099ab (diff) | |
download | frost-e4679b1663fa78a99c6c8225e454595c6c6f4e38.tar.gz frost-e4679b1663fa78a99c6c8225e454595c6c6f4e38.tar.bz2 frost-e4679b1663fa78a99c6c8225e454595c6c6f4e38.zip |
Fix notifications and long press for albums (#69)v1.3.2
* Allow for album images to be viewed
* Update listing info
* Web refractoring
* Test message notifications
* Fix notifications and context press
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/web/MessageWebView.kt')
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/web/MessageWebView.kt | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/MessageWebView.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/MessageWebView.kt new file mode 100644 index 00000000..0f3a12b6 --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/MessageWebView.kt @@ -0,0 +1,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()) + } + } + } + +}
\ No newline at end of file |