Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.1.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed

- Fixed an issue on Android where the Ads API would sometimes return an empty map instead of an array when querying the list of current ads or scheduled ad breaks.

## [10.12.1] - 26-03-19

### Fixed
Expand Down
74 changes: 23 additions & 51 deletions android/src/main/java/com/theoplayer/ads/AdsModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ private const val PROP_OMID_VIEW = "view"
private const val PROP_OMID_PURPOSE = "purpose"
private const val PROP_OMID_REASON = "reason"

private const val ERR_SCHEDULE_AD = "Error scheduling ad"

@Suppress("unused")
@ReactModule(name = AdsModule.NAME)
class AdsModule(context: ReactApplicationContext) : ReactContextBaseJavaModule(context) {
Expand All @@ -42,12 +44,10 @@ class AdsModule(context: ReactApplicationContext) : ReactContextBaseJavaModule(c
@ReactMethod
fun schedule(tag: Int, ad: ReadableMap) {
viewResolver.resolveViewByTag(tag) { view: ReactTHEOplayerView? ->
if (view != null) {
try {
view.adsApi.schedule(sourceHelper.parseAdFromJS(ad))
} catch (exception: THEOplayerException) {
Log.e(NAME, exception.message!!)
}
try {
view?.adsApi?.schedule(sourceHelper.parseAdFromJS(ad))
} catch (exception: THEOplayerException) {
Log.e(NAME, exception.message ?: ERR_SCHEDULE_AD)
}
}
}
Expand All @@ -56,52 +56,36 @@ class AdsModule(context: ReactApplicationContext) : ReactContextBaseJavaModule(c
@ReactMethod
fun currentAdBreak(tag: Int, promise: Promise) {
viewResolver.resolveViewByTag(tag) { view: ReactTHEOplayerView? ->
if (view == null) {
promise.resolve(Arguments.createMap())
} else {
promise.resolve(AdAdapter.fromAdBreak(view.adsApi.currentAdBreak))
}
promise.resolve(if (view == null) Arguments.createMap() else AdAdapter.fromAdBreak(view.adsApi.currentAdBreak))
}
}

// List of currently playing ads.
@ReactMethod
fun currentAds(tag: Int, promise: Promise) {
viewResolver.resolveViewByTag(tag) { view: ReactTHEOplayerView? ->
if (view == null) {
promise.resolve(Arguments.createMap())
} else {
promise.resolve(AdAdapter.fromAds(view.adsApi.currentAds))
}
promise.resolve(if (view == null) Arguments.createArray() else AdAdapter.fromAds(view.adsApi.currentAds))
}
}

// List of ad breaks which still need to be played.
@ReactMethod
fun scheduledAdBreaks(tag: Int, promise: Promise) {
viewResolver.resolveViewByTag(tag) { view: ReactTHEOplayerView? ->
if (view == null) {
promise.resolve(Arguments.createMap())
} else {
promise.resolve(AdAdapter.fromAdBreaks(view.adsApi.scheduledAdBreaks))
}
promise.resolve(if (view == null) Arguments.createArray() else AdAdapter.fromAdBreaks(view.adsApi.scheduledAdBreaks))
}
}

// Whether a linear ad is currently playing.
@ReactMethod
fun playing(tag: Int, promise: Promise) {
viewResolver.resolveViewByTag(tag) { view: ReactTHEOplayerView? ->
if (view == null) {
promise.resolve(false)
} else {
promise.resolve(view.adsApi.isPlaying)
}
promise.resolve(view?.adsApi?.isPlaying ?: false)
}
}

// Skip the current linear ad.
// NOTE: This will have no effect when the current linear ad is (not yet) skippable.
// NOTE: This will have no effect when the current linear ad is not (yet) skippable.
@ReactMethod
fun skip(tag: Int) {
viewResolver.resolveViewByTag(tag) { view: ReactTHEOplayerView? -> view?.adsApi?.skip() }
Expand All @@ -110,46 +94,36 @@ class AdsModule(context: ReactApplicationContext) : ReactContextBaseJavaModule(c
@ReactMethod
fun daiSnapback(tag: Int, promise: Promise) {
viewResolver.resolveViewByTag(tag) { view: ReactTHEOplayerView? ->
val daiIntegration = view?.playerContext?.daiIntegration
if (daiIntegration == null) {
promise.resolve(false)
} else {
promise.resolve(daiIntegration.enableSnapback)
}
promise.resolve(view?.playerContext?.daiIntegration?.enableSnapback ?: false)
}
}

@ReactMethod
fun daiSetSnapback(tag: Int, enabled: Boolean?) {
viewResolver.resolveViewByTag(tag) { view: ReactTHEOplayerView? ->
val daiIntegration = view?.playerContext?.daiIntegration
if (daiIntegration != null) {
daiIntegration.enableSnapback = enabled!!
view?.playerContext?.daiIntegration?.let { integration ->
integration.enableSnapback = enabled ?: false
}
}
}

@ReactMethod
fun daiContentTimeForStreamTime(tag: Int, time: Int, promise: Promise) {
viewResolver.resolveViewByTag(tag) { view: ReactTHEOplayerView? ->
val daiIntegration = view?.playerContext?.daiIntegration
if (daiIntegration == null) {
promise.resolve(time)
} else {
promise.resolve((1e03 * daiIntegration.contentTimeForStreamTime(1e-03 * time)).toInt())
}
val contentTime = view?.playerContext?.daiIntegration?.let { integration ->
(1e03 * integration.contentTimeForStreamTime(1e-03 * time)).toInt()
} ?: time
promise.resolve(contentTime)
}
}

@ReactMethod
fun daiStreamTimeForContentTime(tag: Int, time: Int, promise: Promise) {
viewResolver.resolveViewByTag(tag) { view: ReactTHEOplayerView? ->
val daiIntegration = view?.playerContext?.daiIntegration
if (daiIntegration == null) {
promise.resolve(time)
} else {
promise.resolve((1e03 * daiIntegration.streamTimeForContentTime(1e-03 * time)).toInt())
}
val streamTime = view?.playerContext?.daiIntegration?.let { integration ->
(1e03 * integration.streamTimeForContentTime(1e-03 * time)).toInt()
} ?: time
promise.resolve(streamTime)
}
}

Expand Down Expand Up @@ -181,9 +155,7 @@ class AdsModule(context: ReactApplicationContext) : ReactContextBaseJavaModule(c
purpose: OmidFriendlyObstructionPurpose?,
reason: String?
) {
if (obsView == null || purpose == null) {
return
}
if (obsView == null || purpose == null) return
viewResolver.resolveViewByTag(tag) { view: ReactTHEOplayerView? ->
view?.player?.ads?.omid?.addFriendlyObstruction(
OmidFriendlyObstruction(obsView, purpose, reason)
Expand Down
Loading