2018-07-18 02:25:06 +00:00
|
|
|
package com.adlerosn.brasilfurfest.notification
|
|
|
|
|
|
|
|
import android.app.*
|
|
|
|
import android.content.Intent
|
|
|
|
import android.os.Build
|
|
|
|
import android.support.v4.app.NotificationCompat
|
|
|
|
import android.support.v4.app.NotificationManagerCompat
|
|
|
|
import android.util.Log
|
|
|
|
import com.adlerosn.brasilfurfest.R
|
2018-12-13 02:04:29 +00:00
|
|
|
import com.adlerosn.brasilfurfest.schedule.managers.ScheduleManagerGetter
|
2018-07-18 02:25:06 +00:00
|
|
|
import com.adlerosn.brasilfurfest.schedule.EventActivity
|
|
|
|
import java.io.PrintWriter
|
|
|
|
import java.io.StringWriter
|
|
|
|
import java.util.*
|
|
|
|
|
|
|
|
|
|
|
|
class NotificationIntentService : IntentService(NotificationIntentService::class.java.simpleName) {
|
|
|
|
|
2018-12-12 04:30:34 +00:00
|
|
|
@Suppress("PrivatePropertyName")
|
2018-07-18 02:25:06 +00:00
|
|
|
private val SIMPLE_ID = NotificationIntentService::class.java.simpleName
|
|
|
|
|
|
|
|
override fun onHandleIntent(intent: Intent) {
|
|
|
|
try {
|
2018-12-12 04:30:34 +00:00
|
|
|
val scheduleManager = ScheduleManagerGetter[this]
|
2018-07-18 02:25:06 +00:00
|
|
|
NextNotificationScheduler(this, scheduleManager)
|
|
|
|
val timeForNotification = scheduleManager.convention.notificationFireMinutesBefore*60*1000.toLong() + scheduleManager.conventionTime.timeInMillis
|
|
|
|
val dateForNotification = GregorianCalendar().apply { timeInMillis=timeForNotification }
|
|
|
|
val toNotify =
|
|
|
|
scheduleManager.attendeeFavorites
|
|
|
|
.toList()
|
|
|
|
.filter { dateForNotification in it.timeRange }
|
|
|
|
.filterNot { it.notified }
|
|
|
|
.sortedBy { -it.timeRange.start.timeInMillis }
|
|
|
|
val notificationManager = NotificationManagerCompat.from(this)
|
|
|
|
toNotify.withIndex().forEach {
|
|
|
|
val builder = getChannelBuilder()
|
|
|
|
.setDefaults(Notification.DEFAULT_ALL)
|
|
|
|
.setSmallIcon(R.drawable.ic_notificacao)
|
2018-12-28 00:28:48 +00:00
|
|
|
.setContentTitle(it.value.getString())
|
|
|
|
.setContentText(it.value.getSecondaryString())
|
2018-07-18 02:25:06 +00:00
|
|
|
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
|
|
|
.setTimeoutAfter(
|
2018-12-12 04:30:34 +00:00
|
|
|
it.value.timeRange.let { cal ->
|
|
|
|
cal.finish.timeInMillis - cal.start.timeInMillis
|
2018-07-18 02:25:06 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
.setWhen(it.value.timeRange.start.timeInMillis)
|
|
|
|
.setShowWhen(true)
|
|
|
|
.setContentIntent(
|
|
|
|
PendingIntent.getActivity(
|
|
|
|
this,
|
2018-12-28 00:28:48 +00:00
|
|
|
it.value.getString().hashCode(),
|
2018-07-18 02:25:06 +00:00
|
|
|
Intent(this, EventActivity::class.java).apply {
|
2018-12-12 04:30:34 +00:00
|
|
|
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_TASK_ON_HOME
|
2018-07-18 02:25:06 +00:00
|
|
|
putExtra("favorite", it.value)
|
2018-07-18 15:01:16 +00:00
|
|
|
putExtra("fromNotification", true)
|
2018-07-18 02:25:06 +00:00
|
|
|
},
|
|
|
|
0
|
|
|
|
)
|
|
|
|
)
|
|
|
|
notificationManager.notify(
|
|
|
|
((timeForNotification+it.index)%Int.MAX_VALUE).toInt(),
|
|
|
|
builder.build()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
toNotify.forEach {
|
|
|
|
it.notified = true
|
|
|
|
}
|
|
|
|
} catch (e: Throwable) {
|
|
|
|
val writer = StringWriter()
|
|
|
|
e.printStackTrace(PrintWriter(writer))
|
|
|
|
val s = writer.toString()
|
2018-07-18 15:01:16 +00:00
|
|
|
Log.e(NotificationIntentService::class.java.simpleName, s)
|
2018-07-18 02:25:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun getChannelBuilder() =
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
createDefaultNotificationChannel()
|
|
|
|
NotificationCompat.Builder(this, SIMPLE_ID)
|
|
|
|
} else {
|
|
|
|
@Suppress("DEPRECATION") //older API will have this
|
|
|
|
NotificationCompat.Builder(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun createDefaultNotificationChannel() {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
val notificationManager = getSystemService(NotificationManager::class.java) as NotificationManager
|
|
|
|
if (notificationManager.getNotificationChannel(SIMPLE_ID) == null) {
|
|
|
|
val channelName = getString(R.string.defaultNotificationChannel)
|
|
|
|
val channel = NotificationChannel(SIMPLE_ID, channelName, NotificationManager.IMPORTANCE_HIGH)
|
|
|
|
notificationManager.createNotificationChannel(channel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|