From bb475eea5ffb05243ed8018ae2d47dff9c37f8cd Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 18 May 2017 23:04:49 +0200 Subject: [PATCH] fix #88, add Snooze to reminders --- app/src/main/AndroidManifest.xml | 2 ++ .../calendar/activities/EventActivity.kt | 2 +- .../calendar/extensions/Context.kt | 17 ++++++++++---- .../calendar/helpers/DBHelper.kt | 2 +- .../receivers/NotificationReceiver.kt | 2 +- .../calendar/services/SnoozeService.kt | 22 ++++++++++++++++++ app/src/main/res/drawable-hdpi/ic_snooze.png | Bin 0 -> 631 bytes app/src/main/res/drawable-mdpi/ic_snooze.png | Bin 0 -> 399 bytes app/src/main/res/drawable-xhdpi/ic_snooze.png | Bin 0 -> 824 bytes .../main/res/drawable-xxhdpi/ic_snooze.png | Bin 0 -> 1215 bytes .../main/res/drawable-xxxhdpi/ic_snooze.png | Bin 0 -> 1663 bytes 11 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/calendar/services/SnoozeService.kt create mode 100644 app/src/main/res/drawable-hdpi/ic_snooze.png create mode 100644 app/src/main/res/drawable-mdpi/ic_snooze.png create mode 100644 app/src/main/res/drawable-xhdpi/ic_snooze.png create mode 100644 app/src/main/res/drawable-xxhdpi/ic_snooze.png create mode 100644 app/src/main/res/drawable-xxxhdpi/ic_snooze.png diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index be36da1c6..0ca68c027 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -122,6 +122,8 @@ android:name=".services.WidgetService" android:permission="android.permission.BIND_REMOTEVIEWS"/> + + diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/EventActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/EventActivity.kt index 1336d29a6..617caca1e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/EventActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/EventActivity.kt @@ -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() diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt index e890c7d6a..793c1ceba 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt @@ -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 { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt index dde13aa9a..b24c67d9c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt @@ -346,7 +346,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont return ids.filter { it.trim().isNotEmpty() } as ArrayList } - 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) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/receivers/NotificationReceiver.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/receivers/NotificationReceiver.kt index 308d05b68..80a009ccb 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/receivers/NotificationReceiver.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/receivers/NotificationReceiver.kt @@ -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 diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/services/SnoozeService.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/services/SnoozeService.kt new file mode 100644 index 000000000..11113d5a0 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/services/SnoozeService.kt @@ -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) + } + } +} diff --git a/app/src/main/res/drawable-hdpi/ic_snooze.png b/app/src/main/res/drawable-hdpi/ic_snooze.png new file mode 100644 index 0000000000000000000000000000000000000000..43e17072148074336b39ac1ec2c819fa71071ab4 GIT binary patch literal 631 zcmV--0*L*IP)0_VNVwDkL|&Y8o*dC%{6&V-8)%|b49X)BV;I&B)u z6_u>(s3z+58_o=6YKFnS?mW?`2poGRM-+h*!|9^S%m8-XEeP&2ZN;9lvOQm#a<3q` z->-lPb(NxKp8{6335IM)%bKSIH~9|mv8MB)&jFukcAX`8S=8!Ei-4u`qFxcE)Vb3( z;J~{c)2^VP-GKKT0)BD3dUJ?8P5OgzfB3Iz z9#jVNiH6LrTCfEkt*MU&!H?+;IR(D1sk6TY6X~Kh5qPocU3&$bxISGJ%zz`^LJmES zK}pCGHQE3N`ZF~TI|Qq)$QA`Ri(g2Ww4_{?cbtJ8H&*+ny~7sZ(EA?Op`byBCw$;< zz)!k_YNDXkqP#4)R!A2mdD5D+tQr*32(r1yq%Z7feEkev3Oy&{Ul@-OjK^r^ovTKU7rvW|r>yAWQ{?!Jj<=CA=A%2C#w@El z)75cm*OF&GxU*#}?%7Aj8x=?L4wb#r#n-s!l^^E3bc+kCDhdi_thq!>P0dfQ2Cr_#%q|MYJR36f@E1J-#W#kXal@$}ZNT&u>G~ zX9L@jvV=>M(IXK(O0KaGk2yp`7CAC|IEXxDEi_C;LdhO(Ovh7dTDY>Vs;FqzhHKoI tkI|Ghgu>Jr5 literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-xhdpi/ic_snooze.png b/app/src/main/res/drawable-xhdpi/ic_snooze.png new file mode 100644 index 0000000000000000000000000000000000000000..e27af11a0c9688fb2a4ef7432bdbe63f13b729a6 GIT binary patch literal 824 zcmV-81IPS{P)szpB_%cw7~!T1yezh?<_3cpQTJNekGsn1PC+4xg6KaeNKGx%g0Lbm ztP5KXv(d&D>UeB#blJxw->f^Ae1 z^$YcD_l~#ykC7W}B zEye-N+S3>)c^3euwFx0~d);Ry&6qcD#)MP$=@LR%?+k#<+i76EF92lT62cnK8c1M`3uQKuHefD@ult$?}J0-pfRTf16d2B`ZIzXOL6HZ9kJN|)zv3?60r)@^0oEA@P}L)(d)PGqqgsmrp~E!*i;n3Qb?SE9B7iBI zL;;{ii~%5X!9o2hTD7VeaL9-Z06*I#6bFQLjwrhKwTd*5Zb(($E*li-LgSh}>nHV> z8L?ZFC=R6BW}j2W-7x2tX=6@$$ySkH_@BE20000RRj4hvnRT4df z%%uw&H0U9SUV@>!Lb%n`t%pKv%4}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