Skip to content

Commit

Permalink
feat: add emulator detection to static context
Browse files Browse the repository at this point in the history
- Implemented `isEmulator` method to detect if the app is running on an emulator.
- Integrated emulator detection into the static context cache.
- Added assertions to verify emulator detection in static context.
- Ensured static context includes the new `\$is_emulator` property.
  • Loading branch information
thisames committed Aug 7, 2024
1 parent 600edc7 commit 1ae5e34
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ internal class PostHogAndroidContext(
staticContext["\$lib"] = config.sdkName
staticContext["\$lib_version"] = config.sdkVersion

staticContext["\$is_emulator"] = isEmulator

staticContext
}

Expand Down Expand Up @@ -170,4 +172,20 @@ internal class PostHogAndroidContext(

return dynamicContext
}

private val isEmulator: Boolean
get() = ((Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"))
|| Build.FINGERPRINT.startsWith("generic")
|| Build.FINGERPRINT.startsWith("unknown")
|| Build.HARDWARE.contains("goldfish")
|| Build.HARDWARE.contains("ranchu")
|| Build.MODEL.contains("google_sdk")
|| Build.MODEL.contains("Emulator")
|| Build.MODEL.contains("Android SDK built for x86")
|| Build.MANUFACTURER.contains("Genymotion")
|| Build.PRODUCT.contains("sdk")
|| Build.PRODUCT.contains("vbox86p")
|| Build.PRODUCT.contains("emulator")
|| Build.PRODUCT.contains("simulator"))

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ internal class PostHogAndroidContextTest {

assertEquals(config.sdkName, staticContext["\$lib"])
assertEquals(config.sdkVersion, staticContext["\$lib_version"])

assertNotNull(staticContext["\$is_emulator"])
}

@Test
Expand Down

0 comments on commit 1ae5e34

Please sign in to comment.