Skip to content

Commit d1da6f0

Browse files
authored
Start CT as soon as config is available (#528)
1 parent 6ce169a commit d1da6f0

File tree

4 files changed

+45
-8
lines changed

4 files changed

+45
-8
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import com.leanplum.internal.VarCache;
6565
import com.leanplum.messagetemplates.MessageTemplates;
6666
import com.leanplum.migration.MigrationManager;
67+
import com.leanplum.migration.model.MigrationConfig;
6768
import com.leanplum.models.GeofenceEventType;
6869
import com.leanplum.utils.BuildUtil;
6970
import com.leanplum.utils.SharedPreferencesUtil;
@@ -237,6 +238,7 @@ public static void setAppIdForDevelopmentMode(String appId, String accessKey) {
237238

238239
Constants.isDevelopmentModeEnabled = true;
239240
APIConfig.getInstance().setAppId(appId, accessKey);
241+
MigrationConfig.setAppId(appId);
240242
}
241243

242244
/**
@@ -258,6 +260,7 @@ public static void setAppIdForProductionMode(String appId, String accessKey) {
258260

259261
Constants.isDevelopmentModeEnabled = false;
260262
APIConfig.getInstance().setAppId(appId, accessKey);
263+
MigrationConfig.setAppId(appId);
261264
}
262265

263266
/**

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import androidx.annotation.VisibleForTesting;
2828
import com.leanplum.Leanplum;
2929
import com.leanplum.internal.Constants.Defaults;
30+
import com.leanplum.internal.Constants.Params;
3031
import com.leanplum.utils.SharedPreferencesUtil;
3132
import java.util.Map;
3233

@@ -103,6 +104,11 @@ public void setToken(String token) {
103104

104105
private void load() {
105106
Context context = Leanplum.getContext();
107+
if (context == null) {
108+
Log.e("Leanplum context is null. Please call Leanplum.setApplicationContext(context) "
109+
+ "before anything else.");
110+
return;
111+
}
106112
SharedPreferences defaults = context.getSharedPreferences(
107113
Constants.Defaults.LEANPLUM, Context.MODE_PRIVATE);
108114

@@ -131,6 +137,11 @@ private void load() {
131137

132138
public void save() {
133139
Context context = Leanplum.getContext();
140+
if (context == null) {
141+
Log.e("Leanplum context is null. Please call Leanplum.setApplicationContext(context) "
142+
+ "before anything else.");
143+
return;
144+
}
134145
SharedPreferences defaults = context.getSharedPreferences(
135146
Constants.Defaults.LEANPLUM, Context.MODE_PRIVATE);
136147
SharedPreferences.Editor editor = defaults.edit();

AndroidSDKCore/src/main/java/com/leanplum/migration/model/MigrationConfig.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ object MigrationConfig {
5656
var trackGooglePlayPurchases = true
5757
private set
5858

59+
@JvmStatic
60+
var appId: String? by StringPreferenceNullable(key = "app_id")
61+
5962
fun update(data: ResponseData) {
6063
state = data.state
6164
hash = data.hash

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@
2121

2222
package com.leanplum.migration.wrapper
2323

24+
import android.content.Context
2425
import com.leanplum.Leanplum
2526
import com.leanplum.callbacks.CleverTapInstanceCallback
27+
import com.leanplum.internal.AESCrypt
28+
import com.leanplum.internal.Constants
2629
import com.leanplum.internal.LeanplumInternal
2730
import com.leanplum.internal.Log
2831
import com.leanplum.migration.model.MigrationConfig
32+
import com.leanplum.utils.getLeanplumPrefs
2933
import kotlin.system.measureTimeMillis
3034

3135
internal object WrapperFactory {
@@ -38,16 +42,20 @@ internal object WrapperFactory {
3842
return NullWrapper
3943
}
4044

41-
if (!LeanplumInternal.hasCalledStart()) {
42-
// giving access only to CT static methods, because Leanplum state is not fully initialised
43-
return StaticMethodsWrapper
45+
val context = Leanplum.getContext() ?: return StaticMethodsWrapper
46+
val deviceId: String?
47+
val userId: String?
48+
49+
if (LeanplumInternal.hasCalledStart()) {
50+
deviceId = Leanplum.getDeviceId()
51+
userId = Leanplum.getUserId()
52+
} else {
53+
val profile = getDeviceAndUserFromPrefs(context)
54+
deviceId = profile.first
55+
userId = profile.second
4456
}
4557

46-
val context = Leanplum.getContext()
47-
val deviceId = Leanplum.getDeviceId()
48-
val userId = Leanplum.getUserId()
49-
if (context == null || deviceId == null) {
50-
// giving access only to CT static methods, because Leanplum state is not fully initialised
58+
if (deviceId == null) {
5159
return StaticMethodsWrapper
5260
}
5361

@@ -59,4 +67,16 @@ internal object WrapperFactory {
5967
}
6068
}
6169

70+
private fun getDeviceAndUserFromPrefs(context: Context): Pair<String?, String?> {
71+
val appId = MigrationConfig.appId ?: return Pair(null, null)
72+
73+
val sharedPrefs = context.getLeanplumPrefs()
74+
val aesCrypt = AESCrypt(appId, null)
75+
76+
val deviceId = aesCrypt.decodePreference(sharedPrefs, Constants.Params.DEVICE_ID, null)
77+
val userId = aesCrypt.decodePreference(sharedPrefs, Constants.Params.USER_ID, null)
78+
79+
return Pair(deviceId, userId)
80+
}
81+
6282
}

0 commit comments

Comments
 (0)