Skip to content

Commit 61c6659

Browse files
authored
Merge pull request #736 from prebid/735-crash-for-mraid-ads
Run getGlobalVisibleRect on the main thread instead of a background thread
2 parents 7653e7f + cb27587 commit 61c6659

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

  • PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/rendering/views/webview/mraid

PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/rendering/views/webview/mraid/BaseJSInterface.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
import org.prebid.mobile.rendering.views.webview.WebViewBase;
5757

5858
import java.lang.ref.WeakReference;
59+
import java.util.concurrent.FutureTask;
60+
import java.util.concurrent.RunnableFuture;
5961

6062
@SuppressLint("NewApi")
6163
public class BaseJSInterface implements JSInterface {
@@ -119,9 +121,11 @@ public String getMaxSize() {
119121
JSONObject maxSize = new JSONObject();
120122
try {
121123
final Rect currentMaxSizeRect = screenMetrics.getCurrentMaxSizeRect();
122-
maxSize.put(JSON_WIDTH, currentMaxSizeRect.width());
123-
maxSize.put(JSON_HEIGHT, currentMaxSizeRect.height());
124-
return maxSize.toString();
124+
if (currentMaxSizeRect != null) {
125+
maxSize.put(JSON_WIDTH, currentMaxSizeRect.width());
126+
maxSize.put(JSON_HEIGHT, currentMaxSizeRect.height());
127+
return maxSize.toString();
128+
}
125129
}
126130
catch (Exception e) {
127131
LogUtil.error(TAG, "Failed getMaxSize() for MRAID: " + Log.getStackTraceString(e));
@@ -171,10 +175,12 @@ public String getDefaultPosition() {
171175
public String getCurrentPosition() {
172176
JSONObject position = new JSONObject();
173177
Rect rect = new Rect();
174-
175-
adBaseView.getGlobalVisibleRect(rect);
176-
178+
Handler mainHandler = new Handler(Looper.getMainLooper());
179+
Runnable mainThreadRunnable = () -> adBaseView.getGlobalVisibleRect(rect);
180+
RunnableFuture<Void> task = new FutureTask<>(mainThreadRunnable, null);
177181
try {
182+
mainHandler.post(task);
183+
task.get();
178184
position.put(JSON_X, (int) (rect.left / Utils.DENSITY));
179185
position.put(JSON_Y, (int) (rect.top / Utils.DENSITY));
180186
position.put(JSON_WIDTH, (int) (rect.right / Utils.DENSITY - rect.left / Utils.DENSITY));

0 commit comments

Comments
 (0)