From 382433780c3f4403723a78e409cb161c9fad5034 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Mon, 19 Jun 2017 15:31:10 -0700 Subject: Parse header badges --- .../frost/services/NotificationReceiver.kt | 20 ----- .../frost/services/NotificationService.kt | 85 +++++++++++++++------- .../pitchedapps/frost/services/UpdateReceiver.kt | 20 +++++ 3 files changed, 78 insertions(+), 47 deletions(-) delete mode 100644 app/src/main/kotlin/com/pitchedapps/frost/services/NotificationReceiver.kt create mode 100644 app/src/main/kotlin/com/pitchedapps/frost/services/UpdateReceiver.kt (limited to 'app/src/main/kotlin/com/pitchedapps/frost/services') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationReceiver.kt b/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationReceiver.kt deleted file mode 100644 index b37ca1f8..00000000 --- a/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationReceiver.kt +++ /dev/null @@ -1,20 +0,0 @@ -package com.pitchedapps.frost.services - -import android.content.BroadcastReceiver -import android.content.Context -import android.content.Intent - -/** - * Created by Allan Wang on 2017-05-31. - */ -class NotificationReceiver : BroadcastReceiver() { - - companion object { - const val ACTION = "com.pitchedapps.frost.NOTIFICATIONS" - } - - override fun onReceive(context: Context, intent: Intent) { - if (intent.action != ACTION) return - } - -} \ No newline at end of file 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? = 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) diff --git a/app/src/main/kotlin/com/pitchedapps/frost/services/UpdateReceiver.kt b/app/src/main/kotlin/com/pitchedapps/frost/services/UpdateReceiver.kt new file mode 100644 index 00000000..c40750ef --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/services/UpdateReceiver.kt @@ -0,0 +1,20 @@ +package com.pitchedapps.frost.services + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import com.pitchedapps.frost.utils.L +import com.pitchedapps.frost.utils.Prefs +import com.pitchedapps.frost.utils.scheduleNotifications + +/** + * Created by Allan Wang on 2017-05-31. + */ +class UpdateReceiver : BroadcastReceiver() { + + override fun onReceive(context: Context, intent: Intent) { + L.d("Frost has updated") + context.scheduleNotifications(Prefs.notificationFreq) //Update notifications + } + +} \ No newline at end of file -- cgit v1.2.3