From 44608c79ae79e698bfd815290f07cec090bd9f83 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sun, 17 Sep 2017 12:49:35 -0400 Subject: Add checks to ensure job service exists --- .../com/pitchedapps/frost/services/FrostNotifications.kt | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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 2ab15fac..b892af91 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt @@ -168,7 +168,11 @@ const val NOTIFICATION_PERIODIC_JOB = 7 * returns false if an error occurs; true otherwise */ fun Context.scheduleNotifications(minutes: Long): Boolean { - val scheduler = getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler + val scheduler = getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler? + if (scheduler == null) { + L.e("JobScheduler not found; cannot schedule notifications") + return false + } scheduler.cancel(NOTIFICATION_PERIODIC_JOB) if (minutes < 0L) return true val serviceComponent = ComponentName(this, NotificationService::class.java) @@ -190,7 +194,11 @@ const val NOTIFICATION_JOB_NOW = 6 * Run notification job right now */ fun Context.fetchNotifications(): Boolean { - val scheduler = getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler? ?: return false + val scheduler = getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler? + if (scheduler == null) { + L.e("JobScheduler not found") + return false + } val serviceComponent = ComponentName(this, NotificationService::class.java) val builder = JobInfo.Builder(NOTIFICATION_JOB_NOW, serviceComponent) .setMinimumLatency(0L) -- cgit v1.2.3