forked from flurry/flurry-adapter-mopub-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlurryCustomEventInterstitial.java
190 lines (146 loc) · 5.99 KB
/
FlurryCustomEventInterstitial.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
187
188
189
190
package com.mopub.mobileads;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import com.flurry.android.ads.FlurryAdErrorType;
import com.flurry.android.ads.FlurryAdInterstitial;
import com.flurry.android.ads.FlurryAdInterstitialListener;
import java.util.Map;
import static com.mopub.mobileads.MoPubErrorCode.ADAPTER_CONFIGURATION_ERROR;
import static com.mopub.mobileads.MoPubErrorCode.NETWORK_INVALID_STATE;
import static com.mopub.mobileads.MoPubErrorCode.NETWORK_NO_FILL;
public class FlurryCustomEventInterstitial extends com.mopub.mobileads.CustomEventInterstitial {
public static final String LOG_TAG = FlurryCustomEventInterstitial.class.getSimpleName();
private static final String API_KEY = "apiKey";
private static final String AD_SPACE_NAME = "adSpaceName";
private Context mContext;
private CustomEventInterstitialListener mListener;
private String mApiKey;
private String mAdSpaceName;
private FlurryAdInterstitial mInterstitial;
public FlurryCustomEventInterstitial() {
super();
}
// CustomEventInterstitial
@Override
protected void loadInterstitial(Context context,
CustomEventInterstitialListener listener,
Map<String, Object> localExtras, Map<String, String> serverExtras) {
if (context == null) {
Log.e(LOG_TAG, "Context cannot be null.");
listener.onInterstitialFailed(ADAPTER_CONFIGURATION_ERROR);
return;
}
if (listener == null) {
Log.e(LOG_TAG, "CustomEventInterstitialListener cannot be null.");
return;
}
if (!(context instanceof Activity)) {
Log.e(LOG_TAG, "Ad can be rendered only in Activity context.");
listener.onInterstitialFailed(ADAPTER_CONFIGURATION_ERROR);
return;
}
if (!extrasAreValid(serverExtras)) {
listener.onInterstitialFailed(ADAPTER_CONFIGURATION_ERROR);
return;
}
mContext = context;
mListener = listener;
mApiKey = serverExtras.get(API_KEY);
mAdSpaceName = serverExtras.get(AD_SPACE_NAME);
// Not needed for Flurry Analytics users
FlurryAgentWrapper.getInstance().onStartSession(context, mApiKey);
Log.d(LOG_TAG, "fetch Flurry ad (" + mAdSpaceName + ")");
mInterstitial = new FlurryAdInterstitial(mContext, mAdSpaceName);
mInterstitial.setListener(new FlurryMopubInterstitialListener());
mInterstitial.fetchAd();
}
@Override
protected void onInvalidate() {
if (mContext == null) {
return;
}
Log.d(LOG_TAG, "MoPub issued onInvalidate (" + mAdSpaceName + ")");
if (mInterstitial != null) {
mInterstitial.destroy();
mInterstitial = null;
}
// Not needed for Flurry Analytics users
FlurryAgentWrapper.getInstance().onEndSession(mContext);
mContext = null;
mListener = null;
}
private boolean extrasAreValid(Map<String, String> serverExtras) {
if (serverExtras == null) {
return false;
}
return serverExtras.containsKey(API_KEY) && serverExtras.containsKey(AD_SPACE_NAME);
}
@Override
protected void showInterstitial() {
Log.d(LOG_TAG, "MoPub issued showInterstitial (" + mAdSpaceName + ")");
if (mInterstitial != null) {
mInterstitial.displayAd();
}
}
// FlurryAdListener
private class FlurryMopubInterstitialListener implements FlurryAdInterstitialListener {
private final String LOG_TAG = getClass().getSimpleName();
@Override
public void onFetched(FlurryAdInterstitial adInterstitial) {
Log.d(LOG_TAG, "onFetched(" + adInterstitial.toString() + ")");
if (mListener != null) {
mListener.onInterstitialLoaded();
}
}
@Override
public void onRendered(FlurryAdInterstitial adInterstitial) {
Log.d(LOG_TAG, "onRendered(" + adInterstitial.toString() + ")");
if (mListener != null) {
mListener.onInterstitialShown();
}
}
@Override
public void onDisplay(FlurryAdInterstitial adInterstitial) {
Log.d(LOG_TAG, "onDisplay(" + adInterstitial.toString() + ")");
// no-op
}
@Override
public void onClose(FlurryAdInterstitial adInterstitial) {
Log.d(LOG_TAG, "onClose(" + adInterstitial.toString() + ")");
if (mListener != null) {
mListener.onInterstitialDismissed();
}
}
@Override
public void onAppExit(FlurryAdInterstitial adInterstitial) {
Log.d(LOG_TAG, "onAppExit(" + adInterstitial.toString() + ")");
if (mListener != null) {
mListener.onLeaveApplication();
}
}
@Override
public void onClicked(FlurryAdInterstitial adInterstitial) {
Log.d(LOG_TAG, "onClicked " + adInterstitial.toString());
if (mListener != null) {
mListener.onInterstitialClicked();
}
}
@Override
public void onVideoCompleted(FlurryAdInterstitial adInterstitial) {
Log.d(LOG_TAG, "onVideoCompleted " + adInterstitial.toString());
// no-op
}
@Override
public void onError(FlurryAdInterstitial adBanner, FlurryAdErrorType adErrorType, int errorCode) {
Log.d(LOG_TAG, "onError(" + adBanner.toString() + adErrorType.toString() + errorCode + ")");
if (mListener != null) {
if (FlurryAdErrorType.FETCH.equals(adErrorType)) {
mListener.onInterstitialFailed(NETWORK_NO_FILL);
} else if (FlurryAdErrorType.RENDER.equals(adErrorType)) {
mListener.onInterstitialFailed(NETWORK_INVALID_STATE);
}
}
}
}
}