Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NetworkOnMainThreadException Fix #734

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
15763ed
Found two places where the reference to the WebView was being held on…
jsligh Aug 23, 2023
3048888
Found two places where the reference to the WebView was being held on…
jsligh Aug 23, 2023
8b6533c
Merge branch '673-memory-leak-detected' of https://github.com/prebid/…
jsligh Jan 11, 2024
94adfba
Added the networking task cancellation/destruction to a background th…
jsligh Jan 11, 2024
c739139
Run getGlobalVisibleRect on the main thread instead of a background t…
jsligh Jan 17, 2024
875aa22
Cleaning up mraidWebView as well just to make sure.
jsligh Jan 18, 2024
ea55c56
Crash that does keep occurring in my testing.
jsligh Jan 18, 2024
50d4c66
Fixed to run synchronously.
jsligh Jan 18, 2024
cb27587
Forgot important step.
jsligh Jan 18, 2024
61c6659
Merge pull request #736 from prebid/735-crash-for-mraid-ads
jsligh Jan 19, 2024
12acc24
Merge pull request #733 from prebid/673-memory-leak-detected
jsligh Jan 19, 2024
a368528
2.2.0 changes
jsligh Jan 19, 2024
b4ea7c4
change readme
jsligh Jan 19, 2024
bbcf34e
Merge pull request #737 from prebid/feature/release-2.2.0
jsligh Jan 19, 2024
60d7438
removing the redundant .destroy() call first.
jsligh Jan 30, 2024
214bf16
Added implementation and started writing tests.
jsligh Feb 20, 2024
ae74ef2
Finished up the tests.
jsligh Feb 21, 2024
40d9edb
Forgot to add section about arrays.
jsligh Feb 21, 2024
7e39516
removing the .destroy() call
jsligh Feb 27, 2024
a6a1320
fix: Pin Google Play Services Ads to 22.6.0
shinwan2 Mar 11, 2024
96cf676
Merge pull request #741 from shinwan2/fix/demo-app-build-failure
jsligh Mar 22, 2024
6f1da30
Merge pull request #738 from prebid/877-arbitrary-ortb-params
jsligh Mar 29, 2024
27fa1c9
Made requested changes.
jsligh Apr 9, 2024
e7ef5b5
Added the networking task cancellation/destruction to a background th…
jsligh Jan 11, 2024
6f80558
removing the redundant .destroy() call first.
jsligh Jan 30, 2024
26947c7
removing the .destroy() call
jsligh Feb 27, 2024
be701b2
Made requested changes.
jsligh Apr 9, 2024
cccefa7
Merge branch '678-2-networkonmainthreadexception-when-destroying-medi…
jsligh Apr 15, 2024
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
2 changes: 1 addition & 1 deletion Example/PrebidDemoJava/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'

// Ads
implementation 'com.google.android.gms:play-services-ads:+'
implementation libs.google.play.services.ads

// Video Player
implementation 'com.google.android.exoplayer:exoplayer:2.15.1'
Expand Down
2 changes: 1 addition & 1 deletion Example/PrebidDemoKotlin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ dependencies {
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'

// Advertisement
implementation 'com.google.android.gms:play-services-ads:+'
implementation libs.google.play.services.ads
implementation "com.applovin:applovin-sdk:11.5.3"
implementation "com.google.android.gms:play-services-ads-identifier:18.0.1" // For Applovin Max

Expand Down
2 changes: 1 addition & 1 deletion Example/PrebidInternalTestApp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ dependencies {

// Ad dependencies
implementation 'com.google.android.gms:play-services-base:18.1.0'
implementation 'com.google.android.gms:play-services-ads:+'
implementation libs.google.play.services.ads
implementation "com.applovin:applovin-sdk:11.3.2"
implementation "com.google.android.gms:play-services-ads-identifier:18.0.1" // For Applovin Max

Expand Down
2 changes: 1 addition & 1 deletion PrebidMobile/PrebidMobile-admobAdapters/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ android {

dependencies {
implementation project(":PrebidMobile")
implementation 'com.google.android.gms:play-services-ads:+'
implementation libs.google.play.services.ads
}
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,11 @@ public void setGpid(@Nullable String gpid) {
configuration.setGpid(gpid);
}

@Nullable
public String getOrtbConfig() { return configuration.getOrtbConfig();}

public void setOrtbConfig(@Nullable String ortbConfig) {configuration.setOrtbConfig(ortbConfig);}

protected BidRequesterListener createBidListener(OnCompleteListener originalListener) {
return new BidRequesterListener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.prebid.mobile;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.prebid.mobile.api.data.AdFormat;
import org.prebid.mobile.api.data.AdUnitFormat;
Expand Down Expand Up @@ -52,4 +53,13 @@ HashSet<AdSize> getSizes() {
return configuration.getSizes();
}

@Nullable
public String getOrtbConfig() {
return configuration.getOrtbConfig();
}

public void setOrtbConfig(@Nullable String ortbConfig) {
configuration.setOrtbConfig(ortbConfig);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ public Parameters getParameters() {
return null;
}

@Nullable
public String getOrtbConfig() {
return configuration.getOrtbConfig();
}

public void setOrtbConfig(@Nullable String ortbConfig) {
configuration.setOrtbConfig(ortbConfig);
}

/**
* Describes an <a href="https://www.iab.com/wp-content/uploads/2016/03/OpenRTB-API-Specification-Version-2-5-FINAL.pdf">OpenRTB</a> banner object
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.prebid.mobile;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.prebid.mobile.api.data.AdFormat;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.prebid.mobile.api.data.AdFormat;
import org.prebid.mobile.api.data.AdUnitFormat;
Expand Down Expand Up @@ -59,4 +60,13 @@ public void setMinSizePercentage(
configuration.setMinSizePercentage(new AdSize(width, height));
}

@Nullable
public String getOrtbConfig() {
return configuration.getOrtbConfig();
}

public void setOrtbConfig(@Nullable String ortbConfig) {
configuration.setOrtbConfig(ortbConfig);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.prebid.mobile;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;

import org.json.JSONObject;
Expand Down Expand Up @@ -212,4 +213,10 @@ public NativeAdUnitConfiguration getNativeConfiguration() {
return nativeConfiguration;
}

@Nullable
public String getOrtbConfig() {return configuration.getOrtbConfig();}

public void setOrtbConfig(@Nullable String ortbConfig) {configuration.setOrtbConfig(ortbConfig);}


}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public void setVideoParameters(@Nullable VideoParameters parameters) {
configuration.setVideoParameters(parameters);
}

@Nullable
public String getOrtbConfig() { return configuration.getOrtbConfig(); }

public void setOrtbConfig(@Nullable String ortbConfig) { configuration.setOrtbConfig(ortbConfig);}

/**
* @deprecated use `setVideoParameters()`
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,13 @@ private void cancelRefresh() {
}
}

@Nullable
public String getOrtbConfig() {
return adUnitConfig.getOrtbConfig();
}

public void setOrtbConfig(@Nullable String ortbConfig) {
adUnitConfig.setOrtbConfig(ortbConfig);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,15 @@ public BidResponse getBidResponse() {
return bidResponse;
}

@Nullable
public String getOrtbConfig() {
return adUnitConfig.getOrtbConfig();
}

public void setOrtbConfig(@Nullable String ortbConfig) {
adUnitConfig.setOrtbConfig(ortbConfig);
}

//region ==================== HelperMethods for Unit Tests. Should be used only in tests
@VisibleForTesting
final void setBidResponse(BidResponse response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,15 @@ public void clearUserData() {
adUnitConfig.clearUserData();
}

@Nullable
public String getOrtbConfig() {
return adUnitConfig.getOrtbConfig();
}

public void setOrtbConfig(@Nullable String ortbConfig) {
adUnitConfig.setOrtbConfig(ortbConfig);
}


@Nullable
public String getPbAdSlot() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.prebid.mobile.rendering.bidding.listeners.DisplayVideoListener;
import org.prebid.mobile.api.rendering.pluginrenderer.PrebidMobilePluginRegister;
Expand Down Expand Up @@ -86,4 +87,13 @@ public void destroy() {
displayViewListener = null;
displayVideoListener = null;
}

@Nullable
public String getOrtbConfig() {
return adUnitConfiguration.getOrtbConfig();
}

public void setOrtbConfig(@Nullable String ortbConfig) {
adUnitConfiguration.setOrtbConfig(ortbConfig);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,13 @@ private static BidResponse getBidResponseFromCache(String id) throws AdException
return cachedResponse;
}

@Nullable
public String getOrtbConfig() {
return adUnitConfiguration.getOrtbConfig();
}

public void setOrtbConfig(@Nullable String ortbConfig) {
adUnitConfiguration.setOrtbConfig(ortbConfig);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public class AdUnitConfiguration {
private String fingerprint = Utils.generateUUIDTimeBased();
@Nullable
private String gpid;
@Nullable
private String ortbConfig;

private Position closeButtonPosition = Position.TOP_RIGHT;
private Position skipButtonPosition = Position.TOP_RIGHT;
Expand Down Expand Up @@ -540,6 +542,15 @@ public void setGpid(@Nullable String gpid) {
this.gpid = gpid;
}

@Nullable
public String getOrtbConfig() {
return ortbConfig;
}

public void setOrtbConfig(@Nullable String ortbConfig) {
this.ortbConfig = ortbConfig;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@

import android.text.TextUtils;

import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.prebid.mobile.LogUtil;
import org.prebid.mobile.PrebidMobile;
import org.prebid.mobile.rendering.models.openrtb.bidRequests.*;
import org.prebid.mobile.rendering.models.openrtb.bidRequests.source.Source;

import java.util.ArrayList;
import java.util.Iterator;

public class BidRequest extends BaseBid {

Expand All @@ -37,12 +40,17 @@ public class BidRequest extends BaseBid {
private Regs regs = null;
private User user = null;
private Source source = null;

@Nullable
private JSONObject arbitraryJSONConfig;
@Nullable
private String ortbConfig;
private Ext ext = null;

public JSONObject getJsonObject() throws JSONException {
JSONObject jsonObject = new JSONObject();

if (arbitraryJSONConfig != null) {
deepMerge(arbitraryJSONConfig, jsonObject);
}
if (imps != null && imps.size() > 0) {

JSONArray jsonArray = new JSONArray();
Expand All @@ -62,10 +70,71 @@ public JSONObject getJsonObject() throws JSONException {
toJSON(jsonObject, "source", source != null ? source.getJsonObject() : null);
toJSON(jsonObject, "ext", ext != null ? ext.getJsonObject() : null);
toJSON(jsonObject, "test", PrebidMobile.getPbsDebug() ? 1 : null);

jsonObject = mergeOrtbConfig(jsonObject);
return jsonObject;
}

private JSONObject mergeOrtbConfig(JSONObject bidRequestJson) {
try {
if (ortbConfig == null) {
return bidRequestJson;
}
JSONObject ortbConfigObject = new JSONObject(ortbConfig);
//remove protected fields
if (ortbConfigObject.has("regs")) {
ortbConfigObject.remove("regs");
}
if (ortbConfigObject.has("device")) {
ortbConfigObject.remove("device");
}
if (ortbConfigObject.has("geo")) {
ortbConfigObject.remove("geo");
}
if (ortbConfigObject.has("ext")) {
if (ortbConfigObject.get("ext") instanceof JSONObject) {
ortbConfigObject.getJSONObject("ext").remove("gdpr");
ortbConfigObject.getJSONObject("ext").remove("us_privacy");
ortbConfigObject.getJSONObject("ext").remove("consent");
}
}
return deepMerge(ortbConfigObject, bidRequestJson);
} catch(Exception e) {
LogUtil.error("ORTBConfig is not valid JSON");
return bidRequestJson;
}
}

/**
* Merge "source" into "target". If fields have equal name, merge them recursively.
* @return the merged object (target).
*/
private static JSONObject deepMerge(JSONObject source, JSONObject target) throws JSONException {
for (Iterator<String> it = source.keys(); it.hasNext(); ) {
String key = it.next();
Object value = source.get(key);
if (!target.has(key)) {
// new value for "key":
target.put(key, value);
} else {
// existing value for "key" - recursively deep merge:
if (value instanceof JSONObject) {
JSONObject valueJson = (JSONObject) value;
deepMerge(valueJson, target.getJSONObject(key));
} else if (value instanceof JSONArray) {
if (target.get(key) instanceof JSONArray) {
JSONArray sourceArray = (JSONArray) value;
for (int i = 0; i < sourceArray.length(); i++) {
target.getJSONArray(key).put(sourceArray.getJSONObject(i));
}
}
} else {
target.put(key, value);
}
}
}
return target;
}

// Accessors to prevent NPE while maintaining null if object is not set

// App
Expand Down Expand Up @@ -156,4 +225,22 @@ public Ext getExt() {
}
return ext;
}

@Nullable
public JSONObject getArbitraryConfig() {
return arbitraryJSONConfig;
}

public void setArbitraryConfig(@Nullable JSONObject config) {
this.arbitraryJSONConfig = config;
}

@Nullable
public String getOrtbConfig() {
return ortbConfig;
}

public void setOrtbConfig(@Nullable String ortbConfig) {
this.ortbConfig = ortbConfig;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public void destroy() {
if (expandedDialog != null) {
expandedDialog.dismiss();
}
webViewBanner = null;
}

/**
Expand Down
Loading