Skip to content

Commit

Permalink
Merge pull request #183 from anilibria/fix/ssl-tls-old-androids
Browse files Browse the repository at this point in the history
Fix/ssl tls old androids
  • Loading branch information
RadiationX authored Apr 2, 2024
2 parents 3cf1a08 + 718b0f9 commit 8ce645d
Show file tree
Hide file tree
Showing 31 changed files with 393 additions and 91 deletions.
3 changes: 0 additions & 3 deletions app-mobile/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,7 @@ dependencies {

implementation "androidx.media3:media3-exoplayer:$androidx_media3_version"
implementation "androidx.media3:media3-exoplayer-hls:$androidx_media3_version"
implementation "androidx.media3:media3-datasource-okhttp:$androidx_media3_version"
implementation "androidx.media3:media3-session:$androidx_media3_version"
implementation "androidx.media3:media3-datasource-cronet:$androidx_media3_version"


implementation "org.bsc.util:minitemplator-repackaged:$minitemplator_version"

Expand Down
2 changes: 1 addition & 1 deletion app-mobile/src/main/java/ru/radiationx/anilibria/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class App : Application() {

private fun initDependencies() {
Toothpick.setConfiguration(Configuration.forProduction())
Quill.getRootScope().installModules(AppModule(this), DataModule())
Quill.getRootScope().installModules(AppModule(this), DataModule(this))
}

private fun appVersionCheck() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package ru.radiationx.anilibria

import android.content.Context
import ru.radiationx.data.SharedBuildConfig
import javax.inject.Inject

class AppBuildConfig @Inject constructor() : SharedBuildConfig {
class AppBuildConfig @Inject constructor(
private val context: Context,
) : SharedBuildConfig {

override val applicationName: String = context.getString(R.string.app_name)

override val applicationId: String = BuildConfig.APPLICATION_ID

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import ru.radiationx.anilibria.ads.NativeAdsRepository
import ru.radiationx.anilibria.navigation.CiceroneHolder
import ru.radiationx.anilibria.presentation.common.IErrorHandler
import ru.radiationx.anilibria.presentation.common.ILinkHandler
import ru.radiationx.anilibria.ui.activities.player.PlayerDataSourceProvider
import ru.radiationx.data.player.PlayerDataSourceProvider
import ru.radiationx.anilibria.ui.common.ErrorHandler
import ru.radiationx.anilibria.ui.common.LinkRouter
import ru.radiationx.anilibria.ui.common.Templates
Expand Down Expand Up @@ -118,9 +118,6 @@ class AppModule(application: Application) : QuillModule() {
singleImpl<AnalyticsProfile, AppMetricaAnalyticsProfile>()
singleImpl<AnalyticsErrorReporter, AppMetricaErrorReporter>()
}

/* Player */
single<PlayerDataSourceProvider>()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory
import androidx.media3.session.MediaSession
import ru.radiationx.data.entity.common.PlayerTransport
import ru.radiationx.data.player.PlayerDataSourceProvider
import java.util.UUID
import javax.inject.Inject

Expand Down
6 changes: 3 additions & 3 deletions app-tv/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ dependencies {
implementation "com.github.stephanenicolas.toothpick:ktp:$toothpick_version"
kapt "com.github.stephanenicolas.toothpick:toothpick-compiler:$toothpick_version"


implementation "com.google.android.exoplayer:exoplayer:$exoplayer_leanback_version"
implementation "com.google.android.exoplayer:extension-leanback:$exoplayer_leanback_version"
implementation "androidx.media3:media3-ui-leanback:$androidx_media3_version"
implementation "androidx.media3:media3-exoplayer:$androidx_media3_version"
implementation "androidx.media3:media3-exoplayer-hls:$androidx_media3_version"

implementation "com.github.mintrocket.MintPermissions:mintpermissions:$mintpermissions_version"
implementation "com.github.mintrocket.MintPermissions:mintpermissions-flows:$mintpermissions_version"
Expand Down
2 changes: 1 addition & 1 deletion app-tv/src/main/java/ru/radiationx/anilibria/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class App : Application() {

private fun initDependencies() {
Toothpick.setConfiguration(Configuration.forProduction())
Quill.getRootScope().installModules(AppModule(this), DataModule())
Quill.getRootScope().installModules(AppModule(this), DataModule(this))
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package ru.radiationx.anilibria

import android.content.Context
import ru.radiationx.data.SharedBuildConfig
import javax.inject.Inject

class AppBuildConfig @Inject constructor() : SharedBuildConfig {
class AppBuildConfig @Inject constructor(
private val context: Context,
) : SharedBuildConfig {

override val applicationName: String = context.getString(R.string.app_name)

override val applicationId: String = BuildConfig.APPLICATION_ID

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@file:Suppress("DEPRECATION")

package ru.radiationx.anilibria.screen.player

import android.annotation.SuppressLint
Expand All @@ -13,45 +11,29 @@ import androidx.leanback.app.VideoSupportFragmentGlueHost
import androidx.leanback.widget.ArrayObjectAdapter
import androidx.leanback.widget.ClassPresenterSelector
import androidx.leanback.widget.ListRow
import com.google.android.exoplayer2.C
import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.Player
import com.google.android.exoplayer2.SimpleExoPlayer
import com.google.android.exoplayer2.ext.leanback.LeanbackPlayerAdapter
import com.google.android.exoplayer2.source.MediaSource
import com.google.android.exoplayer2.source.MediaSourceFactory
import com.google.android.exoplayer2.source.ProgressiveMediaSource
import com.google.android.exoplayer2.source.dash.DashMediaSource
import com.google.android.exoplayer2.source.hls.HlsMediaSource
import com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory
import com.google.android.exoplayer2.util.Util
import androidx.media3.common.MediaItem
import androidx.media3.common.Player
import androidx.media3.common.util.UnstableApi
import androidx.media3.datasource.DefaultDataSource
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory
import androidx.media3.ui.leanback.LeanbackPlayerAdapter
import ru.radiationx.anilibria.ui.presenter.cust.CustomListRowPresenter
import ru.radiationx.data.player.PlayerDataSourceProvider
import ru.radiationx.quill.get

@UnstableApi
open class BasePlayerFragment : VideoSupportFragment() {


protected var playerGlue: VideoPlayerGlue? = null
private set

protected var player: SimpleExoPlayer? = null
protected var player: ExoPlayer? = null
private set

protected var skipsPart: PlayerSkipsPart? = null
private set

private val dataSourceFactory by lazy {
val userAgent = Util.getUserAgent(requireActivity(), "VideoPlayerGlue")
DefaultDataSourceFactory(requireContext(), userAgent)
}
private val dashMediaSourceFactory by lazy { DashMediaSource.Factory(dataSourceFactory) }
private val ssMediaSourceFactory by lazy { SsMediaSource.Factory(dataSourceFactory) }
private val hlsMediaSourceFactory by lazy { HlsMediaSource.Factory(dataSourceFactory) }
private val otherMediaSourceFactory by lazy { ProgressiveMediaSource.Factory(dataSourceFactory) }

@SuppressLint("RestrictedApi")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Expand Down Expand Up @@ -132,11 +114,16 @@ open class BasePlayerFragment : VideoSupportFragment() {
if (player != null) {
throw RuntimeException("Player already initialized")
}
val bandwidthMeter = DefaultBandwidthMeter.Builder(requireContext()).build()
val trackSelector = DefaultTrackSelector(requireContext(), AdaptiveTrackSelection.Factory())
val player = SimpleExoPlayer.Builder(requireContext())
.setBandwidthMeter(bandwidthMeter)
.setTrackSelector(trackSelector)

val dataSourceProvider = get<PlayerDataSourceProvider>()
val dataSourceType = dataSourceProvider.get()
val dataSourceFactory = DefaultDataSource.Factory(requireContext(), dataSourceType.factory)
val mediaSourceFactory = DefaultMediaSourceFactory(requireContext()).apply {
setDataSourceFactory(dataSourceFactory)
}
val player = ExoPlayer.Builder(requireContext())
.setMediaSourceFactory(mediaSourceFactory)
.setHandleAudioBecomingNoisy(true)
.build()

player.addListener(object : Player.Listener {
Expand Down Expand Up @@ -172,20 +159,8 @@ open class BasePlayerFragment : VideoSupportFragment() {
}

protected fun preparePlayer(url: String) {
val mediaSource = getMediaSource(url)
player?.prepare(mediaSource, false, false)
}

private fun getMediaSource(url: String): MediaSource = Uri.parse(url).let {
getMediaSourceFactory(it).createMediaSource(MediaItem.fromUri(it))
player?.setMediaItem(MediaItem.fromUri(Uri.parse(url)), false)
player?.prepare()
}

private fun getMediaSourceFactory(uri: Uri): MediaSourceFactory =
when (val type = Util.inferContentType(uri)) {
C.TYPE_DASH -> dashMediaSourceFactory
C.TYPE_SS -> ssMediaSourceFactory
C.TYPE_HLS -> hlsMediaSourceFactory
C.TYPE_OTHER -> otherMediaSourceFactory
else -> throw IllegalStateException("Unsupported type: $type")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ package ru.radiationx.anilibria.screen.player

import android.os.Bundle
import android.view.View
import com.google.android.exoplayer2.PlaybackParameters
import androidx.media3.common.PlaybackParameters
import androidx.media3.common.util.UnstableApi
import kotlinx.coroutines.flow.filterNotNull
import ru.radiationx.data.entity.domain.types.EpisodeId
import ru.radiationx.data.entity.domain.types.ReleaseId
Expand All @@ -14,6 +15,7 @@ import ru.radiationx.shared.ktx.android.getExtraNotNull
import ru.radiationx.shared.ktx.android.putExtra
import ru.radiationx.shared.ktx.android.subscribeTo

@UnstableApi
class PlayerFragment : BasePlayerFragment() {

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import androidx.leanback.widget.PlaybackControlsRow.MultiAction
import androidx.leanback.widget.PlaybackControlsRow.RewindAction
import androidx.leanback.widget.PlaybackControlsRow.SkipNextAction
import androidx.leanback.widget.PlaybackControlsRow.SkipPreviousAction
import com.google.android.exoplayer2.ext.leanback.LeanbackPlayerAdapter
import androidx.media3.common.util.UnstableApi
import androidx.media3.ui.leanback.LeanbackPlayerAdapter
import ru.radiationx.data.entity.common.PlayerQuality
import java.util.concurrent.TimeUnit

Expand All @@ -48,6 +49,7 @@ import java.util.concurrent.TimeUnit
* Note that the superclass, [PlaybackTransportControlGlue], manages the playback controls
* row.
*/
@UnstableApi
class VideoPlayerGlue(
context: Context,
playerAdapter: LeanbackPlayerAdapter,
Expand Down
6 changes: 6 additions & 0 deletions data/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@ dependencies {

api "com.jakewharton.timber:timber:$timber_version"

api 'org.conscrypt:conscrypt-android:2.5.2'

api "com.squareup.moshi:moshi:$moshi_version"
api "com.squareup.moshi:moshi-adapters:$moshi_version"
ksp "com.squareup.moshi:moshi-kotlin-codegen:$moshi_version"

implementation "androidx.media3:media3-exoplayer:$androidx_media3_version"
implementation "androidx.media3:media3-datasource-okhttp:$androidx_media3_version"
implementation "androidx.media3:media3-datasource-cronet:$androidx_media3_version"

implementation "com.github.stephanenicolas.toothpick:ktp:$toothpick_version"
kapt "com.github.stephanenicolas.toothpick:toothpick-compiler:$toothpick_version"

Expand Down
1 change: 1 addition & 0 deletions data/src/main/java/ru/radiationx/data/SharedBuildConfig.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ru.radiationx.data

interface SharedBuildConfig {
val applicationName:String
val applicationId: String
val versionName: String
val versionCode: Int
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ru.radiationx.data.analytics.features

import ru.radiationx.data.analytics.AnalyticsSender
import ru.radiationx.data.sslcompat.SslCompat
import toothpick.InjectConstructor

@InjectConstructor
class SslCompatAnalytics(
private val sender: AnalyticsSender,
) {

private var sentError: Throwable? = null

fun oneShotError(data: Result<SslCompat.Data>) {
val error = data.exceptionOrNull() ?: return
if (error == sentError) return
sentError = error
sender.error("sslCompatError", error.message.orEmpty(), error)
}

}
30 changes: 29 additions & 1 deletion data/src/main/java/ru/radiationx/data/di/DataModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import com.squareup.moshi.Moshi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import okhttp3.ConnectionSpec
import ru.radiationx.data.ApiClient
import ru.radiationx.data.DataPreferences
import ru.radiationx.data.MainClient
import ru.radiationx.data.R
import ru.radiationx.data.SimpleClient
import ru.radiationx.data.ads.AdsConfigApi
import ru.radiationx.data.ads.AdsConfigRepository
Expand All @@ -38,6 +40,7 @@ import ru.radiationx.data.analytics.features.PlayerAnalytics
import ru.radiationx.data.analytics.features.ReleaseAnalytics
import ru.radiationx.data.analytics.features.ScheduleAnalytics
import ru.radiationx.data.analytics.features.SettingsAnalytics
import ru.radiationx.data.analytics.features.SslCompatAnalytics
import ru.radiationx.data.analytics.features.TeamsAnalytics
import ru.radiationx.data.analytics.features.UpdaterAnalytics
import ru.radiationx.data.analytics.features.WebPlayerAnalytics
Expand Down Expand Up @@ -111,6 +114,7 @@ import ru.radiationx.data.interactors.ReleaseInteractor
import ru.radiationx.data.interactors.ReleaseUpdateMiddleware
import ru.radiationx.data.migration.MigrationDataSource
import ru.radiationx.data.migration.MigrationDataSourceImpl
import ru.radiationx.data.player.PlayerDataSourceProvider
import ru.radiationx.data.repository.AuthRepository
import ru.radiationx.data.repository.CheckerRepository
import ru.radiationx.data.repository.ConfigurationRepository
Expand All @@ -125,15 +129,35 @@ import ru.radiationx.data.repository.ScheduleRepository
import ru.radiationx.data.repository.SearchRepository
import ru.radiationx.data.repository.TeamsRepository
import ru.radiationx.data.repository.YoutubeRepository
import ru.radiationx.data.sslcompat.SslCompat
import ru.radiationx.data.system.ApiUtils
import ru.radiationx.data.system.AppCookieJar
import ru.radiationx.quill.QuillModule
import toothpick.InjectConstructor
import javax.inject.Provider

class DataModule : QuillModule() {
class DataModule(context: Context) : QuillModule() {

init {


instance<SslCompat> {
val rawCertResources = listOf(
R.raw.gsr4,
R.raw.gtsr1,
R.raw.gtsr2,
R.raw.gtsr3,
R.raw.gtsr4,
R.raw.isrg_root_x1,
R.raw.isrg_root_x2,
)
val connectionSpecs = listOf(
ConnectionSpec.COMPATIBLE_TLS,
ConnectionSpec.CLEARTEXT
)
SslCompat(context, rawCertResources, connectionSpecs)
}

instance<Moshi> {
Moshi.Builder().build()
}
Expand Down Expand Up @@ -229,6 +253,7 @@ class DataModule : QuillModule() {

/* Analytics */
single<ActivityLaunchAnalytics>()
single<SslCompatAnalytics>()
single<AnalyticsProfileDataSource>()
single<AuthDeviceAnalytics>()
single<AuthMainAnalytics>()
Expand Down Expand Up @@ -261,6 +286,9 @@ class DataModule : QuillModule() {
single<AdsConfigApi>()
single<AdsConfigStorage>()
single<AdsConfigRepository>()

/* Player */
single<PlayerDataSourceProvider>()
}


Expand Down
Loading

0 comments on commit 8ce645d

Please sign in to comment.