hide reminder activity at Alarms too when appropriate

This commit is contained in:
tibbi 2018-03-14 18:36:50 +01:00
parent ee5e15c384
commit 49edf70f61
6 changed files with 35 additions and 7 deletions

View File

@ -38,7 +38,7 @@ class ReminderActivity : SimpleActivity() {
Handler().postDelayed({
if (isAlarmReminder) {
showAlarmNotification(alarm!!)
showAlarmNotification(alarm!!, true)
} else {
showTimerNotification(true)
}

View File

@ -1,5 +1,6 @@
package com.simplemobiletools.clock.activities
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.simplemobiletools.clock.extensions.config
@ -7,6 +8,7 @@ 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.clock.helpers.HIDE_REMINDER_ACTIVITY
import com.simplemobiletools.commons.extensions.showPickSecondsDialog
import com.simplemobiletools.commons.helpers.MINUTE_SECONDS
@ -24,11 +26,21 @@ class SnoozeReminderActivity : AppCompatActivity() {
}
private fun dialogCancelled() {
checkReminderActivityHiding()
finishActivity()
}
private fun finishActivity() {
checkReminderActivityHiding()
finish()
overridePendingTransition(0, 0)
}
private fun checkReminderActivityHiding() {
if (intent.getBooleanExtra(HIDE_REMINDER_ACTIVITY, false)) {
Intent(this, ReminderActivity::class.java).apply {
startActivity(this)
}
}
}
}

View File

@ -236,9 +236,9 @@ fun Context.rescheduleEnabledAlarms() {
fun Context.isScreenOn() = (getSystemService(Context.POWER_SERVICE) as PowerManager).isScreenOn
fun Context.showAlarmNotification(alarm: Alarm) {
fun Context.showAlarmNotification(alarm: Alarm, addDeleteIntent: Boolean) {
val pendingIntent = getOpenAlarmTabIntent()
val notification = getAlarmNotification(pendingIntent, alarm)
val notification = getAlarmNotification(pendingIntent, alarm, addDeleteIntent)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(alarm.id, notification)
scheduleNextAlarm(alarm, false)
@ -303,7 +303,7 @@ fun Context.getTimerPendingIntent(): PendingIntent {
}
@SuppressLint("NewApi")
fun Context.getAlarmNotification(pendingIntent: PendingIntent, alarm: Alarm): Notification {
fun Context.getAlarmNotification(pendingIntent: PendingIntent, alarm: Alarm, addDeleteIntent: Boolean): Notification {
val channelId = "alarm_channel"
if (isOreoPlus()) {
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
@ -317,6 +317,7 @@ fun Context.getAlarmNotification(pendingIntent: PendingIntent, alarm: Alarm): No
}
}
val reminderActivityIntent = getReminderActivityIntent()
val builder = NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.alarm))
.setContentText(getFormattedTime(alarm.timeInMinutes * 60, false, false))
@ -327,7 +328,11 @@ fun Context.getAlarmNotification(pendingIntent: PendingIntent, alarm: Alarm): No
.setAutoCancel(true)
.setSound(Uri.parse(alarm.soundUri), AudioManager.STREAM_ALARM)
.setChannelId(channelId)
.addAction(R.drawable.ic_snooze, getString(R.string.snooze), getSnoozePendingIntent(alarm))
.addAction(R.drawable.ic_snooze, getString(R.string.snooze), getSnoozePendingIntent(alarm, addDeleteIntent))
if (addDeleteIntent) {
builder.setDeleteIntent(reminderActivityIntent)
}
if (isLollipopPlus()) {
builder.setVisibility(Notification.VISIBILITY_PUBLIC)
@ -343,10 +348,11 @@ fun Context.getAlarmNotification(pendingIntent: PendingIntent, alarm: Alarm): No
return notification
}
fun Context.getSnoozePendingIntent(alarm: Alarm): PendingIntent {
fun Context.getSnoozePendingIntent(alarm: Alarm, hideReminderActivity: Boolean): PendingIntent {
val snoozeClass = if (config.useSameSnooze) SnoozeService::class.java else SnoozeReminderActivity::class.java
val intent = Intent(this, snoozeClass).setAction("Snooze")
intent.putExtra(ALARM_ID, alarm.id)
intent.putExtra(HIDE_REMINDER_ACTIVITY, hideReminderActivity)
return if (config.useSameSnooze) {
PendingIntent.getService(this, alarm.id, intent, PendingIntent.FLAG_UPDATE_CURRENT)
} else {

View File

@ -22,6 +22,7 @@ const val DEFAULT_ALARM_MINUTES = 480
const val DAY_MINUTES = 1440
const val DEFAULT_MAX_ALARM_REMINDER_SECS = 300
const val DEFAULT_MAX_TIMER_REMINDER_SECS = 60
const val HIDE_REMINDER_ACTIVITY = "hide_reminder_activity"
const val REMINDER_ACTIVITY_INTENT_ID = 9995
const val OPEN_ALARMS_TAB_INTENT_ID = 9996

View File

@ -14,7 +14,7 @@ class AlarmReceiver : BroadcastReceiver() {
val alarm = context.dbHelper.getAlarmWithId(id) ?: return
if (context.isScreenOn()) {
context.showAlarmNotification(alarm)
context.showAlarmNotification(alarm, false)
Handler().postDelayed({
context.hideNotification(id)
}, context.config.alarmMaxReminderSecs * 1000L)

View File

@ -2,11 +2,13 @@ package com.simplemobiletools.clock.services
import android.app.IntentService
import android.content.Intent
import com.simplemobiletools.clock.activities.ReminderActivity
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.clock.helpers.HIDE_REMINDER_ACTIVITY
class SnoozeService : IntentService("Snooze") {
override fun onHandleIntent(intent: Intent) {
@ -14,5 +16,12 @@ class SnoozeService : IntentService("Snooze") {
val alarm = dbHelper.getAlarmWithId(id) ?: return
hideNotification(id)
setupAlarmClock(alarm, config.snoozeTime * 60)
if (intent.getBooleanExtra(HIDE_REMINDER_ACTIVITY, false)) {
Intent(this, ReminderActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(this)
}
}
}
}