@24jieqi/react-native-brayant-ad 0.1.26 → 0.1.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -128,12 +128,16 @@ public class BannerAdModule extends ReactContextBaseJavaModule {
|
|
|
128
128
|
* @return 缓存的广告,如果没有则返回 null
|
|
129
129
|
*/
|
|
130
130
|
public static TTNativeExpressAd getCachedBannerAd(String codeId) {
|
|
131
|
+
Log.d(TAG, "getCachedBannerAd called with codeId: " + codeId + ", cachedCodeId: " + bannerAdCacheCodeId);
|
|
132
|
+
Log.d(TAG, "cache status - cache null: " + (bannerAdCache == null) + ", codeId null: " + (bannerAdCacheCodeId == null));
|
|
131
133
|
if (hasValidCache(codeId)) {
|
|
134
|
+
Log.d(TAG, "getCachedBannerAd - cache hit!");
|
|
132
135
|
TTNativeExpressAd cachedAd = bannerAdCache;
|
|
133
136
|
// 使用后清除缓存,避免重复使用
|
|
134
137
|
clearCache();
|
|
135
138
|
return cachedAd;
|
|
136
139
|
}
|
|
140
|
+
Log.d(TAG, "getCachedBannerAd - cache miss");
|
|
137
141
|
return null;
|
|
138
142
|
}
|
|
139
143
|
|
|
@@ -3,6 +3,7 @@ package com.brayantad.dy.banner.view;
|
|
|
3
3
|
import static com.facebook.react.bridge.UiThreadUtil.runOnUiThread;
|
|
4
4
|
|
|
5
5
|
import android.app.Activity;
|
|
6
|
+
import android.util.Log;
|
|
6
7
|
import android.view.View;
|
|
7
8
|
import android.view.ViewGroup;
|
|
8
9
|
import android.widget.RelativeLayout;
|
|
@@ -21,6 +22,7 @@ import com.facebook.react.bridge.WritableMap;
|
|
|
21
22
|
import com.facebook.react.uimanager.events.RCTEventEmitter;
|
|
22
23
|
|
|
23
24
|
public class BannerAdView extends RelativeLayout {
|
|
25
|
+
public static final String TAG = "BannerAdView";
|
|
24
26
|
private Activity mActivity;
|
|
25
27
|
private ReactContext mReactContext;
|
|
26
28
|
private String mCodeId;
|
|
@@ -33,6 +35,7 @@ public class BannerAdView extends RelativeLayout {
|
|
|
33
35
|
|
|
34
36
|
public BannerAdView(ReactContext context) {
|
|
35
37
|
super(context);
|
|
38
|
+
Log.d(TAG, "BannerAdView constructor called");
|
|
36
39
|
mReactContext = context;
|
|
37
40
|
mActivity = context.getCurrentActivity();
|
|
38
41
|
|
|
@@ -46,15 +49,18 @@ public class BannerAdView extends RelativeLayout {
|
|
|
46
49
|
mExpectedHeight
|
|
47
50
|
);
|
|
48
51
|
setLayoutParams(params);
|
|
52
|
+
Log.d(TAG, "BannerAdView constructor completed");
|
|
49
53
|
}
|
|
50
54
|
|
|
51
55
|
public void setWidth(int width) {
|
|
52
56
|
mExpectedWidth = width;
|
|
57
|
+
Log.d(TAG, "setWidth called: " + width + ", current state - width:" + mExpectedWidth + " height:" + mExpectedHeight + " codeId:" + mCodeId);
|
|
53
58
|
showAd();
|
|
54
59
|
}
|
|
55
60
|
|
|
56
61
|
public void setHeight(int height) {
|
|
57
62
|
mExpectedHeight = height;
|
|
63
|
+
Log.d(TAG, "setHeight called: " + height + ", current state - width:" + mExpectedWidth + " height:" + mExpectedHeight + " codeId:" + mCodeId);
|
|
58
64
|
|
|
59
65
|
ViewGroup.LayoutParams params = getLayoutParams();
|
|
60
66
|
if (params != null) {
|
|
@@ -67,6 +73,7 @@ public class BannerAdView extends RelativeLayout {
|
|
|
67
73
|
|
|
68
74
|
public void setCodeId(String codeId) {
|
|
69
75
|
mCodeId = codeId;
|
|
76
|
+
Log.d(TAG, "setCodeId called: " + codeId + ", current state - width:" + mExpectedWidth + " height:" + mExpectedHeight + " codeId:" + mCodeId);
|
|
70
77
|
showAd();
|
|
71
78
|
}
|
|
72
79
|
|
|
@@ -75,6 +82,7 @@ public class BannerAdView extends RelativeLayout {
|
|
|
75
82
|
* @param visible true: 可见,false: 不可见
|
|
76
83
|
*/
|
|
77
84
|
public void setVisibility(boolean visible) {
|
|
85
|
+
Log.d(TAG, "setVisibility called: " + visible + ", current state - width:" + mExpectedWidth + " height:" + mExpectedHeight + " codeId:" + mCodeId);
|
|
78
86
|
if (visible) {
|
|
79
87
|
// 触发加载,但暂不显示(等待渲染成功)
|
|
80
88
|
showAd();
|
|
@@ -84,34 +92,41 @@ public class BannerAdView extends RelativeLayout {
|
|
|
84
92
|
}
|
|
85
93
|
|
|
86
94
|
public void showAd() {
|
|
95
|
+
Log.d(TAG, "showAd called - width:" + mExpectedWidth + " height:" + mExpectedHeight + " codeId:" + mCodeId + " isLoading:" + mIsAdLoading);
|
|
87
96
|
// 参数校验
|
|
88
97
|
if (mExpectedWidth <= 0 || mExpectedHeight <= 0 || mCodeId == null || mCodeId.isEmpty()) {
|
|
98
|
+
Log.d(TAG, "showAd skipped - params not ready");
|
|
89
99
|
return;
|
|
90
100
|
}
|
|
91
101
|
|
|
92
102
|
// 防止重复加载
|
|
93
103
|
if (mIsAdLoading) {
|
|
104
|
+
Log.d(TAG, "showAd skipped - already loading");
|
|
94
105
|
return;
|
|
95
106
|
}
|
|
96
107
|
|
|
97
108
|
// 检查 SDK 初始化
|
|
98
109
|
if (DyADCore.TTAdSdk == null) {
|
|
110
|
+
Log.d(TAG, "showAd skipped - SDK not initialized");
|
|
99
111
|
return;
|
|
100
112
|
}
|
|
101
113
|
|
|
102
114
|
// 在UI线程加载广告
|
|
115
|
+
Log.d(TAG, "showAd starting loadBannerAd");
|
|
103
116
|
mIsAdLoading = true;
|
|
104
117
|
runOnUiThread(this::loadBannerAd);
|
|
105
118
|
}
|
|
106
119
|
|
|
107
120
|
// 显示Banner广告
|
|
108
121
|
private void loadBannerAd() {
|
|
122
|
+
Log.d(TAG, "loadBannerAd called for codeId: " + mCodeId);
|
|
109
123
|
if (mBannerAd != null) {
|
|
110
124
|
mBannerAd.destroy();
|
|
111
125
|
}
|
|
112
126
|
|
|
113
127
|
// 先检查是否有预加载的缓存广告
|
|
114
128
|
TTNativeExpressAd cachedAd = BannerAdModule.getCachedBannerAd(mCodeId);
|
|
129
|
+
Log.d(TAG, "loadBannerAd - cachedAd: " + (cachedAd != null ? "found" : "not found"));
|
|
115
130
|
if (cachedAd != null) {
|
|
116
131
|
mBannerAd = cachedAd;
|
|
117
132
|
showBannerAd(mBannerAd);
|
|
@@ -120,6 +135,7 @@ public class BannerAdView extends RelativeLayout {
|
|
|
120
135
|
}
|
|
121
136
|
|
|
122
137
|
// 没有缓存,正常加载
|
|
138
|
+
Log.d(TAG, "loadBannerAd - loading from network");
|
|
123
139
|
// 创建广告请求参数AdSlot
|
|
124
140
|
mAdSlot =
|
|
125
141
|
new AdSlot.Builder()
|
|
@@ -135,6 +151,7 @@ public class BannerAdView extends RelativeLayout {
|
|
|
135
151
|
|
|
136
152
|
@Override
|
|
137
153
|
public void onError(int code, String message) {
|
|
154
|
+
Log.d(TAG, "loadBannerAd - onError: " + code + ", " + message);
|
|
138
155
|
mIsAdLoading = false;
|
|
139
156
|
String errorMsg = "Banner ad error: " + code + ", " + message;
|
|
140
157
|
onAdError(errorMsg);
|
|
@@ -142,13 +159,13 @@ public class BannerAdView extends RelativeLayout {
|
|
|
142
159
|
|
|
143
160
|
@Override
|
|
144
161
|
public void onNativeExpressAdLoad(java.util.List<TTNativeExpressAd> ads) {
|
|
162
|
+
Log.d(TAG, "loadBannerAd - onNativeExpressAdLoad, ads count: " + (ads != null ? ads.size() : 0));
|
|
145
163
|
if (ads == null || ads.isEmpty()) {
|
|
146
164
|
mIsAdLoading = false;
|
|
147
165
|
onAdError("Banner ad loaded but no content");
|
|
148
166
|
return;
|
|
149
167
|
}
|
|
150
168
|
|
|
151
|
-
mIsAdLoading = false;
|
|
152
169
|
mBannerAd = ads.get(0);
|
|
153
170
|
showBannerAd(mBannerAd);
|
|
154
171
|
}
|
|
@@ -158,9 +175,17 @@ public class BannerAdView extends RelativeLayout {
|
|
|
158
175
|
|
|
159
176
|
// 显示广告
|
|
160
177
|
private void showBannerAd(final TTNativeExpressAd ad) {
|
|
178
|
+
// 延迟获取 Activity,因为构造函数中可能还未 attach
|
|
179
|
+
if (mActivity == null) {
|
|
180
|
+
mActivity = mReactContext.getCurrentActivity();
|
|
181
|
+
}
|
|
161
182
|
if (mActivity == null) {
|
|
183
|
+
Log.d(TAG, "showBannerAd skipped - Activity is null");
|
|
184
|
+
// 延迟重试
|
|
185
|
+
postDelayed(() -> showBannerAd(ad), 100);
|
|
162
186
|
return;
|
|
163
187
|
}
|
|
188
|
+
Log.d(TAG, "showBannerAd - rendering ad");
|
|
164
189
|
mActivity.runOnUiThread(() -> {
|
|
165
190
|
bindAdListener(ad);
|
|
166
191
|
ad.render();
|
|
@@ -190,11 +215,16 @@ public class BannerAdView extends RelativeLayout {
|
|
|
190
215
|
|
|
191
216
|
@Override
|
|
192
217
|
public void onRenderFail(View view, String msg, int code) {
|
|
218
|
+
Log.d(TAG, "onRenderFail - code: " + code + ", msg: " + msg);
|
|
219
|
+
mIsAdLoading = false;
|
|
193
220
|
onAdError("渲染失败: " + msg);
|
|
194
221
|
}
|
|
195
222
|
|
|
196
223
|
@Override
|
|
197
224
|
public void onRenderSuccess(View view, float width, float height) {
|
|
225
|
+
Log.d(TAG, "onRenderSuccess - width: " + width + ", height: " + height);
|
|
226
|
+
mIsAdLoading = false;
|
|
227
|
+
|
|
198
228
|
mExpressContainer.removeAllViews();
|
|
199
229
|
|
|
200
230
|
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
|
|
@@ -224,6 +254,7 @@ public class BannerAdView extends RelativeLayout {
|
|
|
224
254
|
mExpressContainer.requestLayout();
|
|
225
255
|
BannerAdView.this.requestLayout();
|
|
226
256
|
|
|
257
|
+
Log.d(TAG, "onRenderSuccess - view is now visible");
|
|
227
258
|
onAdRenderSuccess((int) width, (int) height);
|
|
228
259
|
}
|
|
229
260
|
}
|