-
Notifications
You must be signed in to change notification settings - Fork 532
/
RNAdMobRewardedVideoAdModule.java
186 lines (156 loc) · 6.56 KB
/
RNAdMobRewardedVideoAdModule.java
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
package com.sbugert.rnadmob;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableNativeArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.reward.RewardedVideoAd;
import com.google.android.gms.ads.reward.RewardedVideoAdListener;
import com.google.android.gms.ads.reward.RewardItem;
import com.google.android.gms.ads.AdRequest;
import java.util.ArrayList;
public class RNAdMobRewardedVideoAdModule extends ReactContextBaseJavaModule implements RewardedVideoAdListener {
public static final String REACT_CLASS = "RNAdMobRewarded";
public static final String EVENT_AD_LOADED = "onAdLoaded";
public static final String EVENT_AD_FAILED_TO_LOAD = "onAdFailedToLoad";
public static final String EVENT_AD_OPENED = "onAdOpened";
public static final String EVENT_AD_CLOSED = "onAdClosed";
public static final String EVENT_AD_LEFT_APPLICATION = "onAdLeftApplication";
public static final String EVENT_REWARDED = "rewarded";
public static final String EVENT_VIDEO_STARTED = "videoStarted";
RewardedVideoAd mRewardedVideoAd;
String adUnitID;
String[] testDevices;
private Promise mRequestAdPromise;
@Override
public String getName() {
return REACT_CLASS;
}
public RNAdMobRewardedVideoAdModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public void onRewarded(RewardItem rewardItem) {
WritableMap reward = Arguments.createMap();
reward.putInt("amount", rewardItem.getAmount());
reward.putString("type", rewardItem.getType());
sendEvent(EVENT_REWARDED, reward);
}
@Override
public void onRewardedVideoAdLoaded() {
sendEvent(EVENT_AD_LOADED, null);
mRequestAdPromise.resolve(null);
}
@Override
public void onRewardedVideoAdOpened() {
sendEvent(EVENT_AD_OPENED, null);
}
@Override
public void onRewardedVideoStarted() {
sendEvent(EVENT_VIDEO_STARTED, null);
}
@Override
public void onRewardedVideoAdClosed() {
sendEvent(EVENT_AD_CLOSED, null);
}
@Override
public void onRewardedVideoAdLeftApplication() {
sendEvent(EVENT_AD_LEFT_APPLICATION, null);
}
@Override
public void onRewardedVideoAdFailedToLoad(int errorCode) {
String errorString = "ERROR_UNKNOWN";
String errorMessage = "Unknown error";
switch (errorCode) {
case AdRequest.ERROR_CODE_INTERNAL_ERROR:
errorString = "ERROR_CODE_INTERNAL_ERROR";
errorMessage = "Internal error, an invalid response was received from the ad server.";
break;
case AdRequest.ERROR_CODE_INVALID_REQUEST:
errorString = "ERROR_CODE_INVALID_REQUEST";
errorMessage = "Invalid ad request, possibly an incorrect ad unit ID was given.";
break;
case AdRequest.ERROR_CODE_NETWORK_ERROR:
errorString = "ERROR_CODE_NETWORK_ERROR";
errorMessage = "The ad request was unsuccessful due to network connectivity.";
break;
case AdRequest.ERROR_CODE_NO_FILL:
errorString = "ERROR_CODE_NO_FILL";
errorMessage = "The ad request was successful, but no ad was returned due to lack of ad inventory.";
break;
}
WritableMap event = Arguments.createMap();
WritableMap error = Arguments.createMap();
event.putString("message", errorMessage);
sendEvent(EVENT_AD_FAILED_TO_LOAD, event);
mRequestAdPromise.reject(errorString, errorMessage);
}
private void sendEvent(String eventName, @Nullable WritableMap params) {
getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, params);
}
@ReactMethod
public void setAdUnitID(String adUnitID) {
this.adUnitID = adUnitID;
}
@ReactMethod
public void setTestDevices(ReadableArray testDevices) {
ReadableNativeArray nativeArray = (ReadableNativeArray)testDevices;
ArrayList<Object> list = nativeArray.toArrayList();
this.testDevices = list.toArray(new String[list.size()]);
}
@ReactMethod
public void requestAd(final Promise promise) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
RNAdMobRewardedVideoAdModule.this.mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(getCurrentActivity());
RNAdMobRewardedVideoAdModule.this.mRewardedVideoAd.setRewardedVideoAdListener(RNAdMobRewardedVideoAdModule.this);
if (mRewardedVideoAd.isLoaded()) {
promise.reject("E_AD_ALREADY_LOADED", "Ad is already loaded.");
} else {
mRequestAdPromise = promise;
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
if (testDevices != null) {
for (int i = 0; i < testDevices.length; i++) {
adRequestBuilder.addTestDevice(testDevices[i]);
}
}
AdRequest adRequest = adRequestBuilder.build();
mRewardedVideoAd.loadAd(adUnitID, adRequest);
}
}
});
}
@ReactMethod
public void showAd(final Promise promise) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
if (mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.show();
promise.resolve(null);
} else {
promise.reject("E_AD_NOT_READY", "Ad is not ready.");
}
}
});
}
@ReactMethod
public void isReady(final Callback callback) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
callback.invoke(mRewardedVideoAd.isLoaded());
}
});
}
}