diff --git a/android-core/build.gradle b/android-core/build.gradle index 45da2dd17..288d463fb 100644 --- a/android-core/build.gradle +++ b/android-core/build.gradle @@ -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()) { @@ -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" } diff --git a/android-core/src/androidTest/kotlin/com.mparticle/internal/AppStateManagerInstrumentedTest.kt b/android-core/src/androidTest/kotlin/com.mparticle/internal/AppStateManagerInstrumentedTest.kt index da980a7ba..3fda9adca 100644 --- a/android-core/src/androidTest/kotlin/com.mparticle/internal/AppStateManagerInstrumentedTest.kt +++ b/android-core/src/androidTest/kotlin/com.mparticle/internal/AppStateManagerInstrumentedTest.kt @@ -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 @@ -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) diff --git a/android-core/src/main/java/com/mparticle/internal/MPUtility.java b/android-core/src/main/java/com/mparticle/internal/MPUtility.java index 1381644b1..144effdf2 100644 --- a/android-core/src/main/java/com/mparticle/internal/MPUtility.java +++ b/android-core/src/main/java/com/mparticle/internal/MPUtility.java @@ -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(); @@ -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); } diff --git a/android-core/src/main/kotlin/com/mparticle/internal/AppStateManager.kt b/android-core/src/main/kotlin/com/mparticle/internal/AppStateManager.kt index eba0118f6..2c7a20f64 100644 --- a/android-core/src/main/kotlin/com/mparticle/internal/AppStateManager.kt +++ b/android-core/src/main/kotlin/com/mparticle/internal/AppStateManager.kt @@ -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 @@ -166,6 +169,9 @@ open class AppStateManager @JvmOverloads constructor( interruptions ) } + CoroutineScope(Dispatchers.IO).launch { + mConfigManager?.setPreviousAdId() + } mLastForegroundTime = time if (currentActivity != null) { @@ -207,7 +213,6 @@ open class AppStateManager @JvmOverloads constructor( if (isBackgrounded()) { checkSessionTimeout() logBackgrounded() - mConfigManager?.setPreviousAdId() } } catch (e: Exception) { e.printStackTrace() @@ -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 diff --git a/android-core/src/test/kotlin/com/mparticle/internal/AppStateManagerTest.kt b/android-core/src/test/kotlin/com/mparticle/internal/AppStateManagerTest.kt index 686a9f5dc..7b8990aa7 100644 --- a/android-core/src/test/kotlin/com/mparticle/internal/AppStateManagerTest.kt +++ b/android-core/src/test/kotlin/com/mparticle/internal/AppStateManagerTest.kt @@ -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 @@ -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())