Skip to content

Commit

Permalink
Init Code 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
hearsilent committed Feb 21, 2017
1 parent ff6d4a0 commit 90c99f3
Show file tree
Hide file tree
Showing 4 changed files with 425 additions and 0 deletions.
138 changes: 138 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
Ad2iction Android Adapter for MoPub
=================================

Adapter version 1.0.0 - Updated 2017-02-21
------------------------------------------

This version of the adapter works with MoPub Android SDK 4.12+ and Ad2iction Android SDK 3.3.0.
Otherwise, please upgrade to the newer versions of both SDKs.

### Mediate Ad2iction Ads through MoPub

To integrate Ad2iction as the Custom Native Network in the MoPub ad serving flow, you need the
Custom Event Class code incorporated into your application in addition to the Flurry SDK.
Three quick steps are necessary:

1. Integrate the Ad2iction SDK and Ad2iction adapter for MoPub code into your app
2. Configure Ad2iction's Ad unit(s)
3. Configure MoPub to mediate Ad2iction

#### Integrate the Ad2iction SDK and Ad2iction adapter for MoPub code into your app

1. If your application is not yet using Ad2iction, please contact Ad2iction.

2. Add the Ad2iction Android SDK.

3. Add the Google Play Services SDK to your project. This is required for Android Advertising ID
support. See http://developer.android.com/google/playservices/setup.html.

4. Add the Ad2iction MoPub adapter classes (found in the [com.mopub.nativeads](src/com/mopub/nativeads) package) to your project. Place the following classes in the com.mopub.nativeads package:
* [`Ad2ictionCustomEventNative`](src/com/mopub/nativeads/Ad2ictionCustomEventNative.java)
* [`Ad2ictionNativeAdRenderer`](src/com/mopub/nativeads/Ad2ictionNativeAdRenderer.java)

5. The steps to integrate Flurry Native Ads via MoPub are similar to those described [here](https://github.com/mopub/mopub-android-sdk/wiki/Native-Ads-Integration):
* Create an XML layout for your native ads
* Define where ads should be placed within your feed
* Create a MoPubAdAdapter to wrap your existing `Adapter` subclass and begin loading ads.

And you should register the `Ad2ictionNativeAdRenderer` as a custom ad renderer.

```java
ViewBinder viewBinder = new ViewBinder.Builder(R.layout.native_ad_list_item)
// Set up your regular ViewBinder
.build();

// Register the Ad2ictionNativeAdRenderer to handle static native ads
final Ad2ictionNativeAdRenderer ad2ictionRenderer = new Ad2ictionNativeAdRenderer(viewBinder);
mAdAdapter = new MoPubAdAdapter(getActivity(), adapter);
mAdAdapter.registerAdRenderer(ad2ictionRenderer);

//...register other native ad renderers as required
```

6. Add permissions in AndroidManifest:
```xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
```

7. Add Activity declaration in AndroidManifest:
```xml
<activity
android:name="com.ad2iction.mobileads.Ad2ictionActivity"
android:configChanges="keyboardHidden|orientation|screenSize"/>
<activity
android:name="com.ad2iction.mobileads.MraidActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:theme="@android:style/Theme.Translucent"/>
<activity
android:name="com.ad2iction.common.Ad2ictionBrowser"
android:configChanges="keyboardHidden|orientation|screenSize"/>
<activity
android:name="com.ad2iction.mobileads.MraidVideoPlayerActivity"
android:configChanges="keyboardHidden|orientation|screenSize"/>
```

8. If you plan to run [ProGuard](http://developer.android.com/tools/help/proguard.html) on your app
before release, you will need to add the following to your ProGuard configuration file.

```
# Ad2iction Proguard Config
# NOTE: You should also include the Android Proguard config found with the build tools:
# $ANDROID_HOME/tools/proguard/proguard‐android.txt
# Add this line if you use Admob Adapter
‐libraryjars \libs\ad2iction‐mediation‐adapteradmob.jar
‐libraryjars \libs\ad2iction‐sdk.jar
# Keep public classes and methods.
‐keepclassmembers class com.ad2iction.** { public *;}
‐keep public class com.ad2iction.**
‐keep public class android.webkit.JavascriptInterface {}
‐keep class * extends com.ad2iction.mobileads.CustomEventBanner {}
‐keep class * extends com.ad2iction.mobileads.CustomEventInterstitial {}
‐keep class * extends com.ad2iction.nativeads.CustomEventNative {}
‐keep class com.google.android.gms.common.GooglePlayServicesUtil {*;}
‐keep class com.google.android.gms.ads.identifier.AdvertisingIdClient {*;}
‐keep class com.google.android.gms.ads.identifier.AdvertisingIdClient$Info {*;}
```

#### Configure Ad2iction Ad unit(s)

For each MoPub ad unit that you would like to mediate Ad2iction through, please contact Ad2iction.

#### Configure MoPub to mediate Ad2iction

Ad2iction's custom events are implemented in accordance with [instructions provided by MoPub](https://github.com/mopub/mopub-android-sdk/wiki/Custom-Events).

After you incorporate the Ad2iction files into your project, you need to
configure Ad2iction as a Custom Network. Please follow instructions provided by MoPub
(for [native](https://dev.twitter.com/mopub/ui-setup/network-setup-custom-native)) with any of the Ad2iction custom events class noted below:

* [`com.mopub.nativeads.Ad2ictionCustomEventNative`](src/com/mopub/nativeads/Ad2ictionCustomEventNative.java)
for Ad2iction native ads

**NOTE:** An important step to get this integration working is to configure the custom network (as described above) with the
Ad2iction API key and send them as server extras (in the "Custom Event Class Data" field on the MoPub UI).

The custom event class data should be in the following JSON format:

```json
{"response_body_key":"YOUR_API_KEY"}
```

You can also configure the custom event data as a line item in your MoPub order.

![Screenshot showing ad unit/line item config on MoPub's dashboard](imgs/mopub_line_item_config.png)

Changelog
---------
#### Version 1.0.0 - 2017-02-21
* Introduced Ad2iction Native Ad support in the adapter
* Supports MoPub SDK 4.12+
Binary file added imgs/mopub_line_item_config.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
142 changes: 142 additions & 0 deletions src/com/mopub/nativeads/Ad2ictionCustomEventNative.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package com.mopub.nativeads;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.ad2iction.nativeads.Ad2ictionNative;
import com.ad2iction.nativeads.NativeResponse;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.mopub.nativeads.NativeImageHelper.preCacheImages;

public class Ad2ictionCustomEventNative extends CustomEventNative {

private static final String LOG_TAG = Ad2ictionCustomEventNative.class.getSimpleName();

protected void loadNativeAd(@NonNull Context context,
@NonNull CustomEventNativeListener customEventNativeListener,
@NonNull Map<String, Object> localExtras,
@NonNull Map<String, String> serverExtras) {
try {
Ad2ictionForwardingNativeAd ad2ictionForwardingNativeAd =
new Ad2ictionForwardingNativeAd(context, serverExtras.get("response_body_key"),
customEventNativeListener);
ad2ictionForwardingNativeAd.loadAd();
} catch (IllegalArgumentException e) {
customEventNativeListener.onNativeAdFailed(NativeErrorCode.UNSPECIFIED);
Log.i(LOG_TAG,
"Failed Native AdFetch: Missing required server extras [response_body_key].");
}
}

public static class Ad2ictionForwardingNativeAd extends BaseNativeAd
implements Ad2ictionNative.Ad2ictionNativeNetworkListener,
Ad2ictionNative.Ad2ictionNativeEventListener {

private final Context mContext;
private final String mKey;
private final Ad2ictionNative mAd;
private final CustomEventNativeListener mCustomEventNativeListener;
private NativeResponse mResponse;

private Ad2ictionForwardingNativeAd(Context context, String key,
CustomEventNativeListener customEventNativeListener)
throws IllegalArgumentException {
mContext = context.getApplicationContext();
mKey = key;
if (TextUtils.isEmpty(mKey)) {
throw new IllegalArgumentException("Key cannot be null");
}
mAd = new Ad2ictionNative(context, key, "native", this);
mCustomEventNativeListener = customEventNativeListener;
}

private void loadAd() {
mAd.setNativeEventListener(this);
mAd.makeRequest();
}

@Override
public void onNativeLoad(NativeResponse nativeResponse) {
mResponse = nativeResponse;

final List<String> imageUrls = new ArrayList<>(2);
final String mainImageUrl = nativeResponse.getMainImageUrl();
if (mainImageUrl != null) {
imageUrls.add(mainImageUrl);
Log.d(LOG_TAG, "Ad2iction Native Ad main image found.");
}

final String iconUrl = nativeResponse.getIconImageUrl();
if (iconUrl != null) {
imageUrls.add(iconUrl);
Log.d(LOG_TAG, "Ad2iction Native Ad icon image found.");
}

preCacheImages(mContext, imageUrls, new NativeImageHelper.ImageListener() {

public void onImagesCached() {
mCustomEventNativeListener.onNativeAdLoaded(Ad2ictionForwardingNativeAd.this);
}

public void onImagesFailedToCache(NativeErrorCode errorCode) {
mCustomEventNativeListener.onNativeAdFailed(errorCode);
}
});
}

@Override
public void onNativeFail(com.ad2iction.nativeads.NativeErrorCode nativeErrorCode) {
if (nativeErrorCode == null) {
mCustomEventNativeListener.onNativeAdFailed(NativeErrorCode.UNSPECIFIED);
} else if (nativeErrorCode == com.ad2iction.nativeads.NativeErrorCode.NETWORK_NO_FILL) {
mCustomEventNativeListener.onNativeAdFailed(NativeErrorCode.NETWORK_NO_FILL);
} else if (nativeErrorCode ==
com.ad2iction.nativeads.NativeErrorCode.NETWORK_INVALID_STATE) {
mCustomEventNativeListener.onNativeAdFailed(NativeErrorCode.NETWORK_INVALID_STATE);
} else {
mCustomEventNativeListener.onNativeAdFailed(NativeErrorCode.UNSPECIFIED);
}
}

@Override
public void onNativeImpression(View view) {
Log.d(LOG_TAG, "onNativeImpression: Ad2iction native ad impression logged");
notifyAdImpressed();
}

@Override
public void onNativeClick(View view) {
Log.d(LOG_TAG, "onNativeClick: Ad2iction native ad clicked");
notifyAdClicked();
}

@Override
public void prepare(@NonNull View view) {
mResponse.prepare(view);
Log.d(LOG_TAG, "prepare(" + mResponse.toString() + " " + view.toString() + ")");
}

@Override
public void clear(@NonNull View view) {
mResponse.clear(view);
Log.d(LOG_TAG, "clear(" + mResponse.toString() + ")");
}

@Override
public void destroy() {
mResponse.destroy();
Log.d(LOG_TAG, "destroy(" + mResponse.toString() + ") started.");
}

NativeResponse getNativeResponse() {
return mResponse;
}
}
}
Loading

0 comments on commit 90c99f3

Please sign in to comment.