mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +00:00
Workaround the skip ad button not being focused
Issue: #3258 ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=168669969
This commit is contained in:
parent
b3004ab1c3
commit
f2aed7186e
2 changed files with 33 additions and 0 deletions
|
|
@ -20,6 +20,7 @@ import android.net.Uri;
|
||||||
import android.os.SystemClock;
|
import android.os.SystemClock;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
import android.webkit.WebView;
|
||||||
import com.google.ads.interactivemedia.v3.api.Ad;
|
import com.google.ads.interactivemedia.v3.api.Ad;
|
||||||
import com.google.ads.interactivemedia.v3.api.AdDisplayContainer;
|
import com.google.ads.interactivemedia.v3.api.AdDisplayContainer;
|
||||||
import com.google.ads.interactivemedia.v3.api.AdErrorEvent;
|
import com.google.ads.interactivemedia.v3.api.AdErrorEvent;
|
||||||
|
|
@ -112,6 +113,14 @@ public final class ImaAdsLoader implements Player.EventListener, VideoAdPlayer,
|
||||||
*/
|
*/
|
||||||
private static final long END_OF_CONTENT_POSITION_THRESHOLD_MS = 5000;
|
private static final long END_OF_CONTENT_POSITION_THRESHOLD_MS = 5000;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The "Skip ad" button rendered in the IMA WebView does not gain focus by default and cannot be
|
||||||
|
* clicked via a keypress event. Workaround this issue by calling focus() on the HTML element in
|
||||||
|
* the WebView directly when an ad starts. See [Internal: b/62371030].
|
||||||
|
*/
|
||||||
|
private static final String FOCUS_SKIP_BUTTON_WORKAROUND_JS = "javascript:"
|
||||||
|
+ "try{ document.getElementsByClassName(\"videoAdUiSkipButton\")[0].focus(); } catch (e) {}";
|
||||||
|
|
||||||
private final Uri adTagUri;
|
private final Uri adTagUri;
|
||||||
private final Timeline.Period period;
|
private final Timeline.Period period;
|
||||||
private final List<VideoAdPlayerCallback> adCallbacks;
|
private final List<VideoAdPlayerCallback> adCallbacks;
|
||||||
|
|
@ -121,6 +130,7 @@ public final class ImaAdsLoader implements Player.EventListener, VideoAdPlayer,
|
||||||
|
|
||||||
private EventListener eventListener;
|
private EventListener eventListener;
|
||||||
private Player player;
|
private Player player;
|
||||||
|
private ViewGroup adUiViewGroup;
|
||||||
private VideoProgressUpdate lastContentProgress;
|
private VideoProgressUpdate lastContentProgress;
|
||||||
private VideoProgressUpdate lastAdProgress;
|
private VideoProgressUpdate lastAdProgress;
|
||||||
|
|
||||||
|
|
@ -249,6 +259,7 @@ public final class ImaAdsLoader implements Player.EventListener, VideoAdPlayer,
|
||||||
ViewGroup adUiViewGroup) {
|
ViewGroup adUiViewGroup) {
|
||||||
this.player = player;
|
this.player = player;
|
||||||
this.eventListener = eventListener;
|
this.eventListener = eventListener;
|
||||||
|
this.adUiViewGroup = adUiViewGroup;
|
||||||
lastAdProgress = null;
|
lastAdProgress = null;
|
||||||
lastContentProgress = null;
|
lastContentProgress = null;
|
||||||
adDisplayContainer.setAdContainer(adUiViewGroup);
|
adDisplayContainer.setAdContainer(adUiViewGroup);
|
||||||
|
|
@ -278,6 +289,7 @@ public final class ImaAdsLoader implements Player.EventListener, VideoAdPlayer,
|
||||||
player.removeListener(this);
|
player.removeListener(this);
|
||||||
player = null;
|
player = null;
|
||||||
eventListener = null;
|
eventListener = null;
|
||||||
|
adUiViewGroup = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -363,6 +375,11 @@ public final class ImaAdsLoader implements Player.EventListener, VideoAdPlayer,
|
||||||
imaPausedContent = true;
|
imaPausedContent = true;
|
||||||
pauseContentInternal();
|
pauseContentInternal();
|
||||||
break;
|
break;
|
||||||
|
case STARTED:
|
||||||
|
if (ad.isSkippable()) {
|
||||||
|
focusSkipButton();
|
||||||
|
}
|
||||||
|
break;
|
||||||
case TAPPED:
|
case TAPPED:
|
||||||
if (eventListener != null) {
|
if (eventListener != null) {
|
||||||
eventListener.onAdTapped();
|
eventListener.onAdTapped();
|
||||||
|
|
@ -732,4 +749,13 @@ public final class ImaAdsLoader implements Player.EventListener, VideoAdPlayer,
|
||||||
return adGroupTimesUs;
|
return adGroupTimesUs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void focusSkipButton() {
|
||||||
|
if (playingAd && adUiViewGroup != null && adUiViewGroup.getChildCount() > 0
|
||||||
|
&& adUiViewGroup.getChildAt(0) instanceof WebView) {
|
||||||
|
WebView webView = (WebView) (adUiViewGroup.getChildAt(0));
|
||||||
|
webView.requestFocus();
|
||||||
|
webView.loadUrl(FOCUS_SKIP_BUTTON_WORKAROUND_JS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -516,6 +516,13 @@ public final class SimpleExoPlayerView extends FrameLayout {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean dispatchKeyEvent(KeyEvent event) {
|
public boolean dispatchKeyEvent(KeyEvent event) {
|
||||||
|
if (player != null && player.isPlayingAd()) {
|
||||||
|
// Focus any overlay UI now, in case it's provided by a WebView whose contents may update
|
||||||
|
// dynamically. This is needed to make the "Skip ad" button focused on Android TV when using
|
||||||
|
// IMA [Internal: b/62371030].
|
||||||
|
overlayFrameLayout.requestFocus();
|
||||||
|
return super.dispatchKeyEvent(event);
|
||||||
|
}
|
||||||
maybeShowController(true);
|
maybeShowController(true);
|
||||||
return dispatchMediaKeyEvent(event) || super.dispatchKeyEvent(event);
|
return dispatchMediaKeyEvent(event) || super.dispatchKeyEvent(event);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue