Skip to content

Commit

Permalink
Merge pull request #736 from prebid/735-crash-for-mraid-ads
Browse files Browse the repository at this point in the history
Run getGlobalVisibleRect on the main thread instead of a background thread
  • Loading branch information
jsligh authored Jan 19, 2024
2 parents 7653e7f + cb27587 commit 61c6659
Showing 1 changed file with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
import org.prebid.mobile.rendering.views.webview.WebViewBase;

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

@SuppressLint("NewApi")
public class BaseJSInterface implements JSInterface {
Expand Down Expand Up @@ -119,9 +121,11 @@ public String getMaxSize() {
JSONObject maxSize = new JSONObject();
try {
final Rect currentMaxSizeRect = screenMetrics.getCurrentMaxSizeRect();
maxSize.put(JSON_WIDTH, currentMaxSizeRect.width());
maxSize.put(JSON_HEIGHT, currentMaxSizeRect.height());
return maxSize.toString();
if (currentMaxSizeRect != null) {
maxSize.put(JSON_WIDTH, currentMaxSizeRect.width());
maxSize.put(JSON_HEIGHT, currentMaxSizeRect.height());
return maxSize.toString();
}
}
catch (Exception e) {
LogUtil.error(TAG, "Failed getMaxSize() for MRAID: " + Log.getStackTraceString(e));
Expand Down Expand Up @@ -171,10 +175,12 @@ public String getDefaultPosition() {
public String getCurrentPosition() {
JSONObject position = new JSONObject();
Rect rect = new Rect();

adBaseView.getGlobalVisibleRect(rect);

Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable mainThreadRunnable = () -> adBaseView.getGlobalVisibleRect(rect);
RunnableFuture<Void> task = new FutureTask<>(mainThreadRunnable, null);
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 Down

0 comments on commit 61c6659

Please sign in to comment.