Skip to content

Commit eba1b0e

Browse files
authored
Fix CT initialization when Leanplum.start is called later (#574)
* Fix CT initialization when Leanplum.start is called later than Application.onCreate * Fix deviceId and userId storage when LP token is null (#575) * Fix travis build
1 parent c34246b commit eba1b0e

File tree

4 files changed

+41
-43
lines changed

4 files changed

+41
-43
lines changed

.travis.yml

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
if: (tag IS present) OR (type = pull_request)
22

33
language: android
4+
dist: bionic
5+
jdk: openjdk17
46

57
env:
68
global:
79
- JAVA_TOOL_OPTIONS=-Dhttps.protocols=TLSv1.2
8-
- BUILD_API=34
9-
- BUILD_TOOLS=34.0.0
10-
- ABI=x86
11-
- EMU_API=22
12-
- EMU_FLAVOR=default
13-
- ANDROID_HOME=/usr/local/android-sdk
14-
- TOOLS=${ANDROID_HOME}/cmdline-tools/latest
15-
# PATH order is important, the 'emulator' script exists in more than one place
16-
- PATH=${ANDROID_HOME}:${ANDROID_HOME}/emulator:${TOOLS}:${TOOLS}/bin:${ANDROID_HOME}/platform-tools:${PATH}
1710
# If you want to run snapshot tests change value to 'true'. Build time will be increased by 6 minutes.
1811
- RUN_SNAPSHOT_TESTS=false
1912
- ROBOLECTRIC_SDK8_URL=https://repo1.maven.org/maven2/org/robolectric/android-all/8.0.0_r4-robolectric-r1/android-all-8.0.0_r4-robolectric-r1
@@ -23,36 +16,26 @@ env:
2316
android:
2417
components:
2518
- tools
19+
- platform-tools
20+
- tools
21+
- build-tools-34.0.0
22+
- android-34
23+
- extras;android;m2repository
24+
# Uncomment for snapshot tests
25+
#- sys-img-x86-android-22
2626

27-
licenses:
27+
licenses:
2828
- 'android-sdk-preview-license-.+'
2929
- 'android-sdk-license-.+'
3030
- 'google-gdk-license-.+'
3131

3232
before_install:
33-
- curl -s "https://get.sdkman.io" | bash
34-
- source "$HOME/.sdkman/bin/sdkman-init.sh"
35-
- sdk install java 17.0.2-open
36-
- sdk use java 17.0.2-open
37-
# Install cmdline-tools (older tools doesn't support Java 11)
38-
- wget -q "https://dl.google.com/android/repository/commandlinetools-linux-7583922_latest.zip" -O android-commandline-tools-linux.zip
39-
- mkdir -p ${ANDROID_HOME}/cmdline-tools
40-
- unzip -q android-commandline-tools-linux.zip -d ${ANDROID_HOME}/cmdline-tools
41-
- mv ${ANDROID_HOME}/cmdline-tools/cmdline-tools ${ANDROID_HOME}/cmdline-tools/latest
4233
# Library for image manipulation in snapshot tests
4334
- if [[ $RUN_SNAPSHOT_TESTS == true ]]; then python -m pip install Tools/Pillow-6.2.2-cp27-cp27mu-manylinux1_x86_64.whl --user; fi;
4435

4536
before_script:
46-
# Install Android SDK and run emulator
47-
- echo 'count=0' > /home/travis/.android/repositories.cfg # avoid harmless sdkmanager warning
48-
- echo y | ${TOOLS}/bin/sdkmanager "platform-tools" >/dev/null
49-
- echo y | ${TOOLS}/bin/sdkmanager "tools" >/dev/null
50-
- echo y | ${TOOLS}/bin/sdkmanager "build-tools;$BUILD_TOOLS" >/dev/null
51-
- echo y | ${TOOLS}/bin/sdkmanager "platforms;android-$EMU_API" >/dev/null
52-
- echo y | ${TOOLS}/bin/sdkmanager "platforms;android-$BUILD_API" >/dev/null
53-
- echo y | ${TOOLS}/bin/sdkmanager "extras;android;m2repository" >/dev/null
54-
- if [[ $RUN_SNAPSHOT_TESTS == true ]]; then echo y | ${TOOLS}/bin/sdkmanager "system-images;android-$EMU_API;$EMU_FLAVOR;$ABI"; fi;
55-
- if [[ $RUN_SNAPSHOT_TESTS == true ]]; then echo no | avdmanager create avd --force -n test -k "system-images;android-$EMU_API;$EMU_FLAVOR;$ABI" -c 100M; fi;
37+
# Run emulator
38+
- if [[ $RUN_SNAPSHOT_TESTS == true ]]; then echo no | android create avd --force -n test -t android-22 --abi x86 -c 100M; fi;
5639
- if [[ $RUN_SNAPSHOT_TESTS == true ]]; then emulator -verbose -avd test -no-accel -no-snapshot -no-window -no-audio -camera-back none -camera-front none -selinux permissive -qemu -m 2048 & fi;
5740

5841
script:

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -447,15 +447,22 @@ public static void saveDiffs() {
447447
if (Constants.isNoop()) {
448448
return;
449449
}
450-
if (APIConfig.getInstance().token() == null) {
451-
return;
452-
}
450+
453451
Context context = Leanplum.getContext();
454452
SharedPreferences defaults = context.getSharedPreferences(LEANPLUM, Context.MODE_PRIVATE);
455453
SharedPreferences.Editor editor = defaults.edit();
456454

457455
// Crypt functions return input text if there was a problem.
458456
AESCrypt aesContext = new AESCrypt(APIConfig.getInstance().appId(), APIConfig.getInstance().token());
457+
editor.putString(Constants.Params.DEVICE_ID, aesContext.encrypt(APIConfig.getInstance().deviceId()));
458+
editor.putString(Constants.Params.USER_ID, aesContext.encrypt(APIConfig.getInstance().userId()));
459+
460+
if (APIConfig.getInstance().token() == null) {
461+
// The token is not used when encrypting and decrypting DEVICE_ID and USER_ID
462+
// See WrapperFactory.getDeviceAndUserFromPrefs
463+
SharedPreferencesUtil.commitChanges(editor);
464+
return;
465+
}
459466

460467
String variablesCipher = aesContext.encrypt(JsonConverter.toJson(diffs));
461468
editor.putString(Constants.Defaults.VARIABLES_KEY, variablesCipher);
@@ -493,8 +500,6 @@ public static void saveDiffs() {
493500
editor.putString(Constants.Defaults.VARIABLES_JSON_KEY, aesContext.encrypt(varsJson));
494501
editor.putString(Constants.Defaults.VARIABLES_SIGN_KEY, aesContext.encrypt(varsSignature));
495502

496-
editor.putString(Constants.Params.DEVICE_ID, aesContext.encrypt(APIConfig.getInstance().deviceId()));
497-
editor.putString(Constants.Params.USER_ID, aesContext.encrypt(APIConfig.getInstance().userId()));
498503
editor.putString(Constants.Keys.LOGGING_ENABLED,
499504
aesContext.encrypt(String.valueOf(Constants.loggingEnabled)));
500505
SharedPreferencesUtil.commitChanges(editor);

AndroidSDKCore/src/main/java/com/leanplum/migration/wrapper/CTWrapper.kt

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import android.text.TextUtils
2828
import com.clevertap.android.sdk.ActivityLifecycleCallback
2929
import com.clevertap.android.sdk.CleverTapAPI
3030
import com.clevertap.android.sdk.CleverTapInstanceConfig
31+
import com.clevertap.android.sdk.CoreMetaData
3132
import com.leanplum.LeanplumActivityHelper
3233
import com.leanplum.callbacks.CleverTapInstanceCallback
3334
import com.leanplum.internal.Constants
@@ -59,7 +60,7 @@ internal class CTWrapper(
5960
private var identityManager = IdentityManager(deviceId, userId ?: deviceId, loggedInUserId)
6061
private var firstTimeStart = identityManager.isFirstTimeStart()
6162

62-
@SuppressLint("WrongConstant")
63+
@SuppressLint("WrongConstant", "RestrictedApi")
6364
override fun launch(context: Context, callbacks: List<CleverTapInstanceCallback>) {
6465
instanceCallbackList.addAll(callbacks)
6566

@@ -89,13 +90,22 @@ internal class CTWrapper(
8990
}
9091
cleverTapInstance?.apply {
9192
setLibrary("Leanplum")
92-
if (!ActivityLifecycleCallback.registered && LeanplumActivityHelper.getCurrentActivity() != null) {
93-
ActivityLifecycleCallback.register(context.applicationContext as? Application)
94-
if (!LeanplumActivityHelper.isActivityPaused() && !CleverTapAPI.isAppForeground()) {
95-
// Trigger onActivityResumed because onResume of ActivityLifecycle has already been executed
96-
// in this case. This could happen on first start with ct migration. This method will also
97-
// trigger App Launched if it was not send already.
98-
CleverTapAPI.onActivityResumed(LeanplumActivityHelper.getCurrentActivity())
93+
if (LeanplumActivityHelper.getCurrentActivity() != null) {
94+
if (!ActivityLifecycleCallback.registered) {
95+
ActivityLifecycleCallback.register(context.applicationContext as? Application)
96+
if (!LeanplumActivityHelper.isActivityPaused() && !CleverTapAPI.isAppForeground()) {
97+
// Trigger onActivityResumed because onResume of ActivityLifecycle has already been executed
98+
// in this case. This could happen on first start with ct migration. This method will also
99+
// trigger App Launched if it was not send already.
100+
CleverTapAPI.onActivityResumed(LeanplumActivityHelper.getCurrentActivity(), cleverTapID)
101+
}
102+
} else if (CoreMetaData.getCurrentActivity() == null && !LeanplumActivityHelper.isActivityPaused()) {
103+
// If CT ActivityLifecycleCallback was registered before LP had created the CT instance
104+
// CleverTapAPI.onActivityResumed would have not executed its initialization logic
105+
// (CleverTapAPI.instances would still have been null). This is checked here by
106+
// CoreMetaData.getCurrentActivity() == null.
107+
// In this case call onActivityResumed explicitly.
108+
CleverTapAPI.onActivityResumed(LeanplumActivityHelper.getCurrentActivity(), cleverTapID)
99109
}
100110
}
101111
if (identityManager.isAnonymous()) {

sdk-version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7.6.2
1+
7.6.3-beta2

0 commit comments

Comments
 (0)