Skip to content

Commit

Permalink
add type of Request and solve some errors, add
Browse files Browse the repository at this point in the history
examples
  • Loading branch information
ixiDev committed Jun 5, 2018
1 parent 7e83fbe commit dbce063
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 29 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
6 changes: 2 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ android {
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation'com.android.support:customtabs:27.1.1'
implementation project(':gdpr')
implementation 'com.google.android.gms:play-services-ads:15.0.1'
}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ixidev.gdprchecker_demo">

<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
46 changes: 46 additions & 0 deletions app/src/main/java/com/ixidev/gdprchecker_demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,65 @@
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.google.ads.mediation.admob.AdMobAdapter;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.ixidev.gdpr.GDPRChecker;

public class MainActivity extends AppCompatActivity {
private AdView adView;
private InterstitialAd interstitialAd;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new GDPRChecker()
.withContext(this)
<<<<<<< HEAD
.withPrivacyUrl("https://www.example.com/privacy")
.withPublisherIds("pub-xxxxxxxxxxxxxxxx")
=======
.withPrivacyUrl("Your privacy url ")
.withPublisherIds("Your Admob PublisherId") //
>>>>>>> add type of Request and solve some errors, add
.withTestMode()
.check();
adView = findViewById(R.id.ad_view);
interstitialAd = new InterstitialAd(this);

showBannerADS();
}


// example of using GDPRChecker
private void showBannerADS() {
AdRequest.Builder builder = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
GDPRChecker.Request request = GDPRChecker.getRequest();
if (request == GDPRChecker.Request.NON_PERSONALIZED) {
// load non Personalized ads
Bundle extras = new Bundle();
extras.putString("npa", "1");
builder.addNetworkExtrasBundle(AdMobAdapter.class, extras);
} // else do nothing , it will load PERSONALIZED ads
adView.loadAd(builder.build());

}

private void showInterstitialAds() {
interstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
AdRequest.Builder builder = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
GDPRChecker.Request request = GDPRChecker.getRequest();
if (request == GDPRChecker.Request.NON_PERSONALIZED) {
// load non Personalized ads
Bundle extras = new Bundle();
extras.putString("npa", "1");
builder.addNetworkExtrasBundle(AdMobAdapter.class, extras);
}
interstitialAd.loadAd(builder.build());
interstitialAd.show();
}
}
22 changes: 14 additions & 8 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<com.google.android.gms.ads.AdView
android:id="@+id/ad_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
app:adSize="BANNER"
app:adUnitId="ca-app-pub-3940256099942544/6300978111" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
android:layout_centerInParent="true"
android:text="Hello World!" />
</RelativeLayout>
6 changes: 3 additions & 3 deletions gdpr/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'com.android.support:appcompat-v7:27.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation'com.android.support:customtabs:27.1.1'
implementation 'com.google.android.ads.consent:consent-library:1.0.3'
implementation 'com.google.android.gms:play-services-ads:15.0.1'


}
84 changes: 70 additions & 14 deletions gdpr/src/main/java/com/ixidev/gdpr/GDPRChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.google.ads.consent.ConsentInformation;
import com.google.ads.consent.ConsentStatus;
import com.google.ads.consent.DebugGeography;
import com.google.gson.annotations.SerializedName;

import java.net.URL;

Expand All @@ -25,6 +26,8 @@ public class GDPRChecker {
private ConsentForm form;
private String[] publisherIds;
private static GDPRChecker instance;
private static Request request = Request.PERSONALIZED;
private boolean withAdFreeOption = false;

protected GDPRChecker(Context context) {
this.context = context;
Expand All @@ -45,21 +48,38 @@ public GDPRChecker withPrivacyUrl(String privacyUrl) {
throw new NullPointerException("Please call withContext first");
return instance;
}
@RequiresPermission(Manifest.permission.INTERNET)

@RequiresPermission(Manifest.permission.INTERNET)
private void initGDPR() {
// consentInformation.setDebugGeography(DebugGeography.DEBUG_GEOGRAPHY_EEA);
if (publisherIds == null)
throw new NullPointerException("publisherIds is null, please call withPublisherIds first");
consentInformation.requestConsentInfoUpdate(publisherIds, new ConsentInfoUpdateListener() {
@Override
public void onConsentInfoUpdated(ConsentStatus consentStatus) {
if (consentStatus == ConsentStatus.UNKNOWN)
if (consentInformation.isRequestLocationInEeaOrUnknown())
setupForm();
else consentInformation.setConsentStatus(consentStatus);
else
consentInformation.setConsentStatus(consentStatus);
Log.i(TAG, "onConsentInfoUpdated: " + consentStatus.name());

switch (consentStatus) {
case PERSONALIZED:
request = Request.PERSONALIZED;
Log.i(TAG, "onConsentInfoUpdated: Showing Personalized ads");
break;
case NON_PERSONALIZED:
request = Request.NON_PERSONALIZED;
Log.i(TAG, "onConsentInfoUpdated: Showing Non-Personalized ads");
break;
case UNKNOWN:
if (consentInformation.isRequestLocationInEeaOrUnknown()) {
setupForm();
} else {
request = Request.NON_PERSONALIZED;
Log.i(TAG, "onConsentInfoUpdated: case UNKNOWN :: GDPRChecker Request :: " + request.name());
}

break;
default:
request = Request.PERSONALIZED;
break;
}

}

@Override
Expand All @@ -80,7 +100,7 @@ private void setupForm() {
} catch (Exception e) {
Log.e(TAG, "initGDPR: ", e);
}
form = new ConsentForm.Builder(context, Url)
ConsentForm.Builder builder = new ConsentForm.Builder(context, Url)
.withListener(new ConsentFormListener() {
@Override
public void onConsentFormLoaded() {
Expand All @@ -95,9 +115,25 @@ public void onConsentFormOpened() {
@Override
public void onConsentFormClosed(
ConsentStatus consentStatus, Boolean userPrefersAdFree) {
Log.i(TAG, "onConsentFormClosed: " + consentStatus.name());
consentInformation.setConsentStatus(consentStatus);

if (userPrefersAdFree) {
Log.i(TAG, "Requesting Consent: User prefers AdFree");
} else {
Log.i(TAG, "Requesting Consent: Requesting consent again");
switch (consentStatus) {
case PERSONALIZED:
request = Request.PERSONALIZED;
break;
case NON_PERSONALIZED:
request = Request.NON_PERSONALIZED;
break;
case UNKNOWN:
request = Request.NON_PERSONALIZED;
break;
}
}

}

@Override
Expand All @@ -106,10 +142,11 @@ public void onConsentFormError(String errorDescription) {
}
})
.withPersonalizedAdsOption()
.withNonPersonalizedAdsOption()
.withAdFreeOption()
.build();
.withNonPersonalizedAdsOption();
if (withAdFreeOption)
builder.withAdFreeOption();

form = builder.build();
form.load();
}

Expand Down Expand Up @@ -142,4 +179,23 @@ public GDPRChecker withTestMode() {
throw new NullPointerException("Please call withContext first");
return instance;
}

public static Request getRequest() {
return request;
}

public enum Request {
@SerializedName("ADS_PERSONALIZED")
PERSONALIZED,
@SerializedName("ADS_NON_PERSONALIZED")
NON_PERSONALIZED
}

/**
* @param withAdFreeOption ; if true show " Pay for the ad-free version
* withAdFreeOption is false by default
*/
public void setWithAdFreeOption(boolean withAdFreeOption) {
this.withAdFreeOption = withAdFreeOption;
}
}

0 comments on commit dbce063

Please sign in to comment.