Skip to content

Commit

Permalink
feat: add client side rules
Browse files Browse the repository at this point in the history
  • Loading branch information
willpassidomo authored and mchuangatmp committed May 18, 2022
1 parent eab5533 commit 3d68551
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 19 deletions.
3 changes: 2 additions & 1 deletion android-core/src/main/java/com/mparticle/MParticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ protected MParticle() { }

private MParticle(MParticleOptions options) {
ConfigManager configManager = new ConfigManager(options);

configManager.setUploadInterval(options.getUploadInterval());
configManager.setSessionTimeout(options.getSessionTimeout());
configManager.setIdentityConnectionTimeout(options.getConnectionTimeout());
Expand All @@ -109,7 +110,7 @@ private MParticle(MParticleOptions options) {
mAppContext = options.getContext();
mConfigManager = configManager;
mAppStateManager = appStateManager;
mDatabaseManager = new MParticleDBManager(mAppContext);
mDatabaseManager = new MParticleDBManager(mAppContext, options);
if (options.isUncaughtExceptionLoggingEnabled()) {
enableUncaughtExceptionLogging();
} else {
Expand Down
28 changes: 28 additions & 0 deletions android-core/src/main/java/com/mparticle/MParticleOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class MParticleOptions {
private Boolean mUnCaughtExceptionLogging = false;
private MParticle.LogLevel mLogLevel = MParticle.LogLevel.DEBUG;
private AttributionListener mAttributionListener;
private BatchCreationListener batchCreationListener = null;
private LocationTracking mLocationTracking;
private PushRegistrationHelper.PushRegistration mPushRegistration;
private Integer mIdentityConnectionTimeout = ConfigManager.DEFAULT_CONNECTION_TIMEOUT_SECONDS;
Expand Down Expand Up @@ -120,6 +121,9 @@ public MParticleOptions(@NonNull Builder builder) {
if (builder.attributionListener != null) {
this.mAttributionListener = builder.attributionListener;
}
if (builder.batchCreationListener != null) {
this.batchCreationListener = builder.batchCreationListener;
}
if (builder.locationTracking != null) {
this.mLocationTracking = builder.locationTracking;
}
Expand Down Expand Up @@ -271,6 +275,11 @@ public AttributionListener getAttributionListener() {
return mAttributionListener;
}

@Nullable
public BatchCreationListener getBatchCreationListener() {
return batchCreationListener;
}

public boolean hasLocationTracking() {
return mLocationTracking != null;
}
Expand Down Expand Up @@ -358,6 +367,7 @@ public static class Builder {
private MParticle.LogLevel logLevel = null;
BaseIdentityTask identityTask;
private AttributionListener attributionListener;
private BatchCreationListener batchCreationListener;
private ConfigManager configManager;
private LocationTracking locationTracking;
private PushRegistrationHelper.PushRegistration pushRegistration;
Expand Down Expand Up @@ -593,6 +603,12 @@ public Builder attributionListener(@Nullable AttributionListener attributionList
return this;
}

@NonNull
public Builder batchCreationListener(@Nullable BatchCreationListener batchCreationListener) {
this.batchCreationListener = batchCreationListener;
return this;
}

/**
* Disables Location tracking.
*
Expand Down Expand Up @@ -940,4 +956,16 @@ public DataplanOptions build() {
}
}
}

/**
Custom handler to modify or block batch data before upload.
If set, this will be called when a new batch of data is created. By returning a different value, you can change the batch contents, or by returning 'nil' you can block the batch from being uploaded.
Use with care. This feature was initially added to allow the value of existing fields to be modified. If you add new data in a format that the platform is not expecting, it may be dropped or not parsed correctly.
Note: Use of this handler will also cause the field 'mb' (modified batch) to appear in the batch so we can distinguish for troubleshooting purposes whether data was changed.
Also note: Unlike other callbacks, this block will be called on the SDK queue to prevent batches from being processed out of order. Please avoid excessively blocking in this handler as this will prevent the SDK from doing other tasks.
*/
public interface BatchCreationListener {
@NonNull
JSONObject onBatchCreated(@NonNull JSONObject batch);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import android.os.Handler;
import android.os.Looper;

import androidx.annotation.NonNull;

import com.mparticle.MParticle;
import com.mparticle.MParticleOptions;
import com.mparticle.UserAttributeListener;
import com.mparticle.internal.BatchId;
import com.mparticle.internal.ConfigManager;
Expand Down Expand Up @@ -42,17 +45,23 @@ public class MParticleDBManager {
private SharedPreferences mPreferences;
private Context mContext;
private DatabaseHelper mDatabaseHelper;
private MParticleOptions options;

MParticleDBManager() {
//for unit testing
}

public MParticleDBManager(Context context) {
public MParticleDBManager(Context context, @NonNull MParticleOptions options) {
this.mContext = context;
this.options = options;
mPreferences = context.getSharedPreferences(Constants.PREFS_FILE, Context.MODE_PRIVATE);
mDatabaseHelper = new DatabaseHelper(context);
}

public MParticleDBManager(Context context) {
this(context, MParticleOptions.builder(context).build());
}

/**
* Creates a new SQLiteDatabase instance, if the Database has not been opened yet, it returns
* an instance. Each instance is a singleton, per thread.
Expand Down Expand Up @@ -300,7 +309,23 @@ private void createUploads(Map<BatchId, MessageBatch> uploadMessagesByBatchId, M
uploadMessage.setIdentities(identities);
JSONObject userAttributes = findUserAttributeState(messages, batchId.getMpid());
uploadMessage.setUserAttributes(userAttributes);
UploadService.insertUpload(db, uploadMessage, configManager.getApiKey());

JSONObject batch = uploadMessage;
if (options.getBatchCreationListener() != null) {
try {
batch = options.getBatchCreationListener().onBatchCreated(batch);
} catch (Exception e) {
Logger.error(e, "batch creation listener error, batch will not be uploaded");
return;
}
}

if (batch == null || batch.length() == 0) {
Logger.error("Not uploading batch due to 'onCreateBatch' handler being empty");
return;
}

UploadService.insertUpload(db, batch, configManager.getApiKey());
//if this was to process session history, or
//if we're never going to process history AND
//this batch contains a previous session, then delete the session.
Expand Down Expand Up @@ -335,11 +360,8 @@ private JSONObject findUserAttributeState(JSONArray messages, long mpId) {
userAttributes = messages.getJSONObject(i).getJSONObject(Constants.MessageKey.USER_ATTRIBUTES);
messages.getJSONObject(i).remove(Constants.MessageKey.USER_ATTRIBUTES);
}
}catch (JSONException jse) {

}catch (NullPointerException npe) {

}
} catch (JSONException ignored) {
} catch (NullPointerException ignored) { }
}
}
if (userAttributes == null) {
Expand All @@ -361,11 +383,8 @@ private JSONArray findIdentityState(ConfigManager configManager, JSONArray messa
identities = messages.getJSONObject(i).getJSONArray(Constants.MessageKey.USER_IDENTITIES);
messages.getJSONObject(i).remove(Constants.MessageKey.USER_IDENTITIES);
}
}catch (JSONException jse) {

}catch (NullPointerException npe) {

}
} catch (JSONException ignored) {
} catch (NullPointerException ignored) { }
}
}
if (identities == null) {
Expand Down Expand Up @@ -558,9 +577,7 @@ public JSONObject getAllUserAttributesJson(long mpId) {
}
try {
jsonAttributes.put(entry.getKey(), jsonArray);
} catch (JSONException e) {

}
} catch (JSONException ignored) { }
}else {
try {
Object entryValue = entry.getValue();
Expand Down Expand Up @@ -677,8 +694,7 @@ public void removeUserAttribute(UserAttributeRemoval container, MessageManagerCa
callbacks.logUserAttributeChangeMessage(container.key, null, currentValues.get(container.key), true, false, container.time, container.mpId);
}
db.setTransactionSuccessful();
}catch (Exception e) {

} catch (Exception e) {
} finally {
db.endTransaction();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static int cleanupUploadMessages(MPDatabase database) {
*
* @param message
*/
public static void insertUpload(MPDatabase database, MessageBatch message, String apiKey) {
public static void insertUpload(MPDatabase database, JSONObject message, String apiKey) {
ContentValues contentValues = new ContentValues();
contentValues.put(UploadTableColumns.API_KEY, apiKey);
contentValues.put(UploadTableColumns.CREATED_AT, message.optLong(Constants.MessageKey.TIMESTAMP, System.currentTimeMillis()));
Expand Down

0 comments on commit 3d68551

Please sign in to comment.