|
10 | 10 | import com.facebook.react.bridge.ReadableMap; |
11 | 11 | import com.facebook.react.bridge.ReadableMapKeySetIterator; |
12 | 12 | import com.facebook.react.bridge.Callback; |
| 13 | +import com.facebook.react.bridge.ReadableType; |
13 | 14 | import com.facebook.react.bridge.WritableMap; |
14 | 15 | import com.mparticle.AttributionResult; |
15 | 16 | import com.mparticle.MParticle; |
|
20 | 21 | import com.mparticle.commerce.Product; |
21 | 22 | import com.mparticle.commerce.TransactionAttributes; |
22 | 23 | import com.mparticle.commerce.Promotion; |
| 24 | +import com.mparticle.consent.ConsentState; |
| 25 | +import com.mparticle.consent.GDPRConsent; |
23 | 26 | import com.mparticle.identity.AliasRequest; |
24 | 27 | import com.mparticle.identity.IdentityApi; |
25 | 28 | import com.mparticle.identity.IdentityApiRequest; |
|
35 | 38 | import java.util.List; |
36 | 39 | import java.util.Map; |
37 | 40 |
|
| 41 | +import javax.annotation.Nullable; |
| 42 | + |
38 | 43 | public class MParticleModule extends ReactContextBaseJavaModule { |
39 | 44 |
|
40 | 45 |
|
@@ -383,6 +388,36 @@ public void logPushRegistration(String instanceId, String senderId) { |
383 | 388 | } |
384 | 389 | } |
385 | 390 |
|
| 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 | + |
386 | 421 | private static IdentityApiRequest ConvertIdentityAPIRequest(ReadableMap map) { |
387 | 422 | IdentityApiRequest.Builder identityRequest = IdentityApiRequest.withEmptyUser(); |
388 | 423 | Map<MParticle.IdentityType, String> userIdentities = ConvertUserIdentities(map); |
@@ -716,4 +751,44 @@ private long parseMpid(String longString) { |
716 | 751 | return 0L; |
717 | 752 | } |
718 | 753 | } |
| 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 | + } |
719 | 794 | } |
0 commit comments