Skip to content

Commit

Permalink
Changes due to team discussion.
Browse files Browse the repository at this point in the history
Using kit instance and combine it with created instances.
Support for multiple instances.
Adding condition to filter sideloaded kits with id < 1000000
  • Loading branch information
markvdouw committed Jul 14, 2023
1 parent b27be91 commit c314dc7
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 47 deletions.
10 changes: 9 additions & 1 deletion android-core/src/main/java/com/mparticle/MParticleOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,15 @@ public Builder installType(@NonNull MParticle.InstallType installType) {
*/
@NonNull
public Builder sideloadedKits(@NonNull List<SideloadedKit> kits) {
this.sideloadedKits = kits;
List<SideloadedKit> _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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@ package com.mparticle.internal
import org.json.JSONObject

interface SideloadedKit {

fun getJsonConfig(): JSONObject?

fun kitId(): Int

fun getName(): String
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,26 @@ import org.json.JSONArray
object SideloadedKitsUtils {

fun combineConfig(kitConfig: JSONArray?, kits: List<SideloadedKit>): 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<SideloadedKit>): JSONArray {
var results = JSONArray()
if (!kits.isEmpty()) {
val sideloadedIds = kits.map { it.getJsonConfig()?.optInt("id", -100) }
var addedIds = mutableSetOf<Int>()
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
public class KitIntegrationFactory {

final Map<Integer, Class> supportedKits = new HashMap<>();
public static int minSideloadedKitId = 1000000;
private static int sideloadedKitNextId = minSideloadedKitId;
private Map<Integer, String> knownIntegrations = new HashMap<Integer, String>();
private Map<Integer, MPSideloadedKit> sideloadedKitMap = new HashMap<>();

public KitIntegrationFactory(MParticleOptions options) {
supportedKits.clear();
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -140,6 +145,6 @@ public Set<Integer> getSupportedKits() {
}

public boolean isSupported(int kitModuleId) {
return supportedKits.containsKey(kitModuleId) || kitModuleId >= minSideloadedKitId;
return supportedKits.containsKey(kitModuleId) || kitModuleId >= MPSideloadedKit.MIN_SIDELOADED_KIT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,10 @@ protected synchronized void configureKits(@NonNull List<KitConfiguration> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) })
Expand Down

0 comments on commit c314dc7

Please sign in to comment.