diff options
author | Allan Wang <me@allanwang.ca> | 2017-06-19 15:31:10 -0700 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2017-06-19 15:31:10 -0700 |
commit | 382433780c3f4403723a78e409cb161c9fad5034 (patch) | |
tree | 1138696bdd6e04d2227acaa4ab19ee8b85b4d2f8 /app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt | |
parent | 0720413ee859762fc4c2d8748ba10402dffd0be7 (diff) | |
download | frost-382433780c3f4403723a78e409cb161c9fad5034.tar.gz frost-382433780c3f4403723a78e409cb161c9fad5034.tar.bz2 frost-382433780c3f4403723a78e409cb161c9fad5034.zip |
Parse header badges
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt')
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt | 85 |
1 files changed, 58 insertions, 27 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt b/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt index 85765541..f506bd8f 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt @@ -1,14 +1,18 @@ package com.pitchedapps.frost.services -import android.app.IntentService import android.app.Notification import android.app.PendingIntent +import android.app.job.JobParameters +import android.app.job.JobService import android.content.Context import android.content.Intent +import android.os.Looper import android.support.v4.app.ActivityOptionsCompat import android.support.v4.app.NotificationCompat import android.support.v4.app.NotificationManagerCompat +import ca.allanwang.kau.utils.checkThread import ca.allanwang.kau.utils.string +import com.pitchedapps.frost.BuildConfig import com.pitchedapps.frost.R import com.pitchedapps.frost.WebOverlayActivity import com.pitchedapps.frost.dbflow.* @@ -17,43 +21,58 @@ import com.pitchedapps.frost.facebook.FB_URL_BASE import com.pitchedapps.frost.facebook.FbTab import com.pitchedapps.frost.utils.ARG_URL import com.pitchedapps.frost.utils.L +import org.jetbrains.anko.doAsync import org.jsoup.Jsoup import org.jsoup.nodes.Element +import java.util.concurrent.Future /** * Created by Allan Wang on 2017-06-14. */ -class NotificationService : IntentService(NotificationService::class.java.simpleName) { +class NotificationService : JobService() { - companion object { - const val ARG_ID = "arg_id" - val epochMatcher: Regex by lazy { Regex(":([0-9]*),") } - val notifIdMatcher: Regex by lazy { Regex("notif_id\":([0-9]*),") } + var future: Future<Unit>? = null + + override fun onStopJob(params: JobParameters?): Boolean { + future?.cancel(true) + future = null + return false } - override fun onHandleIntent(intent: Intent) { - val id = intent.getLongExtra(ARG_ID, -1L) - L.i("Handling notifications for $id") - if (id == -1L) return - val data = loadFbCookie(id) ?: return - L.v("Using data $data") - val doc = Jsoup.connect(FbTab.NOTIFICATIONS.url).cookie(FACEBOOK_COM, data.cookie).get() - val unreadNotifications = doc.getElementById("notifications_list").getElementsByClass("aclb") - var notifCount = 0 - var latestEpoch = lastNotificationTime(data.id) - L.v("Latest Epoch $latestEpoch") - unreadNotifications.forEach { - elem -> - val notif = parseNotification(data, elem) - if (notif != null) { - if (notif.timestamp <= latestEpoch) return@forEach - notif.createNotification(this) - latestEpoch = notif.timestamp - notifCount++ + override fun onStartJob(params: JobParameters?): Boolean { + future = doAsync { + loadFbCookiesSync().forEach { + data -> + L.i("Handling notifications for ${data.id}") + L.v("Using data $data") + val doc = Jsoup.connect(FbTab.NOTIFICATIONS.url).cookie(FACEBOOK_COM, data.cookie).get() + val unreadNotifications = doc.getElementById("notifications_list").getElementsByClass("aclb") + var notifCount = 0 + var latestEpoch = lastNotificationTime(data.id) + L.v("Latest Epoch $latestEpoch") + unreadNotifications.forEach unread@ { + elem -> + val notif = parseNotification(data, elem) + if (notif != null) { + if (notif.timestamp <= latestEpoch) return@unread + notif.createNotification(this@NotificationService) + latestEpoch = notif.timestamp + notifCount++ + } + } + if (notifCount > 0) saveNotificationTime(NotificationModel(data.id, latestEpoch)) + summaryNotification(data.id, notifCount) } + L.d("Finished notifications") + jobFinished(params, false) } - if (notifCount > 0) saveNotificationTime(NotificationModel(data.id, latestEpoch)) - summaryNotification(data.id, notifCount) + return true + } + + companion object { + const val ARG_ID = "arg_id" + val epochMatcher: Regex by lazy { Regex(":([0-9]*),") } + val notifIdMatcher: Regex by lazy { Regex("notif_id\":([0-9]*),") } } fun parseNotification(data: CookieModel, element: Element): NotificationContent? { @@ -71,6 +90,18 @@ class NotificationService : IntentService(NotificationService::class.java.simple return NotificationContent(data, notifId.toInt(), a.attr("href"), text, epoch) } + private fun Context.debugNotification(text: String) { + if (BuildConfig.DEBUG) { + val notifBuilder = NotificationCompat.Builder(this) + .setSmallIcon(R.drawable.frost_f_24) + .setContentTitle(string(R.string.app_name)) + .setContentText(text) + .setAutoCancel(true) + + NotificationManagerCompat.from(this).notify(999, notifBuilder.build()) + } + } + data class NotificationContent(val data: CookieModel, val notifId: Int, val href: String, val text: String, val timestamp: Long) { fun createNotification(context: Context) { val intent = Intent(context, WebOverlayActivity::class.java) |