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

Implement relative volume adjustments for the command_volume_level notification command #4056

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class MessagingManager @Inject constructor(
const val HIGH_ACCURACY_UPDATE_INTERVAL = "high_accuracy_update_interval"
const val PACKAGE_NAME = "package_name"
const val CONFIRMATION = "confirmation"
const val RELATIVE_VOLUME = "relative_volume"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which parameter name do you think is the best?

  • relative_volume
  • relative
  • adjust
  • delta


// special intent constants
const val INTENT_PACKAGE_NAME = "intent_package_name"
Expand Down Expand Up @@ -623,24 +624,21 @@ class MessagingManager @Inject constructor(
}
}
COMMAND_VOLUME_LEVEL -> {
val audioManager =
context.getSystemService<AudioManager>()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val notificationManager = context.getSystemService<NotificationManager>()
if (notificationManager?.isNotificationPolicyAccessGranted == false) {
notifyMissingPermission(message.toString(), serverId)
} else {
processStreamVolume(
audioManager!!,
data[NotificationData.MEDIA_STREAM].toString(),
command!!.toInt()
)
}
val audioManager = context.getSystemService<AudioManager>()
val relative = data[RELATIVE_VOLUME]?.toBoolean() ?: false

Toast.makeText(context, "$RELATIVE_VOLUME = $relative", Toast.LENGTH_SHORT).show()

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
context.getSystemService<NotificationManager>()?.isNotificationPolicyAccessGranted == false
) {
notifyMissingPermission(message.toString(), serverId)
} else {
processStreamVolume(
audioManager!!,
data[NotificationData.MEDIA_STREAM].toString(),
command!!.toInt()
audioManager = audioManager!!,
stream = data[NotificationData.MEDIA_STREAM].toString(),
volume = command!!.toInt(),
relative = relative
)
}
}
Expand Down Expand Up @@ -1652,29 +1650,34 @@ class MessagingManager @Inject constructor(
}
}

private fun processStreamVolume(audioManager: AudioManager, stream: String, volume: Int) {
when (stream) {
NotificationData.ALARM_STREAM -> adjustVolumeStream(AudioManager.STREAM_ALARM, volume, audioManager)
NotificationData.MUSIC_STREAM -> adjustVolumeStream(AudioManager.STREAM_MUSIC, volume, audioManager)
NotificationData.NOTIFICATION_STREAM -> adjustVolumeStream(AudioManager.STREAM_NOTIFICATION, volume, audioManager)
NotificationData.RING_STREAM -> adjustVolumeStream(AudioManager.STREAM_RING, volume, audioManager)
NotificationData.CALL_STREAM -> adjustVolumeStream(AudioManager.STREAM_VOICE_CALL, volume, audioManager)
NotificationData.SYSTEM_STREAM -> adjustVolumeStream(AudioManager.STREAM_SYSTEM, volume, audioManager)
NotificationData.DTMF_STREAM -> adjustVolumeStream(AudioManager.STREAM_DTMF, volume, audioManager)
else -> Log.d(TAG, "Skipping command due to invalid channel stream")
private fun processStreamVolume(
audioManager: AudioManager,
stream: String,
volume: Int,
relative: Boolean
) {
val streamType = when (stream) {
NotificationData.ALARM_STREAM -> AudioManager.STREAM_ALARM
NotificationData.MUSIC_STREAM -> AudioManager.STREAM_MUSIC
NotificationData.NOTIFICATION_STREAM -> AudioManager.STREAM_NOTIFICATION
NotificationData.RING_STREAM -> AudioManager.STREAM_RING
NotificationData.CALL_STREAM -> AudioManager.STREAM_VOICE_CALL
NotificationData.SYSTEM_STREAM -> AudioManager.STREAM_SYSTEM
NotificationData.DTMF_STREAM -> AudioManager.STREAM_DTMF
else -> null
}
}

private fun adjustVolumeStream(stream: Int, volume: Int, audioManager: AudioManager) {
var volumeLevel = volume
if (volumeLevel > audioManager.getStreamMaxVolume(stream)) {
volumeLevel = audioManager.getStreamMaxVolume(stream)
} else if (volumeLevel < 0) {
volumeLevel = 0
if (streamType == null) {
Log.d(TAG, "Skipping command due to invalid channel stream")
return
}
val newVolume = if (relative) {
audioManager.getStreamVolume(streamType) + volume
} else {
volume
}.coerceIn(0..audioManager.getStreamMaxVolume(streamType))
audioManager.setStreamVolume(
stream,
volumeLevel,
streamType,
newVolume,
AudioManager.FLAG_SHOW_UI
)
}
Expand Down