move the notification showing related things into context extensions

This commit is contained in:
tibbi 2017-04-02 20:15:51 +02:00
parent abf7bb07bf
commit 4b82b5e5f3
2 changed files with 39 additions and 40 deletions

View file

@ -1,12 +1,15 @@
package com.simplemobiletools.calendar.extensions
import android.app.AlarmManager
import android.app.Notification
import android.app.NotificationManager
import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.net.Uri
import android.os.Build
import android.os.SystemClock
import com.simplemobiletools.calendar.R
@ -149,6 +152,40 @@ fun Context.getFilteredEvents(events: List<Event>): List<Event> {
return events.filter { displayEventTypes.contains(it.eventType.toString()) }
}
fun Context.notifyEvent(event: Event) {
val pendingIntent = getPendingIntent(this, event)
val startTime = Formatter.getTimeFromTS(this, event.startTS)
val endTime = Formatter.getTimeFromTS(this, event.endTS)
val notification = getNotification(this, pendingIntent, event.title, "${getFormattedEventTime(startTime, endTime)} ${event.description}")
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(event.id, notification)
}
private fun getNotification(context: Context, pendingIntent: PendingIntent, title: String, content: String): Notification {
val soundUri = Uri.parse(context.config.reminderSound)
val builder = Notification.Builder(context)
.setContentTitle(title)
.setContentText(content)
.setSmallIcon(R.drawable.ic_calendar)
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_LIGHTS)
.setAutoCancel(true)
.setSound(soundUri)
if (context.config.vibrateOnReminder)
builder.setVibrate(longArrayOf(0, 300, 300, 300))
return builder.build()
}
private fun getFormattedEventTime(startTime: String, endTime: String) = if (startTime == endTime) startTime else "$startTime - $endTime"
private fun getPendingIntent(context: Context, event: Event): PendingIntent {
val intent = Intent(context, EventActivity::class.java)
intent.putExtra(EVENT_ID, event.id)
return PendingIntent.getActivity(context, event.id, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
fun Context.launchNewEventIntent(startNewTask: Boolean = false, today: Boolean = false) {
val code = Formatter.getDayCodeFromDateTime(DateTime(DateTimeZone.getDefault()).plusDays(if (today) 0 else 1))
Intent(applicationContext, EventActivity::class.java).apply {

View file

@ -1,26 +1,17 @@
package com.simplemobiletools.calendar.receivers
import android.app.Notification
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.net.Uri
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.activities.EventActivity
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.dbHelper
import com.simplemobiletools.calendar.extensions.notifyEvent
import com.simplemobiletools.calendar.extensions.scheduleNextEventReminder
import com.simplemobiletools.calendar.extensions.updateListWidget
import com.simplemobiletools.calendar.helpers.EVENT_ID
import com.simplemobiletools.calendar.helpers.Formatter
import com.simplemobiletools.calendar.models.Event
class NotificationReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
context.updateListWidget()
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val id = intent.getIntExtra(EVENT_ID, -1)
if (id == -1)
return
@ -29,38 +20,9 @@ class NotificationReceiver : BroadcastReceiver() {
if (event == null || event.getReminders().isEmpty())
return
val pendingIntent = getPendingIntent(context, event)
val startTime = Formatter.getTimeFromTS(context, event.startTS)
val endTime = Formatter.getTimeFromTS(context, event.endTS)
if (!event.ignoreEventOccurrences.contains(event.startTS)) {
val notification = getNotification(context, pendingIntent, event.title, "${getEventTime(startTime, endTime)} ${event.description}")
notificationManager.notify(id, notification)
context.notifyEvent(event)
}
context.scheduleNextEventReminder(event)
}
private fun getEventTime(startTime: String, endTime: String) = if (startTime == endTime) startTime else "$startTime - $endTime"
private fun getPendingIntent(context: Context, event: Event): PendingIntent {
val intent = Intent(context, EventActivity::class.java)
intent.putExtra(EVENT_ID, event.id)
return PendingIntent.getActivity(context, event.id, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
private fun getNotification(context: Context, pendingIntent: PendingIntent, title: String, content: String): Notification {
val soundUri = Uri.parse(context.config.reminderSound)
val builder = Notification.Builder(context)
.setContentTitle(title)
.setContentText(content)
.setSmallIcon(R.drawable.ic_calendar)
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_LIGHTS)
.setAutoCancel(true)
.setSound(soundUri)
if (context.config.vibrateOnReminder)
builder.setVibrate(longArrayOf(0, 300, 300, 300))
return builder.build()
}
}