diff --git a/android-core/src/main/java/com/mparticle/MParticleOptions.java b/android-core/src/main/java/com/mparticle/MParticleOptions.java index bfda3ea86..286a9eaa2 100644 --- a/android-core/src/main/java/com/mparticle/MParticleOptions.java +++ b/android-core/src/main/java/com/mparticle/MParticleOptions.java @@ -442,7 +442,15 @@ public Builder installType(@NonNull MParticle.InstallType installType) { */ @NonNull public Builder sideloadedKits(@NonNull List kits) { - this.sideloadedKits = kits; + List _kits = new ArrayList<>(); + for (SideloadedKit kit : kits) { + if (kit.kitId() < 1000000) { + Logger.error("Sideloaded kit " + kit.getName() + " must have a kitId greater or equal than 1000000, current one is " + kit.kitId() + " and will not be included."); + } else { + _kits.add(kit); + } + } + this.sideloadedKits = _kits; return this; } diff --git a/android-core/src/main/java/com/mparticle/internal/ConfigManager.java b/android-core/src/main/java/com/mparticle/internal/ConfigManager.java index a0732b19c..cf225f843 100644 --- a/android-core/src/main/java/com/mparticle/internal/ConfigManager.java +++ b/android-core/src/main/java/com/mparticle/internal/ConfigManager.java @@ -357,7 +357,7 @@ public synchronized void configUpToDate() throws JSONException { String config = getKitConfigPreferences().getString(KIT_CONFIG_KEY, ""); if (!config.isEmpty()) { JSONArray kitConfig = new JSONArray(config); - JSONArray combined = SideloadedKitsUtils.INSTANCE.removeSideloadedKitsAndCombine(kitConfig, sideloadedKits); + JSONArray combined = SideloadedKitsUtils.INSTANCE.combineConfig(kitConfig, sideloadedKits); getKitConfigPreferences() .edit() .putString(KIT_CONFIG_KEY, combined.toString()) diff --git a/android-core/src/main/java/com/mparticle/internal/SideloadedKit.kt b/android-core/src/main/java/com/mparticle/internal/SideloadedKit.kt index 9e47b8d29..54021580d 100644 --- a/android-core/src/main/java/com/mparticle/internal/SideloadedKit.kt +++ b/android-core/src/main/java/com/mparticle/internal/SideloadedKit.kt @@ -3,5 +3,10 @@ package com.mparticle.internal import org.json.JSONObject interface SideloadedKit { + fun getJsonConfig(): JSONObject? + + fun kitId(): Int + + fun getName(): String } diff --git a/android-core/src/main/java/com/mparticle/internal/SideloadedKitsUtils.kt b/android-core/src/main/java/com/mparticle/internal/SideloadedKitsUtils.kt index 93de13a50..705dc9e91 100644 --- a/android-core/src/main/java/com/mparticle/internal/SideloadedKitsUtils.kt +++ b/android-core/src/main/java/com/mparticle/internal/SideloadedKitsUtils.kt @@ -5,32 +5,26 @@ import org.json.JSONArray object SideloadedKitsUtils { fun combineConfig(kitConfig: JSONArray?, kits: List): JSONArray { - var kitArray: JSONArray? = kitConfig - if (kitArray == null) { - kitArray = JSONArray() - } - kits.forEach { kit -> - kit.getJsonConfig()?.let { - kitArray.put(it) - } ?: kotlin.run { Logger.debug("Issue with kit ${kit.javaClass.simpleName}") } - } - return kitArray - } - - fun removeSideloadedKitsAndCombine(kitConfig: JSONArray, kits: List): JSONArray { var results = JSONArray() - if (!kits.isEmpty()) { - val sideloadedIds = kits.map { it.getJsonConfig()?.optInt("id", -100) } + var addedIds = mutableSetOf() + kitConfig?.let { kitConfig -> for (i in 0 until kitConfig.length()) { - val obj = kitConfig.getJSONObject(i) - val id = obj.optInt("id", -1) - if (id != -1 && !sideloadedIds.contains(id)) { - results.put(obj) + val kit = kitConfig.getJSONObject(i) + val id = kit.optInt("id", -1) + if (id != -1 && id < 1000000 && !addedIds.contains(id)) { + results.put(kit) + addedIds.add(id) + } + } + } + kits.forEach { kit -> + if (!addedIds.contains(kit.kitId())) { + kit.getJsonConfig()?.let { + results.put(it) + addedIds.add(kit.kitId()) } } - } else { - results = kitConfig } - return combineConfig(results, kits) + return results } } diff --git a/android-kit-base/src/main/java/com/mparticle/kits/KitConfiguration.java b/android-kit-base/src/main/java/com/mparticle/kits/KitConfiguration.java index 88dad8a06..f28483f4f 100644 --- a/android-kit-base/src/main/java/com/mparticle/kits/KitConfiguration.java +++ b/android-kit-base/src/main/java/com/mparticle/kits/KitConfiguration.java @@ -461,7 +461,7 @@ public KitConfiguration() { } public KitConfiguration parseConfiguration(JSONObject json) throws JSONException { - kitId = json.optInt(KEY_ID, KitIntegrationFactory.getSideloadedKitId()); + kitId = json.optInt(KEY_ID); if (json.has(KEY_ATTRIBUTE_VALUE_FILTERING)) { avfIsActive = true; try { diff --git a/android-kit-base/src/main/java/com/mparticle/kits/KitIntegrationFactory.java b/android-kit-base/src/main/java/com/mparticle/kits/KitIntegrationFactory.java index dc09007e1..93dd7fcec 100644 --- a/android-kit-base/src/main/java/com/mparticle/kits/KitIntegrationFactory.java +++ b/android-kit-base/src/main/java/com/mparticle/kits/KitIntegrationFactory.java @@ -15,9 +15,8 @@ public class KitIntegrationFactory { final Map supportedKits = new HashMap<>(); - public static int minSideloadedKitId = 1000000; - private static int sideloadedKitNextId = minSideloadedKitId; private Map knownIntegrations = new HashMap(); + private Map sideloadedKitMap = new HashMap<>(); public KitIntegrationFactory(MParticleOptions options) { supportedKits.clear(); @@ -26,10 +25,6 @@ public KitIntegrationFactory(MParticleOptions options) { loadIntegrations(options); } - public static int getSideloadedKitId() { - return sideloadedKitNextId++; - } - /** * This is the canonical method mapping all known Kit/Module IDs to Kit class names. * Mapping of module Ids to kit classes @@ -72,7 +67,15 @@ private void setupKnownIntegrations() { } public KitIntegration createInstance(KitManagerImpl manager, KitConfiguration configuration) throws JSONException, ClassNotFoundException { - KitIntegration kit = createInstance(manager, configuration.getKitId()); + KitIntegration kit; + if (configuration.getKitId() >= MPSideloadedKit.MIN_SIDELOADED_KIT) { + kit = sideloadedKitMap.get(configuration.getKitId()); + if (kit != null && kit.getKitManager() == null) { + kit.setKitManager(manager); + } + } else { + kit = createInstance(manager, configuration.getKitId()); + } if (kit != null) { kit.setConfiguration(configuration); } @@ -93,11 +96,13 @@ public KitIntegration createInstance(KitManagerImpl manager, int moduleId) throw } private void loadSideloadedIntegrations(MParticleOptions options) { + sideloadedKitMap.clear(); for (SideloadedKit entry : options.getSideloadedKits()) { - if (entry instanceof MPSideloadedKit && !knownIntegrations.containsKey(((MPSideloadedKit) entry).getConfiguration().getKitId())) { + if (entry instanceof MPSideloadedKit && !supportedKits.containsKey(((MPSideloadedKit) entry).getConfiguration().getKitId())) { int kitId = ((MPSideloadedKit) entry).getConfiguration().getKitId(); - Class kitClazz = entry.getClass(); - knownIntegrations.put(kitId, kitClazz.getName()); + supportedKits.put(kitId, entry.getClass()); + sideloadedKitMap.put(kitId, (MPSideloadedKit) entry); + Logger.debug(((MPSideloadedKit) entry).getName() + " detected with kit id " + kitId); } } } @@ -140,6 +145,6 @@ public Set getSupportedKits() { } public boolean isSupported(int kitModuleId) { - return supportedKits.containsKey(kitModuleId) || kitModuleId >= minSideloadedKitId; + return supportedKits.containsKey(kitModuleId) || kitModuleId >= MPSideloadedKit.MIN_SIDELOADED_KIT; } } diff --git a/android-kit-base/src/main/java/com/mparticle/kits/KitManagerImpl.java b/android-kit-base/src/main/java/com/mparticle/kits/KitManagerImpl.java index 07b2c3b0d..1c14aa5d4 100644 --- a/android-kit-base/src/main/java/com/mparticle/kits/KitManagerImpl.java +++ b/android-kit-base/src/main/java/com/mparticle/kits/KitManagerImpl.java @@ -223,8 +223,10 @@ protected synchronized void configureKits(@NonNull List kitCon if (getSupportedKits().contains(configuration.getKitId())) { activeKit = mKitIntegrationFactory.createInstance(KitManagerImpl.this, configuration); activeKitInstanceCreated = activeKit != null; - }} catch (Exception npe) {} - if (!activeKitInstanceCreated && configuration.getKitId() >= KitIntegrationFactory.minSideloadedKitId) { + } + } catch (Exception npe) { + } + if (!activeKitInstanceCreated && configuration.getKitId() >= MPSideloadedKit.MIN_SIDELOADED_KIT) { Logger.verbose("De-initializing sideloaded kit with id: " + configuration.getKitId()); continue; } diff --git a/android-kit-base/src/main/java/com/mparticle/kits/MPSideloadedKit.kt b/android-kit-base/src/main/java/com/mparticle/kits/MPSideloadedKit.kt index 1015df2ff..14a89213c 100644 --- a/android-kit-base/src/main/java/com/mparticle/kits/MPSideloadedKit.kt +++ b/android-kit-base/src/main/java/com/mparticle/kits/MPSideloadedKit.kt @@ -3,20 +3,31 @@ package com.mparticle.kits import com.mparticle.internal.SideloadedKit import org.json.JSONObject -abstract class MPSideloadedKit : KitIntegration(), SideloadedKit { +abstract class MPSideloadedKit(val kitId: Int) : KitIntegration(), SideloadedKit { + + companion object { + const val MIN_SIDELOADED_KIT = 1000000 + } init { - configuration = KitConfiguration.createKitConfiguration(JSONObject()) + configuration = KitConfiguration.createKitConfiguration( + JSONObject().put( + KitConfiguration.KEY_ID, + kitId + ) + ) } + override fun kitId(): Int = kitId + fun addFilters(filter: MPSideloadedFilters): MPSideloadedKit { configuration = configuration.applyFilters(filter) return this } - override fun getJsonConfig(): JSONObject = super.getJsonConfig() + override fun getJsonConfig(): JSONObject? = super.getJsonConfig() - private fun KitConfiguration.applyFilters(filters: MPSideloadedFilters): KitConfiguration { - return configuration.applySideloadedKits(filters) + private fun KitConfiguration.applyFilters(filters: MPSideloadedFilters): KitConfiguration? { + return configuration?.applySideloadedKits(filters) } } diff --git a/android-kit-base/src/test/kotlin/com/mparticle/kits/KitManagerImplTest.kt b/android-kit-base/src/test/kotlin/com/mparticle/kits/KitManagerImplTest.kt index c57ea102e..ac7577106 100644 --- a/android-kit-base/src/test/kotlin/com/mparticle/kits/KitManagerImplTest.kt +++ b/android-kit-base/src/test/kotlin/com/mparticle/kits/KitManagerImplTest.kt @@ -495,7 +495,7 @@ class KitManagerImplTest { fun testMParticleConfigureKitsFromOptions() { val sideloadedKit = Mockito.mock(MPSideloadedKit::class.java) Mockito.`when`(sideloadedKit.onKitCreate(Mockito.any(), Mockito.any())).thenReturn(null) - val kitId = KitIntegrationFactory.getSideloadedKitId() + val kitId = 6000000 val configJSONObj = JSONObject().apply { put("id", kitId) } val mockedKitConfig = KitConfiguration.createKitConfiguration(configJSONObj) Mockito.`when`(sideloadedKit.configuration).thenReturn(mockedKitConfig) @@ -528,7 +528,7 @@ class KitManagerImplTest { val sideloadedKit = Mockito.mock(MPSideloadedKit::class.java) Mockito.`when`(sideloadedKit.onKitCreate(Mockito.any(), Mockito.any())).thenReturn(null) - val kitId = KitIntegrationFactory.getSideloadedKitId() + val kitId = 6000000 val configJSONObj = JSONObject().apply { put("id", kitId) } val mockedKitConfig = KitConfiguration.createKitConfiguration(configJSONObj) Mockito.`when`(sideloadedKit.configuration).thenReturn(mockedKitConfig) @@ -564,8 +564,8 @@ class KitManagerImplTest { @Test fun testSideloadedKitAdded() { val manager: KitManagerImpl = MockKitManagerImpl() - val idOne = KitIntegrationFactory.getSideloadedKitId() - val idTwo = KitIntegrationFactory.getSideloadedKitId() + val idOne = 6000000 + val idTwo = 6000001 val kitConfiguration = JSONArray() .apply { put(JSONObject().apply { put("id", 1) })