title: Android's AudioManager.getMode() seems unreliable. Same situation produces different results
body: <p>I am currently working on an audio player that is supposed to pause/stop when losing audio focus, for example when a call comes in.</p>
  
  <p>While I got this working fine by using an OnAudioFocusChangeListener I want to enhance this function to behave differently under different circumstances.</p>
  
  <p>As the application implements a timeout function in case the user has fallen asleep while listening I only want to resume playback when I can be positive that the user is still awake.</p>
  
  <p>I figured I could pretty much ignore notification sounds like those of an incoming email, as these usually are only short clips and my audio continuing to play will not be a nuisance to the user, as it would be during a phone call.
  Thus I want to limit pausing/stopping to situation where either a phone call comes in or the user makes a call.</p>
  
  <p>I have seen that AudioManager has some convenient states to signal this, but upon calling AudioManager.getMode() in my OnAudioFocusChangeListener I do not get consistent output.
  Sometimes an incoming call does generate MODE_RINGTONE, but often enough to render the function unreliable I get MODE_NORMAL.</p>
  
  <p>I could actually live with not knowing the mode when losing focus as long as I get the correct mode when re-gaining focus, but also this doesn't seem to work reliably.</p>
  
  <p>The only actual device I could test this behavior on is the Galaxy S2, running Android 2.3.6 (the application is API level 10 due to me using MediaMetadataRetriever, thus requires at least 2.3.3).
  I have tested the loss and re-gaining of audio focus in various emulated systems, and overall it seems to be working better there, but I also don't really get the same results each time.</p>
  
  <p>Maybe the problem is that I misunderstand what AudioManager.getMode() should return, maybe somebody could clarify this for me:    </p>
  
  <ol>
  <li>phone rings -> AudioManager.getMode() should return MODE_RINGTONE on focus loss</li>
  <li>user makes or picks up a call -> AudioManager.getMode() should return MODE_IN_CALL on focus loss</li>
  <li>doesn't pick up the call -> AudioManager.getMode() should return MODE_RINGTONE on focus gain</li>
  <li>user hangs up -> AudioManager.getMode() should return MODE_IN_CALL on focus gain</li>
  </ol>
  
  <p>Is that the way it is supposed to work? At least my tests suggest that this is the way it is supposed to be, but for some reason isn't all the time.</p>
  
  <p>Another way I am trying to solve this problem is by using a PhoneStateListener, but the problem I have with this is that this requires my application to request the permission to read the phone state. As I am sharing my application, currently through SourceForge, later possibly through the Android Market, I would like to avoid using this kind of permissions.</p>
  
  <p>So, coming to an end I would like to ask if the way I understand AudioManager.getMode() is correct and if there is any information on its reliability, as for me the results I get from it are kinda useless.</p>