-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Notification sound even when muted or on DnD
- Loading branch information
Showing
9 changed files
with
158 additions
and
4 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
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
94 changes: 94 additions & 0 deletions
94
app/src/main/java/com/futsch1/medtimer/reminders/NotificationSoundManager.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,94 @@ | ||
package com.futsch1.medtimer.reminders | ||
|
||
import android.app.NotificationManager | ||
import android.content.Context | ||
import android.media.AudioManager | ||
import android.os.Handler | ||
import android.os.Looper | ||
import androidx.preference.PreferenceManager | ||
import com.futsch1.medtimer.PreferencesNames | ||
|
||
class NotificationSoundManager(val context: Context) { | ||
private val notificationManager: NotificationManager = | ||
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | ||
private val audioManager: AudioManager = | ||
context.getSystemService(Context.AUDIO_SERVICE) as AudioManager | ||
|
||
init { | ||
if (notificationManager.isNotificationPolicyAccessGranted()) { | ||
if (PreferenceManager.getDefaultSharedPreferences(context) | ||
.getBoolean(PreferencesNames.OVERRIDE_DND, false) | ||
) { | ||
loadPendingRingerMode(audioManager, notificationManager) | ||
} else { | ||
notificationManager.notificationPolicy = NotificationManager.Policy( | ||
0, | ||
NotificationManager.Policy.PRIORITY_SENDERS_ANY, | ||
NotificationManager.Policy.PRIORITY_SENDERS_ANY | ||
) | ||
|
||
} | ||
} | ||
} | ||
|
||
fun restore() { | ||
restorePendingRingerMode(audioManager) | ||
} | ||
|
||
companion object { | ||
private var pending = false | ||
private var pendingWasMuted = false | ||
private var scheduledRunnable: Runnable? = null | ||
private val handler = Handler(Looper.getMainLooper()) | ||
|
||
@Synchronized | ||
fun loadPendingRingerMode( | ||
audioManager: AudioManager, | ||
notificationManager: NotificationManager | ||
) { | ||
if (!pending) { | ||
pending = true | ||
if (audioManager.isStreamMute(AudioManager.STREAM_RING) && notificationManager.currentInterruptionFilter == NotificationManager.INTERRUPTION_FILTER_ALL) { | ||
audioManager.adjustStreamVolume( | ||
AudioManager.STREAM_RING, | ||
AudioManager.ADJUST_UNMUTE, | ||
0 | ||
) | ||
pendingWasMuted = true | ||
} | ||
notificationManager.notificationPolicy = NotificationManager.Policy( | ||
NotificationManager.Policy.PRIORITY_CATEGORY_REMINDERS, | ||
NotificationManager.Policy.PRIORITY_SENDERS_ANY, | ||
NotificationManager.Policy.PRIORITY_SENDERS_ANY, | ||
0 | ||
) | ||
} | ||
} | ||
|
||
@Synchronized | ||
fun restorePendingRingerMode( | ||
audioManager: AudioManager | ||
) { | ||
if (pending) { | ||
if (scheduledRunnable == null) { | ||
scheduledRunnable = Runnable { | ||
if (pendingWasMuted) { | ||
audioManager.adjustStreamVolume( | ||
AudioManager.STREAM_RING, | ||
AudioManager.ADJUST_MUTE, | ||
0 | ||
) | ||
audioManager.ringerMode = AudioManager.RINGER_MODE_VIBRATE | ||
} | ||
pending = false | ||
scheduledRunnable = null | ||
} | ||
} | ||
handler.postDelayed(scheduledRunnable!!, 5000) | ||
} else { | ||
handler.removeCallbacks(scheduledRunnable!!) | ||
handler.postDelayed(scheduledRunnable!!, 5000) | ||
} | ||
} | ||
} | ||
} |
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