Correct HeadsetMediaButton behavior for external calls.

There was an issue where onExternalCallChanged callbacks into
HaedsetMediaButton would not actually change the active state the
MediaSession.  It turns out we were just calling onCallRemoved when a call
became external.  onCallRemoved would just ignore external calls and not
change the MediaSession active status.  Refactored the core logic of the
onCallAdded/onCallRemoved callbacks into separate methods and just call
these directly from onExternalCallChanged; this ensure we skip the
external call checks which were in onCallAdded/onCallRemoved.

Refactored HeadsetMediaButton code to enable abstracting out the
MediaSession for testing purposes and added a new unit test suite to
test the behavior of adding/removing/external calls.

Test: Wrote HeadsetMediaButtonTest unit test suite to verify the behavior
of media session activation/deactivation when calls are added, removed
and move between an external/non-external state.
Test: atest com.android.server.telecom.tests.HeadsetMediaButtonTest
Test: Performed manual call testing using a wired headset USB adapter;
verified that the headset media buttons can be used to mute/unmute, answer
and hangup a call.

Bug: 221492698
Change-Id: I77f6e3737b3246424e831686150cd37562c39107
3 files changed