Skip to content

Commit

Permalink
Changes due to comments
Browse files Browse the repository at this point in the history
  • Loading branch information
markvdouw committed Jul 12, 2023
1 parent e1016e1 commit 0979640
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ public Builder installType(@NonNull MParticle.InstallType installType) {
}

/**
* Add locsideloadedKitsal kits
* Add sideloaded kits
*
* @param kits
* @return
Expand Down
204 changes: 179 additions & 25 deletions android-kit-base/src/main/java/com/mparticle/kits/KitConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ public class KitConfiguration {
private final static int ENTITY_PRODUCT = 1;
private final static int ENTITY_PROMOTION = 2;
final static String KEY_ID = "id";
private final static String KEY_ATTRIBUTE_VALUE_FILTERING = "avf";
final static String KEY_ATTRIBUTE_VALUE_FILTERING = "avf";
private final static String KEY_PROPERTIES = "as";
private final static String KEY_FILTERS = "hs";
private final static String KEY_BRACKETING = "bk";
private final static String KEY_ATTRIBUTE_VALUE_FILTERING_SHOULD_INCLUDE_MATCHES = "i";
private final static String KEY_ATTRIBUTE_VALUE_FILTERING_ATTRIBUTE = "a";
final static String KEY_ATTRIBUTE_VALUE_FILTERING_ATTRIBUTE = "a";
private final static String KEY_ATTRIBUTE_VALUE_FILTERING_VALUE = "v";
private final static String KEY_EVENT_TYPES_FILTER = "et";
private final static String KEY_EVENT_NAMES_FILTER = "ec";
private final static String KEY_EVENT_ATTRIBUTES_FILTER = "ea";
private final static String KEY_SCREEN_NAME_FILTER = "svec";
private final static String KEY_SCREEN_ATTRIBUTES_FILTER = "svea";
private final static String KEY_USER_IDENTITY_FILTER = "uid";
private final static String KEY_USER_ATTRIBUTE_FILTER = "ua";
final static String KEY_EVENT_TYPES_FILTER = "et";
final static String KEY_EVENT_NAMES_FILTER = "ec";
final static String KEY_EVENT_ATTRIBUTES_FILTER = "ea";
final static String KEY_SCREEN_NAME_FILTER = "svec";
final static String KEY_SCREEN_ATTRIBUTES_FILTER = "svea";
final static String KEY_USER_IDENTITY_FILTER = "uid";
final static String KEY_USER_ATTRIBUTE_FILTER = "ua";
private final static String KEY_EVENT_ATTRIBUTE_ADD_USER = "eaa";
private final static String KEY_EVENT_ATTRIBUTE_REMOVE_USER = "ear";
private final static String KEY_EVENT_ATTRIBUTE_SINGLE_ITEM_USER = "eas";
Expand All @@ -56,8 +56,8 @@ public class KitConfiguration {
private final static String KEY_COMMERCE_ENTITY_FILTERS = "ent";
private final static String KEY_COMMERCE_ENTITY_ATTRIBUTE_FILTERS = "afa";
private final static String KEY_CONSENT_FORWARDING_RULES = "crvf";
private final static String KEY_CONSENT_FORWARDING_RULES_SHOULD_INCLUDE_MATCHES = "i";
private final static String KEY_CONSENT_FORWARDING_RULES_ARRAY = "v";
final static String KEY_CONSENT_FORWARDING_RULES_SHOULD_INCLUDE_MATCHES = "i";
final static String KEY_CONSENT_FORWARDING_RULES_ARRAY = "v";
private final static String KEY_CONSENT_FORWARDING_RULES_VALUE_CONSENTED = "c";
private final static String KEY_CONSENT_FORWARDING_RULES_VALUE_HASH = "h";
private final static String KEY_EXCLUDE_ANONYMOUS_USERS = "eau";
Expand Down Expand Up @@ -118,7 +118,162 @@ public static KitConfiguration createKitConfiguration(JSONObject json) throws JS
return new KitConfiguration().parseConfiguration(json);
}

private JSONObject getKeyFilters() throws JSONException{
public KitConfiguration applySideloadedKits(MPSideloadedFilters sideloadedFilters) {
Map<String, JSONObject> sideloadedFiltersMap = sideloadedFilters.getFilters();
if (sideloadedFiltersMap.containsKey(KEY_ATTRIBUTE_VALUE_FILTERING)) {
JSONObject avfShouldIncludeMatchesJSONObject = sideloadedFiltersMap.get(KEY_ATTRIBUTE_VALUE_FILTERING);
if (avfShouldIncludeMatchesJSONObject.has(KEY_ATTRIBUTE_VALUE_FILTERING_SHOULD_INCLUDE_MATCHES)) {
try {
this.avfShouldIncludeMatches = avfShouldIncludeMatchesJSONObject.getBoolean(KEY_ATTRIBUTE_VALUE_FILTERING_SHOULD_INCLUDE_MATCHES);
} catch (JSONException e) {
}
}
if (avfShouldIncludeMatchesJSONObject.has(KEY_ATTRIBUTE_VALUE_FILTERING_ATTRIBUTE)) {
try {
this.avfHashedAttribute = avfShouldIncludeMatchesJSONObject.getInt(KEY_ATTRIBUTE_VALUE_FILTERING_ATTRIBUTE);
} catch (JSONException e) {
}
}
if (avfShouldIncludeMatchesJSONObject.has(KEY_ATTRIBUTE_VALUE_FILTERING_VALUE)) {
try {
this.avfHashedValue = avfShouldIncludeMatchesJSONObject.getInt(KEY_ATTRIBUTE_VALUE_FILTERING_VALUE);
} catch (JSONException e) {
}
}
}

if (sideloadedFiltersMap.containsKey(KEY_EVENT_TYPES_FILTER)) {
try {
JSONObject typeFiltersSparseArray = sideloadedFiltersMap.get(KEY_EVENT_TYPES_FILTER);
if (typeFiltersSparseArray != null && typeFiltersSparseArray.length() > 0) {
this.mTypeFilters = convertToSparseArray(typeFiltersSparseArray);
}
} catch (Exception e) {
}
}

if (sideloadedFiltersMap.containsKey(KEY_EVENT_NAMES_FILTER)) {
try {
JSONObject nameFiltersSparseArray = sideloadedFiltersMap.get(KEY_EVENT_NAMES_FILTER);
if (nameFiltersSparseArray != null && nameFiltersSparseArray.length() > 0) {
this.mNameFilters = convertToSparseArray(nameFiltersSparseArray);
}
} catch (Exception e) {
}
}

if (sideloadedFiltersMap.containsKey(KEY_EVENT_ATTRIBUTES_FILTER)) {
try {
JSONObject attrFiltersSparseArray = sideloadedFiltersMap.get(KEY_EVENT_ATTRIBUTES_FILTER);
if (attrFiltersSparseArray != null && attrFiltersSparseArray.length() > 0) {
this.mAttributeFilters = convertToSparseArray(attrFiltersSparseArray);
}
} catch (Exception e) {
}
}

if (sideloadedFiltersMap.containsKey(KEY_SCREEN_NAME_FILTER)) {
try {
JSONObject screenNameFilterSparseArray = sideloadedFiltersMap.get(KEY_SCREEN_NAME_FILTER);
if (screenNameFilterSparseArray != null && screenNameFilterSparseArray.length() > 0) {
this.mScreenNameFilters = convertToSparseArray(screenNameFilterSparseArray);
}
} catch (Exception e) {
}
}

if (sideloadedFiltersMap.containsKey(KEY_SCREEN_ATTRIBUTES_FILTER)) {
try {
JSONObject screenAttrFilterSparseArray = sideloadedFiltersMap.get(KEY_SCREEN_ATTRIBUTES_FILTER);
if (screenAttrFilterSparseArray != null && screenAttrFilterSparseArray.length() > 0) {
this.mScreenAttributeFilters = convertToSparseArray(screenAttrFilterSparseArray);
}
} catch (Exception e) {
}
}

if (sideloadedFiltersMap.containsKey(KEY_USER_IDENTITY_FILTER)) {
try {
JSONObject userIdentityFilterSparseArray = sideloadedFiltersMap.get(KEY_USER_IDENTITY_FILTER);
if (userIdentityFilterSparseArray != null && userIdentityFilterSparseArray.length() > 0) {
this.mUserIdentityFilters = convertToSparseArray(userIdentityFilterSparseArray);
}
} catch (Exception e) {
}
}

if (sideloadedFiltersMap.containsKey(KEY_COMMERCE_ATTRIBUTE_FILTER)) {
try {
JSONObject commerceAttrFilterSparseArray = sideloadedFiltersMap.get(KEY_COMMERCE_ATTRIBUTE_FILTER);
if (commerceAttrFilterSparseArray != null && commerceAttrFilterSparseArray.length() > 0) {
this.mCommerceAttributeFilters = convertToSparseArray(commerceAttrFilterSparseArray);
}
} catch (Exception e) {
}
}

if (sideloadedFiltersMap.containsKey(KEY_COMMERCE_ENTITY_FILTERS)) {
try {
JSONObject commerceEntityFilterSparseArray = sideloadedFiltersMap.get(KEY_COMMERCE_ENTITY_FILTERS);
if (commerceEntityFilterSparseArray != null && commerceEntityFilterSparseArray.length() > 0) {
this.mCommerceAttributeFilters = convertToSparseArray(commerceEntityFilterSparseArray);
}
} catch (Exception e) {
}
}

if (sideloadedFiltersMap.containsKey(KEY_EVENT_ATTRIBUTE_ADD_USER)) {
try {
JSONObject attrAddToUserMap = sideloadedFiltersMap.get(KEY_EVENT_ATTRIBUTE_ADD_USER);
if (attrAddToUserMap != null && attrAddToUserMap.length() > 0) {
this.mAttributeAddToUser = convertToSparseMap(attrAddToUserMap);
}
} catch (Exception e) {
}
}

if (sideloadedFiltersMap.containsKey(KEY_EVENT_ATTRIBUTE_REMOVE_USER)) {
try {
JSONObject attrRemovalFromUserMap = sideloadedFiltersMap.get(KEY_EVENT_ATTRIBUTE_REMOVE_USER);
if (attrRemovalFromUserMap != null && attrRemovalFromUserMap.length() > 0) {
this.mAttributeRemoveFromUser = convertToSparseMap(attrRemovalFromUserMap);
}
} catch (Exception e) {
}
}

if (sideloadedFiltersMap.containsKey(KEY_EVENT_ATTRIBUTE_SINGLE_ITEM_USER)) {
try {
JSONObject attrSingleItemUserMap = sideloadedFiltersMap.get(KEY_EVENT_ATTRIBUTE_SINGLE_ITEM_USER);
if (attrSingleItemUserMap != null && attrSingleItemUserMap.length() > 0) {
this.mAttributeSingleItemUser = convertToSparseMap(attrSingleItemUserMap);
}
} catch (Exception e) {
}
}

return this;
}

public SparseBooleanArray getValueFromSideloadedFilterToSparseArray(Map<String, JSONObject> sideloadedFiltersMap, String key) {
SparseBooleanArray result = null;
JSONObject value = sideloadedFiltersMap.get(key);
if (value != null && value.length() > 0) {
result = convertToSparseArray(value);
}
return result;
}

public Map<Integer, String> getValueFromSideloadedFilterToMap(Map<String, JSONObject> sideloadedFiltersMap, String key) {
Map<Integer, String> result = null;
JSONObject value = sideloadedFiltersMap.get(key);
if (value != null && value.length() > 0) {
result = convertToSparseMap(value);
}
return result;
}

private JSONObject getKeyFilters() throws JSONException {
JSONObject keyFilters = new JSONObject();
JSONObject typeFilters = convertSparseArrayToJsonObject(this.mTypeFilters);
if (typeFilters != null) {
Expand Down Expand Up @@ -229,7 +384,7 @@ private JSONObject getAttributeValueFiltering() throws JSONException {
return filtering;
}

public JSONObject convertToJsonObject() {
public JSONObject convertToJsonObject() {
JSONObject object = new JSONObject();
try {
object.put(KEY_ID, this.kitId);
Expand Down Expand Up @@ -276,28 +431,27 @@ private static JSONObject convertSparseMapToJsonObject(Map<Integer, String> map)
}
JSONObject obj = new JSONObject();
for (Map.Entry<Integer, String> entry : map.entrySet()) {
// try {
obj.put(Integer.toString(entry.getKey()), entry.getValue());
// } catch (JSONException jse) {
// Logger.error("Issue while parsing kit configuration: " + jse.getMessage());
// }
try {
obj.put(Integer.toString(entry.getKey()), entry.getValue());
} catch (JSONException jse) {
Logger.error("Issue while parsing kit configuration: " + jse.getMessage());
}
}
return obj;
}

public static JSONObject convertSparseArrayToJsonObject(SparseBooleanArray array) throws JSONException{
public static JSONObject convertSparseArrayToJsonObject(SparseBooleanArray array) throws JSONException {
if (array == null || array.size() == 0) {
return null;
}
JSONObject object = new JSONObject();
for (int i = 0; i < array.size(); i++) {
int key = array.keyAt(i);
//TODO Should parse to String?
// try {
object.put(Integer.toString(key), array.get(key));
// } catch (JSONException jse) {
// Logger.error("Issue while parsing kit configuration: " + jse.getMessage());
// }
try {
object.put(Integer.toString(key), array.get(key));
} catch (JSONException jse) {
Logger.error("Issue while parsing kit configuration: " + jse.getMessage());
}
}
return object;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
/**
* Base Kit implementation - all Kits must subclass this.
*/
public abstract class KitIntegration implements SideloadedKit {
public abstract class KitIntegration {

private static final String KIT_PREFERENCES_FILE = "mp::kit::";
private KitManagerImpl kitManager;
Expand Down Expand Up @@ -80,7 +80,6 @@ public KitConfiguration getConfiguration() {
return mKitConfiguration;
}

@Override
public JSONObject getJsonConfig() {
if(mKitConfiguration!=null){
return mKitConfiguration.convertToJsonObject();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,90 +1,10 @@
package com.mparticle.kits

import android.util.SparseBooleanArray
import com.mparticle.MParticle
import org.json.JSONObject

class MPSideloadedFilters {

var filters: MutableMap<String, String> = mutableMapOf()
var filters: MutableMap<String, JSONObject> = mutableMapOf()
private set

// Each of these methods hashes these values using the correct algorithm as described
// in the previous section and adds them to a private dictionary
fun addEventTypeFilter(eventType: MParticle.EventType): MPSideloadedFilters {
return this
}

fun addEventNameFilter(eventType: MParticle.EventType, eventName: String): MPSideloadedFilters {
return this
}

fun addScreenNameFilter(screenName: String): MPSideloadedFilters {
return this
}

fun addEventAttributeFilter(
eventType: MParticle.EventType,
eventName: String,
customAttributeKey: String
): MPSideloadedFilters {
return this
}

fun addScreenAttributeFilter(
screenName: String,
customAttributeKey: String
): MPSideloadedFilters {
return this
}

fun addUserIdentityFilter(userIdentity: SparseBooleanArray): MPSideloadedFilters {
return this
}

fun addUserAttributeFilter(userAttributeKey: String): MPSideloadedFilters {
return this
}

fun addCommerceEventAttributeFilter(
eventType: MParticle.EventType,
eventAttributeKey: String
): MPSideloadedFilters {
return this
}

fun addCommerceEventEntityTypeFilter(commerceEventKind: SparseBooleanArray): MPSideloadedFilters {
return this
}

fun addCommerceEventAppFamilyAttributeFilter(attributeKey: String): MPSideloadedFilters {
return this
}

// Special filter case that can only have 1 at a time unlike the others
// If `forward` is true, ONLY matching events are forwarded, if false, any matching events are blocked
// NOTE: This is iOS/Android only, web has a different signature
fun setEventAttributeConditionalForwarding(
attributeName: String,
attributeValue: String,
onlyForward: Boolean
): MPSideloadedFilters {
return this
}

// NOTE: This is Web only
// If isEvent is true, this is an event attribute conditional filter, if false, it's a user attribute conditional filter,
// there can only be one or the other and that latter is not supported at all on iOS/Android.
fun setAttributeConditionalForwarding(
isEvent: Boolean,
attributeName: String,
attributeValue: String,
onlyForward: Boolean
): MPSideloadedFilters {
return this
}

// iOS only, this accepts a string because the constants are strings, but in the inline docs comments and main docs, we will refer people to the constants
fun addMessageTypeFilter(messageTypeConstant: String): MPSideloadedFilters {
return this
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package com.mparticle.kits

import com.mparticle.internal.SideloadedKit
import org.json.JSONObject

abstract class MPSideloadedKit : KitIntegration() {
abstract class MPSideloadedKit : KitIntegration(), SideloadedKit {

init {
configuration = KitConfiguration.createKitConfiguration(JSONObject())
}

fun addFilters(filters: MPSideloadedFilters): MPSideloadedKit {
configuration = configuration.applyFilters(filters)
fun addFilters(filter: MPSideloadedFilters): MPSideloadedKit {
configuration = configuration.applyFilters(filter)
return this
}

override fun getJsonConfig(): JSONObject = super.getJsonConfig()

private fun KitConfiguration.applyFilters(filters: MPSideloadedFilters): KitConfiguration {
return this
return configuration.applySideloadedKits(filters)
}
}

0 comments on commit 0979640

Please sign in to comment.