Skip to content

Commit acfb62e

Browse files
committed
Add Consent State support for Android
1 parent 5f0747e commit acfb62e

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

android/src/main/java/com/mparticle/react/MParticleModule.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.facebook.react.bridge.ReadableMap;
1111
import com.facebook.react.bridge.ReadableMapKeySetIterator;
1212
import com.facebook.react.bridge.Callback;
13+
import com.facebook.react.bridge.ReadableType;
1314
import com.facebook.react.bridge.WritableMap;
1415
import com.mparticle.AttributionResult;
1516
import com.mparticle.MParticle;
@@ -20,6 +21,8 @@
2021
import com.mparticle.commerce.Product;
2122
import com.mparticle.commerce.TransactionAttributes;
2223
import com.mparticle.commerce.Promotion;
24+
import com.mparticle.consent.ConsentState;
25+
import com.mparticle.consent.GDPRConsent;
2326
import com.mparticle.identity.AliasRequest;
2427
import com.mparticle.identity.IdentityApi;
2528
import com.mparticle.identity.IdentityApiRequest;
@@ -35,6 +38,8 @@
3538
import java.util.List;
3639
import java.util.Map;
3740

41+
import javax.annotation.Nullable;
42+
3843
public class MParticleModule extends ReactContextBaseJavaModule {
3944

4045

@@ -383,6 +388,36 @@ public void logPushRegistration(String instanceId, String senderId) {
383388
}
384389
}
385390

391+
@ReactMethod
392+
public void addGDPRConsentState(final ReadableMap map, String purpose) {
393+
MParticleUser currentUser = MParticle.getInstance().Identity().getCurrentUser();
394+
if (currentUser != null) {
395+
396+
397+
GDPRConsent consent = ConvertToGDPRConsent(map);
398+
if (consent != null) {
399+
ConsentState consentState = ConsentState.withConsentState(currentUser.getConsentState())
400+
.addGDPRConsentState(purpose, consent)
401+
.build();
402+
currentUser.setConsentState(consentState);
403+
Logger.info("GDPRConsentState added, \n\t\"purpose\": " + purpose + "\n" + consentState.toString());
404+
} else {
405+
Logger.warning("GDPRConsentState was not able to be deserialized, will not be added");
406+
}
407+
}
408+
}
409+
410+
@ReactMethod
411+
public void removeGDPRConsentStateWithPurpose(String purpose) {
412+
MParticleUser currentUser = MParticle.getInstance().Identity().getCurrentUser();
413+
if (currentUser != null) {
414+
ConsentState consentState = ConsentState.withConsentState(currentUser.getConsentState())
415+
.removeGDPRConsentState(purpose)
416+
.build();
417+
currentUser.setConsentState(consentState);
418+
}
419+
}
420+
386421
private static IdentityApiRequest ConvertIdentityAPIRequest(ReadableMap map) {
387422
IdentityApiRequest.Builder identityRequest = IdentityApiRequest.withEmptyUser();
388423
Map<MParticle.IdentityType, String> userIdentities = ConvertUserIdentities(map);
@@ -716,4 +751,44 @@ private long parseMpid(String longString) {
716751
return 0L;
717752
}
718753
}
754+
755+
@Nullable
756+
private GDPRConsent ConvertToGDPRConsent(ReadableMap map ) {
757+
Boolean consented;
758+
try {
759+
if (map.getType("consented").equals(ReadableType.Boolean)) {
760+
consented = map.getBoolean("consented");
761+
} else {
762+
consented = Boolean.valueOf(map.getString("consented"));
763+
}
764+
} catch (Exception ex) {
765+
Logger.error("failed to convert \"consented\" value to a Boolean, unable to process addGDPRConsentState");
766+
return null;
767+
}
768+
GDPRConsent.Builder builder = GDPRConsent.builder(consented);
769+
770+
if (map.hasKey("document")) {
771+
String document = map.getString("document");
772+
builder.document(document);
773+
}
774+
if (map.hasKey("hardwareId")) {
775+
String hardwareId = map.getString("hardwareId");
776+
builder.hardwareId(hardwareId);
777+
}
778+
if (map.hasKey("location")) {
779+
String location = map.getString("location");
780+
builder.location(location);
781+
}
782+
if (map.hasKey("timestamp")) {
783+
Long timestamp = null;
784+
try {
785+
String timestampString = map.getString("timestamp");
786+
timestamp = Long.valueOf(timestampString);
787+
builder.timestamp(timestamp);
788+
} catch (Exception ex) {
789+
Logger.warning("failed to convert \"timestamp\" value to Long");
790+
}
791+
}
792+
return builder.build();
793+
}
719794
}

0 commit comments

Comments
 (0)