Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proper Notification Channels #557

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import com.swordfish.lemuroid.lib.library.db.entity.Game
class NotificationsManager(private val applicationContext: Context) {

fun gameRunningNotification(game: Game?): Notification {
createDefaultNotificationChannel()
createNotificationChannels()

val intent = Intent(applicationContext, GameActivity::class.java)
val contentIntent = PendingIntent.getActivity(
Expand All @@ -31,7 +31,7 @@ class NotificationsManager(private val applicationContext: Context) {
applicationContext.getString(R.string.game_running_notification_title, game.title)
} ?: applicationContext.getString(R.string.game_running_notification_title_alternative)

val builder = NotificationCompat.Builder(applicationContext, DEFAULT_CHANNEL_ID)
val builder = NotificationCompat.Builder(applicationContext, GAME_RUNNING_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_lemuroid_tiny)
.setContentTitle(title)
.setContentText(applicationContext.getString(R.string.game_running_notification_message))
Expand All @@ -45,7 +45,7 @@ class NotificationsManager(private val applicationContext: Context) {
}

fun libraryIndexingNotification(): Notification {
createDefaultNotificationChannel()
createNotificationChannels()

val broadcastIntent = Intent(applicationContext, LibraryIndexBroadcastReceiver::class.java)
val broadcastPendingIntent: PendingIntent = PendingIntent.getBroadcast(
Expand All @@ -55,7 +55,7 @@ class NotificationsManager(private val applicationContext: Context) {
PendingIntent.FLAG_IMMUTABLE
)

val builder = NotificationCompat.Builder(applicationContext, DEFAULT_CHANNEL_ID)
val builder = NotificationCompat.Builder(applicationContext, LIBRARY_INDEXING_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_lemuroid_tiny)
.setContentTitle(applicationContext.getString(R.string.library_index_notification_title))
.setContentText(applicationContext.getString(R.string.library_index_notification_message))
Expand All @@ -73,9 +73,9 @@ class NotificationsManager(private val applicationContext: Context) {
}

fun installingCoresNotification(): Notification {
createDefaultNotificationChannel()
createNotificationChannels()

val builder = NotificationCompat.Builder(applicationContext, DEFAULT_CHANNEL_ID)
val builder = NotificationCompat.Builder(applicationContext, CORE_UPDATE_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_lemuroid_tiny)
.setContentTitle(applicationContext.getString(R.string.installing_core_notification_title))
.setContentText(applicationContext.getString(R.string.installing_core_notification_message))
Expand All @@ -86,9 +86,9 @@ class NotificationsManager(private val applicationContext: Context) {
}

fun saveSyncNotification(): Notification {
createDefaultNotificationChannel()
createSyncNotificationChannels()

val builder = NotificationCompat.Builder(applicationContext, DEFAULT_CHANNEL_ID)
val builder = NotificationCompat.Builder(applicationContext, SYNC_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_lemuroid_tiny)
.setContentTitle(applicationContext.getString(R.string.save_sync_notification_title))
.setContentText(applicationContext.getString(R.string.save_sync_notification_message))
Expand All @@ -98,21 +98,62 @@ class NotificationsManager(private val applicationContext: Context) {
return builder.build()
}

private fun createDefaultNotificationChannel() {
private fun createNotificationChannels() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = applicationContext.getString(R.string.notification_channel_name)
val importance = NotificationManager.IMPORTANCE_MIN
val mChannel = NotificationChannel(DEFAULT_CHANNEL_ID, name, importance)

val notificationManager =
ContextCompat.getSystemService(applicationContext, NotificationManager::class.java)

var name = applicationContext.getString(R.string.notification_channel_name_game_running)
var importance = NotificationManager.IMPORTANCE_MIN
var mChannel = NotificationChannel(GAME_RUNNING_CHANNEL_ID, name, importance)
mChannel.description = applicationContext.getString(R.string.notification_channel_description_game_running)

notificationManager?.createNotificationChannel(mChannel)

name = applicationContext.getString(R.string.notification_channel_name_indexing)
importance = NotificationManager.IMPORTANCE_MIN
mChannel = NotificationChannel(LIBRARY_INDEXING_CHANNEL_ID, name, importance)
mChannel.description = applicationContext.getString(R.string.notification_channel_description_indexing)

notificationManager?.createNotificationChannel(mChannel)


name = applicationContext.getString(R.string.notification_channel_name_core_update)
importance = NotificationManager.IMPORTANCE_MIN
mChannel = NotificationChannel(CORE_UPDATE_CHANNEL_ID, name, importance)
mChannel.description = applicationContext.getString(R.string.notification_channel_description_core_update)

notificationManager?.createNotificationChannel(mChannel)

notificationManager?.deleteNotificationChannel(DEFAULT_CHANNEL_ID)
}
}

private fun createSyncNotificationChannels() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager =
ContextCompat.getSystemService(applicationContext, NotificationManager::class.java)

var name = applicationContext.getString(R.string.notification_channel_name_sync)
var importance = NotificationManager.IMPORTANCE_MIN
var mChannel = NotificationChannel(SYNC_CHANNEL_ID, name, importance)
mChannel.description = applicationContext.getString(R.string.notification_channel_description_sync)

notificationManager?.createNotificationChannel(mChannel)
}
}

companion object {
const val GAME_RUNNING_CHANNEL_ID = "GAME_RUNNING_CHANNEL_ID"
const val LIBRARY_INDEXING_CHANNEL_ID = "LIBRARY_INDEXING_CHANNEL_ID"
const val SYNC_CHANNEL_ID = "SYNC_CHANNEL_ID"
const val CORE_UPDATE_CHANNEL_ID = "CORE_UPDATE_CHANNEL_ID"

@Deprecated(message="Dont use! Use the proper channel. This ID is kept to remove the old channel.")
const val DEFAULT_CHANNEL_ID = "DEFAULT_CHANNEL_ID"


const val LIBRARY_INDEXING_NOTIFICATION_ID = 1
const val SAVE_SYNC_NOTIFICATION_ID = 2
const val GAME_RUNNING_NOTIFICATION_ID = 3
Expand Down
9 changes: 8 additions & 1 deletion lemuroid-app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,14 @@

<string name="game_menu_title">Game menu</string>

<string name="notification_channel_name">All notifications</string>
<string name="notification_channel_name_game_running">Game Service</string>
<string name="notification_channel_description_game_running">Persistent Notification for background service.</string>
<string name="notification_channel_name_indexing">Library</string>
<string name="notification_channel_description_indexing">Notification for ROM-Indexing Service</string>
<string name="notification_channel_name_sync">Google Drive</string>
<string name="notification_channel_description_sync">Notification for background sync</string>
<string name="notification_channel_name_core_update">Cores</string>
<string name="notification_channel_description_core_update">Notification for Core updater</string>
<string name="library_index_notification_title">ROMs scanning in progress</string>
<string name="library_index_notification_message">This can take up to a few minutes</string>

Expand Down