Skip to content

Commit

Permalink
fix: previous Google ad Id from being null on cold launch
Browse files Browse the repository at this point in the history
  • Loading branch information
Mansi-mParticle committed Oct 31, 2024
1 parent 268d850 commit 8ada158
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
2 changes: 2 additions & 0 deletions android-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ dependencies {
testImplementation project(':testutils')
testImplementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$kotlin_version"

androidTestImplementation project(':testutils')
if (useOrchestrator()) {
Expand All @@ -167,6 +168,7 @@ dependencies {
}
androidTestImplementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
androidTestImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
androidTestImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$kotlin_version"
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import com.mparticle.internal.database.services.AccessUtils
import com.mparticle.internal.database.services.MParticleDBManager
import com.mparticle.testutils.BaseCleanStartedEachTest
import com.mparticle.testutils.MPLatch
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.runTest
import org.json.JSONException
import org.junit.Assert
import org.junit.Before
Expand Down Expand Up @@ -104,7 +106,7 @@ class AppStateManagerInstrumentedTest : BaseCleanStartedEachTest() {

@Test
@Throws(InterruptedException::class)
fun testOnApplicationForeground() {
fun testOnApplicationForeground() = runTest(StandardTestDispatcher()) {
val latch: CountDownLatch = MPLatch(2)
val kitManagerTester = KitManagerTester(mContext, latch)
com.mparticle.AccessUtils.setKitManager(kitManagerTester)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ 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 infoId = null;

public static long getAvailableMemory(Context context) {
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
Expand Down Expand Up @@ -122,18 +123,21 @@ public static boolean isEmpty(Collection collection) {
@WorkerThread
@Nullable
public static AdIdInfo getAdIdInfo(Context context) {
if (infoId != null) {
return infoId;
}
String packageName = context.getPackageName();
PackageManager packageManager = context.getPackageManager();
String installerName = packageManager.getInstallerPackageName(packageName);
if ((installerName != null && installerName.contains("com.amazon.venezia")) ||
"Amazon".equals(android.os.Build.MANUFACTURER)) {
AdIdInfo infoId = getAmazonAdIdInfo(context);
infoId = getAmazonAdIdInfo(context);
if (infoId == null) {
return getGoogleAdIdInfo(context);
}
return infoId;
} else {
AdIdInfo infoId = getGoogleAdIdInfo(context);
infoId = getGoogleAdIdInfo(context);
if (infoId == null) {
return getAmazonAdIdInfo(context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import com.mparticle.identity.IdentityApi.SingleUserIdentificationCallback
import com.mparticle.identity.IdentityApiRequest
import com.mparticle.identity.MParticleUser
import com.mparticle.internal.listeners.InternalListenerManager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.json.JSONObject
import java.lang.ref.WeakReference
import java.util.concurrent.atomic.AtomicInteger
Expand Down Expand Up @@ -166,6 +169,9 @@ open class AppStateManager @JvmOverloads constructor(
interruptions
)
}
CoroutineScope(Dispatchers.IO).launch {
mConfigManager?.setPreviousAdId()
}
mLastForegroundTime = time

if (currentActivity != null) {
Expand Down Expand Up @@ -207,7 +213,6 @@ open class AppStateManager @JvmOverloads constructor(
if (isBackgrounded()) {
checkSessionTimeout()
logBackgrounded()
mConfigManager?.setPreviousAdId()
}
} catch (e: Exception) {
e.printStackTrace()
Expand Down Expand Up @@ -421,7 +426,9 @@ open class AppStateManager @JvmOverloads constructor(
internal class CheckAdIdRunnable(var configManager: ConfigManager?) : Runnable {
override fun run() {
val adIdInfo =
MPUtility.getAdIdInfo(MParticle.getInstance()?.Internal()?.appStateManager?.mContext)
MPUtility.getAdIdInfo(
MParticle.getInstance()?.Internal()?.appStateManager?.mContext
)
val currentAdId =
(if (adIdInfo == null) null else (if (adIdInfo.isLimitAdTrackingEnabled) null else adIdInfo.id))
val previousAdId = configManager?.previousAdId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import com.mparticle.mock.MockApplication
import com.mparticle.mock.MockContext
import com.mparticle.mock.MockSharedPreferences
import com.mparticle.testutils.AndroidUtils
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.Assert
import org.junit.Before
import org.junit.Test
Expand Down Expand Up @@ -132,7 +134,7 @@ class AppStateManagerTest {
*/
@Test
@Throws(Exception::class)
fun testSecondActivityStart() {
fun testSecondActivityStart() = runTest(StandardTestDispatcher()) {
manager.onActivityPaused(activity)
Thread.sleep(1000)
Assert.assertEquals(true, manager.isBackgrounded())
Expand Down

0 comments on commit 8ada158

Please sign in to comment.