Skip to content

Commit

Permalink
Add internet connectivity check to error analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth Karia committed Oct 18, 2024
1 parent 582404e commit ca8bea8
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 11 deletions.
1 change: 1 addition & 0 deletions Armadillo/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
Expand Down
32 changes: 32 additions & 0 deletions Armadillo/src/main/java/com/scribd/armadillo/Util.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.scribd.armadillo

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Build
import androidx.annotation.ChecksSdkIntAtLeast
import com.scribd.armadillo.models.Chapter
Expand Down Expand Up @@ -47,5 +49,35 @@ fun sanitizeChapters(chapters: List<Chapter>): List<Chapter> {
}
}

fun isInternetAvailable(context: Context): Boolean {
var result = false
val connectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val networkCapabilities = connectivityManager.activeNetwork ?: return false
val actNw =
connectivityManager.getNetworkCapabilities(networkCapabilities) ?: return false
result = when {
actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
actNw.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
else -> false
}
} else {
connectivityManager.run {
connectivityManager.activeNetworkInfo?.run {
result = when (type) {
ConnectivityManager.TYPE_WIFI -> true
ConnectivityManager.TYPE_MOBILE -> true
ConnectivityManager.TYPE_ETHERNET -> true
else -> false
}

}
}
}
return result
}

@ChecksSdkIntAtLeast(api = Build.VERSION_CODES.S)
fun hasSnowCone() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.scribd.armadillo.playback.MediaMetadataCompatBuilderImpl
import com.scribd.armadillo.playback.PlaybackEngineFactoryHolder
import com.scribd.armadillo.playback.PlaybackStateBuilderImpl
import com.scribd.armadillo.playback.PlaybackStateCompatBuilder
import com.scribd.armadillo.playback.PlayerEventListener
import com.scribd.armadillo.playback.mediasource.DrmMediaSourceHelper
import com.scribd.armadillo.playback.mediasource.DrmMediaSourceHelperImpl
import com.scribd.armadillo.playback.mediasource.HeadersMediaSourceFactoryFactory
Expand Down Expand Up @@ -75,4 +76,8 @@ internal class PlaybackModule {
@Provides
@Singleton
fun drmSessionManagerProvider(stateStore: StateStore.Modifier): DrmSessionManagerProvider = ArmadilloDrmSessionManagerProvider(stateStore)

@Provides
@Singleton
fun playerEventListener(context: Context): PlayerEventListener = PlayerEventListener(context)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.scribd.armadillo.playback

import android.content.Context
import com.google.android.exoplayer2.ExoPlaybackException
import com.google.android.exoplayer2.ExoPlaybackException.TYPE_RENDERER
import com.google.android.exoplayer2.ExoPlaybackException.TYPE_SOURCE
Expand All @@ -18,24 +19,25 @@ import com.scribd.armadillo.error.RendererInitializationException
import com.scribd.armadillo.error.RendererWriteException
import com.scribd.armadillo.error.UnexpectedException
import com.scribd.armadillo.error.UnknownRendererException
import java.io.IOException
import com.scribd.armadillo.isInternetAvailable
import java.net.SocketTimeoutException
import java.net.UnknownHostException

internal fun ExoPlaybackException.toArmadilloException(): ArmadilloException {
internal fun ExoPlaybackException.toArmadilloException(context: Context): ArmadilloException {
return if (TYPE_SOURCE == type) {
return this.sourceException.let { source ->
when (source) {
is HttpDataSource.InvalidResponseCodeException ->
HttpResponseCodeException(source.responseCode, source.dataSpec.uri.toString(), source, source.dataSpec.toAnalyticsMap())
HttpResponseCodeException(source.responseCode, source.dataSpec.uri.toString(), source, source.dataSpec.toAnalyticsMap
(context))

is HttpDataSource.HttpDataSourceException ->
HttpResponseCodeException(source.reason, source.dataSpec.uri.toString(), source, source.dataSpec.toAnalyticsMap())
HttpResponseCodeException(source.reason, source.dataSpec.uri.toString(), source, source.dataSpec.toAnalyticsMap(context))

is MediaDrmCallbackException -> {
val httpCause = source.cause as? HttpDataSource.InvalidResponseCodeException
HttpResponseCodeException(httpCause?.responseCode
?: 0, httpCause?.dataSpec?.uri.toString(), source, source.dataSpec.toAnalyticsMap())
?: 0, httpCause?.dataSpec?.uri.toString(), source, source.dataSpec.toAnalyticsMap(context))
}

is UnknownHostException,
Expand Down Expand Up @@ -67,7 +69,7 @@ internal fun ExoPlaybackException.toArmadilloException(): ArmadilloException {
}
}

private fun DataSpec.toAnalyticsMap(): Map<String, String> {
private fun DataSpec.toAnalyticsMap(context: Context): Map<String, String> {
return mapOf(
"uri" to uri.toString(),
"uriPositionOffset" to uriPositionOffset.toString(),
Expand All @@ -76,6 +78,7 @@ private fun DataSpec.toAnalyticsMap(): Map<String, String> {
"length" to length.toString(),
"key" to key.toString(),
"flags" to flags.toString(),
"customData" to customData.toString()
"customData" to customData.toString(),
"isInternetConnected" to isInternetAvailable(context).toString(),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ internal class ExoplayerPlaybackEngine(private var audioPlayable: AudioPlayable)
@VisibleForTesting
internal lateinit var exoPlayer: ExoPlayer

private val playerEventListener = PlayerEventListener()
@Inject
internal lateinit var playerEventListener: PlayerEventListener

override val currentChapterIndex: Int
get() = audioPlayable.chapters.indexOf(currentChapter)
Expand Down Expand Up @@ -155,7 +156,7 @@ internal class ExoplayerPlaybackEngine(private var audioPlayable: AudioPlayable)
stateModifier.dispatch(PlaybackEngineReady(true))
stateModifier.dispatch(PlayerStateAction(PlaybackState.PAUSED))
} catch (ex: Exception) {
val armadilloException = if(ex is ArmadilloException) ex else PlaybackStartFailureException(cause = ex)
val armadilloException = if (ex is ArmadilloException) ex else PlaybackStartFailureException(cause = ex)
stateModifier.dispatch(ErrorAction(armadilloException))
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.scribd.armadillo.playback

import android.content.Context
import android.util.Log
import com.google.android.exoplayer2.ExoPlaybackException
import com.google.android.exoplayer2.ExoPlayer
Expand All @@ -22,7 +23,7 @@ import javax.inject.Inject
*
* It communicates changes by sending [Action]s with [StateStore.Modifier].
*/
internal class PlayerEventListener : Player.Listener {
internal class PlayerEventListener @Inject constructor(val context: Context) : Player.Listener {
init {
Injector.mainComponent.inject(this)
}
Expand All @@ -35,7 +36,7 @@ internal class PlayerEventListener : Player.Listener {
internal lateinit var stateModifier: StateStore.Modifier

override fun onPlayerError(error: PlaybackException) {
val exception = (error as ExoPlaybackException).toArmadilloException()
val exception = (error as ExoPlaybackException).toArmadilloException(context)
stateModifier.dispatch(ErrorAction(exception))
Log.e(TAG, "onPlayerError: $error")
}
Expand Down

0 comments on commit ca8bea8

Please sign in to comment.