Skip to content

Commit

Permalink
Merge pull request #550 from facebook/4.x-branch-rc
Browse files Browse the repository at this point in the history
Add _inBackground for app events to indicate whether the events were …
  • Loading branch information
dreamolight authored Apr 4, 2019
2 parents ec65ba1 + d54d53b commit f27ba1b
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 34 deletions.
29 changes: 24 additions & 5 deletions facebook-core/src/main/java/com/facebook/appevents/AppEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class AppEvent implements Serializable {

private final JSONObject jsonObject;
private final boolean isImplicit;
private final boolean inBackground;
private final String name;
private final String checksum;

Expand All @@ -61,6 +62,7 @@ public AppEvent(
Double valueToSum,
Bundle parameters,
boolean isImplicitlyLogged,
boolean isInBackground,
@Nullable final UUID currentSessionId
) throws JSONException, FacebookException {
jsonObject = getJSONObjectForAppEvent(
Expand All @@ -69,8 +71,10 @@ public AppEvent(
valueToSum,
parameters,
isImplicitlyLogged,
isInBackground,
currentSessionId);
isImplicit = isImplicitlyLogged;
inBackground = isInBackground;
name = eventName;
checksum = calculateChecksum();
}
Expand All @@ -82,11 +86,13 @@ public String getName() {
private AppEvent(
String jsonString,
boolean isImplicit,
boolean inBackground,
String checksum) throws JSONException {
jsonObject = new JSONObject(jsonString);
this.isImplicit = isImplicit;
this.name = jsonObject.optString(Constants.EVENT_NAME_EVENT_KEY);
this.checksum = checksum;
this.inBackground = inBackground;
}

public boolean getIsImplicit() {
Expand Down Expand Up @@ -158,6 +164,7 @@ private static JSONObject getJSONObjectForAppEvent(
Double valueToSum,
Bundle parameters,
boolean isImplicitlyLogged,
boolean isInBackground,
@Nullable final UUID currentSessionId
) throws FacebookException, JSONException{
validateIdentifier(eventName);
Expand All @@ -180,6 +187,10 @@ private static JSONObject getJSONObjectForAppEvent(
eventObject.put("_implicitlyLogged", "1");
}

if (isInBackground) {
eventObject.put("_inBackground", "1");
}

if (parameters != null) {
for (String key : parameters.keySet()) {

Expand Down Expand Up @@ -213,36 +224,44 @@ static class SerializationProxyV1 implements Serializable {
private static final long serialVersionUID = -2488473066578201069L;
private final String jsonString;
private final boolean isImplicit;
private final boolean inBackground;

private SerializationProxyV1(String jsonString, boolean isImplicit) {
private SerializationProxyV1(String jsonString, boolean isImplicit, boolean inBackground) {
this.jsonString = jsonString;
this.isImplicit = isImplicit;
this.inBackground = inBackground;
}

private Object readResolve() throws JSONException {
return new AppEvent(jsonString, isImplicit, null);
return new AppEvent(jsonString, isImplicit, inBackground, null);
}
}

static class SerializationProxyV2 implements Serializable {
private static final long serialVersionUID = 2016_08_03_001L;
private final String jsonString;
private final boolean isImplicit;
private final boolean inBackground;
private final String checksum;

private SerializationProxyV2(String jsonString, boolean isImplicit, String checksum) {
private SerializationProxyV2(
String jsonString,
boolean isImplicit,
boolean inBackground,
String checksum) {
this.jsonString = jsonString;
this.isImplicit = isImplicit;
this.inBackground = inBackground;
this.checksum = checksum;
}

private Object readResolve() throws JSONException {
return new AppEvent(jsonString, isImplicit, checksum);
return new AppEvent(jsonString, isImplicit, inBackground, checksum);
}
}

private Object writeReplace() {
return new SerializationProxyV2(jsonObject.toString(), isImplicit, checksum);
return new SerializationProxyV2(jsonObject.toString(), isImplicit, inBackground, checksum);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,7 @@ private void logEvent(
valueToSum,
parameters,
isImplicitlyLogged,
ActivityLifecycleTracker.isInBackground(),
currentSessionId);
logEvent(event, accessTokenAppId);
} catch (JSONException jsonException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RestrictTo;
import android.util.Log;

import com.facebook.FacebookSdk;
Expand Down Expand Up @@ -82,6 +83,7 @@ public class ActivityLifecycleTracker {
private static String deviceSessionID = null;
private static Boolean isAppIndexingEnabled = false;
private static volatile Boolean isCheckingSession = false;
private static int activityReferences = 0;

public static void startTracking(Application application, final String appId) {
if (!tracking.compareAndSet(false, true)) {
Expand All @@ -103,6 +105,7 @@ public void onActivityCreated(

@Override
public void onActivityStarted(Activity activity) {
ActivityLifecycleTracker.activityReferences++;
Logger.log(LoggingBehavior.APP_EVENTS, TAG, "onActivityStarted");
}

Expand All @@ -124,6 +127,7 @@ public void onActivityPaused(final Activity activity) {
public void onActivityStopped(Activity activity) {
Logger.log(LoggingBehavior.APP_EVENTS, TAG, "onActivityStopped");
AppEventsLogger.onContextStop();
ActivityLifecycleTracker.activityReferences--;
}

@Override
Expand All @@ -138,6 +142,11 @@ public void onActivityDestroyed(Activity activity) {
});
}

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public static boolean isInBackground() {
return 0 == activityReferences;
}

public static boolean isTracking() {
return tracking.get();
}
Expand Down
38 changes: 36 additions & 2 deletions facebook/src/test/java/com/facebook/GraphRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,27 @@

package com.facebook;

import android.content.Context;
import android.graphics.Bitmap;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;

import com.facebook.internal.AttributionIdentifiers;
import com.facebook.internal.Utility;
import com.facebook.share.internal.ShareInternalUtility;

import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.robolectric.RuntimeEnvironment;

import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;

import static com.facebook.TestUtils.assertEqualContentsWithoutOrder;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.times;
Expand All @@ -47,7 +51,14 @@
import static org.powermock.api.mockito.PowerMockito.spy;
import static org.powermock.api.mockito.PowerMockito.when;

@PrepareForTest({FacebookSdk.class, AccessTokenManager.class, GraphResponse.class, Utility.class})
@PrepareForTest({
AccessToken.class,
AccessTokenManager.class,
AttributionIdentifiers.class,
FacebookSdk.class,
GraphResponse.class,
Utility.class
})
public class GraphRequestTest extends FacebookPowerMockTestCase {

@Before
Expand Down Expand Up @@ -83,7 +94,7 @@ public void testCreatePostRequest() {
assertNull(request2.getAccessToken());
assertEquals(HttpMethod.POST, request2.getHttpMethod());
assertEquals(graphPath, request2.getGraphPath());
assertEquals(parameters, request2.getParameters());
assertEqualContentsWithoutOrder(parameters, request2.getParameters());
assertNull(request2.getCallback());
}

Expand Down Expand Up @@ -265,4 +276,27 @@ public void testCallback() throws Exception {

verify(callback, times(1)).onCompleted(any(GraphResponse.class));
}

@Test
public void testRequestForCustomAudienceThirdPartyID() throws Exception {
mockStatic(AttributionIdentifiers.class);
when(AttributionIdentifiers.getAttributionIdentifiers(any(Context.class))).thenReturn(null);
doReturn(false).when(FacebookSdk.class, "getLimitEventAndDataUsage", any(Context.class));
GraphRequest expectedRequest = new GraphRequest(
null,
"mockAppID/custom_audience_third_party_id",
new Bundle(),
HttpMethod.GET,
null);

GraphRequest request = GraphRequest.newCustomAudienceThirdPartyIdRequest(
mock(AccessToken.class),
RuntimeEnvironment.application,
"mockAppID",
null);

assertEquals(expectedRequest.getGraphPath(), request.getGraphPath());
assertEquals(expectedRequest.getHttpMethod(), request.getHttpMethod());
assertEqualContentsWithoutOrder(expectedRequest.getParameters(), request.getParameters());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@

import org.json.JSONException;
import org.json.JSONObject;
import org.mockito.ArgumentMatcher;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.UUID;

import static com.facebook.TestUtils.assertEqualContentsWithoutOrder;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

Expand All @@ -46,35 +48,26 @@ public static AppEvent getTestAppEvent() throws Exception {
1.0,
customParams,
false,
false,
UUID.fromString("65565271-1ace-4580-bd13-b2bc6d0df035"));
appEvent.isChecksumValid();
return appEvent;
}

public static void assertEquals(JSONObject expected, JSONObject actual) throws JSONException {
if (expected == null) {
assertNull(actual);
}
assertNotNull(actual);
public static class BundleMatcher extends ArgumentMatcher<Bundle> {

Set<String> set1 = getKeySet(expected);
Set<String> set2 = getKeySet(actual);
Assert.assertEquals(set1, set2);
private Bundle wanted;

for (String k : set1) {
Assert.assertEquals(expected.get(k), actual.get(k));
public BundleMatcher(Bundle wanted) {
this.wanted = wanted;
}
}

public static Set<String> getKeySet(JSONObject object){
Set<String> set = new HashSet<>();

Iterator<String> keysItr = object.keys();
while(keysItr.hasNext()) {
String key = keysItr.next();
set.add(key);
public boolean matches(Object bundle) {
if (!(bundle instanceof Bundle)) {
return false;
}
assertEqualContentsWithoutOrder(this.wanted, (Bundle)bundle);
return true;
}
return set;
}

}
Loading

0 comments on commit f27ba1b

Please sign in to comment.