Skip to content

Commit

Permalink
fix: do not fetch advertising Id if adid tracking is disabled (#366)
Browse files Browse the repository at this point in the history
  • Loading branch information
falconandy authored Jun 13, 2023
1 parent 48716c2 commit a1f8cc8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/amplitude/api/AmplitudeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2395,7 +2395,7 @@ public void run() {
}

protected DeviceInfo initializeDeviceInfo() {
return new DeviceInfo(context, this.locationListening);
return new DeviceInfo(context, this.locationListening, appliedTrackingOptions.shouldTrackAdid());
}

/**
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/amplitude/api/DeviceInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public class DeviceInfo {
private static final String SETTING_LIMIT_AD_TRACKING = "limit_ad_tracking";
private static final String SETTING_ADVERTISING_ID = "advertising_id";

private boolean locationListening = true;
private boolean locationListening;

private boolean shouldTrackAdid;

private Context context;

Expand Down Expand Up @@ -213,6 +215,10 @@ private String getLanguage() {
}

private String getAdvertisingId() {
if (!shouldTrackAdid) {
return null;
}

// This should not be called on the main thread.
if ("Amazon".equals(getManufacturer())) {
return getAndCacheAmazonAdvertisingId();
Expand Down Expand Up @@ -308,9 +314,10 @@ private boolean checkGPSEnabled() {
}
}

public DeviceInfo(Context context, boolean locationListening) {
public DeviceInfo(Context context, boolean locationListening, boolean shouldTrackAdid) {
this.context = context;
this.locationListening = locationListening;
this.shouldTrackAdid = shouldTrackAdid;
}

private CachedInfo getCachedInfo() {
Expand Down
42 changes: 35 additions & 7 deletions src/test/java/com/amplitude/api/DeviceInfoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void setUp() throws Exception {
ShadowTelephonyManager manager = Shadows.shadowOf((TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE));
manager.setNetworkOperatorName(TEST_CARRIER);
deviceInfo = new DeviceInfo(context, true);
deviceInfo = new DeviceInfo(context, true, false);
}

@After
Expand Down Expand Up @@ -140,7 +140,7 @@ public void testGetCountryFromNetwork() {
.getSystemService(Context.TELEPHONY_SERVICE));
manager.setNetworkCountryIso(TEST_NETWORK_COUNTRY);

DeviceInfo deviceInfo = new DeviceInfo(context, true);
DeviceInfo deviceInfo = new DeviceInfo(context, true, false);
assertEquals(TEST_NETWORK_COUNTRY, deviceInfo.getCountry());
}

Expand Down Expand Up @@ -190,13 +190,25 @@ public void testGetAdvertisingIdFromGoogleDevice() {
} catch (Exception e) {
fail(e.toString());
}
DeviceInfo deviceInfo = new DeviceInfo(context, true);
DeviceInfo deviceInfo = new DeviceInfo(context, true, true);

// still get advertisingId even if limit ad tracking disabled
assertEquals(advertisingId, deviceInfo.getAdvertisingId());
assertFalse(deviceInfo.isLimitAdTrackingEnabled());
}

@Test
public void testGetAdvertisingIdFromGoogleDeviceDisabledTrackAdid() {
PowerMockito.mockStatic(AdvertisingIdClient.class);

DeviceInfo deviceInfo = new DeviceInfo(context, true, false);

assertNull(deviceInfo.getAdvertisingId());
assertFalse(deviceInfo.isLimitAdTrackingEnabled());

PowerMockito.verifyStatic(Mockito.never());
}

@Test
public void testGetAdvertisingIdFromAmazonDevice() {
ReflectionHelpers.setStaticField(Build.class, "MANUFACTURER", "Amazon");
Expand All @@ -207,17 +219,33 @@ public void testGetAdvertisingIdFromAmazonDevice() {
Secure.putInt(cr, "limit_ad_tracking", 1);
Secure.putString(cr, "advertising_id", advertisingId);

DeviceInfo deviceInfo = new DeviceInfo(context, true);
DeviceInfo deviceInfo = new DeviceInfo(context, true, true);

// still get advertisingID even if limit ad tracking enabled
assertEquals(advertisingId, deviceInfo.getAdvertisingId());
assertTrue(deviceInfo.isLimitAdTrackingEnabled());
}

@Test
public void testGetAdvertisingIdFromAmazonDeviceDisabledTrackAdid() {
ReflectionHelpers.setStaticField(Build.class, "MANUFACTURER", "Amazon");

String advertisingId = "advertisingId";
ContentResolver cr = context.getContentResolver();

Secure.putInt(cr, "limit_ad_tracking", 1);
Secure.putString(cr, "advertising_id", advertisingId);

DeviceInfo deviceInfo = new DeviceInfo(context, true, false);

assertNull(deviceInfo.getAdvertisingId());
assertFalse(deviceInfo.isLimitAdTrackingEnabled());
}

@Test
public void testGPSDisabled() {
// GPS not enabled
DeviceInfo deviceInfo = new DeviceInfo(context, true);
DeviceInfo deviceInfo = new DeviceInfo(context, true, false);
assertFalse(deviceInfo.isGooglePlayServicesEnabled());

// GPS bundled but not enabled, GooglePlayUtils.isAvailable returns non-0 value
Expand Down Expand Up @@ -257,7 +285,7 @@ public void testGPSEnabled() {

@Test
public void testNoLocation() {
DeviceInfo deviceInfo = new DeviceInfo(context, true);
DeviceInfo deviceInfo = new DeviceInfo(context, true, false);
Location recent = deviceInfo.getMostRecentLocation();
assertNull(recent);
}
Expand Down Expand Up @@ -410,7 +438,7 @@ protected DeviceInfo initializeDeviceInfo() {
return this.publicInitializeDeviceInfo();
}
public DeviceInfo publicInitializeDeviceInfo() {
return new DeviceInfo(context, true);
return new DeviceInfo(context, true, false);
}
public DeviceInfoAmplitudeClient(String instance) {
super(instance);
Expand Down

0 comments on commit a1f8cc8

Please sign in to comment.