forked from upstream-mirrors/quik
Add notification channel support on a per conversation basis
This commit is contained in:
@@ -49,21 +49,18 @@ class NotificationManager @Inject constructor(
|
||||
) {
|
||||
|
||||
companion object {
|
||||
val DEFAULT_CHANNEL_ID = "channel_1"
|
||||
const val DEFAULT_CHANNEL_ID = "notifications_default"
|
||||
val VIBRATE_PATTERN = longArrayOf(0, 200, 0, 200)
|
||||
}
|
||||
|
||||
private val notificationManager = NotificationManagerCompat.from(context)
|
||||
private val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
|
||||
init {
|
||||
@SuppressLint("NewApi")
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
|
||||
val name = "Message notifications"
|
||||
val name = "Default"
|
||||
val importance = NotificationManager.IMPORTANCE_HIGH
|
||||
val channel = NotificationChannel(DEFAULT_CHANNEL_ID, name, importance).apply {
|
||||
description = "Message notifications description"
|
||||
enableLights(true)
|
||||
lightColor = Color.WHITE
|
||||
enableVibration(true)
|
||||
@@ -112,7 +109,7 @@ class NotificationManager @Inject constructor(
|
||||
val readPI = PendingIntent.getBroadcast(context, threadId.toInt() + 30000, readIntent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
val readAction = NotificationCompat.Action(R.drawable.ic_done_black_24dp, context.getString(R.string.notification_read), readPI)
|
||||
|
||||
val notification = NotificationCompat.Builder(context, DEFAULT_CHANNEL_ID)
|
||||
val notification = NotificationCompat.Builder(context, getChannelIdForNotification(threadId))
|
||||
.setColor(colors.theme.blockingFirst())
|
||||
.setPriority(NotificationManagerCompat.IMPORTANCE_MAX)
|
||||
.setSmallIcon(R.mipmap.ic_launcher)
|
||||
@@ -149,7 +146,7 @@ class NotificationManager @Inject constructor(
|
||||
taskStackBuilder.addNextIntent(contentIntent)
|
||||
val contentPI = taskStackBuilder.getPendingIntent(threadId.toInt() + 40000, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
|
||||
val notification = NotificationCompat.Builder(context, DEFAULT_CHANNEL_ID)
|
||||
val notification = NotificationCompat.Builder(context, getChannelIdForNotification(threadId))
|
||||
.setContentTitle(context.getString(R.string.notification_message_failed_title))
|
||||
.setContentText(context.getString(R.string.notification_message_failed_text, conversation.getTitle()))
|
||||
.setColor(colors.theme.blockingFirst())
|
||||
@@ -183,4 +180,72 @@ class NotificationManager @Inject constructor(
|
||||
.build()
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a notification channel for the given conversation
|
||||
*/
|
||||
fun createNotificationChannel(threadId: Long) {
|
||||
|
||||
// Only proceed if the android version supports notification channels
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return
|
||||
|
||||
messageRepo.getConversation(threadId)?.let { conversation ->
|
||||
val channelId = buildNotificationChannelId(threadId)
|
||||
val name = conversation.getTitle()
|
||||
val importance = NotificationManager.IMPORTANCE_HIGH
|
||||
val channel = NotificationChannel(channelId, name, importance).apply {
|
||||
enableLights(true)
|
||||
lightColor = Color.WHITE
|
||||
enableVibration(true)
|
||||
vibrationPattern = VIBRATE_PATTERN
|
||||
}
|
||||
|
||||
notificationManager.createNotificationChannel(channel)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the notification channel for the given conversation, or null if it doesn't exist
|
||||
*/
|
||||
private fun getNotificationChannel(threadId: Long): NotificationChannel? {
|
||||
val channelId = buildNotificationChannelId(threadId)
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
return notificationManager
|
||||
.notificationChannels
|
||||
.firstOrNull { channel -> channel.id == channelId }
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the channel id that should be used for a notification based on the threadId
|
||||
*
|
||||
* If a notification channel for the conversation exists, use the id for that. Otherwise return
|
||||
* the default channel id
|
||||
*/
|
||||
private fun getChannelIdForNotification(threadId: Long): String {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
val channelId = buildNotificationChannelId(threadId)
|
||||
|
||||
return notificationManager
|
||||
.notificationChannels
|
||||
.map { channel -> channel.id }
|
||||
.firstOrNull { id -> id == channelId }
|
||||
?: DEFAULT_CHANNEL_ID
|
||||
}
|
||||
|
||||
return DEFAULT_CHANNEL_ID
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a notification channel id for a given thread id, whether the channel exists or not
|
||||
*/
|
||||
fun buildNotificationChannelId(threadId: Long): String {
|
||||
return when (threadId) {
|
||||
0L -> DEFAULT_CHANNEL_ID
|
||||
else -> "notifications_$threadId"
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -48,7 +48,7 @@ import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class Navigator @Inject constructor(val context: Context) {
|
||||
class Navigator @Inject constructor(private val context: Context, private val notificationManager: NotificationManager) {
|
||||
|
||||
private fun startActivity(intent: Intent) {
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
@@ -134,9 +134,14 @@ class Navigator @Inject constructor(val context: Context) {
|
||||
}
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.O)
|
||||
fun showNotificationSettings() {
|
||||
fun showNotificationSettings(threadId: Long = 0) {
|
||||
if (threadId != 0L) {
|
||||
notificationManager.createNotificationChannel(threadId)
|
||||
}
|
||||
|
||||
val channelId = notificationManager.buildNotificationChannelId(threadId)
|
||||
val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS)
|
||||
intent.putExtra(Settings.EXTRA_CHANNEL_ID, NotificationManager.DEFAULT_CHANNEL_ID)
|
||||
intent.putExtra(Settings.EXTRA_CHANNEL_ID, channelId)
|
||||
intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
|
||||
startActivity(intent)
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ import javax.inject.Inject
|
||||
class ConversationInfoActivity : QkThemedActivity<ConversationInfoViewModel>(), ConversationInfoView {
|
||||
|
||||
override val viewModelClass = ConversationInfoViewModel::class
|
||||
override val notificationsIntent by lazy { notifications.clicks() }
|
||||
override val archiveIntent by lazy { archive.clicks() }
|
||||
override val deleteIntent by lazy { delete.clicks() }
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ import presentation.common.base.QkView
|
||||
|
||||
interface ConversationInfoView : QkView<ConversationInfoState> {
|
||||
|
||||
val notificationsIntent: Observable<Unit>
|
||||
val archiveIntent: Observable<Unit>
|
||||
val deleteIntent: Observable<Unit>
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ import interactor.MarkUnarchived
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.rxkotlin.plusAssign
|
||||
import io.reactivex.rxkotlin.withLatestFrom
|
||||
import presentation.common.Navigator
|
||||
import presentation.common.base.QkViewModel
|
||||
import javax.inject.Inject
|
||||
|
||||
@@ -44,6 +45,7 @@ class ConversationInfoViewModel(intent: Intent) : QkViewModel<ConversationInfoVi
|
||||
@Inject lateinit var messageRepo: MessageRepository
|
||||
@Inject lateinit var markArchived: MarkArchived
|
||||
@Inject lateinit var markUnarchived: MarkUnarchived
|
||||
@Inject lateinit var navigator: Navigator
|
||||
@Inject lateinit var deleteConversation: DeleteConversation
|
||||
|
||||
private val conversation: Observable<Conversation>
|
||||
@@ -83,6 +85,12 @@ class ConversationInfoViewModel(intent: Intent) : QkViewModel<ConversationInfoVi
|
||||
override fun bindView(view: ConversationInfoView) {
|
||||
super.bindView(view)
|
||||
|
||||
// Show the notifications settings for the conversation
|
||||
view.notificationsIntent
|
||||
.withLatestFrom(conversation, { _, conversation -> conversation })
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { conversation -> navigator.showNotificationSettings(conversation.id) }
|
||||
|
||||
// Toggle the archived state of the conversation
|
||||
view.archiveIntent
|
||||
.withLatestFrom(conversation, { _, conversation -> conversation })
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
<!--
|
||||
~ Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
|
||||
~
|
||||
~ This file is part of QKSMS.
|
||||
~
|
||||
~ QKSMS is free software: you can redistribute it and/or modify
|
||||
~ it under the terms of the GNU General Public License as published by
|
||||
~ the Free Software Foundation, either version 3 of the License, or
|
||||
~ (at your option) any later version.
|
||||
~
|
||||
~ QKSMS is distributed in the hope that it will be useful,
|
||||
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
~ GNU General Public License for more details.
|
||||
~
|
||||
~ You should have received a copy of the GNU General Public License
|
||||
~ along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
-->
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24.0"
|
||||
android:viewportHeight="24.0">
|
||||
<path
|
||||
android:fillColor="#FF000000"
|
||||
android:pathData="M12,22c1.1,0 2,-0.9 2,-2h-4c0,1.1 0.89,2 2,2zM18,16v-5c0,-3.07 -1.64,-5.64 -4.5,-6.32L13.5,4c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v0.68C7.63,5.36 6,7.92 6,11v5l-2,2v1h16v-1l-2,-2z"/>
|
||||
</vector>
|
||||
@@ -51,6 +51,13 @@
|
||||
android:layout_marginBottom="8dp"
|
||||
android:layout_marginTop="8dp" />
|
||||
|
||||
<presentation.common.widget.PreferenceView
|
||||
android:id="@+id/notifications"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
app:icon="@drawable/ic_notifications_black_24dp"
|
||||
app:title="@string/info_notifications" />
|
||||
|
||||
<presentation.common.widget.PreferenceView
|
||||
android:id="@+id/archive"
|
||||
android:layout_width="match_parent"
|
||||
|
||||
@@ -64,6 +64,7 @@
|
||||
<string name="blocked_empty_text">Your blocked senders will appear here</string>
|
||||
|
||||
<string name="info_title">Details</string>
|
||||
<string name="info_notifications">Notifications</string>
|
||||
<string name="info_archive">Archive</string>
|
||||
<string name="info_unarchive">Unarchive</string>
|
||||
<string name="info_delete">Delete conversation</string>
|
||||
|
||||
Reference in New Issue
Block a user