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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.prebid.mobile.rendering.views.webview.mraid.BannerJSInterface;
import org.prebid.mobile.rendering.views.webview.mraid.BaseJSInterface;
import org.prebid.mobile.rendering.views.webview.mraid.JsExecutor;
import org.prebid.mobile.rendering.views.webview.mraid.MainThreadJSInterface;

public class WebViewBanner extends WebViewBase {

Expand Down Expand Up @@ -62,7 +63,7 @@ public void setMRAIDInterface() {
new Handler(Looper.getMainLooper()),
new HandlerQueueManager()));

addJavascriptInterface(mraid, "jsBridge");
addJavascriptInterface(new MainThreadJSInterface(mraid), "jsBridge");
LogUtil.debug(TAG, "JS bridge initialized");
setBaseJSInterface(mraid);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.prebid.mobile.rendering.views.webview.mraid.BaseJSInterface;
import org.prebid.mobile.rendering.views.webview.mraid.InterstitialJSInterface;
import org.prebid.mobile.rendering.views.webview.mraid.JsExecutor;
import org.prebid.mobile.rendering.views.webview.mraid.MainThreadJSInterface;

public class WebViewInterstitial extends WebViewBase {

Expand All @@ -49,7 +50,7 @@ public void setMRAIDInterface() {
new Handler(Looper.getMainLooper()),
new HandlerQueueManager()));

addJavascriptInterface(mraid, "jsBridge");
addJavascriptInterface(new MainThreadJSInterface(mraid), "jsBridge");
LogUtil.debug(TAG, "JS bridge initialized");
setBaseJSInterface(mraid);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@
import org.prebid.mobile.rendering.views.webview.WebViewBase;

import java.lang.ref.WeakReference;
import java.util.concurrent.FutureTask;
import java.util.concurrent.RunnableFuture;

/**
* Handles MRAID calls on the main thread. WebView reaches it through {@link MainThreadJSInterface}.
*/
@SuppressLint("NewApi")
public class BaseJSInterface implements JSInterface {

Expand All @@ -71,7 +72,6 @@ public class BaseJSInterface implements JSInterface {
private final JsExecutor jsExecutor;
private final DeviceVolumeObserver deviceVolumeObserver;

private final MraidEvent mraidEvent = new MraidEvent();
private final MraidVariableContainer mraidVariableContainer = new MraidVariableContainer();

// An ad container, which contains the ad web view in default state, but is empty when expanded.
Expand All @@ -85,6 +85,8 @@ public class BaseJSInterface implements JSInterface {

private MraidOrientationBroadcastReceiver orientationBroadcastReceiver = new MraidOrientationBroadcastReceiver(this);

private boolean isDestroyed;


public BaseJSInterface(
Context context,
Expand Down Expand Up @@ -175,12 +177,10 @@ public String getDefaultPosition() {
public String getCurrentPosition() {
JSONObject position = new JSONObject();
Rect rect = new Rect();
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable mainThreadRunnable = () -> adBaseView.getGlobalVisibleRect(rect);
RunnableFuture<Void> task = new FutureTask<>(mainThreadRunnable, null);

adBaseView.getGlobalVisibleRect(rect);

try {
mainHandler.post(task);
task.get();
position.put(JSON_X, (int) (rect.left / Utils.DENSITY));
position.put(JSON_Y, (int) (rect.top / Utils.DENSITY));
position.put(JSON_WIDTH, (int) (rect.right / Utils.DENSITY - rect.left / Utils.DENSITY));
Expand All @@ -197,10 +197,8 @@ public String getCurrentPosition() {
@JavascriptInterface
public void onOrientationPropertiesChanged(String properties) {
mraidVariableContainer.setOrientationProperties(properties);
mraidEvent.mraidAction = ACTION_ORIENTATION_CHANGE;
mraidEvent.mraidActionHelper = properties;

notifyMraidEventHandler();
notifyMraidEventHandler(ACTION_ORIENTATION_CHANGE, properties);
}

@Override
Expand All @@ -213,8 +211,7 @@ public String getPlacementType() {
@Override
@JavascriptInterface
public void close() {
mraidEvent.mraidAction = ACTION_CLOSE;
notifyMraidEventHandler();
notifyMraidEventHandler(ACTION_CLOSE, null);
}

@Override
Expand All @@ -224,12 +221,10 @@ public void resize() {
// passing this off to the MRAIDResize facade
// trying to thin out this to make this
// refactorable for the future
mraidEvent.mraidAction = ACTION_RESIZE;

if (adBaseView.isMRAID() && orientationBroadcastReceiver != null && orientationBroadcastReceiver.isOrientationChanged()) {
updateScreenMetricsAsync(this::notifyMraidEventHandler);
updateScreenMetricsAsync(() -> notifyMraidEventHandler(ACTION_RESIZE, null));
} else {
notifyMraidEventHandler();
notifyMraidEventHandler(ACTION_RESIZE, null);
}

if (adBaseView.isMRAID() && orientationBroadcastReceiver != null) {
Expand All @@ -249,22 +244,16 @@ public void expand() {
public void expand(final String url) {
LogUtil.debug(TAG, "Expand with url: " + url);

mraidEvent.mraidAction = ACTION_EXPAND;
mraidEvent.mraidActionHelper = url;

//call creative's api that handles all mraid events
notifyMraidEventHandler();
notifyMraidEventHandler(ACTION_EXPAND, url);
}

@Override
@JavascriptInterface
public void open(String url) {
adBaseView.sendClickCallBack(url);

mraidEvent.mraidAction = ACTION_OPEN;
mraidEvent.mraidActionHelper = url;

notifyMraidEventHandler();
notifyMraidEventHandler(ACTION_OPEN, url);
}

@Override
Expand All @@ -289,21 +278,15 @@ public void javaScriptCallback(String handlerHash, String method, String value)
public void createCalendarEvent(String parameters) {
adBaseView.sendClickCallBack(parameters);

mraidEvent.mraidAction = ACTION_CREATE_CALENDAR_EVENT;
mraidEvent.mraidActionHelper = parameters;

notifyMraidEventHandler();
notifyMraidEventHandler(ACTION_CREATE_CALENDAR_EVENT, parameters);
}

@Override
@JavascriptInterface
public void storePicture(String url) {
adBaseView.sendClickCallBack(url);

mraidEvent.mraidAction = ACTION_STORE_PICTURE;
mraidEvent.mraidActionHelper = url;

notifyMraidEventHandler();
notifyMraidEventHandler(ACTION_STORE_PICTURE, url);
}

@Override
Expand All @@ -315,10 +298,7 @@ public boolean supports(String feature) {
@Override
@JavascriptInterface
public void playVideo(String url) {
mraidEvent.mraidAction = ACTION_PLAY_VIDEO;
mraidEvent.mraidActionHelper = url;

notifyMraidEventHandler();
notifyMraidEventHandler(ACTION_PLAY_VIDEO, url);
}

@Override
Expand Down Expand Up @@ -379,8 +359,7 @@ public String getCurrentAppOrientation() {
@JavascriptInterface
public void unload() {
LogUtil.debug(TAG, "unload called");
mraidEvent.mraidAction = ACTION_UNLOAD;
notifyMraidEventHandler();
notifyMraidEventHandler(ACTION_UNLOAD, null);
}

public void onStateChange(String state) {
Expand Down Expand Up @@ -449,6 +428,10 @@ public JsExecutor getJsExecutor() {
return jsExecutor;
}

boolean isDestroyed() {
return isDestroyed;
}

public void loading() {
jsExecutor.loading();
}
Expand Down Expand Up @@ -541,6 +524,7 @@ public void updateScreenMetricsAsync(
}

public void destroy() {
isDestroyed = true;
screenMetricsWaiter.cancelPendingRequests();
orientationBroadcastReceiver.unregister();
deviceVolumeObserver.stop();
Expand Down Expand Up @@ -568,8 +552,13 @@ protected void notifyScreenMetricsChanged() {
jsExecutor.executeOnSizeChange(screenMetrics.getCurrentAdRect());
}

private void notifyMraidEventHandler() {
orientationBroadcastReceiver.setMraidAction(mraidEvent.mraidAction);
private void notifyMraidEventHandler(String action, @Nullable String actionHelper) {
// A new event per command: MRAID handlers may read it after the next command has arrived
MraidEvent mraidEvent = new MraidEvent();
mraidEvent.mraidAction = action;
mraidEvent.mraidActionHelper = actionHelper;

orientationBroadcastReceiver.setMraidAction(action);
HTMLCreative htmlCreative = ((PrebidWebViewBase) adBaseView.getPreloadedListener()).getCreative();
adBaseView.post(new MraidEventHandlerNotifierRunnable(htmlCreative, adBaseView, mraidEvent, jsExecutor));
}
Expand Down
Loading
Loading