From a1f8cc80af0bbaf2b7edc3b47b87c3f518ea8433 Mon Sep 17 00:00:00 2001 From: Andrey Sokolov Date: Tue, 13 Jun 2023 20:47:51 +0400 Subject: [PATCH] fix: do not fetch advertising Id if adid tracking is disabled (#366) --- .../com/amplitude/api/AmplitudeClient.java | 2 +- .../java/com/amplitude/api/DeviceInfo.java | 11 ++++- .../com/amplitude/api/DeviceInfoTest.java | 42 +++++++++++++++---- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/amplitude/api/AmplitudeClient.java b/src/main/java/com/amplitude/api/AmplitudeClient.java index 2f5c6856..feeb28ad 100644 --- a/src/main/java/com/amplitude/api/AmplitudeClient.java +++ b/src/main/java/com/amplitude/api/AmplitudeClient.java @@ -2395,7 +2395,7 @@ public void run() { } protected DeviceInfo initializeDeviceInfo() { - return new DeviceInfo(context, this.locationListening); + return new DeviceInfo(context, this.locationListening, appliedTrackingOptions.shouldTrackAdid()); } /** diff --git a/src/main/java/com/amplitude/api/DeviceInfo.java b/src/main/java/com/amplitude/api/DeviceInfo.java index db5a3c7b..f3599350 100644 --- a/src/main/java/com/amplitude/api/DeviceInfo.java +++ b/src/main/java/com/amplitude/api/DeviceInfo.java @@ -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; @@ -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(); @@ -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() { diff --git a/src/test/java/com/amplitude/api/DeviceInfoTest.java b/src/test/java/com/amplitude/api/DeviceInfoTest.java index 87b5d026..8348bdd6 100644 --- a/src/test/java/com/amplitude/api/DeviceInfoTest.java +++ b/src/test/java/com/amplitude/api/DeviceInfoTest.java @@ -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 @@ -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()); } @@ -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"); @@ -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 @@ -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); } @@ -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);