Skip to content

Commit

Permalink
Refactor SoundConfig factory methods
Browse files Browse the repository at this point in the history
  • Loading branch information
liviu-timar committed Sep 18, 2024
1 parent 1bebed9 commit 5ebb707
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 123 deletions.
50 changes: 8 additions & 42 deletions stream-video-android-core/api/stream-video-android-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -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 <init> (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 <init> ()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 <init> (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 {
Expand All @@ -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 <init> (Landroid/content/Context;)V
}

public final class io/getstream/video/android/core/sounds/UriSoundConfig : io/getstream/video/android/core/sounds/SoundConfig {
public fun <init> (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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 5ebb707

Please sign in to comment.