fix #88, add Snooze to reminders
This commit is contained in:
parent
71bf88bfa3
commit
bb475eea5f
11 changed files with 40 additions and 7 deletions
|
@ -122,6 +122,8 @@
|
|||
android:name=".services.WidgetService"
|
||||
android:permission="android.permission.BIND_REMOTEVIEWS"/>
|
||||
|
||||
<service android:name=".services.SnoozeService"/>
|
||||
|
||||
<receiver android:name=".receivers.NotificationReceiver"/>
|
||||
|
||||
<receiver android:name=".receivers.BootCompletedReceiver">
|
||||
|
|
|
@ -50,7 +50,7 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener {
|
|||
mDialogTheme = getAppropriateTheme()
|
||||
|
||||
val eventId = intent.getIntExtra(EVENT_ID, 0)
|
||||
val event = dbHelper.getEvent(eventId)
|
||||
val event = dbHelper.getEventWithId(eventId)
|
||||
|
||||
if (eventId != 0 && event == null) {
|
||||
finish()
|
||||
|
|
|
@ -12,11 +12,13 @@ import android.content.Intent
|
|||
import android.graphics.Color
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.support.v7.app.NotificationCompat
|
||||
import com.simplemobiletools.calendar.R
|
||||
import com.simplemobiletools.calendar.activities.EventActivity
|
||||
import com.simplemobiletools.calendar.helpers.*
|
||||
import com.simplemobiletools.calendar.models.Event
|
||||
import com.simplemobiletools.calendar.receivers.NotificationReceiver
|
||||
import com.simplemobiletools.calendar.services.SnoozeService
|
||||
import com.simplemobiletools.commons.extensions.getContrastColor
|
||||
import com.simplemobiletools.commons.extensions.isKitkatPlus
|
||||
import com.simplemobiletools.commons.extensions.isLollipopPlus
|
||||
|
@ -154,16 +156,16 @@ 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 notification = getNotification(this, pendingIntent, event, "${getFormattedEventTime(startTime, endTime)} ${event.description}")
|
||||
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
notificationManager.notify(event.id, notification)
|
||||
}
|
||||
|
||||
@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 builder = Notification.Builder(context)
|
||||
.setContentTitle(title)
|
||||
val builder = NotificationCompat.Builder(context)
|
||||
.setContentTitle(event.title)
|
||||
.setContentText(content)
|
||||
.setSmallIcon(R.drawable.ic_calendar)
|
||||
.setContentIntent(pendingIntent)
|
||||
|
@ -171,6 +173,7 @@ private fun getNotification(context: Context, pendingIntent: PendingIntent, titl
|
|||
.setDefaults(Notification.DEFAULT_LIGHTS)
|
||||
.setAutoCancel(true)
|
||||
.setSound(soundUri)
|
||||
.addAction(R.drawable.ic_snooze, context.getString(R.string.snooze), getSnoozePendingIntent(context, event))
|
||||
|
||||
if (context.isLollipopPlus())
|
||||
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)
|
||||
}
|
||||
|
||||
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) {
|
||||
val code = Formatter.getDayCodeFromDateTime(DateTime(DateTimeZone.getDefault()).plusDays(if (today) 0 else 1))
|
||||
Intent(applicationContext, EventActivity::class.java).apply {
|
||||
|
|
|
@ -346,7 +346,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
|||
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 selectionArgs = arrayOf(id.toString())
|
||||
val cursor = getEventsCursor(selection, selectionArgs)
|
||||
|
|
|
@ -17,7 +17,7 @@ class NotificationReceiver : BroadcastReceiver() {
|
|||
if (id == -1)
|
||||
return
|
||||
|
||||
val event = context.dbHelper.getEvent(id)
|
||||
val event = context.dbHelper.getEventWithId(id)
|
||||
if (event == null || event.getReminders().isEmpty())
|
||||
return
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
BIN
app/src/main/res/drawable-hdpi/ic_snooze.png
Normal file
BIN
app/src/main/res/drawable-hdpi/ic_snooze.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 631 B |
BIN
app/src/main/res/drawable-mdpi/ic_snooze.png
Normal file
BIN
app/src/main/res/drawable-mdpi/ic_snooze.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 399 B |
BIN
app/src/main/res/drawable-xhdpi/ic_snooze.png
Normal file
BIN
app/src/main/res/drawable-xhdpi/ic_snooze.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 824 B |
BIN
app/src/main/res/drawable-xxhdpi/ic_snooze.png
Normal file
BIN
app/src/main/res/drawable-xxhdpi/ic_snooze.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.2 KiB |
BIN
app/src/main/res/drawable-xxxhdpi/ic_snooze.png
Normal file
BIN
app/src/main/res/drawable-xxxhdpi/ic_snooze.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.6 KiB |
Loading…
Reference in a new issue