-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathRNAPSAdLoaderModule.java
More file actions
232 lines (201 loc) · 7.22 KB
/
RNAPSAdLoaderModule.java
File metadata and controls
232 lines (201 loc) · 7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package com.adversport.rnaps;
/*
* Copyright (c) 2022-present Adversport & Contributors
*
* This file is part of react-native-aps.
*
* react-native-aps is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* react-native-aps is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Foobar. If not, see <https://www.gnu.org/licenses/>.
*/
import android.util.SparseArray;
import com.amazon.device.ads.*;
import com.facebook.react.bridge.*;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter;
import java.util.List;
import java.util.Map;
@ReactModule(name = RNAPSAdLoaderModule.MODULE_NAME)
public class RNAPSAdLoaderModule extends ReactContextBaseJavaModule {
public static final String MODULE_NAME = "RNAPSAdLoaderModule";
public static final String AD_TYPE_BANNER = "banner";
public static final String AD_TYPE_INTERSTITIAL = "interstitial";
public static final String EVENT_SUCCESS = "onSuccess";
public static final String EVENT_FAILURE = "onFailure";
private static final SparseArray<DTBAdRequest> adLoaders = new SparseArray<>();
private final ReactApplicationContext reactContext;
public RNAPSAdLoaderModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
}
@Override
public String getName() {
return MODULE_NAME;
}
private void sendEvent(String eventName, WritableMap params) {
reactContext.getJSModule(RCTDeviceEventEmitter.class).emit(eventName, params);
}
@ReactMethod
public void addListener(String eventName) {
// Required for event emitter
}
@ReactMethod
public void removeListeners(double count) {
// Required for event emitter
}
private class AdCallback implements DTBAdCallback {
private final int loaderId;
private Promise promise;
public AdCallback(int loaderId, Promise promise) {
this.loaderId = loaderId;
this.promise = promise;
}
@Override
public void onFailure(AdError adError) {
String code;
AdError.ErrorCode errorCode = adError.getCode();
if (errorCode == null) {
errorCode = AdError.ErrorCode.NO_ERROR;
}
switch (errorCode) {
case NO_ERROR:
code = "no_error";
break;
case NETWORK_ERROR:
code = "network_error";
break;
case NETWORK_TIMEOUT:
code = "network_timeout";
break;
case NO_FILL:
code = "no_fill";
break;
case INTERNAL_ERROR:
code = "internal_error";
break;
case REQUEST_ERROR:
code = "request_error";
break;
default:
code = "unknown_error";
break;
}
WritableMap userInfoMap = Arguments.createMap();
userInfoMap.putString("code", code);
userInfoMap.putString("message", adError.getMessage());
WritableMap payload = Arguments.createMap();
payload.putInt("loaderId", loaderId);
payload.putMap("userInfo", userInfoMap);
sendEvent(EVENT_FAILURE, payload);
if (promise != null) {
// Créer une copie pour promise.reject car userInfoMap est déjà utilisé
WritableMap userInfoCopy = Arguments.createMap();
userInfoCopy.putString("code", code);
userInfoCopy.putString("message", adError.getMessage());
promise.reject(code, adError.getMessage(), userInfoCopy);
promise = null;
}
}
@Override
public void onSuccess(DTBAdResponse response) {
WritableMap responseMap = Arguments.createMap();
Map<String, List<String>> customParams = response.getDefaultDisplayAdsRequestCustomParams();
for (Map.Entry<String, List<String>> entry : customParams.entrySet()) {
List<String> values = entry.getValue();
StringBuilder valueBuilder = new StringBuilder();
for (int i = 0; i < values.size(); i++) {
valueBuilder.append(values.get(i));
if (i < values.size() - 1) {
valueBuilder.append(",");
}
}
responseMap.putString(entry.getKey(), valueBuilder.toString());
}
WritableMap payload = Arguments.createMap();
payload.putInt("loaderId", loaderId);
payload.putMap("response", responseMap);
sendEvent(EVENT_SUCCESS, payload);
if (promise != null) {
promise.resolve(responseMap);
promise = null;
}
}
}
@ReactMethod
public void loadAd(int loaderId, String adType, ReadableMap options, Promise promise) {
stopAutoRefresh(loaderId);
DTBAdRequest adLoader = new DTBAdRequest();
String slotUUID = options.getString("slotUUID");
if (slotUUID == null) {
promise.reject("invalid_slot_uuid", "slotUUID is required");
return;
}
DTBAdSize adSize;
switch (adType) {
case AD_TYPE_BANNER:
String size = options.getString("size");
if (size == null) {
promise.reject("invalid_size", "size is required for banner ads");
return;
}
String[] dimensions = size.split("x");
if (dimensions.length != 2) {
promise.reject("invalid_size", "size must be in format 'WIDTHxHEIGHT'");
return;
}
try {
int width = Integer.parseInt(dimensions[0]);
int height = Integer.parseInt(dimensions[1]);
adSize = new DTBAdSize(width, height, slotUUID);
} catch (NumberFormatException e) {
promise.reject("invalid_size", "invalid size format: " + size);
return;
}
break;
case AD_TYPE_INTERSTITIAL:
adSize = new DTBAdSize.DTBInterstitialAdSize(slotUUID);
break;
default:
promise.reject("invalid_ad_type", "unsupported ad type: " + adType);
return;
}
adLoader.setSizes(adSize);
if (options.hasKey("customTargeting")) {
ReadableMap customTargeting = options.getMap("customTargeting");
if (customTargeting != null) {
ReadableMapKeySetIterator iterator = customTargeting.keySetIterator();
while (iterator.hasNextKey()) {
String key = iterator.nextKey();
String value = customTargeting.getString(key);
if (value != null) {
adLoader.putCustomTarget(key, value);
}
}
}
}
boolean autoRefresh = options.hasKey("autoRefresh") && options.getBoolean("autoRefresh");
int refreshInterval =
options.hasKey("refreshInterval") ? options.getInt("refreshInterval") : 60;
if (autoRefresh) {
adLoader.setAutoRefresh(refreshInterval);
adLoaders.put(loaderId, adLoader);
}
adLoader.loadAd(new AdCallback(loaderId, promise));
}
@ReactMethod
public void stopAutoRefresh(int loaderId) {
DTBAdRequest adLoader = adLoaders.get(loaderId);
if (adLoader != null) {
adLoader.stop();
adLoaders.remove(loaderId);
}
}
}