Skip to content

Commit

Permalink
Adding feature flag with default value false
Browse files Browse the repository at this point in the history
Setting min alarm delay to 2mins
Adding logic to handle both background event batching and current behavior
Changing package receiver package name
Removing comments
  • Loading branch information
markvdouw committed Dec 21, 2023
1 parent ee18929 commit eb40eae
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 57 deletions.
2 changes: 1 addition & 1 deletion android-core/proguard.pro
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
-keep class com.mparticle.MParticle$UserAttributes { *; }
-keep class com.mparticle.MParticle$ResetListener { *; }
-keep class com.mparticle.MParticle$OperatingSystem { *; }
-keep class com.mparticle.UploadBatchReceiver
-keep class com.mparticle.uploadbatching.UploadBatchReceiver


-keep class com.mparticle.Session { *; }
Expand Down
2 changes: 1 addition & 1 deletion android-core/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<application>
<receiver
android:name="com.mparticle.UploadBatchReceiver"
android:name="com.mparticle.uploadbatching.UploadBatchReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
Expand Down
1 change: 1 addition & 0 deletions android-core/src/main/java/com/mparticle/MParticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import com.mparticle.messaging.MPMessagingAPI;
import com.mparticle.messaging.ProviderCloudMessage;
import com.mparticle.segmentation.SegmentListener;
import com.mparticle.uploadbatching.UploadBatchReceiver;

import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

import androidx.annotation.Nullable;

import com.mparticle.AlarmSchedulingUtilsKt;
import com.mparticle.MPEvent;
import com.mparticle.MParticle;
import com.mparticle.identity.IdentityApi;
import com.mparticle.identity.IdentityApiRequest;
import com.mparticle.identity.MParticleUser;
import com.mparticle.internal.listeners.InternalListenerManager;
import com.mparticle.uploadbatching.AlarmSchedulingUtilsKt;

import org.json.JSONObject;

Expand Down Expand Up @@ -365,7 +365,9 @@ public void onActivityStopped(Activity activity) {
private void logBackgrounded() {
MParticle instance = MParticle.getInstance();
if (instance != null) {
AlarmSchedulingUtilsKt.scheduleUploadBatchAlarm(mContext, mConfigManager.getUploadInterval());
if (mConfigManager.isBackgroundBatchUploadingEnabled()) {
AlarmSchedulingUtilsKt.scheduleUploadBatchAlarm(mContext, mConfigManager.getUploadInterval());
}
logStateTransition(Constants.StateTransitionType.STATE_TRANS_BG, mCurrentActivityName);
instance.Internal().getKitManager().onApplicationBackground();
mCurrentActivityName = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public class ConfigManager {
public static final int DEFAULT_UPLOAD_INTERVAL = 600;
private List<ConfigLoadedListener> configUpdatedListeners = new ArrayList<>();
private List<SideloadedKit> sideloadedKits = new ArrayList<>();
private boolean enableBackgroundBatchingUpload = false;

private ConfigManager() {
super();
Expand Down Expand Up @@ -402,6 +403,11 @@ private synchronized void updateCoreConfig(JSONObject responseJSON, boolean newC
mLogUnhandledExceptions = responseJSON.getString(KEY_UNHANDLED_EXCEPTIONS);
}

if (responseJSON.has(ENABLE_BACKGROUND_BATCHING)) {
enableBackgroundBatchingUpload = responseJSON.getBoolean(ENABLE_BACKGROUND_BATCHING);
}
editor.putBoolean(ENABLE_BACKGROUND_BATCHING, enableBackgroundBatchingUpload);

if (responseJSON.has(KEY_PUSH_MESSAGES) && newConfig) {
sPushKeys = responseJSON.getJSONArray(KEY_PUSH_MESSAGES);
editor.putString(KEY_PUSH_MESSAGES, sPushKeys.toString());
Expand Down Expand Up @@ -913,6 +919,10 @@ public JSONArray getTriggerMessageHashes() {
return mTriggerMessageHashes;
}

public boolean isBackgroundBatchUploadingEnabled() {
return enableBackgroundBatchingUpload;
}

public boolean shouldTrigger(BaseMPMessage message) {
JSONArray messageMatches = getTriggerMessageMatches();
JSONArray triggerHashes = getTriggerMessageHashes();
Expand All @@ -922,11 +932,11 @@ public boolean shouldTrigger(BaseMPMessage message) {
isBackgroundAst = (message.getMessageType().equals(Constants.MessageType.APP_STATE_TRANSITION) && message.get(Constants.MessageKey.STATE_TRANSITION_TYPE).equals(Constants.StateTransitionType.STATE_TRANS_BG));
} catch (JSONException ex) {
}
if (isBackgroundAst) {
if (enableBackgroundBatchingUpload && isBackgroundAst) {
return false;
}
boolean shouldTrigger = message.getMessageType().equals(Constants.MessageType.PUSH_RECEIVED)
|| message.getMessageType().equals(Constants.MessageType.COMMERCE_EVENT);
|| message.getMessageType().equals(Constants.MessageType.COMMERCE_EVENT) || isBackgroundAst;

if (!shouldTrigger && messageMatches != null && messageMatches.length() > 0) {
shouldTrigger = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ public void handleMessageImpl(Message msg) {
}
}
}
if ((mAppStateManager.getSession().isActive() && uploadInterval > 0 && msg.arg1 == 0) ||
(mParticleDBManager.hasMessagesForUpload() && mAppStateManager.isBackgrounded())) {
this.sendEmptyDelayed(UPLOAD_MESSAGES, uploadInterval);
}
break;
case UPLOAD_HISTORY:
removeMessage(UPLOAD_HISTORY);
Expand Down Expand Up @@ -208,7 +212,6 @@ protected void prepareMessageUploads(boolean history) throws Exception {
protected boolean upload(boolean history) {
mParticleDBManager.cleanupUploadMessages();
boolean processingSessionEnd = false;
boolean uploadFailed = false;
try {
List<MParticleDBManager.ReadyUpload> readyUploads = mParticleDBManager.getReadyUploads();
if (readyUploads.size() > 0) {
Expand Down Expand Up @@ -237,24 +240,12 @@ protected boolean upload(boolean history) {
}
}
} catch (MParticleApiClientImpl.MPThrottleException e) {
uploadFailed = true;
} catch (SSLHandshakeException ssle) {
Logger.debug("SSL handshake failed while preparing uploads - possible MITM attack detected.");
uploadFailed = true;
} catch (MParticleApiClientImpl.MPConfigException e) {
Logger.error("Bad API request - is the correct API key and secret configured?");
uploadFailed = true;
} catch (Exception e) {
Logger.error(e, "Error processing batch uploads in mParticle DB.");
uploadFailed = true;
}
boolean activeNotBackgrounded = !mAppStateManager.isBackgrounded() && mAppStateManager.getSession().isActive();
boolean backgroundedWithPendingMessages = mAppStateManager.isBackgrounded() && (uploadFailed || mParticleDBManager.hasMessagesForUpload());
if (mConfigManager.getUploadInterval() > 0 && (activeNotBackgrounded || backgroundedWithPendingMessages)) {
Logger.debug("Upload scheduled with message in interval due to activeNotBackgrounded: " + activeNotBackgrounded + " and/or backgroundedWithPendingMessages: " + backgroundedWithPendingMessages);
this.sendEmptyDelayed(UPLOAD_MESSAGES, mConfigManager.getUploadInterval());
} else {
Logger.debug("No uploads are being schedules do to activeNotBackgrounded: " + activeNotBackgrounded + " and/or backgroundedWithPendingMessages: " + backgroundedWithPendingMessages);
}
return processingSessionEnd;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package com.mparticle
package com.mparticle.uploadbatching

import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import com.mparticle.internal.Logger
import com.mparticle.millisToLoggingDate

fun scheduleUploadBatchAlarm(context: Context, delay: Long) {
val intent = Intent(context, UploadBatchReceiver::class.java).apply {
action = UploadBatchReceiver.ACTION_UPLOAD_BATCH
}
val pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)

val time = System.currentTimeMillis() + delay
var alarmDelay = delay
//Setting alarm delay to 2min MINIMUM to prevent collision with end session message triggers
if (delay < 120000) {
alarmDelay = 120000L
}
val time = System.currentTimeMillis() + alarmDelay
val alarmType = AlarmManager.RTC_WAKEUP
(context.getSystemService(Context.ALARM_SERVICE) as AlarmManager?)?.let { manager ->

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.mparticle
package com.mparticle.uploadbatching

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import com.mparticle.MParticle
import com.mparticle.internal.Logger

internal class UploadBatchReceiver : BroadcastReceiver() {
Expand All @@ -24,11 +25,8 @@ internal class UploadBatchReceiver : BroadcastReceiver() {
//Do if there is a non-null mParticle instance, force upload messages
it.upload()
Logger.debug("Uploading events in upload batch receiver")
} ?: run {
Logger.debug("Batches cant be uploaded in receiver because MParticle instance is null")
}
} catch (e: Exception) {
Logger.error("Error while uploading batches in upload batch reveiver")
}
}
}
Expand Down

0 comments on commit eb40eae

Please sign in to comment.