Last active
September 14, 2018 11:56
-
-
Save raymondctc/36cefe397895d8dce1e395bce1c83119 to your computer and use it in GitHub Desktop.
DebugAds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ninegag.android.app.ui.fragments; | |
import android.content.Context; | |
import android.os.Bundle; | |
import android.support.v4.util.ArrayMap; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.LinearLayout; | |
import com.google.ads.consent.ConsentInformation; | |
import com.google.ads.consent.ConsentStatus; | |
import com.google.ads.mediation.nexage.NexageAdapter; | |
import com.google.android.gms.ads.AdListener; | |
import com.google.android.gms.ads.AdSize; | |
import com.google.android.gms.ads.doubleclick.PublisherAdRequest; | |
import com.google.android.gms.ads.doubleclick.PublisherAdView; | |
import com.google.android.gms.ads.mediation.MediationAdapter; | |
import com.ninegag.android.x_dev.R; | |
import java.util.Map; | |
public class DebugAdFragmentV2 extends BaseFragment { | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
return inflater.inflate(R.layout.fragment_debug_ad, container, false); | |
} | |
@Override | |
public void onViewCreated(View view, Bundle savedInstanceState) { | |
super.onViewCreated(view, savedInstanceState); | |
final ViewGroup.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, getDpAsPixels(getContext(), 250)); | |
final PublisherAdView adView = new PublisherAdView(getContext()); | |
adView.setAdUnitId("/16921351/9gag-Android-ListView-Banner"); | |
adView.setAdSizes(AdSize.MEDIUM_RECTANGLE); | |
adView.setAdListener(new AdListener() { | |
@Override | |
public void onAdClicked() { | |
super.onAdClicked(); | |
} | |
@Override | |
public void onAdFailedToLoad(int i) { | |
super.onAdFailedToLoad(i); | |
Log.d("DebugAdFragment", "failed code=" + i); | |
} | |
@Override | |
public void onAdLoaded() { | |
super.onAdLoaded(); | |
Log.d("DebugAdFragment", "Yay loaded"); | |
} | |
}); | |
if (view instanceof ViewGroup) { | |
((ViewGroup) view).addView(adView, params); | |
} | |
adView.loadAd(createAdRequest(createMediationAdapterExtras())); | |
} | |
private int getDpAsPixels(final Context context, final int sizeInDp) { | |
return (int) (sizeInDp * context.getResources().getDisplayMetrics().density + 0.5f); | |
} | |
private PublisherAdRequest createAdRequest(final Map<Class<? extends MediationAdapter>, Bundle> extras) { | |
final PublisherAdRequest.Builder builder = new PublisherAdRequest.Builder(); | |
if (extras != null) { | |
for (final Map.Entry<Class<? extends MediationAdapter>, Bundle> entry : extras.entrySet()) { | |
builder.addNetworkExtrasBundle(entry.getKey(), entry.getValue()); | |
} | |
} | |
// final LoginAccount account = ObjectManager.getInstance().getDC().getLoginAccount(); | |
// if (account != null) { | |
// if (!TextUtils.isEmpty(account.birthday)) { | |
// final Date date = LoginAccountUtil.parseDate(account.birthday); | |
// if (date != null) { | |
// builder.setBirthday(date); | |
// } | |
// } | |
// | |
// if (!TextUtils.isEmpty(account.gender)) { | |
// builder.setGender("M".equals(account.gender) ? AdRequest.GENDER_MALE : AdRequest.GENDER_FEMALE); | |
// } | |
// } | |
builder.addTestDevice("83B7A6E9D41DF2677106718556CE971E") | |
.addTestDevice("1A3592996585AE1FE5D03E2AA08F3281") | |
; | |
return builder.build(); | |
} | |
private Map<Class<? extends MediationAdapter>, Bundle> createMediationAdapterExtras() { | |
final Map<Class<? extends MediationAdapter>, Bundle> extras = new ArrayMap<>(); | |
final Bundle bundleExtra = new Bundle(); | |
bundleExtra.putString("dcn", getResources().getString(R.string.mm_sdk_site_id)); | |
// Ref: https://developers.google.com/mobile-ads-sdk/docs/dfp/android/eu-consent | |
if (getContext() != null) { | |
ConsentStatus consentStatus = ConsentInformation.getInstance(getContext().getApplicationContext()).getConsentStatus(); | |
if (consentStatus.equals(ConsentStatus.NON_PERSONALIZED)) { | |
bundleExtra.putString("npa", "1"); | |
} | |
} | |
extras.put(NexageAdapter.class, bundleExtra); | |
return extras; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical"> | |
</LinearLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment