Skip to content

Commit

Permalink
ViewCrawler improvements (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio Alonso Fernández authored Sep 22, 2017
1 parent 0b77467 commit 9d5b34e
Show file tree
Hide file tree
Showing 13 changed files with 260 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ public void startUpdates() {
mStarted = true;
}

@Override
public void applyPersistedUpdates() {

}

@Override
public void setEventBindings(JSONArray bindings) {
assertTrue(mStarted);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,12 @@ public void reportResults(List<InAppNotification> newNotifications, JSONArray ne
}

private class MockUpdates implements UpdatesFromMixpanel {

@Override
public void applyPersistedUpdates() {

}

@Override
public void startUpdates() {
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public void startUpdates() {
; // do nothing
}

@Override
public void applyPersistedUpdates() {
}

@Override
public void setEventBindings(JSONArray bindings) {
; // TODO should observe bindings here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,15 @@ public PeopleDescription(JSONObject message, String token) {
this.message = message;
}

@Override
public String toString() {
return message.toString();
}

public JSONObject getMessage() {
return message;
}


private final JSONObject message;
}

Expand All @@ -193,6 +197,7 @@ protected FlushDescription(String token, boolean checkDecide) {
this.checkDecide = checkDecide;
}


public boolean shouldCheckDecide() {
return checkDecide;
}
Expand Down
36 changes: 19 additions & 17 deletions src/main/java/com/mixpanel/android/mpmetrics/DecideChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ public void runDecideCheck(final String token, final RemoteService poster) throw
final String distinctId = updates.getDistinctId();
try {
final Result result = runDecideCheck(updates.getToken(), distinctId, poster);
updates.reportResults(result.notifications, result.eventBindings, result.variants, result.automaticEvents);
if (result != null) {
updates.reportResults(result.notifications, result.eventBindings, result.variants, result.automaticEvents);
}
} catch (final UnintelligibleMessageException e) {
MPLog.e(LOGTAG, e.getMessage(), e);
}
Expand All @@ -104,25 +106,25 @@ private Result runDecideCheck(final String token, final String distinctId, final

MPLog.v(LOGTAG, "Mixpanel decide server response was:\n" + responseString);

Result parsed = new Result();
if (null != responseString) {
parsed = parseDecideResponse(responseString);
}

final Iterator<InAppNotification> notificationIterator = parsed.notifications.iterator();
while (notificationIterator.hasNext()) {
final InAppNotification notification = notificationIterator.next();
final Bitmap image = getNotificationImage(notification, mContext);
if (null == image) {
MPLog.i(LOGTAG, "Could not retrieve image for notification " + notification.getId() +
", will not show the notification.");
notificationIterator.remove();
} else {
notification.setImage(image);
Result parsedResult = null;
if (responseString != null) {
parsedResult = parseDecideResponse(responseString);

final Iterator<InAppNotification> notificationIterator = parsedResult.notifications.iterator();
while (notificationIterator.hasNext()) {
final InAppNotification notification = notificationIterator.next();
final Bitmap image = getNotificationImage(notification, mContext);
if (null == image) {
MPLog.i(LOGTAG, "Could not retrieve image for notification " + notification.getId() +
", will not show the notification.");
notificationIterator.remove();
} else {
notification.setImage(image);
}
}
}

return parsed;
return parsedResult;
}// runDecideCheck

/* package */ static Result parseDecideResponse(String responseString)
Expand Down
30 changes: 17 additions & 13 deletions src/main/java/com/mixpanel/android/mpmetrics/DecideMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public DecideMessages(Context context, String token, OnNewResultsListener listen
mDistinctId = null;
mUnseenNotifications = new LinkedList<InAppNotification>();
mNotificationIds = new HashSet<Integer>(notificationIds);
mVariants = new JSONArray();
mVariants = null;
}

public String getToken() {
Expand All @@ -52,6 +52,9 @@ public synchronized String getDistinctId() {

public synchronized void reportResults(List<InAppNotification> newNotifications, JSONArray eventBindings, JSONArray variants, boolean automaticEvents) {
boolean newContent = false;
int newVariantsLength = variants.length();
boolean hasNewVariants = false;

mUpdatesFromMixpanel.setEventBindings(eventBindings);

for (final InAppNotification n : newNotifications) {
Expand All @@ -65,14 +68,12 @@ public synchronized void reportResults(List<InAppNotification> newNotifications,

// the following logic checks if the variants have been applied by looking up their id's in the HashSet
// this is needed to make sure the user defined `mListener` will get called on new variants receiving
int newVariantsLength = variants.length();
boolean hasNewVariants = false;
mVariants = variants;

for (int i = 0; i < newVariantsLength; i++) {
try {
JSONObject variant = variants.getJSONObject(i);
if (!mLoadedVariants.contains(variant.getInt("id"))) {
mVariants = variants;
newContent = true;
hasNewVariants = true;
break;
Expand All @@ -82,7 +83,7 @@ public synchronized void reportResults(List<InAppNotification> newNotifications,
}
}

if (hasNewVariants) {
if (hasNewVariants && mVariants != null) {
mLoadedVariants.clear();

for (int i = 0; i < newVariantsLength; i++) {
Expand All @@ -94,17 +95,20 @@ public synchronized void reportResults(List<InAppNotification> newNotifications,
}
}
}
if (mAutomaticEventsEnabled == null && !automaticEvents) {
MPDbAdapter.getInstance(mContext).cleanupAutomaticEvents(mToken);
}
mAutomaticEventsEnabled = automaticEvents;

// in the case we do not receive a new variant, this means the A/B test should be turned off
if (newVariantsLength == 0 && mLoadedVariants.size() > 0) {
mLoadedVariants.clear();
if (newVariantsLength == 0) {
mVariants = new JSONArray();
newContent = true;
if (mLoadedVariants.size() > 0) {
mLoadedVariants.clear();
newContent = true;
}
}

if (mAutomaticEventsEnabled == null && !automaticEvents) {
MPDbAdapter.getInstance(mContext).cleanupAutomaticEvents(mToken);
}
mAutomaticEventsEnabled = automaticEvents;

MPLog.v(LOGTAG, "New Decide content has become available. " +
newNotifications.size() + " notifications and " +
Expand Down Expand Up @@ -156,7 +160,7 @@ public synchronized void markNotificationAsUnseen(InAppNotification notif) {
}

public synchronized boolean hasUpdatesAvailable() {
return (! mUnseenNotifications.isEmpty()) || mVariants.length() > 0;
return (! mUnseenNotifications.isEmpty()) || (mVariants != null && mVariants.length() > 0);
}

public Boolean isAutomaticEventsEnabled() {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/mixpanel/android/mpmetrics/MixpanelAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,12 @@ public boolean isAppInForeground() {
return false;
}


/* package */ void onBackground() {
flush();
mUpdatesFromMixpanel.applyPersistedUpdates();
}

// Package-level access. Used (at least) by GCMReceiver
// when OS-level events occur.
/* package */ interface InstanceProcessor {
Expand Down Expand Up @@ -1983,6 +1989,11 @@ public void startUpdates() {
// No op
}

@Override
public void applyPersistedUpdates() {
// No op
}

@Override
public void setEventBindings(JSONArray bindings) {
// No op
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void run() {
} catch (JSONException e) {
e.printStackTrace();
}
mMpInstance.flush();
mMpInstance.onBackground();
}
}
}, CHECK_DELAY);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
package com.mixpanel.android.mpmetrics;

import java.util.Set;

/**
* For use with {@link MixpanelAPI.People#addOnMixpanelTweaksUpdatedListener(OnMixpanelTweaksUpdatedListener)}
*/
public interface OnMixpanelTweaksUpdatedListener {
public void onMixpanelTweakUpdated();
/**
* Called when the Mixpanel library has updated tweaks.
* This method will not be called once per tweak update, but rather any time a batch of updates
* becomes available.
*
* @param updatedTweaksName The set of tweak names that were updated.
*/
public void onMixpanelTweakUpdated(Set<String> updatedTweaksName);
}
6 changes: 5 additions & 1 deletion src/main/java/com/mixpanel/android/mpmetrics/Tweaks.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,13 @@ public Number getMaximum() {
return maximum;
}

public Object getValue() {
return value;
}

public final @TweakType int type;

protected final Object value;
private final Object value;
private final Object defaultValue;
private final Number minimum;
private final Number maximum;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public boolean isValid() {
return !mClient.isClosed() && !mClient.isClosing() && !mClient.isFlushAndClose();
}

public boolean isConnected() {
return mClient.isOpen();
}

public BufferedOutputStream getBufferedOutputStream() {
return new BufferedOutputStream(new WebSocketOutputStream());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
implemented in client code. */
public interface UpdatesFromMixpanel {
public void startUpdates();
public void applyPersistedUpdates();
public void setEventBindings(JSONArray bindings);
public void setVariants(JSONArray variants);
public Tweaks getTweaks();
Expand Down
Loading

0 comments on commit 9d5b34e

Please sign in to comment.