Skip to content

Commit

Permalink
Workaround the skip ad button not being focused
Browse files Browse the repository at this point in the history
Issue: #3258

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=168669969
  • Loading branch information
andrewlewis authored and ojw28 committed Sep 19, 2017
1 parent b34aad8 commit 3fc9d34
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.os.SystemClock;
import android.util.Log;
import android.view.ViewGroup;
import android.webkit.WebView;
import com.google.ads.interactivemedia.v3.api.Ad;
import com.google.ads.interactivemedia.v3.api.AdDisplayContainer;
import com.google.ads.interactivemedia.v3.api.AdErrorEvent;
Expand Down Expand Up @@ -112,6 +113,14 @@ public final class ImaAdsLoader implements Player.EventListener, VideoAdPlayer,
*/
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 Timeline.Period period;
private final List<VideoAdPlayerCallback> adCallbacks;
Expand All @@ -121,6 +130,7 @@ public final class ImaAdsLoader implements Player.EventListener, VideoAdPlayer,

private EventListener eventListener;
private Player player;
private ViewGroup adUiViewGroup;
private VideoProgressUpdate lastContentProgress;
private VideoProgressUpdate lastAdProgress;

Expand Down Expand Up @@ -249,6 +259,7 @@ public ImaAdsLoader(Context context, Uri adTagUri, ImaSdkSettings imaSdkSettings
ViewGroup adUiViewGroup) {
this.player = player;
this.eventListener = eventListener;
this.adUiViewGroup = adUiViewGroup;
lastAdProgress = null;
lastContentProgress = null;
adDisplayContainer.setAdContainer(adUiViewGroup);
Expand Down Expand Up @@ -278,6 +289,7 @@ public ImaAdsLoader(Context context, Uri adTagUri, ImaSdkSettings imaSdkSettings
player.removeListener(this);
player = null;
eventListener = null;
adUiViewGroup = null;
}

/**
Expand Down Expand Up @@ -363,6 +375,11 @@ public void onAdEvent(AdEvent adEvent) {
imaPausedContent = true;
pauseContentInternal();
break;
case STARTED:
if (ad.isSkippable()) {
focusSkipButton();
}
break;
case TAPPED:
if (eventListener != null) {
eventListener.onAdTapped();
Expand Down Expand Up @@ -727,4 +744,13 @@ private static long[] getAdGroupTimesUs(List<Float> cuePoints) {
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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,13 @@ public void setUseController(boolean useController) {

@Override
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);
return dispatchMediaKeyEvent(event) || super.dispatchKeyEvent(event);
}
Expand Down

0 comments on commit 3fc9d34

Please sign in to comment.