fix #88, add Snooze to reminders

This commit is contained in:
tibbi 2017-05-18 23:04:49 +02:00
parent 71bf88bfa3
commit bb475eea5f
11 changed files with 40 additions and 7 deletions

View file

@ -122,6 +122,8 @@
android:name=".services.WidgetService" android:name=".services.WidgetService"
android:permission="android.permission.BIND_REMOTEVIEWS"/> android:permission="android.permission.BIND_REMOTEVIEWS"/>
<service android:name=".services.SnoozeService"/>
<receiver android:name=".receivers.NotificationReceiver"/> <receiver android:name=".receivers.NotificationReceiver"/>
<receiver android:name=".receivers.BootCompletedReceiver"> <receiver android:name=".receivers.BootCompletedReceiver">

View file

@ -50,7 +50,7 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener {
mDialogTheme = getAppropriateTheme() mDialogTheme = getAppropriateTheme()
val eventId = intent.getIntExtra(EVENT_ID, 0) val eventId = intent.getIntExtra(EVENT_ID, 0)
val event = dbHelper.getEvent(eventId) val event = dbHelper.getEventWithId(eventId)
if (eventId != 0 && event == null) { if (eventId != 0 && event == null) {
finish() finish()

View file

@ -12,11 +12,13 @@ import android.content.Intent
import android.graphics.Color import android.graphics.Color
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.support.v7.app.NotificationCompat
import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.activities.EventActivity import com.simplemobiletools.calendar.activities.EventActivity
import com.simplemobiletools.calendar.helpers.* import com.simplemobiletools.calendar.helpers.*
import com.simplemobiletools.calendar.models.Event import com.simplemobiletools.calendar.models.Event
import com.simplemobiletools.calendar.receivers.NotificationReceiver import com.simplemobiletools.calendar.receivers.NotificationReceiver
import com.simplemobiletools.calendar.services.SnoozeService
import com.simplemobiletools.commons.extensions.getContrastColor import com.simplemobiletools.commons.extensions.getContrastColor
import com.simplemobiletools.commons.extensions.isKitkatPlus import com.simplemobiletools.commons.extensions.isKitkatPlus
import com.simplemobiletools.commons.extensions.isLollipopPlus import com.simplemobiletools.commons.extensions.isLollipopPlus
@ -154,16 +156,16 @@ fun Context.notifyEvent(event: Event) {
val pendingIntent = getPendingIntent(this, event) val pendingIntent = getPendingIntent(this, event)
val startTime = Formatter.getTimeFromTS(this, event.startTS) val startTime = Formatter.getTimeFromTS(this, event.startTS)
val endTime = Formatter.getTimeFromTS(this, event.endTS) val endTime = Formatter.getTimeFromTS(this, event.endTS)
val notification = getNotification(this, pendingIntent, event.title, "${getFormattedEventTime(startTime, endTime)} ${event.description}") val notification = getNotification(this, pendingIntent, event, "${getFormattedEventTime(startTime, endTime)} ${event.description}")
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(event.id, notification) notificationManager.notify(event.id, notification)
} }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) @TargetApi(Build.VERSION_CODES.LOLLIPOP)
private fun getNotification(context: Context, pendingIntent: PendingIntent, title: String, content: String): Notification { private fun getNotification(context: Context, pendingIntent: PendingIntent, event: Event, content: String): Notification {
val soundUri = Uri.parse(context.config.reminderSound) val soundUri = Uri.parse(context.config.reminderSound)
val builder = Notification.Builder(context) val builder = NotificationCompat.Builder(context)
.setContentTitle(title) .setContentTitle(event.title)
.setContentText(content) .setContentText(content)
.setSmallIcon(R.drawable.ic_calendar) .setSmallIcon(R.drawable.ic_calendar)
.setContentIntent(pendingIntent) .setContentIntent(pendingIntent)
@ -171,6 +173,7 @@ private fun getNotification(context: Context, pendingIntent: PendingIntent, titl
.setDefaults(Notification.DEFAULT_LIGHTS) .setDefaults(Notification.DEFAULT_LIGHTS)
.setAutoCancel(true) .setAutoCancel(true)
.setSound(soundUri) .setSound(soundUri)
.addAction(R.drawable.ic_snooze, context.getString(R.string.snooze), getSnoozePendingIntent(context, event))
if (context.isLollipopPlus()) if (context.isLollipopPlus())
builder.setVisibility(Notification.VISIBILITY_PUBLIC) builder.setVisibility(Notification.VISIBILITY_PUBLIC)
@ -189,6 +192,12 @@ private fun getPendingIntent(context: Context, event: Event): PendingIntent {
return PendingIntent.getActivity(context, event.id, intent, PendingIntent.FLAG_UPDATE_CURRENT) return PendingIntent.getActivity(context, event.id, intent, PendingIntent.FLAG_UPDATE_CURRENT)
} }
private fun getSnoozePendingIntent(context: Context, event: Event): PendingIntent {
val intent = Intent(context, SnoozeService::class.java).setAction("snooze")
intent.putExtra(EVENT_ID, event.id)
return PendingIntent.getService(context, event.id, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
fun Context.launchNewEventIntent(startNewTask: Boolean = false, today: Boolean = false) { fun Context.launchNewEventIntent(startNewTask: Boolean = false, today: Boolean = false) {
val code = Formatter.getDayCodeFromDateTime(DateTime(DateTimeZone.getDefault()).plusDays(if (today) 0 else 1)) val code = Formatter.getDayCodeFromDateTime(DateTime(DateTimeZone.getDefault()).plusDays(if (today) 0 else 1))
Intent(applicationContext, EventActivity::class.java).apply { Intent(applicationContext, EventActivity::class.java).apply {

View file

@ -346,7 +346,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
return ids.filter { it.trim().isNotEmpty() } as ArrayList<String> return ids.filter { it.trim().isNotEmpty() } as ArrayList<String>
} }
fun getEvent(id: Int): Event? { fun getEventWithId(id: Int): Event? {
val selection = "$MAIN_TABLE_NAME.$COL_ID = ?" val selection = "$MAIN_TABLE_NAME.$COL_ID = ?"
val selectionArgs = arrayOf(id.toString()) val selectionArgs = arrayOf(id.toString())
val cursor = getEventsCursor(selection, selectionArgs) val cursor = getEventsCursor(selection, selectionArgs)

View file

@ -17,7 +17,7 @@ class NotificationReceiver : BroadcastReceiver() {
if (id == -1) if (id == -1)
return return
val event = context.dbHelper.getEvent(id) val event = context.dbHelper.getEventWithId(id)
if (event == null || event.getReminders().isEmpty()) if (event == null || event.getReminders().isEmpty())
return return

View file

@ -0,0 +1,22 @@
package com.simplemobiletools.calendar.services
import android.app.IntentService
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import com.simplemobiletools.calendar.extensions.dbHelper
import com.simplemobiletools.calendar.extensions.scheduleEventIn
import com.simplemobiletools.calendar.helpers.EVENT_ID
class SnoozeService : IntentService("Snooze") {
override fun onHandleIntent(intent: Intent) {
val eventId = intent.getIntExtra(EVENT_ID, 0)
val event = dbHelper.getEventWithId(eventId)
if (eventId != 0 && event != null) {
applicationContext.scheduleEventIn(System.currentTimeMillis() + 600000, event)
val manager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
manager.cancel(eventId)
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 631 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 399 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 824 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB