Skip to content

Commit 837d535

Browse files
malandr2copybara-github
authored andcommitted
Add GDPR snippets for consent choices and TFUA
PiperOrigin-RevId: 828523758
1 parent ac892f4 commit 837d535

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

java/advanced/APIDemo/app/src/main/java/com/google/android/gms/snippets/UMPSnippets.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,39 @@
1717
package com.google.android.gms.snippets;
1818

1919
import android.app.Activity;
20+
import android.content.Context;
21+
import android.content.SharedPreferences;
2022
import androidx.annotation.NonNull;
23+
import androidx.preference.PreferenceManager;
2124
import com.google.android.gms.appset.AppSet;
2225
import com.google.android.gms.appset.AppSetIdClient;
2326
import com.google.android.ump.ConsentRequestParameters;
2427

2528
final class UMPSnippets {
2629

30+
private void setTagForUnderAgeOfConsent() {
31+
// [START set_tag_for_under_age_of_consent]
32+
ConsentRequestParameters params =
33+
new ConsentRequestParameters.Builder()
34+
// Indicate the user is under age of consent.
35+
.setTagForUnderAgeOfConsent(true)
36+
.build();
37+
// [END set_tag_for_under_age_of_consent]
38+
}
39+
40+
private void readConsentChoices(Context context) {
41+
// [START read_consent_choices]
42+
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
43+
// Example value: "1111111111"
44+
String purposeConsents = sharedPref.getString("IABTCF_PurposeConsents", "");
45+
// Purposes are zero-indexed. Index 0 contains information about Purpose 1.
46+
if (!purposeConsents.isEmpty()) {
47+
String purposeOneString = String.valueOf(purposeConsents.charAt(0));
48+
boolean hasConsentForPurposeOne = purposeOneString.equals("1");
49+
}
50+
// [END read_consent_choices]
51+
}
52+
2753
private void syncConsentIdentifier(@NonNull Activity activity) {
2854
// [START sync_consent_identifier]
2955
// Example fetching App Set ID to identify the user across apps.

kotlin/advanced/APIDemo/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ dependencies {
4343
// AndroidX KTX Extensions (Fragment, Activity)
4444
implementation 'androidx.activity:activity-ktx:1.10.1'
4545
implementation 'androidx.fragment:fragment-ktx:1.8.8'
46+
implementation 'androidx.preference:preference-ktx:1.2.1'
4647

4748
// AndroidX Lifecycle Components (ViewModel, LiveData, Runtime)
4849
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.9.1'

kotlin/advanced/APIDemo/app/src/main/java/com/google/android/gms/snippets/UMPSnippets.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,37 @@
1717
package com.google.android.gms.snippets
1818

1919
import android.app.Activity
20+
import android.content.Context
21+
import androidx.preference.PreferenceManager
2022
import com.google.android.gms.appset.AppSet
2123
import com.google.android.gms.appset.AppSetIdInfo
2224
import com.google.android.ump.ConsentRequestParameters
2325

2426
private class UMPSnippets {
2527

28+
private fun setTagForUnderAgeOfConsent() {
29+
// [START set_tag_for_under_age_of_consent]
30+
val params =
31+
ConsentRequestParameters.Builder()
32+
// Indicate the user is under age of consent.
33+
.setTagForUnderAgeOfConsent(true)
34+
.build()
35+
// [END set_tag_for_under_age_of_consent]
36+
}
37+
38+
private fun readConsentChoices(context: Context) {
39+
// [START read_consent_choices]
40+
val sharedPref = PreferenceManager.getDefaultSharedPreferences(context)
41+
// Example value: "1111111111"
42+
val purposeConsents = sharedPref.getString("IABTCF_PurposeConsents", "")
43+
// Purposes are zero-indexed. Index 0 contains information about Purpose 1.
44+
if (!purposeConsents.isNullOrEmpty()) {
45+
val purposeOneString = purposeConsents.first().toString()
46+
val hasConsentForPurposeOne = purposeOneString == "1"
47+
}
48+
// [END read_consent_choices]
49+
}
50+
2651
private fun syncConsentIdentifier(activity: Activity) {
2752
// [START sync_consent_identifier]
2853
// Example fetching App Set ID to identify the user across apps.

0 commit comments

Comments
 (0)