diff --git a/app/build.gradle b/app/build.gradle
index 2a6b59a..9f97b34 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -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 {
@@ -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']
+}
\ No newline at end of file
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index a54b321..f4e305b 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -10,6 +10,9 @@
android:supportsRtl="true"
android:name=".MyApplication"
android:theme="@style/AppTheme">
+
diff --git a/app/src/main/java/com/spitslide/ukpetitions/Consent.java b/app/src/main/java/com/spitslide/ukpetitions/Consent.java
new file mode 100644
index 0000000..f529c7c
--- /dev/null
+++ b/app/src/main/java/com/spitslide/ukpetitions/Consent.java
@@ -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();
+ }
+}
\ No newline at end of file
diff --git a/app/src/main/java/com/spitslide/ukpetitions/MainActivity.java b/app/src/main/java/com/spitslide/ukpetitions/MainActivity.java
index 411cc36..a99f9fe 100644
--- a/app/src/main/java/com/spitslide/ukpetitions/MainActivity.java
+++ b/app/src/main/java/com/spitslide/ukpetitions/MainActivity.java
@@ -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;
@@ -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) {
@@ -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
@@ -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);
}
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index bd22f52..aa798d8 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -6,8 +6,12 @@
android:layout_height="match_parent"
android:fitsSystemWindows="true">
+
+
+
+
\ No newline at end of file