mirror of
https://github.com/szkolny-eu/szkolny-android.git
synced 2025-01-19 05:16:56 -06:00
[Sync] Fix sync notification crashing on Oreo+.
This commit is contained in:
parent
1035e411ab
commit
3ba30ede92
@ -29,7 +29,7 @@ import kotlin.math.roundToInt
|
|||||||
class ApiService : Service() {
|
class ApiService : Service() {
|
||||||
companion object {
|
companion object {
|
||||||
const val TAG = "ApiService"
|
const val TAG = "ApiService"
|
||||||
const val NOTIFICATION_API_CHANNEL_ID = "pl.szczodrzynski.edziennik.GET_DATA"
|
const val NOTIFICATION_API_CHANNEL_ID = "pl.szczodrzynski.edziennik.SYNC"
|
||||||
fun start(context: Context) {
|
fun start(context: Context) {
|
||||||
context.startService(Intent(context, ApiService::class.java))
|
context.startService(Intent(context, ApiService::class.java))
|
||||||
}
|
}
|
||||||
@ -60,7 +60,7 @@ class ApiService : Service() {
|
|||||||
private var taskProgress = -1f
|
private var taskProgress = -1f
|
||||||
private var taskProgressText: String? = null
|
private var taskProgressText: String? = null
|
||||||
|
|
||||||
private val notification by lazy { EdziennikNotification(this) }
|
private val notification by lazy { EdziennikNotification(app) }
|
||||||
|
|
||||||
private var lastEventTime = System.currentTimeMillis()
|
private var lastEventTime = System.currentTimeMillis()
|
||||||
private var taskCancelTries = 0
|
private var taskCancelTries = 0
|
||||||
|
@ -11,19 +11,20 @@ import android.content.Context
|
|||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import androidx.core.app.NotificationCompat
|
import androidx.core.app.NotificationCompat
|
||||||
import androidx.core.app.NotificationCompat.PRIORITY_MIN
|
import androidx.core.app.NotificationCompat.PRIORITY_MIN
|
||||||
|
import pl.szczodrzynski.edziennik.App
|
||||||
import pl.szczodrzynski.edziennik.R
|
import pl.szczodrzynski.edziennik.R
|
||||||
import kotlin.math.roundToInt
|
import kotlin.math.roundToInt
|
||||||
|
|
||||||
|
|
||||||
class EdziennikNotification(val context: Context) {
|
class EdziennikNotification(val app: App) {
|
||||||
companion object {
|
companion object {
|
||||||
const val NOTIFICATION_ID = 20191001
|
const val NOTIFICATION_ID = 20191001
|
||||||
}
|
}
|
||||||
|
|
||||||
private val notificationManager by lazy { context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager }
|
private val notificationManager by lazy { app.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager }
|
||||||
|
|
||||||
private val notificationBuilder: NotificationCompat.Builder by lazy {
|
private val notificationBuilder: NotificationCompat.Builder by lazy {
|
||||||
NotificationCompat.Builder(context, ApiService.NOTIFICATION_API_CHANNEL_ID)
|
NotificationCompat.Builder(app, ApiService.NOTIFICATION_API_CHANNEL_ID)
|
||||||
.setSmallIcon(R.drawable.ic_notification)
|
.setSmallIcon(R.drawable.ic_notification)
|
||||||
.setPriority(PRIORITY_MIN)
|
.setPriority(PRIORITY_MIN)
|
||||||
.setOngoing(true)
|
.setOngoing(true)
|
||||||
@ -41,34 +42,34 @@ class EdziennikNotification(val context: Context) {
|
|||||||
val intent = Intent("pl.szczodrzynski.edziennik.SZKOLNY_MAIN")
|
val intent = Intent("pl.szczodrzynski.edziennik.SZKOLNY_MAIN")
|
||||||
intent.putExtra("task", "TaskCancelRequest")
|
intent.putExtra("task", "TaskCancelRequest")
|
||||||
intent.putExtra("taskId", taskId)
|
intent.putExtra("taskId", taskId)
|
||||||
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT) as PendingIntent
|
return PendingIntent.getBroadcast(app, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT) as PendingIntent
|
||||||
}
|
}
|
||||||
private val closePendingIntent: PendingIntent
|
private val closePendingIntent: PendingIntent
|
||||||
get() {
|
get() {
|
||||||
val intent = Intent("pl.szczodrzynski.edziennik.SZKOLNY_MAIN")
|
val intent = Intent("pl.szczodrzynski.edziennik.SZKOLNY_MAIN")
|
||||||
intent.putExtra("task", "ServiceCloseRequest")
|
intent.putExtra("task", "ServiceCloseRequest")
|
||||||
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT) as PendingIntent
|
return PendingIntent.getBroadcast(app, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT) as PendingIntent
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun errorCountText(): String? {
|
private fun errorCountText(): String? {
|
||||||
var result = ""
|
var result = ""
|
||||||
if (criticalErrorCount > 0) {
|
if (criticalErrorCount > 0) {
|
||||||
result += context.resources.getQuantityString(R.plurals.critical_errors_format, criticalErrorCount, criticalErrorCount)
|
result += app.resources.getQuantityString(R.plurals.critical_errors_format, criticalErrorCount, criticalErrorCount)
|
||||||
}
|
}
|
||||||
if (criticalErrorCount > 0 && errorCount > 0) {
|
if (criticalErrorCount > 0 && errorCount > 0) {
|
||||||
result += ", "
|
result += ", "
|
||||||
}
|
}
|
||||||
if (errorCount > 0) {
|
if (errorCount > 0) {
|
||||||
result += context.resources.getQuantityString(R.plurals.normal_errors_format, errorCount, errorCount)
|
result += app.resources.getQuantityString(R.plurals.normal_errors_format, errorCount, errorCount)
|
||||||
}
|
}
|
||||||
return if (result.isEmpty()) null else result
|
return if (result.isEmpty()) null else result
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setIdle(): EdziennikNotification {
|
fun setIdle(): EdziennikNotification {
|
||||||
notificationBuilder.setContentTitle(context.getString(R.string.edziennik_notification_api_title))
|
notificationBuilder.setContentTitle(app.getString(R.string.edziennik_notification_api_title))
|
||||||
notificationBuilder.setProgress(0, 0, false)
|
notificationBuilder.setProgress(0, 0, false)
|
||||||
notificationBuilder.apply {
|
notificationBuilder.apply {
|
||||||
val str = context.getString(R.string.edziennik_notification_api_text)
|
val str = app.getString(R.string.edziennik_notification_api_text)
|
||||||
setStyle(NotificationCompat.BigTextStyle().bigText(str))
|
setStyle(NotificationCompat.BigTextStyle().bigText(str))
|
||||||
setContentText(str)
|
setContentText(str)
|
||||||
}
|
}
|
||||||
@ -82,7 +83,7 @@ class EdziennikNotification(val context: Context) {
|
|||||||
}
|
}
|
||||||
fun setCriticalError(): EdziennikNotification {
|
fun setCriticalError(): EdziennikNotification {
|
||||||
criticalErrorCount++
|
criticalErrorCount++
|
||||||
notificationBuilder.setContentTitle(context.getString(R.string.edziennik_notification_api_error_title))
|
notificationBuilder.setContentTitle(app.getString(R.string.edziennik_notification_api_error_title))
|
||||||
notificationBuilder.setProgress(0, 0, false)
|
notificationBuilder.setProgress(0, 0, false)
|
||||||
notificationBuilder.apply {
|
notificationBuilder.apply {
|
||||||
val str = errorCountText()
|
val str = errorCountText()
|
||||||
@ -119,7 +120,7 @@ class EdziennikNotification(val context: Context) {
|
|||||||
notificationBuilder.addAction(
|
notificationBuilder.addAction(
|
||||||
NotificationCompat.Action(
|
NotificationCompat.Action(
|
||||||
R.drawable.ic_notification,
|
R.drawable.ic_notification,
|
||||||
context.getString(R.string.edziennik_notification_api_close),
|
app.getString(R.string.edziennik_notification_api_close),
|
||||||
closePendingIntent
|
closePendingIntent
|
||||||
))
|
))
|
||||||
return this
|
return this
|
||||||
@ -129,7 +130,7 @@ class EdziennikNotification(val context: Context) {
|
|||||||
notificationBuilder.addAction(
|
notificationBuilder.addAction(
|
||||||
NotificationCompat.Action(
|
NotificationCompat.Action(
|
||||||
R.drawable.ic_notification,
|
R.drawable.ic_notification,
|
||||||
context.getString(R.string.edziennik_notification_api_cancel),
|
app.getString(R.string.edziennik_notification_api_cancel),
|
||||||
cancelPendingIntent(taskId)
|
cancelPendingIntent(taskId)
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
@ -137,7 +138,7 @@ class EdziennikNotification(val context: Context) {
|
|||||||
fun post() {
|
fun post() {
|
||||||
if (serviceClosed)
|
if (serviceClosed)
|
||||||
return
|
return
|
||||||
notificationManager.notify(NOTIFICATION_ID, notification)
|
notificationManager.notify(app.notifications.syncId, notification)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user