With landscape view enabled (e.g. e06b2c7) in the app and auto
rotation enabled on the phone, switching between portrait and
landscape orientations leaks instances of MainActivity. This prevents
garbage collection of not just the MainActivity object, but fragments
and other objects referenced by the Activity.
This is caused by repositories, the AppContext instance, the player
service, and authentication code maintaining a reference to the
context which with they are initialized. So rather than initialize
these with an Activity context, pass them the Application context.
Activities are torn down and rebuilt on screen rotation. The
Application context is not.
To enable instantiation of the FavoritedRepository with the
Application context, delay that repository’s initialization until
first use. This ensures the Application context is fully initialized.
It is not fully initialized until the MainActivity has been fully
initialized.
This is part of an effort to resolve deprecation warnings.
Most of this is simple refactoring of interfaces that change between
the two Player implementations. There are a few other changes that
deserve further explanation.
Testing indicated that the play/pause button was being reset to pause
in MainActivity:refreshCurrentTrack. In the past this was likely
masked by the ordering of other callbacks. We have removed the
nowPlayingToggle.icon update from MainActivity, leaving that UI update
to PlayerService.
One of the bigger refactorings in PlayerService was forced by the
deprecation of Player.EventListener.onPlayerStateChanged. That forced
separation of handling playWhenReady and playbackState transitions.
In the SimpleExoPlayer implementations, where these transitions were
combined, the module attempted to work out playing state from a
combination of these two state variables.
In addition to separating the reaction to these state changes, we have
added a listener to onIsPlayingChanged, eliminating the need for some
of the earlier logic in Player.EventListener.onPlayerStateChanged.
This addition, along with the separation of state transition
processing, seems to provide a simpler implementation. But it is,
certainly, a possible source of bugs.