Skip to content

Commit

Permalink
Merge pull request #234 from gavine99/silent_notifications_for_unknow…
Browse files Browse the repository at this point in the history
…n_numbers_60

silent notifications if no recipients are a contact
  • Loading branch information
octoshrimpy authored Jan 28, 2025
2 parents f4d8e5a + 76922b5 commit 81eb310
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ class ContactRepositoryImpl @Inject constructor(
}
}

override fun getUnmanagedAllContacts(): List<Contact> {
val realm = Realm.getDefaultInstance()

return realm
.where(Contact::class.java)
.findAll()
.map { realm.copyFromRealm(it) }
}

override fun getUnmanagedContacts(starred: Boolean): Observable<List<Contact>> {
val realm = Realm.getDefaultInstance()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ interface ContactRepository {

fun getUnmanagedContact(lookupKey: String): Contact?

fun getUnmanagedAllContacts(): List<Contact>

fun getUnmanagedContacts(starred: Boolean = false): Observable<List<Contact>>

fun getUnmanagedContactGroups(): Observable<List<ContactGroup>>
Expand Down
1 change: 1 addition & 0 deletions domain/src/main/java/com/moez/QKSMS/util/Preferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class Preferences @Inject constructor(
val textSize = rxPrefs.getInteger("textSize", TEXT_SIZE_NORMAL)
val blockingManager = rxPrefs.getInteger("blockingManager", BLOCKING_MANAGER_QKSMS)
val drop = rxPrefs.getBoolean("drop", false)
val silentNotContact = rxPrefs.getBoolean("silentNotContact", false)
val notifAction1 = rxPrefs.getInteger("notifAction1", NOTIFICATION_ACTION_READ)
val notifAction2 = rxPrefs.getInteger("notifAction2", NOTIFICATION_ACTION_REPLY)
val notifAction3 = rxPrefs.getInteger("notifAction3", NOTIFICATION_ACTION_NONE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import dev.octoshrimpy.quik.receiver.MarkReadReceiver
import dev.octoshrimpy.quik.receiver.MarkSeenReceiver
import dev.octoshrimpy.quik.receiver.RemoteMessagingReceiver
import dev.octoshrimpy.quik.receiver.SpeakThreadsReceiver
import dev.octoshrimpy.quik.repository.ContactRepository
import dev.octoshrimpy.quik.repository.ConversationRepository
import dev.octoshrimpy.quik.repository.MessageRepository
import dev.octoshrimpy.quik.util.GlideApp
Expand All @@ -70,7 +71,8 @@ class NotificationManagerImpl @Inject constructor(
private val prefs: Preferences,
private val messageRepo: MessageRepository,
private val permissions: PermissionManager,
private val phoneNumberUtils: PhoneNumberUtils
private val phoneNumberUtils: PhoneNumberUtils,
private val contactRepo: ContactRepository,
) : dev.octoshrimpy.quik.manager.NotificationManager {

companion object {
Expand Down Expand Up @@ -145,12 +147,28 @@ class NotificationManagerImpl @Inject constructor(
.setAutoCancel(true)
.setContentIntent(contentPI)
.setDeleteIntent(seenPI)
.setSound(ringtone)
.setLights(Color.WHITE, 500, 2000)
.setWhen(conversation.lastMessage?.date ?: System.currentTimeMillis())
.setVibrate(if (prefs.vibration(threadId).get()) VIBRATE_PATTERN else longArrayOf(0))

// Tell the notification if it's a group message
// if preference set to silence notifications if no recipients in contacts
if (prefs.silentNotContact.get() && run {
val msgRecipientNumbers = conversation.recipients.map { it.address }

// true if any message recipients are in device's contacts
!contactRepo
.getUnmanagedAllContacts()
.flatMap { it.numbers }
.map { it.address }
.any { contactNumber ->
msgRecipientNumbers.any { phoneNumberUtils.compare(contactNumber, it) }
}
})
notification.setSilent(true)
else
notification.setSound(ringtone)

// Tell the notification if it's a group message
val messagingStyle = NotificationCompat.MessagingStyle("Me")
if (conversation.recipients.size >= 2) {
messagingStyle.isGroupConversation = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ class NotificationPrefsActivity : QkThemedActivity(), NotificationPrefsView {
previews.summary = state.previewSummary
previewModeDialog.adapter.selectedItem = state.previewId
wake.checkbox.isChecked = state.wakeEnabled
silentNotContact.checkbox.isChecked = state.silentNotContact
silentNotContact.isVisible = state.threadId == 0L
vibration.checkbox.isChecked = state.vibrationEnabled
ringtone.summary = state.ringtoneName

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ data class NotificationPrefsState(
val previewSummary: String = "",
val previewId: Int = Preferences.NOTIFICATION_PREVIEWS_ALL,
val wakeEnabled: Boolean = false,
val silentNotContact: Boolean = false,
val action1Summary: String = "",
val action2Summary: String = "",
val action3Summary: String = "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ class NotificationPrefsViewModel @Inject constructor(
disposables += wake.asObservable()
.subscribe { enabled -> newState { copy(wakeEnabled = enabled) } }

disposables += prefs.silentNotContact.asObservable()
.subscribe { enabled -> newState { copy(silentNotContact = enabled) } }

disposables += vibration.asObservable()
.subscribe { enabled -> newState { copy(vibrationEnabled = enabled) } }

Expand Down Expand Up @@ -113,6 +116,8 @@ class NotificationPrefsViewModel @Inject constructor(

R.id.wake -> wake.set(!wake.get())

R.id.silentNotContact -> prefs.silentNotContact.set(!prefs.silentNotContact.get())

R.id.vibration -> vibration.set(!vibration.get())

R.id.ringtone -> view.showRingtonePicker(ringtone.get().takeIf { it.isNotEmpty() }?.let(Uri::parse))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="@string/settings_notification_previews_title"
tools:summary="Show name and message" />
tools:summary="@string/settings_notification_previews_summary" />

<dev.octoshrimpy.quik.common.widget.PreferenceView
android:id="@+id/wake"
Expand All @@ -79,6 +79,14 @@
app:title="@string/settings_notification_wake_title"
app:widget="@layout/settings_switch_widget" />

<dev.octoshrimpy.quik.common.widget.PreferenceView
android:id="@+id/silentNotContact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="@string/settings_notification_silent_title"
app:summary="@string/settings_notification_silent_summary"
app:widget="@layout/settings_switch_widget" />

<dev.octoshrimpy.quik.common.widget.PreferenceView
android:id="@+id/vibration"
android:layout_width="match_parent"
Expand Down
3 changes: 3 additions & 0 deletions presentation/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,10 @@
<string name="settings_notification_action_2">Button 2</string>
<string name="settings_notification_action_3">Button 3</string>
<string name="settings_notification_previews_title">Notification previews</string>
<string name="settings_notification_previews_summary">Show name and message</string>
<string name="settings_notification_wake_title">Wake screen</string>
<string name="settings_notification_silent_title">Silent if not a contact</string>
<string name="settings_notification_silent_summary">Silences notifications for messages without recipients in your contacts</string>
<string name="settings_vibration_title">Vibration</string>
<string name="settings_ringtone_title">Sound</string>
<string name="settings_ringtone_none">None</string>
Expand Down

0 comments on commit 81eb310

Please sign in to comment.