Skip to content

Commit 42971ab

Browse files
authored
Update phone detection sample (#388)
1 parent 024bb4e commit 42971ab

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

Phone-Call-Detection/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# Phone Call Detection
22

3-
This application provides a Video Chat tutorial for Android. Upon deploying this sample application, you should be
4-
able to have two-way audio and video communication using OpenTok. You will be notified about incoming and outgoing
5-
phone calls (happening during the video call).
3+
This application provides a sample demonstrating phone calls detection.
4+
Upon deploying this sample application, you should be able to have two-way audio and video communication using OpenTok.
5+
Audio and video streams published from device to OpenTok session will be halted for the duration of ongoing phone call.
66

77
Main features:
88
* Create 2-way OpenTok video call
9-
* Detect incoming and outgoing native phone calls (while being on video call)
9+
* Detect incoming and outgoing native phone calls
10+
* Disable device video/video while being onCallStateChanged the phone call
1011

1112
# Configure the app
1213
Open the `OpenTokConfig` file and configure the `API_KEY`, `SESSION_ID`, and `TOKEN` variables. You can obtain these values from your [TokBox account](https://tokbox.com/account/#/).

Phone-Call-Detection/app/src/main/java/com/tokbox/sample/phonecalldetection/MainActivity.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ public void onError(PublisherKit publisherKit, OpentokError opentokError) {
5353
public void onConnected(Session session) {
5454
Log.d(TAG, "onConnected: Connected to session: " + session.getSessionId());
5555

56+
startVideoPublish(session);
57+
registerPhoneListener();
58+
}
59+
60+
private void startVideoPublish(Session session) {
5661
publisher = new Publisher.Builder(MainActivity.this).build();
5762
publisher.setPublisherListener(publisherListener);
5863
publisher.getRenderer().setStyle(BaseVideoRenderer.STYLE_VIDEO_SCALE, BaseVideoRenderer.STYLE_VIDEO_FILL);
@@ -66,6 +71,11 @@ public void onConnected(Session session) {
6671
session.publish(publisher);
6772
}
6873

74+
private void registerPhoneListener() {
75+
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
76+
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
77+
}
78+
6979
@Override
7080
public void onDisconnected(Session session) {
7181
Log.d(TAG, "onDisconnected: Disconnected from session: " + session.getSessionId());
@@ -125,7 +135,8 @@ public void onCallStateChanged(int state, String incomingNumber) {
125135

126136
switch (state) {
127137
case TelephonyManager.CALL_STATE_IDLE: //Initial state
128-
Log.d("onCallStateChanged", "CALL_STATE_IDLE");
138+
publisher.setPublishVideo(true);
139+
publisher.setPublishAudio(true);
129140
break;
130141

131142
case TelephonyManager.CALL_STATE_RINGING: // Incoming call Ringing
@@ -134,6 +145,8 @@ public void onCallStateChanged(int state, String incomingNumber) {
134145

135146
case TelephonyManager.CALL_STATE_OFFHOOK: // Outgoing Call | Accepted incoming call
136147
Log.d("onCallStateChanged", "CALL_STATE_OFFHOOK");
148+
publisher.setPublishVideo(false);
149+
publisher.setPublishAudio(false);
137150
break;
138151

139152
default:
@@ -152,9 +165,6 @@ protected void onCreate(Bundle savedInstanceState) {
152165
publisherViewContainer = findViewById(R.id.publisher_container);
153166
subscriberViewContainer = findViewById(R.id.subscriber_container);
154167

155-
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
156-
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
157-
158168
requestPermissions();
159169
}
160170

0 commit comments

Comments
 (0)