Collect Advertising ID from Amazon Fire devices.
Reference:
MODIFY
analytics/src/main/java/com/segment/analytics/GetAdvertisingIdTask.java
analytics/src/main/java/com/segment/analytics/GetAdvertisingIdTask.java
package com.segment.analytics;
+import android.content.ContentResolver;
import android.content.Context;
import android.os.AsyncTask;
+import android.provider.Settings.Secure;
import android.util.Pair;
import com.segment.analytics.integrations.Logger;
import java.util.concurrent.CountDownLatch;
@@ -12,9 +14,9 @@ import java.util.concurrent.CountDownLatch;
*/
class GetAdvertisingIdTask extends AsyncTask<Context, Void, Pair<String, Boolean>> {
- final AnalyticsContext analyticsContext;
- final CountDownLatch latch;
- final Logger logger;
+ private final AnalyticsContext analyticsContext;
+ private final CountDownLatch latch;
+ private final Logger logger;
GetAdvertisingIdTask(AnalyticsContext analyticsContext, CountDownLatch latch, Logger logger) {
this.analyticsContext = analyticsContext;
@@ -22,10 +24,8 @@ class GetAdvertisingIdTask extends AsyncTask<Context, Void, Pair<String, Boolean
this.logger = logger;
}
- @Override
- protected Pair<String, Boolean> doInBackground(Context... contexts) {
- final Context context = contexts[0];
- try {
+ private Pair<String, Boolean> getGooglePlayServicesAdvertisingID(Context context)
+ throws Exception {
Object advertisingInfo =
Class.forName("com.google.android.gms.ads.identifier.AdvertisingIdClient")
.getMethod("getAdvertisingIdInfo", Context.class)
@@ -38,16 +38,47 @@ class GetAdvertisingIdTask extends AsyncTask<Context, Void, Pair<String, Boolean
.invoke(advertisingInfo);
if (isLimitAdTrackingEnabled) {
- logger.debug("Not collecting advertising ID because isLimitAdTrackingEnabled is true.");
+ logger.debug(
+ "Not collecting advertising ID because isLimitAdTrackingEnabled (Google Play Services) is true.");
return Pair.create(null, false);
}
String advertisingId =
(String) advertisingInfo.getClass().getMethod("getId").invoke(advertisingInfo);
return Pair.create(advertisingId, true);
+ }
+
+ private Pair<String, Boolean> getAmazonFireAdvertisingID(Context context) throws Exception {
+ ContentResolver contentResolver = context.getContentResolver();
+
+ // Ref: http://prateeks.link/2uGs6bf
+ // limit_ad_tracking != 0 indicates user wants to limit ad tracking.
+ boolean limitAdTracking = Secure.getInt(contentResolver, "limit_ad_tracking") != 0;
+
+ if (limitAdTracking) {
+ logger.debug(
+ "Not collecting advertising ID because limit_ad_tracking (Amazon Fire OS) is true.");
+ return Pair.create(null, false);
+ }
+
+ String advertisingId = Secure.getString(contentResolver, "advertising_id");
+ return Pair.create(advertisingId, true);
+ }
+
+ @Override
+ protected Pair<String, Boolean> doInBackground(Context... contexts) {
+ final Context context = contexts[0];
+ try {
+ return getGooglePlayServicesAdvertisingID(context);
+ } catch (Exception e) {
+ logger.error(e, "Unable to collect advertising ID from Google Play Services.");
+ }
+ try {
+ return getAmazonFireAdvertisingID(context);
} catch (Exception e) {
- logger.error(e, "Unable to collect advertising ID.");
+ logger.error(e, "Unable to collect advertising ID from Amazon Fire OS.");
}
+ logger.debug("Unable to collect advertising ID from Amazon Fire OS and Google Play Services.");
return null;
}

MODIFY
analytics/src/test/java/com/segment/analytics/GetAdvertisingIdTaskTest.java
analytics/src/test/java/com/segment/analytics/GetAdvertisingIdTaskTest.java
@@ -3,8 +3,10 @@ package com.segment.analytics;
import static org.assertj.core.api.Assertions.assertThat;
import static org.robolectric.annotation.Config.NONE;
+import android.content.ContentResolver;
import android.content.Context;
import android.os.AsyncTask;
+import android.provider.Settings.Secure;
import android.util.Pair;
import com.segment.analytics.integrations.Logger;
import java.util.concurrent.CountDownLatch;
@@ -29,4 +31,46 @@ public class GetAdvertisingIdTaskTest {
latch.await();
assertThat(context.device()).doesNotContainKey("advertisingId");
}
+
+ @Test
+ public void getAdvertisingIdAmazonFireOSLimitAdTracking1() throws Exception {
+ Context context = RuntimeEnvironment.application;
+ ContentResolver contentResolver = context.getContentResolver();
+ Secure.putInt(contentResolver, "limit_ad_tracking", 1);
+ CountDownLatch latch = new CountDownLatch(1);
+
+ Traits traits = Traits.create();
+ AnalyticsContext analyticsContext =
+ AnalyticsContext.create(RuntimeEnvironment.application, traits, true);
+
+ AsyncTask<Context, Void, Pair<String, Boolean>> task =
+ new GetAdvertisingIdTask(analyticsContext, latch, Logger.with(Analytics.LogLevel.VERBOSE));
+ task.execute(context);
+ latch.await();
+
+ assertThat(analyticsContext.device()).doesNotContainKey("advertisingId");
+ assertThat(analyticsContext.device()).containsEntry("adTrackingEnabled", false);
+ }
+
+ @Test
+ public void getAdvertisingIdAmazonFireOSLimitAdTracking0() throws Exception {
+ Context context = RuntimeEnvironment.application;
+ ContentResolver contentResolver = context.getContentResolver();
+ Secure.putInt(contentResolver, "limit_ad_tracking", 0);
+ Secure.putString(contentResolver, "advertising_id", "df07c7dc-cea7-4a89-b328-810ff5acb15d");
+
+ CountDownLatch latch = new CountDownLatch(1);
+
+ Traits traits = Traits.create();
+ AnalyticsContext analyticsContext =
+ AnalyticsContext.create(RuntimeEnvironment.application, traits, true);
+
+ AsyncTask<Context, Void, Pair<String, Boolean>> task =
+ new GetAdvertisingIdTask(analyticsContext, latch, Logger.with(Analytics.LogLevel.VERBOSE));
+ task.execute(context);
+ latch.await();
+
+ assertThat(analyticsContext.device()).containsEntry("advertisingId", "df07c7dc-cea7-4a89-b328-810ff5acb15d");
+ assertThat(analyticsContext.device()).containsEntry("adTrackingEnabled", true);
+ }
}

Collect Advertising ID from Amazon Fire devices.

Reference:
https://developer.amazon.com/public/solutions/devices/fire-tv/docs/fire-tv-advertising-id#verifying-and-responding-to-the-users-advertising-id-choices