diff --git a/stream-video-android-core/api/stream-video-android-core.api b/stream-video-android-core/api/stream-video-android-core.api index f1286c1c7c..0a2d929a21 100644 --- a/stream-video-android-core/api/stream-video-android-core.api +++ b/stream-video-android-core/api/stream-video-android-core.api @@ -4471,34 +4471,19 @@ public final class io/getstream/video/android/core/socket/SocketState$NotConnect public fun toString ()Ljava/lang/String; } -public class io/getstream/video/android/core/sounds/DeviceRingtoneSoundConfig : io/getstream/video/android/core/sounds/StreamResSoundConfig { - public fun (Landroid/content/Context;)V - public final fun getContext ()Landroid/content/Context; - public fun getIncomingCallSoundUri ()Landroid/net/Uri; -} - -public class io/getstream/video/android/core/sounds/MutedSoundConfig : io/getstream/video/android/core/sounds/SoundConfig { - public fun ()V - public fun getIncomingCallSoundUri ()Landroid/net/Uri; - public fun getOutgoingCallSoundUri ()Landroid/net/Uri; -} - -public class io/getstream/video/android/core/sounds/ResSoundConfig : io/getstream/video/android/core/sounds/SoundConfig { - public fun (Landroid/content/Context;Ljava/lang/Integer;Ljava/lang/Integer;)V - public fun getIncomingCallSoundUri ()Landroid/net/Uri; - public fun getOutgoingCallSoundUri ()Landroid/net/Uri; - protected final fun parseSoundUri (Landroid/content/Context;Ljava/lang/Integer;)Landroid/net/Uri; -} - public abstract interface class io/getstream/video/android/core/sounds/SoundConfig { + public static final field Companion Lio/getstream/video/android/core/sounds/SoundConfig$Companion; public abstract fun getIncomingCallSoundUri ()Landroid/net/Uri; public abstract fun getOutgoingCallSoundUri ()Landroid/net/Uri; } -public final class io/getstream/video/android/core/sounds/SoundConfigKt { - public static final fun deviceRingtoneSoundConfig (Landroid/content/Context;)Lio/getstream/video/android/core/sounds/SoundConfig; - public static final fun mutedSoundConfig ()Lio/getstream/video/android/core/sounds/SoundConfig; - public static final fun streamResourcesSoundConfig (Landroid/content/Context;)Lio/getstream/video/android/core/sounds/SoundConfig; +public final class io/getstream/video/android/core/sounds/SoundConfig$Companion { + public final fun createCustomSoundConfig (Ljava/lang/Object;Ljava/lang/Object;Landroid/content/Context;)Lio/getstream/video/android/core/sounds/SoundConfig; + public static synthetic fun createCustomSoundConfig$default (Lio/getstream/video/android/core/sounds/SoundConfig$Companion;Ljava/lang/Object;Ljava/lang/Object;Landroid/content/Context;ILjava/lang/Object;)Lio/getstream/video/android/core/sounds/SoundConfig; + public final fun createDeviceRingtoneSoundConfig (Landroid/content/Context;)Lio/getstream/video/android/core/sounds/SoundConfig; + public final fun createEmptySoundConfig ()Lio/getstream/video/android/core/sounds/SoundConfig; + public final fun createStreamResourcesSoundConfig (Landroid/content/Context;)Lio/getstream/video/android/core/sounds/SoundConfig; + public final fun getSoundUriFromRes (Landroid/content/Context;Ljava/lang/Integer;)Landroid/net/Uri; } public final class io/getstream/video/android/core/sounds/Sounds { @@ -4517,25 +4502,6 @@ public final class io/getstream/video/android/core/sounds/Sounds { public fun toString ()Ljava/lang/String; } -public class io/getstream/video/android/core/sounds/StreamResSoundConfig : io/getstream/video/android/core/sounds/ResSoundConfig { - public fun (Landroid/content/Context;)V -} - -public final class io/getstream/video/android/core/sounds/UriSoundConfig : io/getstream/video/android/core/sounds/SoundConfig { - public fun (Landroid/net/Uri;Landroid/net/Uri;)V - public final fun component1 ()Landroid/net/Uri; - public final fun component2 ()Landroid/net/Uri; - public final fun copy (Landroid/net/Uri;Landroid/net/Uri;)Lio/getstream/video/android/core/sounds/UriSoundConfig; - public static synthetic fun copy$default (Lio/getstream/video/android/core/sounds/UriSoundConfig;Landroid/net/Uri;Landroid/net/Uri;ILjava/lang/Object;)Lio/getstream/video/android/core/sounds/UriSoundConfig; - public fun equals (Ljava/lang/Object;)Z - public fun getIncomingCallSoundUri ()Landroid/net/Uri; - public final fun getIncomingCallSoundUriValue ()Landroid/net/Uri; - public fun getOutgoingCallSoundUri ()Landroid/net/Uri; - public final fun getOutgoingCallSoundUriValue ()Landroid/net/Uri; - public fun hashCode ()I - public fun toString ()Ljava/lang/String; -} - public final class io/getstream/video/android/core/utils/AndroidUtilsKt { public static final fun safeCall (Ljava/lang/Object;Lkotlin/jvm/functions/Function0;)Ljava/lang/Object; public static final fun shouldShowRequestPermissionsRationale (Landroid/app/Activity;[Ljava/lang/String;)Z diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/StreamVideoBuilder.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/StreamVideoBuilder.kt index dbcbb8985d..51db557c96 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/StreamVideoBuilder.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/StreamVideoBuilder.kt @@ -33,8 +33,8 @@ import io.getstream.video.android.core.notifications.internal.service.callServic import io.getstream.video.android.core.notifications.internal.storage.DeviceTokenStorage import io.getstream.video.android.core.permission.android.DefaultStreamPermissionCheck import io.getstream.video.android.core.permission.android.StreamPermissionCheck +import io.getstream.video.android.core.sounds.SoundConfig import io.getstream.video.android.core.sounds.Sounds -import io.getstream.video.android.core.sounds.deviceRingtoneSoundConfig import io.getstream.video.android.model.ApiKey import io.getstream.video.android.model.User import io.getstream.video.android.model.UserToken @@ -100,7 +100,7 @@ public class StreamVideoBuilder @JvmOverloads constructor( private val runForegroundServiceForCalls: Boolean = true, private val callServiceConfig: CallServiceConfig? = null, private val localSfuAddress: String? = null, - private val sounds: Sounds = Sounds(deviceRingtoneSoundConfig(context)), + private val sounds: Sounds = Sounds(SoundConfig.createDeviceRingtoneSoundConfig(context)), private val crashOnMissingPermission: Boolean = false, private val permissionCheck: StreamPermissionCheck = DefaultStreamPermissionCheck(), private val audioUsage: Int = defaultAudioUsage, diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/sounds/SoundConfig.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/sounds/SoundConfig.kt index bb48b3412d..62df1c7734 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/sounds/SoundConfig.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/sounds/SoundConfig.kt @@ -19,93 +19,110 @@ package io.getstream.video.android.core.sounds import android.content.Context import android.media.RingtoneManager import android.net.Uri -import androidx.annotation.RawRes import io.getstream.video.android.core.R import io.getstream.video.android.core.utils.safeCall /** * Base sound configuration. * - * @see deviceRingtoneSoundConfig - * @see streamResourcesSoundConfig - * @see mutedSoundConfig - * @see ResSoundConfig - * @see UriSoundConfig + * @see createDeviceRingtoneSoundConfig + * @see createStreamResourcesSoundConfig + * @see createEmptySoundConfig + * @see createCustomSoundConfig */ interface SoundConfig { val incomingCallSoundUri: Uri? val outgoingCallSoundUri: Uri? - fun parseSoundUri(context: Context, soundResId: Int?): Uri? = soundResId?.let { - Uri.parse("android.resource://${context.packageName}/$soundResId") + companion object { + + /** + * Returns a sound config that uses the device ringtone for incoming calls and the SDK default ringing tone for outgoing calls. + * + * @param context Context used for retrieving the sounds. + */ + fun createDeviceRingtoneSoundConfig(context: Context): SoundConfig = object : SoundConfig { + + private val streamResSoundConfig = createStreamResourcesSoundConfig(context) + + override val incomingCallSoundUri: Uri? + get() = safeCall(default = null) { + RingtoneManager.getActualDefaultRingtoneUri( + context, + RingtoneManager.TYPE_RINGTONE, + ) + } ?: streamResSoundConfig.incomingCallSoundUri + + override val outgoingCallSoundUri: Uri? = streamResSoundConfig.outgoingCallSoundUri + } + + /** + * Returns a sound config that uses the SDK default sounds for incoming and outgoing calls. + * + * @param context Context used for retrieving the sounds. + */ + fun createStreamResourcesSoundConfig(context: Context): SoundConfig = object : SoundConfig { + + override val incomingCallSoundUri: Uri? = + getSoundUriFromRes(context, R.raw.call_incoming_sound) + override val outgoingCallSoundUri: Uri? = + getSoundUriFromRes(context, R.raw.call_outgoing_sound) + } + + fun getSoundUriFromRes(context: Context, soundResId: Int?): Uri? = soundResId?.let { + safeCall(default = null) { + Uri.parse("android.resource://${context.packageName}/$soundResId") + } + } + + /** + * Returns a sound config that mutes (disables) all sounds. + */ + fun createEmptySoundConfig(): SoundConfig = object : SoundConfig { + + override val incomingCallSoundUri: Uri? = null + override val outgoingCallSoundUri: Uri? = null + } + + /** + * Returns a sound config that uses custom sounds for incoming and outgoing calls. + * + * @param incomingCallSound The incoming call sound. Can be a resource ID or a URI. + * @param outgoingCallSound The outgoing call sound. Can be a resource ID or a URI. + * @param context Context used for retrieving the sounds. Mandatory when one of the sound parameters is a resource ID. + * + * @return A sound config with the provided sounds. + * + * @throws IllegalArgumentException If one of the sound parameters is a resource ID and the context is not provided. + */ + fun createCustomSoundConfig( + incomingCallSound: Any?, + outgoingCallSound: Any?, + context: Context? = null, + ) = object : SoundConfig { + + override val incomingCallSoundUri: Uri? = when (incomingCallSound) { + is Uri -> incomingCallSound + is Int -> { + requireNotNull( + context, + ) { "Context is required when incomingCallSound is a resource ID." } + getSoundUriFromRes(context, incomingCallSound) + } + else -> null + } + + override val outgoingCallSoundUri: Uri? = when (outgoingCallSound) { + is Uri -> outgoingCallSound + is Int -> { + requireNotNull( + context, + ) { "Context is required when outgoingCallSound is a resource ID." } + getSoundUriFromRes(context, outgoingCallSound) + } + else -> null + } + } } } - - -/** - * Returns a sound config that uses the device ringtone for incoming calls and the SDK default ringing tone for outgoing calls. - * - * @param context Context used for retrieving the sounds. - */ -fun deviceRingtoneSoundConfig(context: Context): SoundConfig = object : SoundConfig { - - private val streamResSoundConfig = streamResourcesSoundConfig(context) - - override val incomingCallSoundUri: Uri? - get() = safeCall(default = null) { - RingtoneManager.getActualDefaultRingtoneUri( - context, - RingtoneManager.TYPE_RINGTONE, - ) - } ?: streamResSoundConfig.incomingCallSoundUri - - override val outgoingCallSoundUri: Uri? = streamResSoundConfig.outgoingCallSoundUri -} - -/** - * Returns a sound config that uses the SDK default sounds for incoming and outgoing calls. - * - * @param context Context used for retrieving the sounds. - */ -fun streamResourcesSoundConfig( - context: Context, -): SoundConfig = object : SoundConfig { - - override val incomingCallSoundUri: Uri? = parseSoundUri(context, R.raw.call_incoming_sound) - override val outgoingCallSoundUri: Uri? = parseSoundUri(context, R.raw.call_outgoing_sound) -} - -/** - * Returns a sound config that mutes (disables) all sounds. - */ -fun mutedSoundConfig(): SoundConfig = object : SoundConfig { - - override val incomingCallSoundUri: Uri? = null - override val outgoingCallSoundUri: Uri? = null -} - -/** - * A class that represents a sound config that uses raw resources to specify the sounds. - */ -class ResSoundConfig( - context: Context, - @RawRes incomingCallSoundResId: Int?, - @RawRes outgoingCallSoundResId: Int?, -) : SoundConfig { - - override val incomingCallSoundUri: Uri? = parseSoundUri(context, incomingCallSoundResId) - override val outgoingCallSoundUri: Uri? = parseSoundUri(context, outgoingCallSoundResId) -} - -/** - * A class that represents a sound config that uses URIs to specify the sounds. - */ -data class UriSoundConfig( - val incomingCallSoundUriValue: Uri?, - val outgoingCallSoundUriValue: Uri?, -) : SoundConfig { - - override val incomingCallSoundUri: Uri? = incomingCallSoundUriValue - override val outgoingCallSoundUri: Uri? = outgoingCallSoundUriValue -} diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/sounds/Sounds.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/sounds/Sounds.kt index ddac951cf0..e42d26ce94 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/sounds/Sounds.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/sounds/Sounds.kt @@ -46,9 +46,10 @@ constructor( /** * Configure sounds by passing a [SoundConfig]. * - * @see deviceRingtoneSoundConfig - * @see streamResourcesSoundConfig - * @see mutedSoundConfig + * @see SoundConfig.createDeviceRingtoneSoundConfig + * @see SoundConfig.createStreamResourcesSoundConfig + * @see SoundConfig.createEmptySoundConfig + * @see SoundConfig.createCustomSoundConfig * @see SoundConfig */ constructor(soundConfig: SoundConfig) : this() {