From 665d1e709d0fa741d23a02f0317e7f78e2150b73 Mon Sep 17 00:00:00 2001 From: m1ga Date: Thu, 4 Apr 2024 19:53:55 +0200 Subject: [PATCH] feat(android): consent mode --- android/build.gradle | 2 +- android/manifest | 2 +- .../TitaniumFirebaseAnalyticsModule.java | 41 +++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index dc52dbe..753bb13 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -3,6 +3,6 @@ repositories { } dependencies { - implementation 'com.google.firebase:firebase-analytics:21.0.0' + implementation 'com.google.firebase:firebase-analytics:21.5.1' implementation 'com.google.firebase:firebase-installations:17.0.1' } diff --git a/android/manifest b/android/manifest index 4819cba..809f797 100644 --- a/android/manifest +++ b/android/manifest @@ -2,7 +2,7 @@ # this is your module manifest and used by Titanium # during compilation, packaging, distribution, etc. # -version: 5.2.0 +version: 5.3.0 apiversion: 4 architectures: arm64-v8a armeabi-v7a x86 x86_64 description: titanium-firebase-analytics diff --git a/android/src/firebase/analytics/TitaniumFirebaseAnalyticsModule.java b/android/src/firebase/analytics/TitaniumFirebaseAnalyticsModule.java index 2671aeb..fec189a 100644 --- a/android/src/firebase/analytics/TitaniumFirebaseAnalyticsModule.java +++ b/android/src/firebase/analytics/TitaniumFirebaseAnalyticsModule.java @@ -11,9 +11,12 @@ import android.os.Bundle; import com.google.firebase.analytics.FirebaseAnalytics; +import com.google.firebase.analytics.FirebaseAnalytics.ConsentStatus; +import com.google.firebase.analytics.FirebaseAnalytics.ConsentType; import com.google.firebase.installations.FirebaseInstallations; import java.io.IOException; +import java.util.EnumMap; import java.util.Map; import org.appcelerator.kroll.KrollDict; import org.appcelerator.kroll.KrollFunction; @@ -32,6 +35,9 @@ public class TitaniumFirebaseAnalyticsModule extends KrollModule private static final boolean DBG = TiConfig.LOGD; private static FirebaseAnalytics mFirebaseAnalytics; + public static final int GRANTED = 0; + public static final int DENIED = 1; + @SuppressLint("MissingPermission") // Android Studio only doesn't know it's included via tiapp.xml, so all good here! private FirebaseAnalytics analyticsInstance() { @@ -53,6 +59,41 @@ public void resetAnalyticsData() { this.analyticsInstance().resetAnalyticsData(); } + @Kroll.method + public void setConsent(KrollDict opts) + { + int analyticsStorage = TiConvert.toInt(opts.get("analyticsStorage"), -1); + int adStorage = TiConvert.toInt(opts.get("adStorage"), -1); + int adUserData = TiConvert.toInt(opts.get("adUserData"), -1); + int adPerson = TiConvert.toInt(opts.get("adPersonalization"), -1); + + Map consentMap = new EnumMap<>(ConsentType.class); + if (analyticsStorage == GRANTED) { + consentMap.put(ConsentType.ANALYTICS_STORAGE, ConsentStatus.GRANTED); + } else if (analyticsStorage == DENIED) { + consentMap.put(ConsentType.ANALYTICS_STORAGE, ConsentStatus.DENIED); + } + if (adStorage == GRANTED) { + consentMap.put(ConsentType.AD_STORAGE, ConsentStatus.GRANTED); + } else if (adStorage == DENIED) { + consentMap.put(ConsentType.AD_STORAGE, ConsentStatus.DENIED); + } + if (adUserData == GRANTED) { + consentMap.put(ConsentType.AD_USER_DATA, ConsentStatus.GRANTED); + } else if (adUserData == DENIED) { + consentMap.put(ConsentType.AD_USER_DATA, ConsentStatus.DENIED); + } + if (adPerson == GRANTED) { + consentMap.put(ConsentType.AD_PERSONALIZATION, ConsentStatus.GRANTED); + } else if (adPerson == DENIED) { + consentMap.put(ConsentType.AD_PERSONALIZATION, ConsentStatus.DENIED); + } + + if (mFirebaseAnalytics != null) { + mFirebaseAnalytics.setConsent(consentMap); + } + + } @Kroll.method @Kroll.setProperty