Skip to content
This repository has been archived by the owner on Apr 8, 2020. It is now read-only.

Commit

Permalink
This should fix #11 and #24
Browse files Browse the repository at this point in the history
  • Loading branch information
quarck committed May 8, 2016
1 parent f65715e commit 2e269ce
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import android.content.Intent
import android.database.Cursor
import android.provider.CalendarContract
import com.github.quarck.calnotify.Consts
import com.github.quarck.calnotify.eventsstorage.EventDisplayStatus
import com.github.quarck.calnotify.eventsstorage.EventRecord
import com.github.quarck.calnotify.logs.Logger
import com.github.quarck.calnotify.permissions.PermissionsManager
Expand Down Expand Up @@ -70,7 +71,7 @@ object CalendarUtils {
endTime = end ?: (start + Consts.HOUR_IN_SECONDS*1000L),
location = location ?: "",
lastEventUpdate = System.currentTimeMillis(),
isDisplayed = false,
displayStatus = EventDisplayStatus.Hidden,
color = color ?: Consts.DEFAULT_COLOR
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,27 @@

package com.github.quarck.calnotify.eventsstorage

enum class EventDisplayStatus(val code: Int) {
Hidden(0),
DisplayedNormal(1),
DisplayedCollapsed(2);

companion object {
fun fromInt(v: Int) = EventDisplayStatus.values()[v]
}
}

data class EventRecord(
val eventId: Long,
val alertTime: Long,
var notificationId: Int,
var title: String,
// var description: String,
var startTime: Long,
var endTime: Long,
var location: String,
var lastEventUpdate: Long,
var snoozedUntil: Long = 0,
var isDisplayed: Boolean = false,
var displayStatus: EventDisplayStatus = EventDisplayStatus.Hidden,
var color: Int = 0
) {
fun updateFrom(newEvent: EventRecord): Boolean {
Expand All @@ -41,11 +50,6 @@ data class EventRecord(
ret = true
}

// if (description != newEvent.description) {
// description = newEvent.description
// ret = true
// }

if (startTime != newEvent.startTime) {
startTime = newEvent.startTime
ret = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class EventsStorage(context: Context)
startTime: Long, endTime: Long,
location: String,
lastEventUpdate: Long,
isDisplayed: Boolean,
displayStatus: EventDisplayStatus,
color: Int
): EventRecord {
var ret =
Expand All @@ -54,7 +54,7 @@ public class EventsStorage(context: Context)
endTime = endTime,
location = location,
lastEventUpdate = lastEventUpdate,
isDisplayed = isDisplayed,
displayStatus = displayStatus,
color = color
)

Expand All @@ -76,7 +76,7 @@ public class EventsStorage(context: Context)
endTime: Long? = null,
location: String? = null,
lastEventUpdate: Long? = null,
isDisplayed: Boolean? = null,
displayStatus: EventDisplayStatus? = null,
color: Int? = null
) {
var newEvent =
Expand All @@ -88,7 +88,7 @@ public class EventsStorage(context: Context)
endTime = endTime ?: event.endTime,
location = location ?: event.location,
lastEventUpdate = lastEventUpdate ?: event.lastEventUpdate,
isDisplayed = isDisplayed ?: event.isDisplayed,
displayStatus = displayStatus ?: event.displayStatus,
color = color ?: event.color
);

Expand Down Expand Up @@ -308,7 +308,7 @@ public class EventsStorage(context: Context)
values.put(KEY_LOCATION, event.location);
values.put(KEY_SNOOZED_UNTIL, event.snoozedUntil);
values.put(KEY_LAST_EVENT_FIRE, event.lastEventUpdate);
values.put(KEY_IS_DISPLAYED, if (event.isDisplayed) 1 else 0);
values.put(KEY_IS_DISPLAYED, event.displayStatus.code);
values.put(KEY_COLOR, event.color)
values.put(KEY_ALERT_TIME, event.alertTime)

Expand All @@ -327,7 +327,7 @@ public class EventsStorage(context: Context)
location = cursor.getString(6),
snoozedUntil = cursor.getLong(7),
lastEventUpdate = cursor.getLong(8),
isDisplayed = (cursor.getInt(9) != 0),
displayStatus = EventDisplayStatus.fromInt(cursor.getInt(9)),
color = cursor.getInt(10),
alertTime = cursor.getLong(11)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.github.quarck.calnotify.Consts
import com.github.quarck.calnotify.NotificationSettingsSnapshot
import com.github.quarck.calnotify.Settings
import com.github.quarck.calnotify.calendar.CalendarUtils
import com.github.quarck.calnotify.eventsstorage.EventDisplayStatus
import com.github.quarck.calnotify.eventsstorage.EventRecord
import com.github.quarck.calnotify.eventsstorage.EventsStorage
import com.github.quarck.calnotify.eventsstorage.formatText
Expand Down Expand Up @@ -101,19 +102,19 @@ class EventNotificationManager : IEventNotificationManager {
hideCollapsedNotifications(context, db, older, force);
postRegularEvents(context, db, settings, recent, force);

postNumNotificationsCollapsed(context, older.size);
postNumNotificationsCollapsed(context, older);
}
}

private fun hideCollapsedNotifications(context: Context, db: EventsStorage, events: List<EventRecord>, force: Boolean) {
logger.debug("Hiding notifications for ${events.size} notification")

for (event in events) {
if (event.isDisplayed || force) {
if ((event.displayStatus != EventDisplayStatus.Hidden) || force) {
logger.debug("Hiding notification id ${event.notificationId}, eventId ${event.eventId}")
removeNotification(context, event.eventId, event.notificationId);

event.isDisplayed = false;
event.displayStatus = EventDisplayStatus.DisplayedCollapsed;
db.updateEvent(event);
} else {
logger.debug("Skipping hiding of notification id ${event.notificationId}, eventId ${event.eventId} - already hidden");
Expand All @@ -140,19 +141,21 @@ class EventNotificationManager : IEventNotificationManager {
for (event in events) {
if (event.snoozedUntil == 0L) {
// This should be currently displayed, if snoozedUntil is zero
if (!event.isDisplayed || force) {
if ((event.displayStatus != EventDisplayStatus.DisplayedNormal) || force) {
// currently not displayed or forced -- post notifications
logger.debug("Posting notification id ${event.notificationId}, eventId ${event.eventId}");

// Unless forced - play sound
postNotification(
context,
event,
if (!force) notificationsSettings else notificationsSettingsQuiet
)
var settings = notificationsSettings

// If forced or if we are posting notification that was previously posted as collapsed
// - don't play sound
if (force || (event.displayStatus == EventDisplayStatus.DisplayedCollapsed))
settings = notificationsSettingsQuiet

postNotification( context, event, settings )

// Update db to indicate that this event is currently actively displayed
db.updateEvent(event, isDisplayed = true);
db.updateEvent(event, displayStatus = EventDisplayStatus.DisplayedNormal);

wasQuiet = wasQuiet && force

Expand All @@ -168,7 +171,7 @@ class EventNotificationManager : IEventNotificationManager {

// Update Db to indicate that event is currently displayed and no longer snoozed
// Since it is displayed now -- it is no longer snoozed, set snoozedUntil to zero also
db.updateEvent(event, isDisplayed = true, snoozedUntil = 0);
db.updateEvent(event, displayStatus = EventDisplayStatus.DisplayedNormal, snoozedUntil = 0);

wasQuiet = false
}
Expand Down Expand Up @@ -320,13 +323,13 @@ class EventNotificationManager : IEventNotificationManager {
notificationManager.cancel(notificationId);
}

private fun postNumNotificationsCollapsed(context: Context, numCollapsed: Int) {
private fun postNumNotificationsCollapsed(context: Context, events: List<EventRecord>) {
logger.debug("Posting 'collapsed view' notification");

var intent = Intent(context, ActivityMain::class.java);
val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0)

var title = java.lang.String.format(context.getString(com.github.quarck.calnotify.R.string.multiple_events), numCollapsed);
var title = java.lang.String.format(context.getString(com.github.quarck.calnotify.R.string.multiple_events), events.size);

val notification =
Notification.Builder(context)
Expand All @@ -337,8 +340,6 @@ class EventNotificationManager : IEventNotificationManager {
.setContentIntent(pendingIntent)
.setAutoCancel(false)
.setOngoing(true)
.setDefaults(Notification.DEFAULT_SOUND)
.setVibrate(longArrayOf(Consts.VIBRATION_DURATION))
.setLights(Consts.LED_COLOR, Consts.LED_DURATION_ON, Consts.LED_DURATION_OFF)
.build()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import android.widget.ToggleButton
import com.github.quarck.calnotify.R
import com.github.quarck.calnotify.Settings
import com.github.quarck.calnotify.calendar.CalendarUtils
import com.github.quarck.calnotify.eventsstorage.EventDisplayStatus
import com.github.quarck.calnotify.eventsstorage.EventsStorage
import com.github.quarck.calnotify.logs.DebugTransactionLog
import com.github.quarck.calnotify.notification.EventNotificationManager
Expand Down Expand Up @@ -134,7 +135,7 @@ class ActivityTestButtonsAndToDo : Activity() {
currentTime + 2 * 3600L * 1000L,
if ((cnt % 2) == 0) "" else "Connolly st., Dublin, IFSC",
System.currentTimeMillis(),
false,
EventDisplayStatus.Hidden,
0xffFFC107.toInt()
)

Expand Down

0 comments on commit 2e269ce

Please sign in to comment.