Skip to content

Commit c31c054

Browse files
authored
Use main thread when presenting action with async handlers flag is true (#545)
1 parent db9ca23 commit c31c054

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

AndroidSDKCore/src/main/java/com/leanplum/actions/internal/ActionManagerExecution.kt

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ fun ActionManager.dismissCurrentAction() {
9191
val definition = definitions.findDefinition(currentContext.actionName())
9292

9393
definition?.dismissHandler?.also {
94-
Log.d("[ActionManager]: dismiss requested for: ${currentContext}.")
94+
Log.d("[ActionManager][${Util.getThread()}]: dismiss requested for: ${currentContext}.")
9595
try {
9696
it.onResponse(currentContext)
9797
} catch (t: Throwable) {
@@ -106,6 +106,7 @@ private fun ActionManager.performActionsImpl() {
106106

107107
// do not continue if we have action running
108108
if (currentAction != null) {
109+
Log.d("[ActionManager][${Util.getThread()}]: will not pop queue, because an action is already presenting")
109110
if (!LeanplumActions.useWorkerThreadForDecisionHandlers) { // disable prioritization with worker thread
110111
prioritizePushNotificationActions()
111112
}
@@ -116,11 +117,11 @@ private fun ActionManager.performActionsImpl() {
116117
currentAction = queue.pop() ?: return
117118

118119
val currentContext = currentAction.context
119-
Log.d("[ActionManager]: action popped from queue: ${currentContext}.")
120+
Log.d("[ActionManager][${Util.getThread()}]: action popped from queue: ${currentContext}.")
120121

121122
if (currentAction.actionType == Action.ActionType.SINGLE
122123
&& LeanplumInternal.shouldSuppressMessage(currentContext)) {
123-
Log.i("[ActionManager]: Local IAM caps reached, suppressing $currentContext")
124+
Log.i("[ActionManager][${Util.getThread()}]: Local IAM caps reached, suppressing $currentContext")
124125
currentAction = null
125126
performActions()
126127
return
@@ -149,7 +150,7 @@ private fun ActionManager.askUserAndPresentAction(currentContext: ActionContext)
149150
// if message is delayed, add it to the scheduler to be delayed
150151
// by the amount of seconds, and exit
151152
MessageDisplayChoice.Type.DELAY -> {
152-
Log.d("[ActionManager]: delaying action: ${currentContext} for ${displayDecision.delaySeconds}s.")
153+
Log.d("[ActionManager][${Util.getThread()}]: delaying action: ${currentContext} for ${displayDecision.delaySeconds}s.")
153154
if (displayDecision.delaySeconds > 0) {
154155
// Schedule for delayed time
155156
scheduler.schedule(currentAction, displayDecision.delaySeconds)
@@ -165,6 +166,14 @@ private fun ActionManager.askUserAndPresentAction(currentContext: ActionContext)
165166
else -> Unit
166167
}
167168

169+
if (LeanplumActions.useWorkerThreadForDecisionHandlers) {
170+
OperationQueue.sharedInstance().addUiOperation { presentAction(currentContext) }
171+
} else {
172+
presentAction(currentContext)
173+
}
174+
}
175+
176+
private fun ActionManager.presentAction(currentContext: ActionContext) {
168177
// logic:
169178
// 1) ask client to show view controller
170179
// 2) ask and wait for client to execute action
@@ -176,7 +185,7 @@ private fun ActionManager.askUserAndPresentAction(currentContext: ActionContext)
176185
// 3) set dismiss block
177186
currentContext.setActionDidDismiss {
178187
val dismissOperation = {
179-
Log.d("[ActionManager]: actionDidDismiss: ${currentContext}.")
188+
Log.d("[ActionManager][${Util.getThread()}]: actionDidDismiss: ${currentContext}.")
180189
messageDisplayListener?.onMessageDismissed(currentContext)
181190
currentAction = null // stop executing current action
182191
performActions()
@@ -187,13 +196,12 @@ private fun ActionManager.askUserAndPresentAction(currentContext: ActionContext)
187196
} else {
188197
dismissOperation.invoke()
189198
}
190-
191199
}
192200

193201
// 2) set the action block
194202
currentContext.setActionDidExecute { actionNamedContext ->
195203
val actionExecutedOperation = {
196-
Log.d("[ActionManager]: actionDidExecute: ${actionNamedContext}.")
204+
Log.d("[ActionManager][${Util.getThread()}]: actionDidExecute: ${actionNamedContext}.")
197205
messageDisplayListener?.onActionExecuted(actionNamedContext.actionName(), actionNamedContext)
198206
Unit
199207
}
@@ -208,7 +216,7 @@ private fun ActionManager.askUserAndPresentAction(currentContext: ActionContext)
208216
// 1) ask to present, return if it's not
209217
val presented: Boolean = definition?.presentHandler?.onResponse(currentContext) ?: false
210218
if (!presented) {
211-
Log.d("[ActionManager]: action NOT presented: ${currentContext}.")
219+
Log.d("[ActionManager][${Util.getThread()}]: action NOT presented: ${currentContext}.")
212220
currentAction = null
213221
performActions()
214222
return
@@ -218,9 +226,15 @@ private fun ActionManager.askUserAndPresentAction(currentContext: ActionContext)
218226
recordImpression(currentAction)
219227
}
220228

221-
Log.i("[ActionManager]: action presented: ${currentContext}.")
229+
Log.i("[ActionManager][${Util.getThread()}]: action presented: ${currentContext}.")
222230
// propagate event that message is displayed
223-
messageDisplayListener?.onMessageDisplayed(currentContext)
231+
if (LeanplumActions.useWorkerThreadForDecisionHandlers) {
232+
OperationQueue.sharedInstance().addActionOperation {
233+
messageDisplayListener?.onMessageDisplayed(currentContext)
234+
}
235+
} else {
236+
messageDisplayListener?.onMessageDisplayed(currentContext)
237+
}
224238

225239
performActions()
226240
}

AndroidSDKCore/src/main/java/com/leanplum/actions/internal/ActionManagerTriggering.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import com.leanplum.actions.LeanplumActions
2626
import com.leanplum.internal.ActionManager
2727
import com.leanplum.internal.Log
2828
import com.leanplum.internal.OperationQueue
29+
import com.leanplum.internal.Util
2930

3031
fun ActionManager.trigger(
3132
context: ActionContext,
@@ -68,7 +69,7 @@ private fun ActionManager.triggerImpl(
6869

6970
val actions: List<Action> = orderedContexts.map { context -> Action.create(context) }
7071

71-
Log.d("[ActionManager]: triggering with priority: ${priority.name} and actions: $orderedContexts")
72+
Log.d("[ActionManager][${Util.getThread()}]: triggering with priority: ${priority.name} and actions: $orderedContexts")
7273

7374
when (priority) {
7475
Priority.HIGH -> insertActions(actions)

AndroidSDKCore/src/main/java/com/leanplum/internal/Util.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,4 +697,8 @@ public static boolean isHuaweiServicesAvailable(Context context) {
697697
public static boolean isMainThread() {
698698
return Thread.currentThread() == Looper.getMainLooper().getThread();
699699
}
700+
701+
public static String getThread() {
702+
return isMainThread() ? "UI" : "BG";
703+
}
700704
}

0 commit comments

Comments
 (0)