Skip to content

Commit

Permalink
fix: Remove Code for Handling Google AdId on a Separate Thread (#502)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mansi-mParticle authored Aug 5, 2024
1 parent eafccf5 commit 17928b9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 94 deletions.
56 changes: 13 additions & 43 deletions android-core/src/main/java/com/mparticle/internal/MPUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@
import java.util.Map;
import java.util.TimeZone;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import javax.crypto.Mac;
Expand All @@ -73,7 +70,6 @@ public class MPUtility {
private static String sOpenUDID;
private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
private static final String TAG = MPUtility.class.toString();
private static AdIdInfo googleAdIdInfo;

public static long getAvailableMemory(Context context) {
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
Expand Down Expand Up @@ -146,49 +142,23 @@ public static AdIdInfo getAdIdInfo(Context context) {
}

private static AdIdInfo getGoogleAdIdInfo(Context context) {
if (googleAdIdInfo != null) {
fetchGoogleAdInfo(context, false);
return googleAdIdInfo;
} else {
fetchGoogleAdInfo(context, true);
}
return null;
}

private static void fetchGoogleAdInfo(Context context, Boolean wait) {
ExecutorService executorService = Executors.newSingleThreadExecutor();
Future<?> future = executorService.submit(() -> {
try {
Class AdvertisingIdClient = Class
.forName("com.google.android.gms.ads.identifier.AdvertisingIdClient");
Method getAdvertisingInfo = AdvertisingIdClient.getMethod("getAdvertisingIdInfo",
Context.class);
Object advertisingInfo = getAdvertisingInfo.invoke(null, context);
Method isLimitAdTrackingEnabled = advertisingInfo.getClass().getMethod(
"isLimitAdTrackingEnabled");
Boolean limitAdTrackingEnabled = (Boolean) isLimitAdTrackingEnabled
.invoke(advertisingInfo);

Method getId = advertisingInfo.getClass().getMethod("getId");
String advertisingId = (String) getId.invoke(advertisingInfo);
googleAdIdInfo = new AdIdInfo(advertisingId, Boolean.TRUE.equals(limitAdTrackingEnabled), AdIdInfo.Advertiser.GOOGLE);
} catch (Exception e) {
Logger.info(TAG, "Could not locate Google Play Ads Identifier library");
}
});
try {
if (Boolean.TRUE.equals(wait)) {
future.get();
}
} catch (InterruptedException ie) {
Logger.info(TAG, "Interrupted while waiting for Google Play Ads Identifier library" + ie);
Thread.currentThread().interrupt();
Class AdvertisingIdClient = Class
.forName("com.google.android.gms.ads.identifier.AdvertisingIdClient");
Method getAdvertisingInfo = AdvertisingIdClient.getMethod("getAdvertisingIdInfo",
Context.class);
Object advertisingInfo = getAdvertisingInfo.invoke(null, context);
Method isLimitAdTrackingEnabled = advertisingInfo.getClass().getMethod(
"isLimitAdTrackingEnabled");
Boolean limitAdTrackingEnabled = (Boolean) isLimitAdTrackingEnabled
.invoke(advertisingInfo);
Method getId = advertisingInfo.getClass().getMethod("getId");
String advertisingId = (String) getId.invoke(advertisingInfo);
return new AdIdInfo(advertisingId, limitAdTrackingEnabled, AdIdInfo.Advertiser.GOOGLE);
} catch (Exception e) {
Logger.info(TAG, "Could not locate Google Play Ads Identifier library");
} finally {
// Shutdown the executor service to release its resources
executorService.shutdown();
}
return null;
}

private static AdIdInfo getAmazonAdIdInfo(Context context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import android.content.res.Configuration
import android.content.res.Resources
import android.telephony.TelephonyManager
import android.util.DisplayMetrics
import com.mparticle.internal.MPUtility.AdIdInfo
import com.mparticle.mock.MockContext
import com.mparticle.mock.utils.RandomUtils
import junit.framework.TestCase
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
Expand All @@ -20,12 +18,8 @@ import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.Mockito.`when`
import org.mockito.MockitoAnnotations
import java.lang.reflect.Field
import java.lang.reflect.Method
import java.util.Collections
import java.util.UUID
import kotlin.test.assertNotNull
import kotlin.test.assertNull

class MPUtilityTest {

Expand Down Expand Up @@ -105,12 +99,6 @@ class MPUtilityTest {
@Test
@Throws(Exception::class)
fun googleAdIdInfoWithoutPlayServicesAvailable() {
val adIDInfo: AdIdInfo? = null
val field: Field = MPUtility::class.java.getDeclaredField("googleAdIdInfo")
field.apply {
isAccessible = true
}
field.set(instance, adIDInfo)
Assert.assertNull(MPUtility.getAdIdInfo(MockContext()))
}

Expand Down Expand Up @@ -285,45 +273,6 @@ class MPUtilityTest {
Assert.assertNull(MPUtility.toNumberOrString(null))
}

@Test
fun testGetGoogleAdIdInfo() {
val adIDInfo = AdIdInfo("12345", true, AdIdInfo.Advertiser.GOOGLE)
val field: Field = MPUtility::class.java.getDeclaredField("googleAdIdInfo")
field.apply {
isAccessible = true
}
field.set(instance, adIDInfo)

val method: Method = MPUtility::class.java.getDeclaredMethod(
"getGoogleAdIdInfo",
Context::class.java
)
method.isAccessible = true
val result = method.invoke(instance, mockContext)
val mpUtilityResult: AdIdInfo = result as AdIdInfo
assertNotNull(result)
TestCase.assertEquals("12345", mpUtilityResult.id)
TestCase.assertEquals(true, mpUtilityResult.isLimitAdTrackingEnabled)
TestCase.assertEquals(AdIdInfo.Advertiser.GOOGLE, mpUtilityResult.advertiser)
}

@Test
fun testGetGoogleAdIdInfo_WHEN_adIDInfo_IS_NULL() {
val adIDInfo: AdIdInfo? = null
val field: Field = MPUtility::class.java.getDeclaredField("googleAdIdInfo")
field.apply {
isAccessible = true
}
field.set(instance, adIDInfo)
val method: Method = MPUtility::class.java.getDeclaredMethod(
"getGoogleAdIdInfo",
Context::class.java
)
method.isAccessible = true
val result = method.invoke(instance, mockContext)
assertNull(result)
}

@Test
fun testGetOrientation() {
val mockResources = Mockito.mock(
Expand Down

0 comments on commit 17928b9

Please sign in to comment.