snowplow / snowplow-android-tracker

Snowplow event tracker for Android. Add analytics to your Android apps and games
http://snowplowanalytics.com
Apache License 2.0
107 stars 63 forks source link

Add Subject config flag to avoid WindowManager bug #683

Closed mscwilson closed 2 months ago

mscwilson commented 3 months ago

For issue #657.

This bug occurs during tracker initialisation when a) an Android application context from a dependency injection framework is passed in, and b) the Android StrictMode rule detectIncorrectContextUse() is set for the app.

That rule is violated when creating Subject, in Subject.setDefaultScreenResolution(), because it uses a deprecated method, via WindowManager, to fetch the screen size. The resolution set there in Subject is added to the payload as the "res" property, which eventually becomes the "dvce_screenwidth/height" columns.

The current preferred way is DisplayManager or from the context.resources. We already use the context Resources to get the screen resolution for the mobile/platform context entity.

Unfortunately, WindowManager gives a different answer from DisplayManager/Resources. The old way includes the menu bar at the top of the screen but that's not included anymore. Changing how Subject fetches the size is therefore a breaking change.

This PR adds a new flag to SubjectConfiguration. When set to true (false is default), the default Subject screen size is obtained from the context Resources and WindowManager is not accessed - meaning no StrictMode violation.

Currently, the flag is set up just like the other Subject properties. This doesn't seem necessary since it's only used in Subject initialisation. SubjectController doesn't really need it, but it inherits from SubjectConfigurationInterface. Anyway, it's Friday evening and this fix works.