diff --git a/core/src/main/java/org/openedx/core/module/TranscriptManager.kt b/core/src/main/java/org/openedx/core/module/TranscriptManager.kt index b80500ad1..e225bbae6 100644 --- a/core/src/main/java/org/openedx/core/module/TranscriptManager.kt +++ b/core/src/main/java/org/openedx/core/module/TranscriptManager.kt @@ -5,6 +5,7 @@ import okhttp3.OkHttpClient import org.openedx.core.module.download.AbstractDownloader import org.openedx.core.utils.Directories import org.openedx.core.utils.IOUtils +import org.openedx.core.utils.Logger import org.openedx.core.utils.Sha1Util import org.openedx.foundation.utils.FileUtil import subtitleFile.FormatSRT @@ -21,6 +22,8 @@ class TranscriptManager( val fileUtil: FileUtil ) { + private val logger = Logger(TAG) + private val transcriptDownloader = object : AbstractDownloader() { override val client: OkHttpClient get() = OkHttpClient.Builder().build() @@ -62,17 +65,18 @@ class TranscriptManager( } private suspend fun startTranscriptDownload(downloadLink: String) { - if (!has(downloadLink)) { - val file = File(getTranscriptDir(), Sha1Util.SHA1(downloadLink)) - val result = transcriptDownloader.download( - downloadLink, - file.path - ) - if (result == AbstractDownloader.DownloadResult.SUCCESS) { - getInputStream(downloadLink)?.let { - val transcriptTimedTextObject = - convertIntoTimedTextObject(it) - transcriptObject = transcriptTimedTextObject + if (has(downloadLink)) return + val file = File(getTranscriptDir(), Sha1Util.SHA1(downloadLink)) + val result = transcriptDownloader.download( + downloadLink, + file.path + ) + if (result == AbstractDownloader.DownloadResult.SUCCESS) { + getInputStream(downloadLink)?.let { + try { + transcriptObject = convertIntoTimedTextObject(it) + } catch (e: NullPointerException) { + logger.e(throwable = e, submitCrashReport = true) } } } @@ -86,7 +90,7 @@ class TranscriptManager( try { transcriptObject = convertIntoTimedTextObject(transcriptInputStream) } catch (e: Exception) { - e.printStackTrace() + logger.e(throwable = e, submitCrashReport = true) } } else { startTranscriptDownload(transcriptUrl) @@ -127,6 +131,7 @@ class TranscriptManager( } companion object { + private const val TAG = "TranscriptManager" private const val FILE_VALIDITY_DURATION_HOURS = 5L } } diff --git a/core/src/main/java/org/openedx/core/utils/Logger.kt b/core/src/main/java/org/openedx/core/utils/Logger.kt index 41cd9a3a6..e08e2d357 100644 --- a/core/src/main/java/org/openedx/core/utils/Logger.kt +++ b/core/src/main/java/org/openedx/core/utils/Logger.kt @@ -1,9 +1,16 @@ package org.openedx.core.utils import android.util.Log +import com.google.firebase.crashlytics.FirebaseCrashlytics +import org.koin.core.component.KoinComponent +import org.koin.core.component.inject import org.openedx.core.BuildConfig +import org.openedx.core.config.Config + +class Logger(private val tag: String) : KoinComponent { + + private val config by inject() -class Logger(private val tag: String) { fun d(message: () -> String) { if (BuildConfig.DEBUG) Log.d(tag, message()) } @@ -12,6 +19,13 @@ class Logger(private val tag: String) { if (BuildConfig.DEBUG) Log.e(tag, message()) } + fun e(throwable: Throwable, submitCrashReport: Boolean = false) { + if (BuildConfig.DEBUG) throwable.printStackTrace() + if (submitCrashReport && config.getFirebaseConfig().enabled) { + FirebaseCrashlytics.getInstance().recordException(throwable) + } + } + fun i(message: () -> String) { if (BuildConfig.DEBUG) Log.i(tag, message()) }