From d9e2562267e549ee407e683262406581f2c4888e Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Thu, 12 Apr 2018 18:50:58 -0400 Subject: Misc (#865) * Clean up git code * Update download link * Update theme * Reorder settings, resolvees #856 * Do not log undeliverable exception * Handle potential lack of webview * Set notification epoch default to now, resolves #857 * Fix notification epoch time init * Update changelog * Update theme * Add slack url, resolves #880 * Prepare for image update --- .../frost/services/FrostNotifications.kt | 2 + .../frost/services/NotificationService.kt | 2 - .../frost/services/NotificationUtils.kt | 6 +- .../pitchedapps/frost/services/UpdateService.kt | 146 --------------------- 4 files changed, 3 insertions(+), 153 deletions(-) delete mode 100644 app/src/main/kotlin/com/pitchedapps/frost/services/UpdateService.kt (limited to 'app/src/main/kotlin/com/pitchedapps/frost/services') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt b/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt index 0af76b31..bc2e66a5 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt @@ -120,6 +120,8 @@ enum class NotificationType( if (newLatestEpoch > prevLatestEpoch) putTime(prevNotifTime, newLatestEpoch).save() L.d { "Notif $name new epoch ${getTime(lastNotificationTime(userId))}" } + if (prevLatestEpoch == -1L) + return 0 // do not notify the first time frostEvent("Notifications", "Type" to name, "Count" to notifs.size) if (notifs.size > 1) summaryNotification(context, userId, notifs.size).notify(context) 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 22477eab..35dd9571 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt @@ -55,8 +55,6 @@ class NotificationService : JobService() { override fun onStartJob(params: JobParameters?): Boolean { L.i { "Fetching notifications" } future = doAsync { - val context = weakRef.get() - ?: return@doAsync L.eThrow("NotificationService had null weakRef to self") val currentId = Prefs.userId val cookies = loadFbCookiesSync() val jobId = params?.extras?.getInt(NOTIFICATION_PARAM_ID, -1) ?: -1 diff --git a/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationUtils.kt b/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationUtils.kt index 7014cb78..59352f8e 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationUtils.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationUtils.kt @@ -24,24 +24,20 @@ import com.pitchedapps.frost.utils.Prefs */ const val NOTIF_CHANNEL_GENERAL = "general" const val NOTIF_CHANNEL_MESSAGES = "messages" -const val NOTIF_CHANNEL_UPDATES = "updates" fun setupNotificationChannels(c: Context) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return val manager = c.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val appName = c.string(R.string.frost_name) val msg = c.string(R.string.messages) - val updates = c.string(R.string.updates) manager.notificationChannels .filter { it.id != NOTIF_CHANNEL_GENERAL && it.id != NOTIF_CHANNEL_MESSAGES - && it.id != NOTIF_CHANNEL_UPDATES } .forEach { manager.deleteNotificationChannel(it.id) } manager.createNotificationChannel(NOTIF_CHANNEL_GENERAL, appName) manager.createNotificationChannel(NOTIF_CHANNEL_MESSAGES, "$appName: $msg") - manager.createNotificationChannel(NOTIF_CHANNEL_UPDATES, "$appName: $updates") L.d { "Created notification channels: ${manager.notificationChannels.size} channels, ${manager.notificationChannelGroups.size} groups" } } @@ -144,4 +140,4 @@ inline fun Context.fetchJob(id: Int): Boolean { return false } return true -} \ No newline at end of file +} diff --git a/app/src/main/kotlin/com/pitchedapps/frost/services/UpdateService.kt b/app/src/main/kotlin/com/pitchedapps/frost/services/UpdateService.kt deleted file mode 100644 index 0a528a0f..00000000 --- a/app/src/main/kotlin/com/pitchedapps/frost/services/UpdateService.kt +++ /dev/null @@ -1,146 +0,0 @@ -package com.pitchedapps.frost.services - -import android.app.job.JobParameters -import android.app.job.JobService -import android.content.Context -import android.support.v4.app.NotificationManagerCompat -import ca.allanwang.kau.kotlin.firstOrNull -import ca.allanwang.kau.utils.string -import com.fasterxml.jackson.databind.JsonNode -import com.fasterxml.jackson.databind.ObjectMapper -import com.pitchedapps.frost.BuildConfig -import com.pitchedapps.frost.R -import com.pitchedapps.frost.facebook.requests.httpClient -import com.pitchedapps.frost.utils.L -import com.pitchedapps.frost.utils.Prefs -import com.pitchedapps.frost.utils.ReleasePrefs -import com.pitchedapps.frost.utils.frostEvent -import okhttp3.Request -import org.jetbrains.anko.doAsync -import org.joda.time.DateTime -import java.util.concurrent.Future - -/** - * Created by Allan Wang on 07/04/18. - */ -data class FrostRelease(val versionName: String, - val timestamp: Long, - val apk: FrostApkRelease? = null, - val category: String = "") - -data class FrostApkRelease(val size: Long, - val name: String, - val url: String, - val timestamp: Long, - val downloadCount: Long = -1) - -object UpdateManager { - internal fun getLatestGithubRelease(): FrostRelease? { - try { - val data = getGithubReleaseJsonV3() ?: return null - return parseGithubReleaseV3(data) - } catch (e: Exception) { - L.e(e) { - "Failed to get github release" - } - return null - } - } - - private fun JsonNode.asMillis(): Long = DateTime(asText()).millis - - private fun getGithubReleaseJsonV3(): JsonNode? { - val mapper = ObjectMapper() - val response = httpClient.newCall(Request.Builder() - .url("https://api.github.com/repos/AllanWang/Frost-for-Facebook/releases/latest") - .get().build()).execute().body()?.string() ?: return null - return mapper.readTree(response) - } - - private fun parseGithubReleaseV3(data: JsonNode): FrostRelease? { - val versionName = data.get("tag_name").asText() - if (versionName.isEmpty()) return null - val release = FrostRelease( - versionName = versionName, - timestamp = data.get("created_at").asMillis(), - category = "Github") - val assets = data.get("assets") - if (!assets.isArray) return release - val apkRelease = assets.elements().firstOrNull { - it.get("content_type").asText().contains("android") - } ?: return release - val apk = FrostApkRelease(size = apkRelease.get("size").asLong(), - name = apkRelease.get("name").asText(), - url = apkRelease.get("browser_download_url").asText(), - timestamp = apkRelease.get("updated_at").asMillis(), - downloadCount = apkRelease.get("download_count").asLong()) - return release.copy(apk = apk) - } -} - -class UpdateService : JobService() { - - private var future: Future? = null - - private val startTime = System.currentTimeMillis() - - override fun onStopJob(params: JobParameters?): Boolean { - val time = System.currentTimeMillis() - startTime - L.d { "Update service has finished abruptly in $time ms" } - frostEvent("UpdateTime", - "Type" to "Service force stop", - "Duration" to time) - future?.cancel(true) - future = null - return false - } - - fun finish(params: JobParameters?) { - val time = System.currentTimeMillis() - startTime - L.i { "Update service has finished in $time ms" } - frostEvent("UpdateTime", - "Type" to "Service", - "Duration" to time) - jobFinished(params, false) - future?.cancel(true) - future = null - } - - override fun onStartJob(params: JobParameters?): Boolean { -// L.i { "Fetching update" } -// future = doAsync { -// fetch() -// finish(params) -// } -// return true - return false - } - - private fun fetch() { - val release = UpdateManager.getLatestGithubRelease() ?: return - val timestamp = release.apk?.timestamp ?: return - if (ReleasePrefs.lastTimeStamp >= timestamp) return - ReleasePrefs.lastTimeStamp = timestamp - if (BuildConfig.VERSION_NAME.contains(release.apk.name)) return - updateNotification(release) - } - - private fun updateNotification(release: FrostRelease) { - val notifBuilder = frostNotification(NOTIF_CHANNEL_UPDATES) - .setFrostAlert(true, Prefs.notificationRingtone) - .setContentTitle(string(R.string.frost_name)) - .setContentText(string(R.string.update_notif_message)) - NotificationManagerCompat.from(this).notify(release.versionName.hashCode(), notifBuilder.build()) - } - -} - -const val UPDATE_PERIODIC_JOB = 7 - -fun Context.scheduleUpdater(enable: Boolean): Boolean = - scheduleJob(UPDATE_PERIODIC_JOB, if (enable) 1440 else -1) - -const val UPDATE_JOB_NOW = 6 - -fun Context.fetchUpdates(): Boolean = - fetchJob(UPDATE_JOB_NOW) \ No newline at end of file -- cgit v1.2.3