Advertisement
Quimbox

SDK Consent

Sep 29th, 2019
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.21 KB | None | 0 0
  1. package sample.app;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.widget.TextView;
  7.  
  8. import com.google.ads.consent.ConsentForm;
  9. import com.google.ads.consent.ConsentFormListener;
  10. import com.google.ads.consent.ConsentInfoUpdateListener;
  11. import com.google.ads.consent.ConsentInformation;
  12. import com.google.ads.consent.ConsentStatus;
  13.  
  14. import java.net.MalformedURLException;
  15. import java.net.URL;
  16.  
  17. import static com.google.ads.consent.ConsentStatus.NON_PERSONALIZED;
  18. import static com.google.ads.consent.ConsentStatus.PERSONALIZED;
  19.  
  20. public class MainActivity extends AppCompatActivity {
  21.  
  22.     private String TAG = this.getClass().getSimpleName();
  23.  
  24.     private TextView textView;
  25.  
  26.     private ConsentForm form;
  27.  
  28.     @Override
  29.     protected void onCreate(Bundle savedInstanceState) {
  30.         super.onCreate(savedInstanceState);
  31.         setContentView(R.layout.activity_main);
  32.  
  33.         textView = findViewById(R.id.text);
  34.  
  35.         checkForConsent();
  36.     }
  37.  
  38.     private void checkForConsent() {
  39.         ConsentInformation consentInformation = ConsentInformation.getInstance(MainActivity.this);
  40.         String[] publisherIds = {"pub-id-from-publishers-like-admob"};
  41.         consentInformation.requestConsentInfoUpdate(publisherIds, new ConsentInfoUpdateListener() {
  42.             @Override
  43.             public void onConsentInfoUpdated(ConsentStatus consentStatus) {
  44.                 // User's consent status successfully updated.
  45.                 switch (consentStatus) {
  46.                     case PERSONALIZED:
  47.                         Log.d(TAG, "Showing Personalized ads");
  48.                         showPersonalizedAds();
  49.                         break;
  50.                     case NON_PERSONALIZED:
  51.                         Log.d(TAG, "Showing Non-Personalized ads");
  52.                         showNonPersonalizedAds();
  53.                         break;
  54.                     case UNKNOWN:
  55.                         Log.d(TAG, "Requesting Consent");
  56.                         if (ConsentInformation.getInstance(getBaseContext())
  57.                                 .isRequestLocationInEeaOrUnknown()) {
  58.                         requestConsent();
  59.                         } else {
  60.                             showPersonalizedAds();
  61.                         }
  62.                         break;
  63.                     default:
  64.                         break;
  65.                 }
  66.             }
  67.  
  68.             @Override
  69.             public void onFailedToUpdateConsentInfo(String errorDescription) {
  70.                 // User's consent status failed to update.
  71.             }
  72.         });
  73.     }
  74.  
  75.     private void requestConsent() {
  76.         URL privacyUrl = null;
  77.         try {
  78.             // TODO: Replace with your app's privacy policy URL.
  79.             privacyUrl = new URL("https://your.privacy.url/");
  80.         } catch (MalformedURLException e) {
  81.             e.printStackTrace();
  82.             // Handle error.
  83.         }
  84.         form = new ConsentForm.Builder(MainActivity.this, privacyUrl)
  85.                 .withListener(new ConsentFormListener() {
  86.                     @Override
  87.                     public void onConsentFormLoaded() {
  88.                         // Consent form loaded successfully.
  89.                         Log.d(TAG, "Requesting Consent: onConsentFormLoaded");
  90.                         showForm();
  91.                     }
  92.  
  93.                     @Override
  94.                     public void onConsentFormOpened() {
  95.                         // Consent form was displayed.
  96.                         Log.d(TAG, "Requesting Consent: onConsentFormOpened");
  97.                     }
  98.  
  99.                     @Override
  100.                     public void onConsentFormClosed(
  101.                             ConsentStatus consentStatus, Boolean userPrefersAdFree) {
  102.                         Log.d(TAG, "Requesting Consent: onConsentFormClosed");
  103.                         if (userPrefersAdFree) {
  104.                             // Buy or Subscribe
  105.                             Log.d(TAG, "Requesting Consent: User prefers AdFree");
  106.                         } else {
  107.                             Log.d(TAG, "Requesting Consent: Requesting consent again");
  108.                             switch (consentStatus) {
  109.                                 case PERSONALIZED:
  110.                                     showPersonalizedAds();break;
  111.                                 case NON_PERSONALIZED:
  112.                                     showNonPersonalizedAds();break;
  113.                                 case UNKNOWN:
  114.                                     showNonPersonalizedAds();break;
  115.                             }
  116.                          
  117.                         }
  118.                         // Consent form was closed.
  119.                     }
  120.  
  121.                     @Override
  122.                     public void onConsentFormError(String errorDescription) {
  123.                         Log.d(TAG, "Requesting Consent: onConsentFormError. Error - " + errorDescription);
  124.                         // Consent form error.
  125.                     }
  126.                 })
  127.                 .withPersonalizedAdsOption()
  128.                 .withNonPersonalizedAdsOption()
  129.                 .withAdFreeOption()
  130.                 .build();
  131.         form.load();
  132.     }
  133.  
  134.     private void showPersonalizedAds() {
  135.         AdView mAdView = findViewById(R.id.adView);
  136.         AdRequest adRequest = new AdRequest.Builder()
  137.                 .addTestDevice("your device id from logcat") // Todo: Remove in Release build
  138.                 .build();
  139.         mAdView.loadAd(adRequest);
  140.     }
  141.  
  142.     private void showNonPersonalizedAds() {
  143.         AdView mAdView = findViewById(R.id.adView);
  144.         AdRequest adRequest = new AdRequest.Builder()
  145.                 .addTestDevice("your device id from logcat") // Todo: Remove in Release build
  146.                 .addNetworkExtrasBundle(AdMobAdapter.class, getNonPersonalizedAdsBundle())
  147.                 .build();
  148.         mAdView.loadAd(adRequest);
  149.     }
  150.  
  151.     private void showForm() {
  152.         if (form == null) {
  153.             Log.d(TAG, "Consent form is null");
  154.         }
  155.         if (form != null) {
  156.             Log.d(TAG, "Showing consent form");
  157.             form.show();
  158.         } else {
  159.             Log.d(TAG, "Not Showing consent form");
  160.         }
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement