Skip to content

Commit

Permalink
Merge dcfc0dc into b5f116a
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon6193 committed Jul 2, 2019
2 parents b5f116a + dcfc0dc commit 8cb0ff2
Show file tree
Hide file tree
Showing 20 changed files with 104 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ interface ButtonApi {

@Nullable
@WorkerThread
PostInstallLink getPendingLink(String applicationId, String ifa,
boolean limitAdTrackingEnabled, Map<String, String> signalsMap)
PostInstallLink getPendingLink(String applicationId, @Nullable String advertisingId,
Map<String, String> signalsMap)
throws ButtonNetworkException;

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,15 @@ static ButtonApi getInstance(ConnectionManager connectionManager) {
@Nullable
@WorkerThread
@Override
public PostInstallLink getPendingLink(String applicationId, String ifa,
boolean limitAdTrackingEnabled, Map<String, String> signalsMap) throws
public PostInstallLink getPendingLink(String applicationId, @Nullable String advertisingId,
Map<String, String> signalsMap) throws
ButtonNetworkException {

try {
// Create request body
JSONObject requestBody = new JSONObject();
requestBody.put("application_id", applicationId);
requestBody.put("ifa", ifa);
requestBody.put("ifa_limited", limitAdTrackingEnabled);
requestBody.put("ifa", advertisingId);
requestBody.put("signals", new JSONObject(signalsMap));

ApiRequest apiRequest = new ApiRequest.Builder(ApiRequest.RequestMethod.POST,
Expand Down Expand Up @@ -151,6 +150,7 @@ public Void postOrder(Order order, String applicationId, String sourceToken,
requestBody.put("order_id", order.getId());
requestBody.put("purchase_date", ButtonUtil.formatDate(order.getPurchaseDate()));
requestBody.put("customer_order_id", order.getCustomerOrderId());
requestBody.put("advertising_id", advertisingId);

for (Order.LineItem lineItem : order.getLineItems()) {
JSONArray lineItemsJson = new JSONArray();
Expand Down Expand Up @@ -201,7 +201,6 @@ public Void postOrder(Order order, String applicationId, String sourceToken,
customerJson.put("email_sha256", email);
}

customerJson.put("device_id", advertisingId);
requestBody.put("customer", customerJson);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.usebutton.merchant.module.Features;

/**
* Internal implementation of the {@link ButtonMerchant} interface.
*/
Expand Down Expand Up @@ -58,6 +60,6 @@ void removeAttributionTokenListener(ButtonRepository buttonRepository, @NonNull
void handlePostInstallIntent(ButtonRepository buttonRepository,
PostInstallIntentListener listener, String packageName, DeviceManager deviceManager);

void reportOrder(ButtonRepository buttonRepository, DeviceManager deviceManager, Order order,
@Nullable OrderListener orderListener);
void reportOrder(ButtonRepository buttonRepository, DeviceManager deviceManager,
Features features, Order order, @Nullable OrderListener orderListener);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import android.support.annotation.VisibleForTesting;

import com.usebutton.merchant.exception.ApplicationIdNotFoundException;
import com.usebutton.merchant.module.Features;

import java.util.ArrayList;
import java.util.concurrent.Executor;
Expand Down Expand Up @@ -227,7 +228,7 @@ public void run() {

@Override
public void reportOrder(ButtonRepository buttonRepository, DeviceManager deviceManager,
Order order, @Nullable final OrderListener orderListener) {
Features features, Order order, @Nullable final OrderListener orderListener) {

if (buttonRepository.getApplicationId() == null) {
executor.execute(new Runnable() {
Expand All @@ -241,7 +242,7 @@ public void run() {
return;
}

buttonRepository.postOrder(order, deviceManager, new Task.Listener() {
buttonRepository.postOrder(order, deviceManager, features, new Task.Listener() {
@Override
public void onTaskComplete(@Nullable Object object) {
if (orderListener != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ public static void trackOrder(@NonNull Context context, @NonNull Order order,
*/
public static void reportOrder(@NonNull Context context, @NonNull Order order,
@Nullable OrderListener orderListener) {
buttonInternal.reportOrder(getButtonRepository(context), getDeviceManager(context), order,
orderListener);
buttonInternal.reportOrder(getButtonRepository(context), getDeviceManager(context),
FeaturesImpl.getInstance(), order, orderListener);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

import android.support.annotation.Nullable;

import com.usebutton.merchant.module.Features;

/**
* Internal data layer interface.
*/
Expand All @@ -52,5 +54,6 @@ interface ButtonRepository {

void updateCheckDeferredDeepLink(boolean checkedDeferredDeepLink);

void postOrder(Order order, DeviceManager deviceManager, Task.Listener listener);
void postOrder(Order order, DeviceManager deviceManager, Features features,
Task.Listener listener);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;

import com.usebutton.merchant.module.Features;

import java.util.concurrent.ExecutorService;

/**
Expand Down Expand Up @@ -117,9 +119,10 @@ public void updateCheckDeferredDeepLink(boolean checkedDeferredDeepLink) {
}

@Override
public void postOrder(Order order, DeviceManager deviceManager, Task.Listener listener) {
public void postOrder(Order order, DeviceManager deviceManager, Features features,
Task.Listener listener) {
executorService.submit(
new PostOrderTask(listener, buttonApi, order, getApplicationId(),
getSourceToken(), deviceManager, new ThreadManager()));
getSourceToken(), deviceManager, features, new ThreadManager()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ interface DeviceManager {
@Nullable
String getAdvertisingId();

/**
* @return true is ad tracking is limited
*/
@WorkerThread
boolean isLimitAdTrackingEnabled();

Map<String, String> getSignals();

String getTimeStamp();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ static DeviceManager getInstance(Context context) {
@Override
public String getAdvertisingId() {
try {
return AdvertisingIdClient.getAdvertisingIdInfo(context).getId();
return isLimitAdTrackingEnabled() ? null
: AdvertisingIdClient.getAdvertisingIdInfo(context).getId();
} catch (IOException e) {
Log.e(TAG, "Error has occurred", e);
} catch (GooglePlayServicesNotAvailableException e) {
Expand All @@ -107,24 +108,6 @@ public String getAdvertisingId() {
return null;
}

@WorkerThread
@Override
public boolean isLimitAdTrackingEnabled() {
try {
return AdvertisingIdClient.getAdvertisingIdInfo(context).isLimitAdTrackingEnabled();
} catch (IOException e) {
Log.e(TAG, "Error has occurred", e);
} catch (GooglePlayServicesNotAvailableException e) {
Log.e(TAG, "Error has occurred", e);
} catch (GooglePlayServicesRepairableException e) {
Log.e(TAG, "Error has occurred", e);
} catch (IllegalStateException e) {
Log.e(TAG, "Error has occurred", e);
}

return false;
}

@VisibleForTesting
String getScreenSize() {
WindowManager windowManager =
Expand Down Expand Up @@ -213,6 +196,16 @@ public String getUserAgent() {
return sb.toString();
}

/**
* @return true is ad tracking is limited
*/
@WorkerThread
private boolean isLimitAdTrackingEnabled()
throws GooglePlayServicesNotAvailableException, IOException,
GooglePlayServicesRepairableException {
return AdvertisingIdClient.getAdvertisingIdInfo(context).isLimitAdTrackingEnabled();
}

private String getSdkVersionName() {
return BuildConfig.VERSION_NAME;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public void setIncludesIfa(boolean includesIfa) {
this.includesIfa = includesIfa;
}

public boolean includesIfa() {
@Override
public boolean getIncludesIfa() {
return includesIfa;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ final class GetPendingLinkTask extends Task<PostInstallLink> {
@Override
PostInstallLink execute() throws Exception {
return buttonApi.getPendingLink(applicationId, deviceManager.getAdvertisingId(),
deviceManager.isLimitAdTrackingEnabled(), deviceManager.getSignals());
deviceManager.getSignals());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.usebutton.merchant.exception.ButtonNetworkException;
import com.usebutton.merchant.exception.HttpStatusException;
import com.usebutton.merchant.exception.NetworkNotFoundException;
import com.usebutton.merchant.module.Features;

/**
* Asynchronous task used to report order to the Button API.
Expand All @@ -42,6 +43,7 @@ class PostOrderTask extends Task {
private final String sourceToken;
private final Order order;
private final DeviceManager deviceManager;
private final Features features;
private final ThreadManager threadManager;

@VisibleForTesting
Expand All @@ -51,22 +53,21 @@ class PostOrderTask extends Task {

PostOrderTask(@Nullable Listener listener, ButtonApi buttonApi, Order order,
String applicationId, String sourceToken, DeviceManager deviceManager,
ThreadManager threadManager) {
Features features, ThreadManager threadManager) {
super(listener);
this.buttonApi = buttonApi;
this.order = order;
this.applicationId = applicationId;
this.sourceToken = sourceToken;
this.deviceManager = deviceManager;
this.features = features;
this.threadManager = threadManager;
}

@Nullable
@Override
Void execute() throws Exception {
String advertisingId = deviceManager.isLimitAdTrackingEnabled()
? null : deviceManager.getAdvertisingId();

String advertisingId = features.getIncludesIfa() ? deviceManager.getAdvertisingId() : null;
// loop and execute postOrder until max retries is met or non case exception is met
while (true) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ public interface Features {

void setIncludesIfa(boolean includesIfa);

boolean getIncludesIfa();
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void getPendingLink_returnValidResponse_validatePostInstallLink() throws

PostInstallLink postInstallLink =
buttonApi.getPendingLink("valid_application_id", "valid_ifa",
true, Collections.<String, String>emptyMap());
Collections.<String, String>emptyMap());

assertNotNull(postInstallLink);
assertEquals(true, postInstallLink.isMatch());
Expand All @@ -92,7 +92,7 @@ public void getPendingLink_validateRequest() throws Exception {
.thenReturn(response);

buttonApi.getPendingLink("valid_application_id", "valid_ifa",
true, Collections.singletonMap("key", "value"));
Collections.singletonMap("key", "value"));

ApiRequest apiRequest = argumentCaptor.getValue();
JSONObject body = apiRequest.getBody();
Expand All @@ -104,7 +104,6 @@ public void getPendingLink_validateRequest() throws Exception {
// request body
assertEquals("valid_application_id", body.getString("application_id"));
assertEquals("valid_ifa", body.getString("ifa"));
assertEquals(true, body.getBoolean("ifa_limited"));
assertEquals("value", signals.getString("key"));
}

Expand All @@ -114,7 +113,7 @@ public void getPendingLink_returnJSONException_catchException() throws Exception
when(response.getBody()).thenThrow(JSONException.class);
when(connectionManager.executeRequest(any(ApiRequest.class))).thenReturn(response);

buttonApi.getPendingLink("valid_application_id", "valid_ifa", true,
buttonApi.getPendingLink("valid_application_id", "valid_ifa",
Collections.<String, String>emptyMap());
}

Expand All @@ -123,7 +122,7 @@ public void getPendingLink_returnsException_catchException() throws Exception {
when(connectionManager.executeRequest(any(ApiRequest.class)))
.thenThrow(ButtonNetworkException.class);

buttonApi.getPendingLink("valid_application_id", "valid_ifa", true,
buttonApi.getPendingLink("valid_application_id", "valid_ifa",
Collections.<String, String>emptyMap());
}

Expand Down Expand Up @@ -211,9 +210,10 @@ public void postOrder_validateOrder() throws Exception {
.build();

String sourceToken = "valid_source_token";
String advertisingId = "valid_advertising_id";

buttonApi.postOrder(order, "valid_application_id",
sourceToken, "valid_advertising_id");
sourceToken, advertisingId);

ArgumentCaptor<ApiRequest> argumentCaptor = ArgumentCaptor.forClass(ApiRequest.class);
verify(connectionManager).executeRequest(argumentCaptor.capture());
Expand All @@ -226,6 +226,7 @@ public void postOrder_validateOrder() throws Exception {
assertEquals(ButtonUtil.formatDate(purchaseDate),
requestBody.getString("purchase_date"));
assertEquals(customerOrderId, requestBody.getString("customer_order_id"));
assertEquals(advertisingId, requestBody.getString("advertising_id"));

assertNull(requestBody.optJSONArray("line_items"));
assertNull(requestBody.optJSONObject("customer"));
Expand Down Expand Up @@ -298,10 +299,8 @@ public void postOrder_validateCustomer() throws Exception {
.setCustomer(customer)
.build();

String advertisingId = "valid_advertising_id";

buttonApi.postOrder(order, "valid_application_id",
"valid_source_token", advertisingId);
"valid_source_token", "valid_advertising_id");

ArgumentCaptor<ApiRequest> argumentCaptor = ArgumentCaptor.forClass(ApiRequest.class);
verify(connectionManager).executeRequest(argumentCaptor.capture());
Expand All @@ -311,7 +310,6 @@ public void postOrder_validateCustomer() throws Exception {
JSONObject customerJson = requestBody.getJSONObject("customer");
assertEquals(customerId, customerJson.getString("id"));
assertEquals(customerEmail, customerJson.getString("email_sha256"));
assertEquals(advertisingId, customerJson.getString("device_id"));
}

@Test(expected = ButtonNetworkException.class)
Expand Down

0 comments on commit 8cb0ff2

Please sign in to comment.