Slide 1

Slide 1 text

How to detect phone call @magiepooh

Slide 2

Slide 2 text

About me @magiepooh @magie_pooh Takuma Fujita

Slide 3

Slide 3 text

Company AbemaTV, Inc. (Cyber Agent, Inc.)

Slide 4

Slide 4 text

How to detect phone call

Slide 5

Slide 5 text

Pattern 1: Use TelephonyManager

Slide 6

Slide 6 text

PhoneStateChangeListener l = new PhoneStateChangeListener(); TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVI CE); tm.listen(l, PhoneStateListener.LISTEN_CALL_STATE); public class MyListener extends PhoneStateListener { @Override public void onCallStateChanged(int state, String incomingNumber) { switch(state){ case TelephonyManager.CALL_STATE_RINGING: //… case TelephonyManager.CALL_STATE_OFFHOOK: //… case TelephonyManager.CALL_STATE_IDLE: //… } } } http://stackoverflow.com/questions/9684866/how-to-detect-when-phone-is-answered-or-rejected

Slide 7

Slide 7 text

PhoneStateChangeListener l = new PhoneStateChangeListener(); TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVI CE); tm.listen(l, PhoneStateListener.LISTEN_CALL_STATE); public class MyListener extends PhoneStateListener { @Override public void onCallStateChanged(int state, String incomingNumber) { switch(state){ case TelephonyManager.CALL_STATE_RINGING: //… case TelephonyManager.CALL_STATE_OFFHOOK: //… case TelephonyManager.CALL_STATE_IDLE: //… } } } http://stackoverflow.com/questions/9684866/how-to-detect-when-phone-is-answered-or-rejected

Slide 8

Slide 8 text

PhoneStateChangeListener l = new PhoneStateChangeListener(); TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVI CE); tm.listen(l, PhoneStateListener.LISTEN_CALL_STATE); public class MyListener extends PhoneStateListener { @Override public void onCallStateChanged(int state, String incomingNumber) { switch(state){ case TelephonyManager.CALL_STATE_RINGING: //… case TelephonyManager.CALL_STATE_OFFHOOK: //… case TelephonyManager.CALL_STATE_IDLE: //… } } } http://stackoverflow.com/questions/9684866/how-to-detect-when-phone-is-answered-or-rejected

Slide 9

Slide 9 text

PhoneStateChangeListener l = new PhoneStateChangeListener(); TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVI CE); tm.listen(l, PhoneStateListener.LISTEN_CALL_STATE); public class MyListener extends PhoneStateListener { @Override public void onCallStateChanged(int state, String incomingNumber) { switch(state){ case TelephonyManager.CALL_STATE_RINGING: //… case TelephonyManager.CALL_STATE_OFFHOOK: //… case TelephonyManager.CALL_STATE_IDLE: //… } } } http://stackoverflow.com/questions/9684866/how-to-detect-when-phone-is-answered-or-rejected

Slide 10

Slide 10 text

PhoneStateChangeListener l = new PhoneStateChangeListener(); TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVI CE); tm.listen(l, PhoneStateListener.LISTEN_CALL_STATE); public class MyListener extends PhoneStateListener { @Override public void onCallStateChanged(int state, String incomingNumber) { switch(state){ case TelephonyManager.CALL_STATE_RINGING: //… case TelephonyManager.CALL_STATE_OFFHOOK: //… case TelephonyManager.CALL_STATE_IDLE: //… } } } http://stackoverflow.com/questions/9684866/how-to-detect-when-phone-is-answered-or-rejected

Slide 11

Slide 11 text

PhoneStateChangeListener l = new PhoneStateChangeListener(); TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVI CE); tm.listen(l, PhoneStateListener.LISTEN_CALL_STATE); public class MyListener extends PhoneStateListener { @Override public void onCallStateChanged(int state, String incomingNumber) { switch(state){ case TelephonyManager.CALL_STATE_RINGING: //… case TelephonyManager.CALL_STATE_OFFHOOK: //… case TelephonyManager.CALL_STATE_IDLE: //… } } } http://stackoverflow.com/questions/9684866/how-to-detect-when-phone-is-answered-or-rejected

Slide 12

Slide 12 text

Need Permission…

Slide 13

Slide 13 text

Pattern 2: Use AudioManager

Slide 14

Slide 14 text

AudioManager. OnAudioFocusChangeListener • Interface definition for a callback to be invoked when the audio focus of the system is updated • Added in API level 8

Slide 15

Slide 15 text

public class MyAudioFocusMonitor implements AudioManager.OnAudioFocusChangeListener { private final AudioManager audioManager; public MyAudioFocusMonitor(Context context) { audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); } public void start() { audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); } public void stop() { audioManager.abandonAudioFocus(this); } }

Slide 16

Slide 16 text

public class MyAudioFocusMonitor implements AudioManager.OnAudioFocusChangeListener { private final AudioManager audioManager; public MyAudioFocusMonitor(Context context) { audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); } public void start() { audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); } public void stop() { audioManager.abandonAudioFocus(this); } }

Slide 17

Slide 17 text

public class MyAudioFocusMonitor implements AudioManager.OnAudioFocusChangeListener { private final AudioManager audioManager; public MyAudioFocusMonitor(Context context) { audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); } public void start() { audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, // Request permanent focus. AudioManager.AUDIOFOCUS_GAIN); } public void stop() { audioManager.abandonAudioFocus(this); } }

Slide 18

Slide 18 text

public class MyAudioFocusMonitor implements AudioManager.OnAudioFocusChangeListener { private final AudioManager audioManager; public MyAudioFocusMonitor(Context context) { audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); } public void start() { audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, // Request permanent focus. AudioManager.AUDIOFOCUS_GAIN); } public void stop() { audioManager.abandonAudioFocus(this); } }

Slide 19

Slide 19 text

Audio Focus • AUDIOFOCUS_GAIN • unknown duration • AUDIOFOCUS_GAIN_TRANSIENT • a short amount of time • AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE • ≒AUDIOFOCUS_GAIN_TRANSIENT • a short amount of time, during which no other applications, or system components, should play anything. • AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK • ≒AUDIOFOCUS_GAIN_TRANSIENT • other audio applications to keep playing after having lowered their output level

Slide 20

Slide 20 text

@Override public void onAudioFocusChange(int focusChange) { switch (focusChange) { case AudioManager.AUDIOFOCUS_REQUEST_GRANTED: // play break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: // pause break; case AudioManager.AUDIOFOCUS_LOSS: // stop audioManager.abandonAudioFocus(this); break; default: break; } }

Slide 21

Slide 21 text

@Override public void onAudioFocusChange(int focusChange) { switch (focusChange) { case AudioManager.AUDIOFOCUS_REQUEST_GRANTED: // … (play break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: // … (pause break; case AudioManager.AUDIOFOCUS_LOSS: // … (stop audioManager.abandonAudioFocus(this); break; } }

Slide 22

Slide 22 text

@Override public void onAudioFocusChange(int focusChange) { switch (focusChange) { case AudioManager. AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: // Lower the volume case AudioManager. AUDIOFOCUS_GAIN: // Raise it back to normal } }

Slide 23

Slide 23 text

Conclusion • TelephonyManager needs permission • AudioManager do not need permission • but status is complicated

Slide 24

Slide 24 text

(๑•̀ŷ•́)و✧