aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationUtils.kt
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/services/NotificationUtils.kt')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/services/NotificationUtils.kt69
1 files changed, 44 insertions, 25 deletions
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 707220f6..20a497e3 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationUtils.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationUtils.kt
@@ -1,3 +1,19 @@
+/*
+ * Copyright 2018 Allan Wang
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
package com.pitchedapps.frost.services
import android.app.Notification
@@ -31,11 +47,11 @@ fun setupNotificationChannels(c: Context) {
val appName = c.string(R.string.frost_name)
val msg = c.string(R.string.messages)
manager.notificationChannels
- .filter {
- it.id != NOTIF_CHANNEL_GENERAL
- && it.id != NOTIF_CHANNEL_MESSAGES
- }
- .forEach { manager.deleteNotificationChannel(it.id) }
+ .filter {
+ it.id != NOTIF_CHANNEL_GENERAL &&
+ it.id != NOTIF_CHANNEL_MESSAGES
+ }
+ .forEach { manager.deleteNotificationChannel(it.id) }
manager.createNotificationChannel(NOTIF_CHANNEL_GENERAL, appName)
manager.createNotificationChannel(NOTIF_CHANNEL_MESSAGES, "$appName: $msg")
L.d { "Created notification channels: ${manager.notificationChannels.size} channels, ${manager.notificationChannelGroups.size} groups" }
@@ -43,8 +59,10 @@ fun setupNotificationChannels(c: Context) {
@RequiresApi(Build.VERSION_CODES.O)
private fun NotificationManager.createNotificationChannel(id: String, name: String): NotificationChannel {
- val channel = NotificationChannel(id,
- name, NotificationManager.IMPORTANCE_DEFAULT)
+ val channel = NotificationChannel(
+ id,
+ name, NotificationManager.IMPORTANCE_DEFAULT
+ )
channel.enableLights(true)
channel.lightColor = Prefs.accentColor
channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
@@ -53,14 +71,14 @@ private fun NotificationManager.createNotificationChannel(id: String, name: Stri
}
fun Context.frostNotification(id: String) =
- NotificationCompat.Builder(this, id)
- .apply {
- setSmallIcon(R.drawable.frost_f_24)
- setAutoCancel(true)
- setOnlyAlertOnce(true)
- setStyle(NotificationCompat.BigTextStyle())
- color = color(R.color.frost_notification_accent)
- }
+ NotificationCompat.Builder(this, id)
+ .apply {
+ setSmallIcon(R.drawable.frost_f_24)
+ setAutoCancel(true)
+ setOnlyAlertOnce(true)
+ setStyle(NotificationCompat.BigTextStyle())
+ color = color(R.color.frost_notification_accent)
+ }
/**
* Dictates whether a notification should have sound/vibration/lights or not
@@ -70,8 +88,9 @@ fun Context.frostNotification(id: String) =
fun NotificationCompat.Builder.setFrostAlert(enable: Boolean, ringtone: String): NotificationCompat.Builder {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
setGroupAlertBehavior(
- if (enable) NotificationCompat.GROUP_ALERT_CHILDREN
- else NotificationCompat.GROUP_ALERT_SUMMARY)
+ if (enable) NotificationCompat.GROUP_ALERT_CHILDREN
+ else NotificationCompat.GROUP_ALERT_SUMMARY
+ )
} else if (!enable) {
setDefaults(0)
} else {
@@ -111,10 +130,10 @@ inline fun <reified T : JobService> Context.scheduleJob(id: Int, minutes: Long):
if (minutes < 0L) return true
val serviceComponent = ComponentName(this, T::class.java)
val builder = JobInfo.Builder(id, serviceComponent)
- .setPeriodic(minutes * 60000)
- .setExtras(id)
- .setPersisted(true)
- .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) //TODO add options
+ .setPeriodic(minutes * 60000)
+ .setExtras(id)
+ .setPersisted(true)
+ .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) //TODO add options
val result = scheduler.schedule(builder.build())
if (result <= 0) {
L.eThrow("${T::class.java.simpleName} scheduler failed")
@@ -130,10 +149,10 @@ inline fun <reified T : JobService> Context.fetchJob(id: Int): Boolean {
val scheduler = getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
val serviceComponent = ComponentName(this, T::class.java)
val builder = JobInfo.Builder(id, serviceComponent)
- .setMinimumLatency(0L)
- .setExtras(id)
- .setOverrideDeadline(2000L)
- .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
+ .setMinimumLatency(0L)
+ .setExtras(id)
+ .setOverrideDeadline(2000L)
+ .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
val result = scheduler.schedule(builder.build())
if (result <= 0) {
L.eThrow("${T::class.java.simpleName} instant scheduler failed")