move the Alarm notification handling in context extension

This commit is contained in:
tibbi 2018-03-14 17:27:44 +01:00
parent afe7c695af
commit 4fa13283c8
3 changed files with 80 additions and 89 deletions

View File

@ -3,6 +3,7 @@ package com.simplemobiletools.clock.activities
import android.os.Bundle
import android.os.Handler
import com.simplemobiletools.clock.R
import com.simplemobiletools.clock.extensions.config
import com.simplemobiletools.clock.extensions.hideTimerNotification
import com.simplemobiletools.clock.extensions.showOverLockscreen
import com.simplemobiletools.clock.extensions.showTimerNotification
@ -12,6 +13,7 @@ import com.simplemobiletools.commons.extensions.updateTextColors
import kotlinx.android.synthetic.main.activity_reminder.*
class ReminderActivity : SimpleActivity() {
private val hideNotificationHandler = Handler()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -28,11 +30,16 @@ class ReminderActivity : SimpleActivity() {
Handler().postDelayed({
showTimerNotification()
hideNotificationHandler.postDelayed({
finish()
}, config.timerMaxReminderSecs * 1000L)
}, 1000L)
}
override fun onStop() {
super.onStop()
hideTimerNotification()
hideNotificationHandler.removeCallbacksAndMessages(null)
}
}

View File

@ -16,7 +16,7 @@ import android.text.SpannableString
import android.text.style.RelativeSizeSpan
import android.widget.Toast
import com.simplemobiletools.clock.R
import com.simplemobiletools.clock.activities.MainActivity
import com.simplemobiletools.clock.activities.SnoozeReminderActivity
import com.simplemobiletools.clock.activities.SplashActivity
import com.simplemobiletools.clock.helpers.*
import com.simplemobiletools.clock.models.Alarm
@ -25,6 +25,7 @@ import com.simplemobiletools.clock.models.MyTimeZone
import com.simplemobiletools.clock.receivers.AlarmReceiver
import com.simplemobiletools.clock.receivers.DateTimeWidgetUpdateReceiver
import com.simplemobiletools.clock.receivers.TimerReceiver
import com.simplemobiletools.clock.services.SnoozeService
import com.simplemobiletools.commons.extensions.formatMinutesToTimeString
import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor
import com.simplemobiletools.commons.extensions.toast
@ -141,11 +142,17 @@ fun Context.setupAlarmClock(alarm: Alarm, triggerInSeconds: Int) {
}
fun Context.getOpenAlarmTabIntent(): PendingIntent {
val intent = Intent(this, MainActivity::class.java)
val intent = Intent(this, SplashActivity::class.java)
intent.putExtra(OPEN_TAB, TAB_ALARM)
return PendingIntent.getActivity(this, OPEN_ALARMS_TAB_INTENT_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
fun Context.getOpenTimerTabIntent(): PendingIntent {
val intent = Intent(this, SplashActivity::class.java)
intent.putExtra(OPEN_TAB, TAB_TIMER)
return PendingIntent.getActivity(this, TIMER_NOTIF_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
fun Context.getAlarmIntent(alarm: Alarm): PendingIntent {
val intent = Intent(this, AlarmReceiver::class.java)
intent.putExtra(ALARM_ID, alarm.id)
@ -228,6 +235,14 @@ fun Context.rescheduleEnabledAlarms() {
fun Context.isScreenOn() = (getSystemService(Context.POWER_SERVICE) as PowerManager).isScreenOn
fun Context.showAlarmNotification(alarm: Alarm) {
val pendingIntent = getOpenAlarmTabIntent()
val notification = getAlarmNotification(pendingIntent, alarm)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(alarm.id, notification)
scheduleNextAlarm(alarm, false)
}
fun Context.showTimerNotification() {
val pendingIntent = getOpenTimerTabIntent()
val notification = getTimerNotification(pendingIntent)
@ -281,8 +296,54 @@ fun Context.getTimerPendingIntent(): PendingIntent {
return PendingIntent.getBroadcast(this, TIMER_NOTIF_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
fun Context.getOpenTimerTabIntent(): PendingIntent {
val intent = Intent(this, SplashActivity::class.java)
intent.putExtra(OPEN_TAB, TAB_TIMER)
return PendingIntent.getActivity(this, TIMER_NOTIF_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT)
@SuppressLint("NewApi")
fun Context.getAlarmNotification(pendingIntent: PendingIntent, alarm: Alarm): Notification {
val channelId = "alarm_channel"
if (isOreoPlus()) {
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val name = getString(R.string.alarm)
val importance = NotificationManager.IMPORTANCE_HIGH
NotificationChannel(channelId, name, importance).apply {
enableLights(true)
lightColor = getAdjustedPrimaryColor()
enableVibration(alarm.vibrate)
notificationManager.createNotificationChannel(this)
}
}
val builder = NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.alarm))
.setContentText(getFormattedTime(alarm.timeInMinutes * 60, false, false))
.setSmallIcon(R.drawable.ic_alarm)
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_LIGHTS)
.setAutoCancel(true)
.setSound(Uri.parse(alarm.soundUri), AudioManager.STREAM_ALARM)
.setChannelId(channelId)
.addAction(R.drawable.ic_snooze, getString(R.string.snooze), getSnoozePendingIntent(alarm))
if (isLollipopPlus()) {
builder.setVisibility(Notification.VISIBILITY_PUBLIC)
}
if (alarm.vibrate) {
val vibrateArray = LongArray(2) { 500 }
builder.setVibrate(vibrateArray)
}
val notification = builder.build()
notification.flags = notification.flags or Notification.FLAG_INSISTENT
return notification
}
fun Context.getSnoozePendingIntent(alarm: Alarm): 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)
return if (config.useSameSnooze) {
PendingIntent.getService(this, alarm.id, intent, PendingIntent.FLAG_UPDATE_CURRENT)
} else {
PendingIntent.getActivity(this, alarm.id, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
}

View File

@ -1,101 +1,24 @@
package com.simplemobiletools.clock.receivers
import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.media.AudioManager
import android.net.Uri
import android.os.Handler
import android.support.v4.app.NotificationCompat
import com.simplemobiletools.clock.R
import com.simplemobiletools.clock.activities.SnoozeReminderActivity
import com.simplemobiletools.clock.activities.SplashActivity
import com.simplemobiletools.clock.extensions.*
import com.simplemobiletools.clock.helpers.ALARM_ID
import com.simplemobiletools.clock.helpers.OPEN_TAB
import com.simplemobiletools.clock.helpers.TAB_ALARM
import com.simplemobiletools.clock.models.Alarm
import com.simplemobiletools.clock.services.SnoozeService
import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor
import com.simplemobiletools.commons.helpers.isLollipopPlus
import com.simplemobiletools.commons.helpers.isOreoPlus
class AlarmReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val id = intent.getIntExtra(ALARM_ID, -1)
val alarm = context.dbHelper.getAlarmWithId(id) ?: return
val pendingIntent = getOpenAppIntent(context, alarm)
val notification = getNotification(context, pendingIntent, alarm)
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(alarm.id, notification)
context.scheduleNextAlarm(alarm, false)
Handler().postDelayed({
context.hideNotification(id)
}, context.config.alarmMaxReminderSecs * 1000L)
}
@SuppressLint("NewApi")
private fun getNotification(context: Context, pendingIntent: PendingIntent, alarm: Alarm): Notification {
val channelId = "alarm_channel"
if (isOreoPlus()) {
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val name = context.resources.getString(R.string.alarm)
val importance = NotificationManager.IMPORTANCE_HIGH
NotificationChannel(channelId, name, importance).apply {
enableLights(true)
lightColor = context.getAdjustedPrimaryColor()
enableVibration(alarm.vibrate)
notificationManager.createNotificationChannel(this)
}
}
val builder = NotificationCompat.Builder(context)
.setContentTitle(context.getString(R.string.alarm))
.setContentText(context.getFormattedTime(alarm.timeInMinutes * 60, false, false))
.setSmallIcon(R.drawable.ic_alarm)
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_LIGHTS)
.setAutoCancel(true)
.setSound(Uri.parse(alarm.soundUri), AudioManager.STREAM_ALARM)
.setChannelId(channelId)
.addAction(R.drawable.ic_snooze, context.getString(R.string.snooze), getSnoozePendingIntent(context, alarm))
if (isLollipopPlus()) {
builder.setVisibility(Notification.VISIBILITY_PUBLIC)
}
if (alarm.vibrate) {
val vibrateArray = LongArray(2) { 500 }
builder.setVibrate(vibrateArray)
}
val notification = builder.build()
notification.flags = notification.flags or Notification.FLAG_INSISTENT
return notification
}
private fun getSnoozePendingIntent(context: Context, alarm: Alarm): PendingIntent {
val snoozeClass = if (context.config.useSameSnooze) SnoozeService::class.java else SnoozeReminderActivity::class.java
val intent = Intent(context, snoozeClass).setAction("Snooze")
intent.putExtra(ALARM_ID, alarm.id)
return if (context.config.useSameSnooze) {
PendingIntent.getService(context, alarm.id, intent, PendingIntent.FLAG_UPDATE_CURRENT)
if (context.isScreenOn()) {
context.showAlarmNotification(alarm)
Handler().postDelayed({
context.hideNotification(id)
}, context.config.alarmMaxReminderSecs * 1000L)
} else {
PendingIntent.getActivity(context, alarm.id, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
}
private fun getOpenAppIntent(context: Context, alarm: Alarm): PendingIntent {
val intent = Intent(context, SplashActivity::class.java)
intent.putExtra(OPEN_TAB, TAB_ALARM)
return PendingIntent.getActivity(context, alarm.id, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
}
}