From 244a137e5b8f227576fb35304449a659497f2c31 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 7 Mar 2018 15:09:34 +0100 Subject: [PATCH] handle dynamic snooze picker --- .../activities/SnoozeReminderActivity.kt | 23 +++++++++++++++++++ .../clock/extensions/Context.kt | 6 +++++ .../clock/receivers/AlarmReceiver.kt | 1 - .../clock/services/SnoozeService.kt | 7 ++---- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/activities/SnoozeReminderActivity.kt b/app/src/main/kotlin/com/simplemobiletools/clock/activities/SnoozeReminderActivity.kt index ca003eb..f085474 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/activities/SnoozeReminderActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/activities/SnoozeReminderActivity.kt @@ -2,9 +2,32 @@ package com.simplemobiletools.clock.activities import android.os.Bundle import android.support.v7.app.AppCompatActivity +import com.simplemobiletools.clock.extensions.config +import com.simplemobiletools.clock.extensions.dbHelper +import com.simplemobiletools.clock.extensions.hideNotification +import com.simplemobiletools.clock.extensions.setupAlarmClock +import com.simplemobiletools.clock.helpers.ALARM_ID +import com.simplemobiletools.commons.extensions.showPickIntervalDialog class SnoozeReminderActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + val id = intent.getIntExtra(ALARM_ID, -1) + val alarm = dbHelper.getAlarmWithId(id) ?: return + hideNotification(id) + showPickIntervalDialog(config.snoozeTime, true, cancelCallback = { dialogCancelled() }) { + config.snoozeTime = it + setupAlarmClock(alarm, it * 60) + finishActivity() + } + } + + private fun dialogCancelled() { + finishActivity() + } + + private fun finishActivity() { + finish() + overridePendingTransition(0, 0) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Context.kt index 9d09f1d..d88746e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Context.kt @@ -2,6 +2,7 @@ package com.simplemobiletools.clock.extensions import android.annotation.SuppressLint import android.app.AlarmManager +import android.app.NotificationManager import android.app.PendingIntent import android.content.Context import android.content.Intent @@ -133,3 +134,8 @@ fun Context.cancelAlarmClock(alarm: Alarm) { val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager alarmManager.cancel(getPendingIntent(alarm)) } + +fun Context.hideNotification(id: Int) { + val manager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + manager.cancel(id) +} diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/receivers/AlarmReceiver.kt b/app/src/main/kotlin/com/simplemobiletools/clock/receivers/AlarmReceiver.kt index b0dd66b..7282be5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/receivers/AlarmReceiver.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/receivers/AlarmReceiver.kt @@ -34,7 +34,6 @@ class AlarmReceiver : BroadcastReceiver() { val notification = getNotification(context, pendingIntent, alarm) val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager.notify(alarm.id, notification) - context.scheduleNextAlarm(alarm, false) } diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/services/SnoozeService.kt b/app/src/main/kotlin/com/simplemobiletools/clock/services/SnoozeService.kt index 325b8f1..031a210 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/services/SnoozeService.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/services/SnoozeService.kt @@ -1,11 +1,10 @@ package com.simplemobiletools.clock.services import android.app.IntentService -import android.app.NotificationManager -import android.content.Context import android.content.Intent import com.simplemobiletools.clock.extensions.config import com.simplemobiletools.clock.extensions.dbHelper +import com.simplemobiletools.clock.extensions.hideNotification import com.simplemobiletools.clock.extensions.setupAlarmClock import com.simplemobiletools.clock.helpers.ALARM_ID @@ -13,9 +12,7 @@ class SnoozeService : IntentService("Snooze") { override fun onHandleIntent(intent: Intent) { val id = intent.getIntExtra(ALARM_ID, -1) val alarm = dbHelper.getAlarmWithId(id) ?: return + hideNotification(id) setupAlarmClock(alarm, config.snoozeTime * 60) - - val manager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager - manager.cancel(alarm.id) } }