Skip to content

Commit c695ab2

Browse files
committed
Merge branch 'master' into release/5.3.3
2 parents 7c01fd2 + f3c04ff commit c695ab2

File tree

5 files changed

+44
-31
lines changed

5 files changed

+44
-31
lines changed

AndroidSDKCore/src/main/java/com/leanplum/Leanplum.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -501,15 +501,12 @@ static synchronized void start(final Context context, final String userId,
501501
final Map<String, ?> attributes, StartCallback response, final Boolean isBackground) {
502502
try {
503503
OsHandler.getInstance();
504-
505-
if (context instanceof Activity) {
506-
LeanplumActivityHelper.currentActivity = (Activity) context;
507-
}
504+
LeanplumActivityHelper.setCurrentActivity(context);
508505

509506
// Detect if app is in background automatically if isBackground is not set.
510507
final boolean actuallyInBackground;
511508
if (isBackground == null) {
512-
actuallyInBackground = LeanplumActivityHelper.currentActivity == null ||
509+
actuallyInBackground = LeanplumActivityHelper.getCurrentActivity() == null ||
513510
LeanplumActivityHelper.isActivityPaused();
514511
} else {
515512
actuallyInBackground = isBackground;
@@ -858,9 +855,9 @@ protected Void doInBackground(Void... params) {
858855
if (Constants.isDevelopmentModeEnabled) {
859856

860857
final Context currentContext = (
861-
LeanplumActivityHelper.currentActivity != context &&
862-
LeanplumActivityHelper.currentActivity != null) ?
863-
LeanplumActivityHelper.currentActivity
858+
LeanplumActivityHelper.getCurrentActivity() != context &&
859+
LeanplumActivityHelper.getCurrentActivity() != null) ?
860+
LeanplumActivityHelper.getCurrentActivity()
864861
: context;
865862

866863
// Register device.

AndroidSDKCore/src/main/java/com/leanplum/LeanplumActivityHelper.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import android.app.Activity;
2525
import android.app.Application;
2626
import android.app.Application.ActivityLifecycleCallbacks;
27+
import android.content.Context;
2728
import android.content.res.Resources;
2829
import android.os.Build;
2930
import android.os.Bundle;
@@ -60,7 +61,7 @@ public class LeanplumActivityHelper {
6061
*/
6162
private static boolean registeredCallbacks;
6263

63-
static Activity currentActivity;
64+
private static Activity currentActivity;
6465

6566
private final Activity activity;
6667
private LeanplumResources res;
@@ -80,6 +81,18 @@ public LeanplumActivityHelper(Activity activity) {
8081
Parser.parseVariables(activity);
8182
}
8283

84+
/**
85+
* Set activity and run pending actions
86+
*/
87+
public static void setCurrentActivity(Context context) {
88+
if (context instanceof Activity) {
89+
currentActivity = (Activity) context;
90+
91+
// run pending actions if any upon start
92+
LeanplumInternal.addStartIssuedHandler(runPendingActionsRunnable);
93+
}
94+
}
95+
8396
/**
8497
* Retrieves the currently active activity.
8598
*/
@@ -158,6 +171,8 @@ public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
158171

159172
});
160173
registeredCallbacks = true;
174+
// run pending actions if any upon start
175+
LeanplumInternal.addStartIssuedHandler(runPendingActionsRunnable);
161176
}
162177

163178
public LeanplumResources getLeanplumResources() {

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,12 @@ private void sendNow() {
477477
if (Constants.isTestMode) {
478478
return;
479479
}
480+
481+
// always save request first
482+
sendEventually();
483+
484+
// in case appId and accessKey are set later, request is already saved and will be
485+
// sent when variables are set.
480486
if (appId == null) {
481487
Log.e("Cannot send request. appId is not set.");
482488
return;
@@ -486,9 +492,6 @@ private void sendNow() {
486492
return;
487493
}
488494

489-
// We need to save request first.
490-
sendEventually();
491-
492495
Leanplum.countAggregator().incrementCount("send_now");
493496

494497
// Try to send all saved requests.

AndroidSDKPush/src/main/java/com/leanplum/LeanplumPushService.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import android.graphics.Bitmap;
3434
import android.net.Uri;
3535
import android.os.Bundle;
36-
import androidx.core.app.NotificationCompat;
3736

3837
import com.leanplum.callbacks.VariablesChangedCallback;
3938
import com.leanplum.internal.ActionManager;
@@ -58,6 +57,9 @@
5857
import java.util.Map;
5958
import java.util.Random;
6059
import java.util.concurrent.CountDownLatch;
60+
import java.util.concurrent.TimeUnit;
61+
62+
import androidx.core.app.NotificationCompat;
6163

6264
/**
6365
* Leanplum push notification service class, handling initialization, opening, showing, integration
@@ -239,7 +241,7 @@ private static String getMessageId(Bundle message) {
239241
}
240242

241243
static void handleNotification(final Context context, final Bundle message) {
242-
if (LeanplumActivityHelper.currentActivity != null
244+
if (LeanplumActivityHelper.getCurrentActivity() != null
243245
&& !LeanplumActivityHelper.isActivityPaused
244246
&& (message.containsKey(Keys.PUSH_MESSAGE_ID_MUTE_WITH_ACTION)
245247
|| message.containsKey(Keys.PUSH_MESSAGE_ID_MUTE))) {
@@ -369,30 +371,26 @@ private static void showNotification(Context context, final Bundle message) {
369371
if (ActionContext.shouldForceContentUpdateForChainedMessage(
370372
JsonConverter.fromJson(message.getString(Keys.PUSH_MESSAGE_ACTION)))) {
371373

374+
// try to fetch referenced chained message and wait a bit for result
372375
final CountDownLatch countDownLatch = new CountDownLatch(1);
373-
374-
final int currentNotificationId = notificationId;
375-
final Notification.Builder currentNotificationBuilder = notificationBuilder;
376-
final NotificationCompat.Builder currentNotificationCompatBuilder = notificationCompatBuilder;
377376
Leanplum.forceContentUpdate(new VariablesChangedCallback() {
378377
@Override
379378
public void variablesChanged() {
380-
if (currentNotificationBuilder != null) {
381-
notificationManager.notify(currentNotificationId, currentNotificationBuilder.build());
382-
} else {
383-
notificationManager.notify(currentNotificationId, currentNotificationCompatBuilder.build());
384-
}
385379
countDownLatch.countDown();
386380
}
387381
});
388-
countDownLatch.await();
382+
383+
// continue after 3 seconds and post the notification
384+
countDownLatch.await(3, TimeUnit.SECONDS);
385+
}
386+
387+
// always post notification
388+
if (notificationBuilder != null) {
389+
notificationManager.notify(notificationId, notificationBuilder.build());
389390
} else {
390-
if (notificationBuilder != null) {
391-
notificationManager.notify(notificationId, notificationBuilder.build());
392-
} else {
393-
notificationManager.notify(notificationId, notificationCompatBuilder.build());
394-
}
391+
notificationManager.notify(notificationId, notificationCompatBuilder.build());
395392
}
393+
396394
} catch (NullPointerException e) {
397395
Log.e("Unable to show push notification.", e);
398396
} catch (Throwable t) {
@@ -418,7 +416,7 @@ static void openNotification(Context context, Intent intent) {
418416
// Start activity.
419417
Class<? extends Activity> callbackClass = LeanplumPushService.getCallbackClass();
420418
boolean shouldStartActivity = true;
421-
Activity currentActivity = LeanplumActivityHelper.currentActivity;
419+
Activity currentActivity = LeanplumActivityHelper.getCurrentActivity();
422420
if (currentActivity != null && !LeanplumActivityHelper.isActivityPaused) {
423421
if (callbackClass == null) {
424422
shouldStartActivity = false;

AndroidSDKTests/src/test/java/com/leanplum/LeanplumActivityHelperTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void run() {
5656
assertTrue(isRunnableQueued(runnable));
5757

5858
FakeActivity fakeActivity = new FakeActivity();
59-
LeanplumActivityHelper.currentActivity = fakeActivity;
59+
LeanplumActivityHelper.setCurrentActivity(fakeActivity);
6060

6161
// Test activity is finishing.
6262
final CountDownLatch latch2 = new CountDownLatch(1);

0 commit comments

Comments
 (0)