Skip to content

Commit 64cb304

Browse files
committed
Android 4.1.0
2 parents 4ff48e3 + 3cf0f58 commit 64cb304

File tree

15 files changed

+637
-10
lines changed

15 files changed

+637
-10
lines changed

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

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,15 @@ public static void trackAllAppScreens() {
250250
LeanplumInternal.enableAutomaticScreenTracking();
251251
}
252252

253+
/**
254+
* Set this to true if you want details about the variable assignments
255+
* on the server.
256+
* Default is NO.
257+
*/
258+
public static void setVariantDebugInfoEnabled(boolean variantDebugInfoEnabled) {
259+
LeanplumInternal.setIsVariantDebugInfoEnabled(variantDebugInfoEnabled);
260+
}
261+
253262
/**
254263
* Whether screen tracking is enabled or not.
255264
*
@@ -499,7 +508,8 @@ static synchronized void start(final Context context, final String userId,
499508
VarCache.getUpdateRuleDiffs(),
500509
VarCache.getEventRuleDiffs(),
501510
new HashMap<String, Object>(),
502-
new ArrayList<Map<String, Object>>());
511+
new ArrayList<Map<String, Object>>(),
512+
new HashMap<String, Object>());
503513
LeanplumInbox.getInstance().update(new HashMap<String, LeanplumInboxMessage>(), 0, false);
504514
return;
505515
}
@@ -571,6 +581,8 @@ protected Void doInBackground(Void... params) {
571581
return null;
572582
}
573583
});
584+
585+
Util.initExceptionHandling(context);
574586
} catch (Throwable t) {
575587
Util.handleException(t);
576588
}
@@ -664,6 +676,8 @@ private static void startHelper(
664676
// Get the current inbox messages on the device.
665677
params.put(Constants.Params.INBOX_MESSAGES, LeanplumInbox.getInstance().messagesIds());
666678

679+
params.put(Constants.Params.INCLUDE_VARIANT_DEBUG_INFO, LeanplumInternal.getIsVariantDebugInfoEnabled());
680+
667681
Util.initializePreLeanplumInstall(params);
668682

669683
// Issue start API call.
@@ -800,6 +814,8 @@ protected Void doInBackground(Void... params) {
800814
Constants.loggingEnabled = true;
801815
}
802816

817+
parseVariantDebugInfo(response);
818+
803819
// Allow bidirectional realtime variable updates.
804820
if (Constants.isDevelopmentModeEnabled) {
805821

@@ -921,6 +937,8 @@ private static void applyContentInResponse(JSONObject response, boolean alwaysAp
921937
response.optJSONObject(Constants.Keys.REGIONS));
922938
List<Map<String, Object>> variants = JsonConverter.listFromJsonOrDefault(
923939
response.optJSONArray(Constants.Keys.VARIANTS));
940+
Map<String, Object> variantDebugInfo = JsonConverter.mapFromJsonOrDefault(
941+
response.optJSONObject(Constants.Keys.VARIANT_DEBUG_INFO));
924942

925943
if (alwaysApply
926944
|| !values.equals(VarCache.getDiffs())
@@ -929,7 +947,7 @@ private static void applyContentInResponse(JSONObject response, boolean alwaysAp
929947
|| !eventRules.equals(VarCache.getEventRuleDiffs())
930948
|| !regions.equals(VarCache.regions())) {
931949
VarCache.applyVariableDiffs(values, messages, updateRules,
932-
eventRules, regions, variants);
950+
eventRules, regions, variants, variantDebugInfo);
933951
}
934952
}
935953

@@ -1088,6 +1106,19 @@ public static boolean hasStarted() {
10881106
return LeanplumInternal.hasStarted();
10891107
}
10901108

1109+
/**
1110+
* Returns the userId in the current Leanplum session. This should only be called after
1111+
* Leanplum.start().
1112+
*/
1113+
public static String getUserId() {
1114+
if (hasStarted()) {
1115+
return Request.userId();
1116+
} else {
1117+
Log.e("Leanplum.start() must be called before calling getUserId()");
1118+
}
1119+
return null;
1120+
}
1121+
10911122
/**
10921123
* Returns an instance to the singleton LeanplumInbox object.
10931124
*/
@@ -1924,6 +1955,8 @@ public static void forceContentUpdate(final VariablesChangedCallback callback) {
19241955
Map<String, Object> params = new HashMap<>();
19251956
params.put(Constants.Params.INCLUDE_DEFAULTS, Boolean.toString(false));
19261957
params.put(Constants.Params.INBOX_MESSAGES, LeanplumInbox.getInstance().messagesIds());
1958+
params.put(Constants.Params.INCLUDE_VARIANT_DEBUG_INFO, LeanplumInternal.getIsVariantDebugInfoEnabled());
1959+
19271960
Request req = Request.post(Constants.Methods.GET_VARS, params);
19281961
req.onResponse(new Request.ResponseCallback() {
19291962
@Override
@@ -1941,6 +1974,8 @@ public void response(JSONObject response) {
19411974
if (response.optBoolean(Constants.Keys.LOGGING_ENABLED, false)) {
19421975
Constants.loggingEnabled = true;
19431976
}
1977+
1978+
parseVariantDebugInfo(response);
19441979
}
19451980
if (callback != null) {
19461981
OsHandler.getInstance().post(callback);
@@ -2043,6 +2078,13 @@ public static Map<String, Object> messageMetadata() {
20432078
return messages;
20442079
}
20452080

2081+
/**
2082+
* Details about the variable assignments on the server.
2083+
*/
2084+
public static Map<String, Object> getVariantDebugInfo() {
2085+
return VarCache.getVariantDebugInfo();
2086+
}
2087+
20462088
/**
20472089
* Set location manually. Calls setDeviceLocation with cell type. Best if used in after calling
20482090
* disableLocationCollection.
@@ -2092,4 +2134,12 @@ public static void disableLocationCollection() {
20922134
public static boolean isLocationCollectionEnabled() {
20932135
return locationCollectionEnabled;
20942136
}
2137+
2138+
private static void parseVariantDebugInfo(JSONObject response) {
2139+
Map<String, Object> variantDebugInfo = JsonConverter.mapFromJsonOrDefault(
2140+
response.optJSONObject(Constants.Keys.VARIANT_DEBUG_INFO));
2141+
if (variantDebugInfo.size() > 0) {
2142+
VarCache.setVariantDebugInfo(variantDebugInfo);
2143+
}
2144+
}
20952145
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class Constants {
3636
public static int SOCKET_PORT = 80;
3737
public static int NETWORK_TIMEOUT_SECONDS = 10;
3838
public static int NETWORK_TIMEOUT_SECONDS_FOR_DOWNLOADS = 10;
39-
public static String LEANPLUM_VERSION = "4.0.4";
39+
public static String LEANPLUM_VERSION = "4.1.0";
4040
public static String CLIENT = "android";
4141

4242
static final String LEANPLUM_PACKAGE_IDENTIFIER = BuildConfig.LEANPLUM_PACKAGE_IDENTIFIER;
@@ -144,6 +144,7 @@ public static class Params {
144144
public static final String IAP_CURRENCY_CODE = "currencyCode";
145145
public static final String IAP_ITEM = "item";
146146
public static final String INCLUDE_DEFAULTS = "includeDefaults";
147+
public static final String INCLUDE_VARIANT_DEBUG_INFO = "includeVariantDebugInfo";
147148
public static final String INCLUDE_MESSAGE_ID = "includeMessageId";
148149
public static final String INFO = "info";
149150
public static final String INSTALL_DATE = "installDate";
@@ -200,6 +201,7 @@ public static class Keys {
200201
public static final String REGION = "region";
201202
public static final String REGION_STATE = "regionState";
202203
public static final String REGIONS = "regions";
204+
public static final String VARIANT_DEBUG_INFO = "variantDebugInfo";
203205
public static final String SIZE = "size";
204206
public static final String SUBTITLE = "Subtitle";
205207
public static final String SYNC_INBOX = "syncNewsfeed";

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public class LeanplumInternal {
7373
private static final Queue<Map<String, ?>> userAttributeChanges = new ConcurrentLinkedQueue<>();
7474
private static final ArrayList<Runnable> startIssuedHandlers = new ArrayList<>();
7575
private static boolean isScreenTrackingEnabled = false;
76+
private static boolean isVariantDebugInfoEnabled = false;
7677

7778
private static void onHasStartedAndRegisteredAsDeveloperAndFinishedSyncing() {
7879
if (!hasStartedAndRegisteredAsDeveloper) {
@@ -673,6 +674,14 @@ public static boolean getIsScreenTrackingEnabled() {
673674
return isScreenTrackingEnabled;
674675
}
675676

677+
public static boolean getIsVariantDebugInfoEnabled() {
678+
return isVariantDebugInfoEnabled;
679+
}
680+
681+
public static void setIsVariantDebugInfoEnabled(boolean isVariantDebugInfoEnabled) {
682+
LeanplumInternal.isVariantDebugInfoEnabled = isVariantDebugInfoEnabled;
683+
}
684+
676685
public static void enableAutomaticScreenTracking() {
677686
isScreenTrackingEnabled = true;
678687
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public class Request {
5959
private static final long DEVELOPMENT_MIN_DELAY_MS = 100;
6060
private static final long DEVELOPMENT_MAX_DELAY_MS = 5000;
6161
private static final long PRODUCTION_DELAY = 60000;
62+
private RequestSequenceRecorder requestSequenceRecorder;
6263
static final int MAX_EVENTS_PER_API_CALL;
6364
static final String LEANPLUM = "__leanplum__";
6465
static final String UUID_KEY = "uuid";
@@ -164,6 +165,28 @@ public static void saveToken() {
164165
SharedPreferencesUtil.commitChanges(editor);
165166
}
166167

168+
private static class NoRequestSequenceRecorder implements RequestSequenceRecorder {
169+
@Override
170+
public void beforeRead() {
171+
// No op.
172+
}
173+
174+
@Override
175+
public void afterRead() {
176+
// No op.
177+
}
178+
179+
@Override
180+
public void beforeWrite() {
181+
// No op.
182+
}
183+
184+
@Override
185+
public void afterWrite() {
186+
// No op.
187+
}
188+
}
189+
167190
public static String appId() {
168191
return appId;
169192
}
@@ -177,6 +200,10 @@ public static String userId() {
177200
}
178201

179202
public Request(String httpMethod, String apiMethod, Map<String, Object> params) {
203+
this(httpMethod, apiMethod, params, new NoRequestSequenceRecorder());
204+
}
205+
206+
Request(String httpMethod, String apiMethod, Map<String, Object> params, RequestSequenceRecorder requestSequenceRecorder) {
180207
this.httpMethod = httpMethod;
181208
this.apiMethod = apiMethod;
182209
this.params = params != null ? params : new HashMap<String, Object>();
@@ -187,6 +214,7 @@ public Request(String httpMethod, String apiMethod, Map<String, Object> params)
187214
// Make sure the Handler is initialized on the main thread.
188215
OsHandler.getInstance();
189216
dataBaseIndex = -1;
217+
this.requestSequenceRecorder = requestSequenceRecorder;
190218
}
191219

192220
public static Request get(String apiMethod, Map<String, Object> params) {
@@ -232,6 +260,8 @@ private Map<String, Object> createArgsDictionary() {
232260

233261
private void saveRequestForLater(Map<String, Object> args) {
234262
try {
263+
requestSequenceRecorder.beforeWrite();
264+
235265
synchronized (Request.class) {
236266
Context context = Leanplum.getContext();
237267
SharedPreferences preferences = context.getSharedPreferences(
@@ -255,6 +285,8 @@ private void saveRequestForLater(Map<String, Object> args) {
255285
eventCallbackManager.addCallbacks(this, response, error);
256286
}
257287
}
288+
289+
requestSequenceRecorder.afterWrite();
258290
} catch (Throwable t) {
259291
Util.handleException(t);
260292
}
@@ -549,8 +581,12 @@ private RequestsWithEncoding getRequestsWithEncodedString() {
549581
}
550582

551583
private void sendRequests() {
584+
requestSequenceRecorder.beforeRead();
585+
552586
RequestsWithEncoding requestsWithEncoding = getRequestsWithEncodedString();
553587

588+
requestSequenceRecorder.afterRead();
589+
554590
List<Map<String, Object>> unsentRequests = requestsWithEncoding.unsentRequests;
555591
List<Map<String, Object>> requestsToSend = requestsWithEncoding.requestsToSend;
556592
String jsonEncodedString = requestsWithEncoding.jsonEncodedString;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.leanplum.internal;
2+
3+
/** Records request call sequence of read/write operations to database. */
4+
public interface RequestSequenceRecorder {
5+
/** Executes before database read in Request. */
6+
void beforeRead();
7+
8+
/** Executes after database read in Request. */
9+
void afterRead();
10+
11+
/** Executes before database write in Request. */
12+
void beforeWrite();
13+
14+
/** Executes after database write in Request. */
15+
void afterWrite();
16+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ static void handleApplyVarsEvent(JSONArray args) {
307307
return;
308308
}
309309
VarCache.applyVariableDiffs(
310-
JsonConverter.mapFromJson(object), null, null, null, null, null);
310+
JsonConverter.mapFromJson(object), null, null, null, null, null, null);
311311
} catch (JSONException e) {
312312
Log.e("Couldn't applyVars for preview.", e);
313313
} catch (Throwable e) {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import com.leanplum.LeanplumException;
4848
import com.leanplum.internal.Constants.Methods;
4949
import com.leanplum.internal.Constants.Params;
50+
import com.leanplum.monitoring.ExceptionHandler;
5051
import com.leanplum.utils.SharedPreferencesUtil;
5152

5253
import org.json.JSONException;
@@ -837,10 +838,19 @@ private static void setUpdateTime(Map<String, Object> params, PackageManager pac
837838
}
838839
}
839840

841+
/**
842+
* Initialize exception handling in the SDK.
843+
*/
844+
public static void initExceptionHandling(Context context) {
845+
ExceptionHandler.getInstance().setContext(context);
846+
}
847+
840848
/**
841849
* Handles uncaught exceptions in the SDK.
842850
*/
843851
public static void handleException(Throwable t) {
852+
ExceptionHandler.getInstance().reportException(t);
853+
844854
if (t instanceof OutOfMemoryError) {
845855
if (Constants.isDevelopmentModeEnabled) {
846856
throw (OutOfMemoryError) t;

0 commit comments

Comments
 (0)