Skip to content

Commit

Permalink
Implement AdMob and Consent SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
digitapex committed Jun 15, 2020
1 parent 7887875 commit 32fbf89
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 3 deletions.
29 changes: 26 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ android {
compileSdkVersion 28
defaultConfig {
applicationId "com.spitslide.ukpetitions"
minSdkVersion 15
minSdkVersion 16
targetSdkVersion 28
versionCode 3
versionName "1.02"
versionCode 4
versionName "1.03"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
manifestPlaceholders = [ ADMOB_APPLICATION_ID:getAdmobApplicationId() ]
resValue 'string', 'ADMOB_ADUNIT_ID', getAdmobAdUnitId()
buildConfigField "String", "ADMOB_PUBLISHER_ID", "\""+getAdMobPublisherId()+"\""
}
buildTypes {
release {
Expand All @@ -27,7 +30,27 @@ dependencies {
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.google.android.gms:play-services-auth:16.0.1'
implementation 'com.google.android.gms:play-services-ads:19.1.0'
implementation 'com.google.android.ads.consent:consent-library:1.0.8'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
}

def getAdmobApplicationId(){
def Properties props = new Properties()
props.load(new FileInputStream(new File('local.properties')))
return props['ADMOB_APPLICATION_ID']
}

def getAdmobAdUnitId(){
def Properties props = new Properties()
props.load(new FileInputStream(new File('local.properties')))
return props['ADMOB_ADUNIT_ID']
}

def getAdMobPublisherId(){
def Properties props = new Properties()
props.load(new FileInputStream(new File('local.properties')))
return props['ADMOB_PUBLISHER_ID']
}
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
android:supportsRtl="true"
android:name=".MyApplication"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="${ADMOB_APPLICATION_ID}"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
141 changes: 141 additions & 0 deletions app/src/main/java/com/spitslide/ukpetitions/Consent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package com.spitslide.ukpetitions;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

import com.google.ads.consent.ConsentForm;
import com.google.ads.consent.ConsentFormListener;
import com.google.ads.consent.ConsentInfoUpdateListener;
import com.google.ads.consent.ConsentInformation;
import com.google.ads.consent.ConsentStatus;
import com.google.ads.consent.DebugGeography;
import com.google.ads.mediation.admob.AdMobAdapter;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

import java.net.MalformedURLException;
import java.net.URL;

public class Consent {

private Context context;
private ConsentForm form;
public boolean shouldAddConsentToMenu;
private String privacyPolicy = "https://gist.githubusercontent.com/digitapex/5dfadd2e7522660033a3d37b0d5dee8b/raw/73e38a6116f98b3e804270c7eef447a60ad369f3/UKPetitionsPrivacyPolicy";

public Consent(Context context) {
this.context = context;
}

void checkForConsentAndDisplayAds(boolean isUserChangingConsent) {

// uncomment for testing purposes
// ConsentInformation.getInstance(context).
// setDebugGeography(DebugGeography.DEBUG_GEOGRAPHY_NOT_EEA);
// ConsentInformation.getInstance(context).
// setDebugGeography(DebugGeography.DEBUG_GEOGRAPHY_EEA);

if (isUserChangingConsent) {
ConsentInformation.getInstance(context).setConsentStatus(ConsentStatus.UNKNOWN);
}

ConsentInformation consentInformation = ConsentInformation.getInstance(context);
String[] publisherIds = { BuildConfig.ADMOB_PUBLISHER_ID };
consentInformation.requestConsentInfoUpdate(publisherIds, new ConsentInfoUpdateListener() {
@Override
public void onConsentInfoUpdated(ConsentStatus consentStatus) {
switch (consentStatus) {
case PERSONALIZED:
shouldAddConsentToMenu = true;
showPersonalizedAds();
break;
case NON_PERSONALIZED:
shouldAddConsentToMenu = true;
showNonPersonalizedAds();
break;
case UNKNOWN:
if (ConsentInformation.getInstance(context)
.isRequestLocationInEeaOrUnknown()) {
requestConsent();
} else {
showPersonalizedAds();
}
break;
default:
break;
}
}

@Override
public void onFailedToUpdateConsentInfo(String errorDescription) {
}
});
}

private void requestConsent() {
URL privacyUrl = null;
try {
privacyUrl = new URL(privacyPolicy);
} catch (MalformedURLException e) {
e.printStackTrace();
}
form = new ConsentForm.Builder(context, privacyUrl)
.withListener(new ConsentFormListener() {
@Override
public void onConsentFormLoaded() {
showForm();
}

@Override
public void onConsentFormOpened() {
}

@Override
public void onConsentFormClosed(
ConsentStatus consentStatus, Boolean userPrefersAdFree) {
switch (consentStatus) {
case PERSONALIZED:
shouldAddConsentToMenu = true;
showPersonalizedAds();
break;
case NON_PERSONALIZED:
shouldAddConsentToMenu = true;
showNonPersonalizedAds();
break;
case UNKNOWN:
showNonPersonalizedAds();
break;
}
}

@Override
public void onConsentFormError(String errorDescription) {
}
})
.withPersonalizedAdsOption()
.withNonPersonalizedAdsOption()
.build();
form.load();
}

private void showPersonalizedAds() {
AdView adView = ((Activity) context).findViewById(R.id.adview1);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}

private void showNonPersonalizedAds() {
Bundle extras = new Bundle();
extras.putString("npa", "1");
AdView adView = ((Activity) context).findViewById(R.id.adview1);
AdRequest adRequest = new AdRequest.Builder()
.addNetworkExtrasBundle(AdMobAdapter.class, extras)
.build();
adView.loadAd(adRequest);
}

private void showForm() {
form.show();
}
}
27 changes: 27 additions & 0 deletions app/src/main/java/com/spitslide/ukpetitions/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
import android.view.Menu;
import android.view.MenuItem;

import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;


public class MainActivity extends AppCompatActivity {

private DrawerLayout drawerLayout;
Expand All @@ -25,6 +30,7 @@ public class MainActivity extends AppCompatActivity {
private boolean isSearchActivity;
private String state;
private String archived = "";
private Consent consent;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -100,6 +106,23 @@ public boolean onNavigationItemSelected(MenuItem menuItem) {
.add(R.id.content_frame, petitionsFragment)
.commit();

MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});

consent = new Consent(this);
consent.checkForConsentAndDisplayAds(false);
invalidateOptionsMenu();
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (consent.shouldAddConsentToMenu) {
menu.findItem(R.id.consent).setVisible(true);
}
return super.onPrepareOptionsMenu(menu);
}

@Override
Expand Down Expand Up @@ -166,6 +189,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
startActivity(rateIntentWeb);
}
break;
case R.id.consent:
consent = new Consent(MainActivity.this);
consent.checkForConsentAndDisplayAds(true);
break;
}
return super.onOptionsItemSelected(item);
}
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_above="@id/adview1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
Expand All @@ -17,6 +21,15 @@
android:background="?attr/colorPrimary"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
</FrameLayout>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/ADMOB_ADUNIT_ID"/>
</RelativeLayout>

<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/menu/menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@
android:id="@+id/rate"
app:showAsAction="never"
android:title="Rate"/>
<item
android:id="@+id/consent"
app:showAsAction="never"
android:visible="false"
android:title="Consent"/>
</menu>

0 comments on commit 32fbf89

Please sign in to comment.