Skip to content

Commit d225842

Browse files
authored
refactor: Rename AttributeListener to ModifyIdentityListener (#692)
* refactor: decouple AttributeListener from BaseAttributeListener AttributeListener now only declares identity APIs; kits implement BaseAttributeListener alongside it for user attribute callbacks. Update affected kits, AttributeListenerTestKit, and KitManagerImplTest mocks. Made-with: Cursor * Rename AttributeListener to ModifyIdentityListener Replace KitIntegration.AttributeListener with ModifyIdentityListener for set/remove user identity forwarding. Update KitManagerImpl, affected kits, and rename AttributeListenerTestKit to ModifyIdentityListenerTestKit. Clarify BaseAttributeListener Javadoc links and KitManagerImpl section comment. Made-with: Cursor * Remove BaseAttributeListener; fold into UserAttributeListener Drop the deprecated BaseAttributeListener type and merge its callbacks into UserAttributeListener. KitManagerImpl now forwards user attributes using UserAttributeListener only; incrementUserAttribute calls both increment and setUserAttribute on the same listener. Kits that previously implemented BaseAttributeListener without the extended callbacks now implement UserAttributeListener with no-op onIncrementUserAttribute, onSetUserTag, and onConsentStateUpdated. Update ModifyIdentityListenerTestKit and KitManagerImplTest accordingly. Made-with: Cursor
1 parent 6584c0e commit d225842

20 files changed

Lines changed: 350 additions & 140 deletions

File tree

android-kit-base/src/androidTest/kotlin/com/mparticle/kits/DataplanBlockingUserTests.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import com.mparticle.Utils.randomString
1111
import com.mparticle.identity.IdentityApiRequest
1212
import com.mparticle.internal.AccessUtils
1313
import com.mparticle.kits.DataplanFilterImpl.Companion.getEventsApiName
14-
import com.mparticle.kits.testkits.AttributeListenerTestKit
1514
import com.mparticle.kits.testkits.IdentityListenerTestKit
1615
import com.mparticle.kits.testkits.ListenerTestKit
16+
import com.mparticle.kits.testkits.ModifyIdentityListenerTestKit
1717
import com.mparticle.kits.testkits.UserAttributeListenerTestKit
1818
import com.mparticle.testutils.MPLatch
1919
import org.junit.Assert.assertEquals
@@ -25,7 +25,7 @@ import org.junit.Test
2525
import kotlin.random.Random
2626

2727
class DataplanBlockingUserTests : BaseKitOptionsTest() {
28-
private lateinit var attributeListenerKitKit: AttributeListenerTestKit
28+
private lateinit var attributeListenerKitKit: ModifyIdentityListenerTestKit
2929
private lateinit var identityListenerKitKit: IdentityListenerTestKit
3030
private lateinit var userAttributeListenerKitKit: UserAttributeListenerTestKit
3131
private lateinit var kitIntegrationTestKits: List<ListenerTestKit>
@@ -36,15 +36,15 @@ class DataplanBlockingUserTests : BaseKitOptionsTest() {
3636
.builder(mContext)
3737
.configuration(
3838
KitOptions {
39-
addKit(-1, AttributeListenerTestKit::class.java)
39+
addKit(-1, ModifyIdentityListenerTestKit::class.java)
4040
addKit(-2, IdentityListenerTestKit::class.java)
4141
addKit(-3, UserAttributeListenerTestKit::class.java)
4242
},
4343
).let {
4444
startMParticle(it)
4545
}
4646
attributeListenerKitKit =
47-
MParticle.getInstance()?.getKitInstance(-1) as AttributeListenerTestKit
47+
MParticle.getInstance()?.getKitInstance(-1) as ModifyIdentityListenerTestKit
4848
identityListenerKitKit =
4949
MParticle.getInstance()?.getKitInstance(-2) as IdentityListenerTestKit
5050
userAttributeListenerKitKit =

android-kit-base/src/androidTest/kotlin/com/mparticle/kits/KitManagerImplTests.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import com.mparticle.MParticle
77
import com.mparticle.MParticleOptions
88
import com.mparticle.internal.ConfigManager
99
import com.mparticle.internal.KitManager
10-
import com.mparticle.kits.testkits.AttributeListenerTestKit
1110
import com.mparticle.kits.testkits.IdentityListenerTestKit
11+
import com.mparticle.kits.testkits.ModifyIdentityListenerTestKit
1212
import com.mparticle.kits.testkits.UserAttributeListenerTestKit
1313
import com.mparticle.networking.Matcher
1414
import com.mparticle.testutils.MPLatch
@@ -22,7 +22,7 @@ class KitManagerImplTests : BaseKitOptionsTest() {
2222
@Test
2323
fun testKitIntializationViaKitOptions() {
2424
KitOptions()
25-
.addKit(1001, AttributeListenerTestKit::class.java)
25+
.addKit(1001, ModifyIdentityListenerTestKit::class.java)
2626
.addKit(1002, IdentityListenerTestKit::class.java)
2727
.addKit(1003, UserAttributeListenerTestKit::class.java)
2828
.let {
@@ -35,7 +35,7 @@ class KitManagerImplTests : BaseKitOptionsTest() {
3535

3636
fun getKit(kitId: Int) = MParticle.getInstance()?.getKitInstance(kitId)
3737

38-
assertTrue(getKit(1001) is AttributeListenerTestKit)
38+
assertTrue(getKit(1001) is ModifyIdentityListenerTestKit)
3939
assertTrue(getKit(1002) is IdentityListenerTestKit)
4040
assertTrue(getKit(1003) is UserAttributeListenerTestKit)
4141
}
@@ -62,10 +62,10 @@ class KitManagerImplTests : BaseKitOptionsTest() {
6262
.builder(mContext)
6363
.configuration(
6464
ConfiguredKitOptions {
65-
addKit(-1, AttributeListenerTestKit::class.java, JSONObject().put("eau", true))
65+
addKit(-1, ModifyIdentityListenerTestKit::class.java, JSONObject().put("eau", true))
6666
addKit(-2, IdentityListenerTestKit::class.java)
6767
addKit(-3, UserAttributeListenerTestKit::class.java)
68-
addKit(-4, AttributeListenerTestKit::class.java, JSONObject().put("eau", true))
68+
addKit(-4, ModifyIdentityListenerTestKit::class.java, JSONObject().put("eau", true))
6969
addKit(-5, IdentityListenerTestKit::class.java)
7070
addKit(-6, UserAttributeListenerTestKit::class.java)
7171
}.apply {

android-kit-base/src/androidTest/kotlin/com/mparticle/kits/testkits/AttributeListenerTestKit.kt renamed to android-kit-base/src/androidTest/kotlin/com/mparticle/kits/testkits/ModifyIdentityListenerTestKit.kt

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package com.mparticle.kits.testkits
22

33
import com.mparticle.MParticle
4+
import com.mparticle.consent.ConsentState
45
import com.mparticle.kits.FilteredMParticleUser
5-
import com.mparticle.kits.KitIntegration.AttributeListener
66
import com.mparticle.kits.KitIntegration.LogoutListener
7+
import com.mparticle.kits.KitIntegration.ModifyIdentityListener
8+
import com.mparticle.kits.KitIntegration.UserAttributeListener
79
import com.mparticle.kits.ReportingMessage
810

9-
open class AttributeListenerTestKit :
11+
open class ModifyIdentityListenerTestKit :
1012
ListenerTestKit(),
11-
AttributeListener,
13+
UserAttributeListener,
14+
ModifyIdentityListener,
1215
LogoutListener {
1316
var setUserAttributeCallback: ((attributeKey: String?, attributeValue: String?) -> Unit)? = null
1417
var setUserAttributeList: ((attributeKey: String?, attributeValueList: List<String>?) -> Unit)? =
@@ -76,5 +79,26 @@ open class AttributeListenerTestKit :
7679
onAttributeReceived?.invoke(key, value)
7780
}
7881

82+
override fun onIncrementUserAttribute(
83+
key: String?,
84+
incrementedBy: Number?,
85+
value: String?,
86+
user: FilteredMParticleUser?,
87+
) {
88+
}
89+
90+
override fun onSetUserTag(
91+
key: String?,
92+
user: FilteredMParticleUser?,
93+
) {
94+
}
95+
96+
override fun onConsentStateUpdated(
97+
oldState: ConsentState?,
98+
newState: ConsentState?,
99+
user: FilteredMParticleUser?,
100+
) {
101+
}
102+
79103
override fun logout(): List<ReportingMessage> = logout?.invoke() ?: listOf()
80104
}

android-kit-base/src/main/java/com/mparticle/kits/KitIntegration.java

Lines changed: 59 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -361,69 +361,10 @@ public interface LogoutListener {
361361
}
362362

363363
/**
364-
* Temporary shared contract used while {@link AttributeListener} behavior is migrated to
365-
* {@link UserAttributeListener}. Factoring out common API surface lets the SDK land incremental changes
366-
* and smaller pull requests instead of a single large refactor.
367-
* <p>
368-
* Kits implement {@link AttributeListener} and/or {@link UserAttributeListener}; they do not implement this
369-
* type directly.
364+
* Identity forwarding for kits that also receive user attribute callbacks. Implement together with
365+
* {@link UserAttributeListener} when the kit should receive user attribute updates.
370366
*/
371-
@Deprecated
372-
public interface BaseAttributeListener {
373-
374-
/**
375-
* Indicate to the mParticle Kit framework if this listener supports attribute-values as lists.
376-
* <p>
377-
* If false, list-specific APIs are not used; values are passed via scalar/csv paths instead.
378-
*
379-
* @return true if this listener supports attribute values as lists.
380-
*/
381-
boolean supportsAttributeLists();
382-
383-
/**
384-
* Called when a user attribute is removed for the current user.
385-
*
386-
* @param key attribute key
387-
* @param user filtered user context for this kit
388-
*/
389-
void onRemoveUserAttribute(String key, FilteredMParticleUser user);
390-
391-
/**
392-
* Called when a scalar user attribute is set for the current user.
393-
*
394-
* @param key attribute key
395-
* @param value attribute value (may be non-String for some {@link UserAttributeListener} call paths)
396-
* @param user filtered user context for this kit
397-
*/
398-
void onSetUserAttribute(String key, Object value, FilteredMParticleUser user);
399-
400-
/**
401-
* Called when a list-valued user attribute is set and {@link #supportsAttributeLists()} returns true.
402-
*
403-
* @param attributeKey attribute key (may be null)
404-
* @param attributeValueList attribute values (may be null)
405-
* @param user filtered user context for this kit (may be null)
406-
*/
407-
void onSetUserAttributeList(
408-
@Nullable String attributeKey,
409-
@Nullable List<String> attributeValueList,
410-
@Nullable FilteredMParticleUser user);
411-
412-
/**
413-
* Called when the full set of user attributes is synchronized for the current user.
414-
*
415-
* @param userAttributes scalar user attributes
416-
* @param userAttributeLists list-valued user attributes when {@link #supportsAttributeLists()} is true;
417-
* otherwise list values may be merged into scalars by the framework
418-
* @param user filtered user context for this kit
419-
*/
420-
void onSetAllUserAttributes(
421-
Map<String, String> userAttributes,
422-
Map<String, List<String>> userAttributeLists,
423-
FilteredMParticleUser user);
424-
}
425-
426-
public interface AttributeListener extends BaseAttributeListener {
367+
public interface ModifyIdentityListener {
427368

428369
void setUserIdentity(MParticle.IdentityType identityType, String identity);
429370

@@ -580,14 +521,68 @@ public interface IdentityListener {
580521

581522
}
582523

583-
public interface UserAttributeListener extends BaseAttributeListener {
524+
/**
525+
* Kits should implement this interface to receive user attribute updates, tags, increments, consent changes,
526+
* and full attribute syncs from the mParticle SDK.
527+
*/
528+
public interface UserAttributeListener {
529+
530+
/**
531+
* Indicate to the mParticle Kit framework if this listener supports attribute-values as lists.
532+
* <p>
533+
* If false, list-specific APIs are not used; values are passed via scalar/csv paths instead.
534+
*
535+
* @return true if this listener supports attribute values as lists.
536+
*/
537+
boolean supportsAttributeLists();
538+
539+
/**
540+
* Called when a user attribute is removed for the current user.
541+
*
542+
* @param key attribute key
543+
* @param user filtered user context for this kit
544+
*/
545+
void onRemoveUserAttribute(String key, FilteredMParticleUser user);
546+
547+
/**
548+
* Called when a scalar user attribute is set for the current user.
549+
*
550+
* @param key attribute key
551+
* @param value attribute value (may be non-String for some call paths)
552+
* @param user filtered user context for this kit
553+
*/
554+
void onSetUserAttribute(String key, Object value, FilteredMParticleUser user);
555+
556+
/**
557+
* Called when a list-valued user attribute is set and {@link #supportsAttributeLists()} returns true.
558+
*
559+
* @param attributeKey attribute key (may be null)
560+
* @param attributeValueList attribute values (may be null)
561+
* @param user filtered user context for this kit (may be null)
562+
*/
563+
void onSetUserAttributeList(
564+
@Nullable String attributeKey,
565+
@Nullable List<String> attributeValueList,
566+
@Nullable FilteredMParticleUser user);
567+
568+
/**
569+
* Called when the full set of user attributes is synchronized for the current user.
570+
*
571+
* @param userAttributes scalar user attributes
572+
* @param userAttributeLists list-valued user attributes when {@link #supportsAttributeLists()} is true;
573+
* otherwise list values may be merged into scalars by the framework
574+
* @param user filtered user context for this kit
575+
*/
576+
void onSetAllUserAttributes(
577+
Map<String, String> userAttributes,
578+
Map<String, List<String>> userAttributeLists,
579+
FilteredMParticleUser user);
584580

585581
void onIncrementUserAttribute(String key, Number incrementedBy, String value, FilteredMParticleUser user);
586582

587583
void onSetUserTag(String key, FilteredMParticleUser user);
588584

589585
void onConsentStateUpdated(ConsentState oldState, ConsentState newState, FilteredMParticleUser user);
590-
591586
}
592587

593588
public interface BatchListener {

android-kit-base/src/main/java/com/mparticle/kits/KitManagerImpl.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,8 @@ private void initializeKit(KitIntegration activeKit) {
310310
}
311311
}
312312

313-
if (activeKit instanceof KitIntegration.AttributeListener) {
314-
syncUserIdentities((KitIntegration.AttributeListener) activeKit, activeKit.getConfiguration());
313+
if (activeKit instanceof KitIntegration.ModifyIdentityListener) {
314+
syncUserIdentities((KitIntegration.ModifyIdentityListener) activeKit, activeKit.getConfiguration());
315315
}
316316

317317
MParticle instance = MParticle.getInstance();
@@ -592,14 +592,14 @@ public boolean onPushRegistration(String token, String senderId) {
592592
}
593593

594594
//================================================================================
595-
// KitIntegration.AttributeListener forwarding
595+
// UserAttributeListener user-attribute forwarding
596596
//================================================================================
597597
public void onUserAttributesReceived(Map<String, String> userAttributes, Map<String, List<String>> userAttributeLists, Long mpid) {
598598
userAttributes = mDataplanFilter.transformUserAttributes(userAttributes);
599599
userAttributeLists = mDataplanFilter.transformUserAttributes(userAttributeLists);
600600
for (KitIntegration provider : providers.values()) {
601601
try {
602-
if ((provider instanceof KitIntegration.BaseAttributeListener listener)
602+
if ((provider instanceof KitIntegration.UserAttributeListener listener)
603603
&& !provider.isDisabled()) {
604604
Map<String, String> filteredAttributeSingles = (Map<String, String>) KitConfiguration.filterAttributes(provider.getConfiguration().getUserAttributeFilters(),
605605
userAttributes);
@@ -623,7 +623,7 @@ public void onUserAttributesReceived(Map<String, String> userAttributes, Map<Str
623623
}
624624
}
625625

626-
private void syncUserIdentities(KitIntegration.AttributeListener attributeListener, KitConfiguration configuration) {
626+
private void syncUserIdentities(KitIntegration.ModifyIdentityListener listener, KitConfiguration configuration) {
627627
MParticle instance = MParticle.getInstance();
628628
if (instance != null) {
629629
MParticleUser user = instance.Identity().getCurrentUser();
@@ -632,7 +632,7 @@ private void syncUserIdentities(KitIntegration.AttributeListener attributeListen
632632
if (identities != null) {
633633
for (Map.Entry<MParticle.IdentityType, String> entry : identities.entrySet()) {
634634
if (configuration.shouldSetIdentity(entry.getKey())) {
635-
attributeListener.setUserIdentity(entry.getKey(), entry.getValue());
635+
listener.setUserIdentity(entry.getKey(), entry.getValue());
636636
}
637637
}
638638
}
@@ -669,7 +669,7 @@ public void setUserAttributeList(String attributeKey, List<String> valuesList, l
669669
}
670670

671671
private void setUserAttribute(KitIntegration provider, String attributeKey, List<String> valueList, long mpid) {
672-
if ((provider instanceof KitIntegration.BaseAttributeListener listener)
672+
if ((provider instanceof KitIntegration.UserAttributeListener listener)
673673
&& !provider.isDisabled()
674674
&& KitConfiguration.shouldForwardAttribute(provider.getConfiguration().getUserAttributeFilters(), attributeKey)) {
675675
boolean supportsAttributeLists = listener.supportsAttributeLists();
@@ -683,7 +683,7 @@ private void setUserAttribute(KitIntegration provider, String attributeKey, List
683683
}
684684

685685
private void setUserAttribute(KitIntegration provider, String attributeKey, String attributeValue, long mpid) {
686-
if ((provider instanceof KitIntegration.BaseAttributeListener listener)
686+
if ((provider instanceof KitIntegration.UserAttributeListener listener)
687687
&& !provider.isDisabled()
688688
&& KitConfiguration.shouldForwardAttribute(provider.getConfiguration().getUserAttributeFilters(),
689689
attributeKey)) {
@@ -698,7 +698,7 @@ public void removeUserAttribute(String key, long mpid) {
698698
}
699699
for (KitIntegration provider : providers.values()) {
700700
try {
701-
if ((provider instanceof KitIntegration.BaseAttributeListener listener)
701+
if ((provider instanceof KitIntegration.UserAttributeListener listener)
702702
&& !provider.isDisabled()
703703
&& KitConfiguration.shouldForwardAttribute(provider.getConfiguration().getUserAttributeFilters(), key)) {
704704
listener.onRemoveUserAttribute(key, FilteredMParticleUser.getInstance(mpid, provider));
@@ -716,12 +716,11 @@ public void incrementUserAttribute(String key, Number incrementedBy, String newV
716716
}
717717
for (KitIntegration provider : providers.values()) {
718718
try {
719-
if (!provider.isDisabled() && KitConfiguration.shouldForwardAttribute(provider.getConfiguration().getUserAttributeFilters(), key))
720-
if (provider instanceof KitIntegration.UserAttributeListener) {
721-
((KitIntegration.UserAttributeListener) provider).onIncrementUserAttribute(key, incrementedBy, newValue, FilteredMParticleUser.getInstance(mpid, provider));
719+
if (!provider.isDisabled() && KitConfiguration.shouldForwardAttribute(provider.getConfiguration().getUserAttributeFilters(), key)) {
720+
if (provider instanceof KitIntegration.UserAttributeListener listener) {
721+
listener.onIncrementUserAttribute(key, incrementedBy, newValue, FilteredMParticleUser.getInstance(mpid, provider));
722+
listener.onSetUserAttribute(key, newValue, FilteredMParticleUser.getInstance(mpid, provider));
722723
}
723-
if (provider instanceof KitIntegration.BaseAttributeListener listener) {
724-
listener.onSetUserAttribute(key, newValue, FilteredMParticleUser.getInstance(mpid, provider));
725724
}
726725
} catch (Exception e) {
727726
Logger.warning("Failed to call onIncrementUserAttribute for kit: " + provider.getName() + ": " + e.getMessage());
@@ -753,8 +752,8 @@ public void setUserIdentity(String id, MParticle.IdentityType identityType) {
753752
}
754753
for (KitIntegration provider : providers.values()) {
755754
try {
756-
if (provider instanceof KitIntegration.AttributeListener && !provider.isDisabled() && provider.getConfiguration().shouldSetIdentity(identityType)) {
757-
((KitIntegration.AttributeListener) provider).setUserIdentity(identityType, id);
755+
if (provider instanceof KitIntegration.ModifyIdentityListener && !provider.isDisabled() && provider.getConfiguration().shouldSetIdentity(identityType)) {
756+
((KitIntegration.ModifyIdentityListener) provider).setUserIdentity(identityType, id);
758757
}
759758
} catch (Exception e) {
760759
Logger.warning("Failed to call setUserIdentity for kit: " + provider.getName() + ": " + e.getMessage());
@@ -769,8 +768,8 @@ public void removeUserIdentity(MParticle.IdentityType identityType) {
769768
}
770769
for (KitIntegration provider : providers.values()) {
771770
try {
772-
if (provider instanceof KitIntegration.AttributeListener && !provider.isDisabled()) {
773-
((KitIntegration.AttributeListener) provider).removeUserIdentity(identityType);
771+
if (provider instanceof KitIntegration.ModifyIdentityListener && !provider.isDisabled()) {
772+
((KitIntegration.ModifyIdentityListener) provider).removeUserIdentity(identityType);
774773
}
775774
} catch (Exception e) {
776775
Logger.warning("Failed to call removeUserIdentity for kit: " + provider.getName() + ": " + e.getMessage());

0 commit comments

Comments
 (0)