Skip to content

Commit fa9b79b

Browse files
fix: previous Google ad Id from being null on cold launch (#518)
1 parent 669ae88 commit fa9b79b

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

android-core/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ dependencies {
159159
testImplementation project(':testutils')
160160
testImplementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
161161
testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
162+
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$kotlin_version"
162163

163164
androidTestImplementation project(':testutils')
164165
if (useOrchestrator()) {
@@ -167,6 +168,7 @@ dependencies {
167168
}
168169
androidTestImplementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
169170
androidTestImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
171+
androidTestImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$kotlin_version"
170172
}
171173

172174

android-core/src/androidTest/kotlin/com.mparticle/internal/AppStateManagerInstrumentedTest.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import com.mparticle.internal.database.services.AccessUtils
88
import com.mparticle.internal.database.services.MParticleDBManager
99
import com.mparticle.testutils.BaseCleanStartedEachTest
1010
import com.mparticle.testutils.MPLatch
11+
import kotlinx.coroutines.test.StandardTestDispatcher
12+
import kotlinx.coroutines.test.runTest
1113
import org.json.JSONException
1214
import org.junit.Assert
1315
import org.junit.Before
@@ -104,7 +106,7 @@ class AppStateManagerInstrumentedTest : BaseCleanStartedEachTest() {
104106

105107
@Test
106108
@Throws(InterruptedException::class)
107-
fun testOnApplicationForeground() {
109+
fun testOnApplicationForeground() = runTest(StandardTestDispatcher()) {
108110
val latch: CountDownLatch = MPLatch(2)
109111
val kitManagerTester = KitManagerTester(mContext, latch)
110112
com.mparticle.AccessUtils.setKitManager(kitManagerTester)

android-core/src/main/java/com/mparticle/internal/MPUtility.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public class MPUtility {
7070
private static String sOpenUDID;
7171
private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
7272
private static final String TAG = MPUtility.class.toString();
73+
private static AdIdInfo adInfoId = null;
7374

7475
public static long getAvailableMemory(Context context) {
7576
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
@@ -122,22 +123,25 @@ public static boolean isEmpty(Collection collection) {
122123
@WorkerThread
123124
@Nullable
124125
public static AdIdInfo getAdIdInfo(Context context) {
126+
if (adInfoId != null) {
127+
return adInfoId;
128+
}
125129
String packageName = context.getPackageName();
126130
PackageManager packageManager = context.getPackageManager();
127131
String installerName = packageManager.getInstallerPackageName(packageName);
128132
if ((installerName != null && installerName.contains("com.amazon.venezia")) ||
129133
"Amazon".equals(android.os.Build.MANUFACTURER)) {
130-
AdIdInfo infoId = getAmazonAdIdInfo(context);
131-
if (infoId == null) {
134+
adInfoId = getAmazonAdIdInfo(context);
135+
if (adInfoId == null) {
132136
return getGoogleAdIdInfo(context);
133137
}
134-
return infoId;
138+
return adInfoId;
135139
} else {
136-
AdIdInfo infoId = getGoogleAdIdInfo(context);
137-
if (infoId == null) {
140+
adInfoId = getGoogleAdIdInfo(context);
141+
if (adInfoId == null) {
138142
return getAmazonAdIdInfo(context);
139143
}
140-
return infoId;
144+
return adInfoId;
141145
}
142146
}
143147

android-core/src/main/kotlin/com/mparticle/internal/AppStateManager.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import com.mparticle.identity.IdentityApi.SingleUserIdentificationCallback
1717
import com.mparticle.identity.IdentityApiRequest
1818
import com.mparticle.identity.MParticleUser
1919
import com.mparticle.internal.listeners.InternalListenerManager
20+
import kotlinx.coroutines.CoroutineScope
21+
import kotlinx.coroutines.Dispatchers
22+
import kotlinx.coroutines.launch
2023
import org.json.JSONObject
2124
import java.lang.ref.WeakReference
2225
import java.util.concurrent.atomic.AtomicInteger
@@ -166,6 +169,9 @@ open class AppStateManager @JvmOverloads constructor(
166169
interruptions
167170
)
168171
}
172+
CoroutineScope(Dispatchers.IO).launch {
173+
mConfigManager?.setPreviousAdId()
174+
}
169175
mLastForegroundTime = time
170176

171177
if (currentActivity != null) {
@@ -207,7 +213,6 @@ open class AppStateManager @JvmOverloads constructor(
207213
if (isBackgrounded()) {
208214
checkSessionTimeout()
209215
logBackgrounded()
210-
mConfigManager?.setPreviousAdId()
211216
}
212217
} catch (e: Exception) {
213218
e.printStackTrace()
@@ -421,7 +426,9 @@ open class AppStateManager @JvmOverloads constructor(
421426
internal class CheckAdIdRunnable(var configManager: ConfigManager?) : Runnable {
422427
override fun run() {
423428
val adIdInfo =
424-
MPUtility.getAdIdInfo(MParticle.getInstance()?.Internal()?.appStateManager?.mContext)
429+
MPUtility.getAdIdInfo(
430+
MParticle.getInstance()?.Internal()?.appStateManager?.mContext
431+
)
425432
val currentAdId =
426433
(if (adIdInfo == null) null else (if (adIdInfo.isLimitAdTrackingEnabled) null else adIdInfo.id))
427434
val previousAdId = configManager?.previousAdId

android-core/src/test/kotlin/com/mparticle/internal/AppStateManagerTest.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import com.mparticle.mock.MockApplication
1111
import com.mparticle.mock.MockContext
1212
import com.mparticle.mock.MockSharedPreferences
1313
import com.mparticle.testutils.AndroidUtils
14+
import kotlinx.coroutines.test.StandardTestDispatcher
15+
import kotlinx.coroutines.test.runTest
1416
import org.junit.Assert
1517
import org.junit.Before
1618
import org.junit.Test
@@ -132,7 +134,7 @@ class AppStateManagerTest {
132134
*/
133135
@Test
134136
@Throws(Exception::class)
135-
fun testSecondActivityStart() {
137+
fun testSecondActivityStart() = runTest(StandardTestDispatcher()) {
136138
manager.onActivityPaused(activity)
137139
Thread.sleep(1000)
138140
Assert.assertEquals(true, manager.isBackgrounded())

0 commit comments

Comments
 (0)