-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fixed backup file time format is bad-understanding] [Fixed auto del history task cannot del > 1 history at a time if it has > 6 history task] [Optimize reminder notification logic] [Fix reminder notification delay in battery save mode via ALARM] [Update dependencies]
- Loading branch information
Showing
14 changed files
with
590 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
app/src/main/java/com/sqz/checklist/notification/BootReceiver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.sqz.checklist.notification | ||
|
||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.util.Log | ||
import androidx.room.Room | ||
import com.sqz.checklist.R | ||
import com.sqz.checklist.database.TaskDatabase | ||
import com.sqz.checklist.database.taskDatabaseName | ||
import kotlinx.coroutines.DelicateCoroutinesApi | ||
import kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.launch | ||
import java.util.concurrent.TimeUnit | ||
|
||
/** | ||
* Restore delayed notification (reminder) when boot or restart app | ||
*/ | ||
class BootReceiver : BroadcastReceiver() { | ||
@OptIn(DelicateCoroutinesApi::class) | ||
override fun onReceive(context: Context, intent: Intent) { | ||
if (intent.action == Intent.ACTION_BOOT_COMPLETED) { | ||
val notificationManager = MutableStateFlow(NotifyManager()) | ||
GlobalScope.launch { | ||
notificationManager.value.requestPermission(context) | ||
if (notificationManager.value.getAlarmPermission()) { | ||
val db = Room.databaseBuilder( | ||
context, TaskDatabase::class.java, taskDatabaseName | ||
).build() | ||
val taskDao = db.taskDao() | ||
val list = taskDao.getIsRemindedList() | ||
val notification = NotificationCreator(context) | ||
for (data in list) { | ||
val parts = data.reminder?.split(":") | ||
val queryCharacter = if (parts?.size!! >= 2) parts[0] else null | ||
if (queryCharacter != null) try { | ||
if (!notification.getAlarmNotificationState(queryCharacter.toInt()) && | ||
parts[1].toLong() >= System.currentTimeMillis() | ||
) { | ||
notificationManager.value.createNotification( | ||
channelId = context.getString(R.string.tasks), | ||
channelName = context.getString(R.string.task_reminder), | ||
channelDescription = context.getString(R.string.description), | ||
description = data.description, notifyId = data.id, | ||
delayDuration = parts[1].toLong(), | ||
timeUnit = TimeUnit.MILLISECONDS, context = context | ||
).also { | ||
Log.d("RestoreReminder", "Restore NotifyId: $queryCharacter") | ||
} | ||
} | ||
} catch (e: NumberFormatException) { | ||
Log.d("RestoreReminder", "Ignored: work manager no need this!") | ||
} catch (e: Exception) { | ||
Log.e("RestoreReminder", "Failed: ${e.message}") | ||
} | ||
} | ||
Log.d("BootReceiver", "Delayed notification is restored!") | ||
db.close() | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
app/src/main/java/com/sqz/checklist/notification/NotificationReceiver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.sqz.checklist.notification | ||
|
||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.Intent | ||
|
||
class NotificationReceiver : BroadcastReceiver() { | ||
override fun onReceive(context: Context, intent: Intent) { | ||
val channelId = intent.getStringExtra("channelId") | ||
val channelName = intent.getStringExtra("channelName") | ||
val channelDescription = intent.getStringExtra("channelDescription") | ||
val title = intent.getStringExtra("title") | ||
val content = intent.getStringExtra("content") | ||
val notifyId = intent.getIntExtra("notifyId", -1) | ||
|
||
if (channelId != null && channelName != null && channelDescription != null && | ||
title != null && content != null && notifyId != -1 | ||
) { | ||
val notification = NotificationCreator(context) | ||
notification.creator( | ||
channelId = channelId, | ||
channelName = channelName, | ||
channelDescription = channelDescription, | ||
title = title, | ||
content = content, | ||
notifyId = notifyId | ||
) | ||
} else throw Exception("Notification data error!") | ||
} | ||
} |
Oops, something went wrong.