Skip to content

Commit 84c7f55

Browse files
committed
Fixes #68: Add workaround for incoming media, so that the MS announcements aren't truncated. Fixes #64: Crash when reopening the App if it was closed with 'back' button. Fixes #69: Add Audio Focus in the activity as well
1 parent 16f27cf commit 84c7f55

File tree

6 files changed

+189
-92
lines changed

6 files changed

+189
-92
lines changed

Examples/restcomm-messenger/app/src/main/java/com/telestax/restcomm_messenger/MainActivity.java

Lines changed: 90 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
//import android.support.v7.app.ActionBarActivity;
44
import android.app.Activity;
55
import android.app.AlertDialog;
6+
import android.content.Context;
67
import android.content.DialogInterface;
78
import android.content.Intent;
89
import android.content.SharedPreferences;
910
import android.content.res.Configuration;
11+
import android.media.AudioManager;
1012
import android.media.MediaPlayer;
1113
import android.os.Bundle;
1214
import android.preference.PreferenceManager;
@@ -29,12 +31,13 @@
2931
import org.mobicents.restcomm.android.client.sdk.RCDeviceListener;
3032
import org.mobicents.restcomm.android.client.sdk.RCPresenceEvent;
3133

34+
import java.util.ArrayList;
3235
import java.util.HashMap;
3336

3437

3538
public class MainActivity extends Activity implements RCDeviceListener, RCConnectionListener,
3639
OnClickListener, SharedPreferences.OnSharedPreferenceChangeListener, OnCheckedChangeListener,
37-
MediaPlayer.OnPreparedListener {
40+
MediaPlayer.OnPreparedListener, AudioManager.OnAudioFocusChangeListener {
3841

3942
SharedPreferences prefs;
4043
private RCDevice device;
@@ -44,6 +47,7 @@ public class MainActivity extends Activity implements RCDeviceListener, RCConnec
4447
MediaPlayer ringingPlayer;
4548
MediaPlayer callingPlayer;
4649
MediaPlayer messagePlayer;
50+
AudioManager audioManager;
4751

4852
// UI elements
4953
Button btnRegister;
@@ -115,14 +119,21 @@ public void onError(Exception exception)
115119

116120
cbMuted.setEnabled(false);
117121

122+
// volume control should be by default 'music' which will control the ringing sounds and 'voice call' when within a call
123+
setVolumeControlStream(AudioManager.STREAM_MUSIC);
118124
// Setup Media (notice that I'm not preparing the media as create does that implicitly plus
119125
// I'm not ever stopping a player -instead I'm pausing so no additional preparation is needed
120126
// there either. We might need to revisit this at some point though
121127
ringingPlayer = MediaPlayer.create(getApplicationContext(), R.raw.ringing);
128+
ringingPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
122129
ringingPlayer.setLooping(true);
123130
callingPlayer = MediaPlayer.create(getApplicationContext(), R.raw.calling);
131+
callingPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
124132
callingPlayer.setLooping(true);
125133
messagePlayer = MediaPlayer.create(getApplicationContext(), R.raw.message);
134+
messagePlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
135+
136+
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
126137
}
127138

128139
@Override
@@ -168,12 +179,16 @@ public void onClick(View view) {
168179
pendingConnection.accept();
169180
connection = this.pendingConnection;
170181
ringingPlayer.pause();
182+
// Abandon audio focus when playback complete
183+
audioManager.abandonAudioFocus(this);
171184
}
172185
} else if (view.getId() == R.id.button_decline) {
173186
if (pendingConnection != null) {
174187
pendingConnection.reject();
175188
pendingConnection = null;
176189
ringingPlayer.pause();
190+
// Abandon audio focus when playback complete
191+
audioManager.abandonAudioFocus(this);
177192
}
178193
} else if (view.getId() == R.id.button_cancel) {
179194
if (connection == null) {
@@ -184,6 +199,8 @@ public void onClick(View view) {
184199
connection = null;
185200
pendingConnection = null;
186201
callingPlayer.pause();
202+
// Abandon audio focus when playback complete
203+
audioManager.abandonAudioFocus(this);
187204
}
188205
} else if (view.getId() == R.id.button_send) {
189206
HashMap<String, String> sendParams = new HashMap<String, String>();
@@ -197,7 +214,10 @@ public void onClick(View view) {
197214
*/
198215
txtWall.append("Me: " + txtMessage.getText().toString() + "\n");
199216

200-
messagePlayer.start();
217+
int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
218+
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
219+
messagePlayer.start();
220+
}
201221
}
202222
}
203223
}
@@ -248,37 +268,48 @@ public void onPresenceChanged(RCDevice device, RCPresenceEvent presenceEvent)
248268
public void onIncomingConnection(RCDevice device, RCConnection connection)
249269
{
250270
Log.i(TAG, "Connection arrived");
251-
ringingPlayer.start();
271+
int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
272+
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
273+
ringingPlayer.start();
274+
}
252275
pendingConnection = connection;
253276
}
254277

255278
public void onIncomingMessage(RCDevice device, String message, HashMap<String, String> parameters)
256279
{
257280
Log.i(TAG, "Message arrived: " + message);
281+
int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
282+
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
283+
messagePlayer.start();
284+
}
258285
/* put new text on top
259286
String text = txtWall.getText().toString();
260287
String newText = parameters.get("username") + ": " + message + "\n" + text;
261288
txtWall.setText(newText, TextView.BufferType.EDITABLE);
262289
*/
263290
// put new text on the bottom
264291
txtWall.append(parameters.get("username") + ": " + message + "\n");
265-
266-
messagePlayer.start();
267292
}
268293

269294
// RCConnection Listeners
270295
public void onConnecting(RCConnection connection)
271296
{
272297
Log.i(TAG, "RCConnection connecting");
273-
callingPlayer.start();
298+
int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
299+
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
300+
callingPlayer.start();
301+
}
274302
}
275303

276304
public void onConnected(RCConnection connection) {
277305
Log.i(TAG, "RCConnection connected");
278306
cbMuted.setEnabled(true);
279307
if (!connection.isIncoming()) {
280308
callingPlayer.pause();
309+
// Abandon audio focus when playback complete
310+
audioManager.abandonAudioFocus(this);
281311
}
312+
setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
282313
}
283314

284315
public void onDisconnected(RCConnection connection) {
@@ -287,6 +318,7 @@ public void onDisconnected(RCConnection connection) {
287318

288319
this.connection = null;
289320
pendingConnection = null;
321+
setVolumeControlStream(AudioManager.STREAM_MUSIC);
290322
}
291323

292324
public void onCancelled(RCConnection connection) {
@@ -297,6 +329,8 @@ public void onCancelled(RCConnection connection) {
297329
else {
298330
callingPlayer.pause();
299331
}
332+
// Abandon audio focus when playback complete
333+
audioManager.abandonAudioFocus(this);
300334

301335
this.connection = null;
302336
pendingConnection = null;
@@ -305,6 +339,9 @@ public void onCancelled(RCConnection connection) {
305339
public void onDeclined(RCConnection connection) {
306340
Log.i(TAG, "RCConnection declined");
307341
callingPlayer.pause();
342+
// Abandon audio focus when playback complete
343+
audioManager.abandonAudioFocus(this);
344+
308345

309346
this.connection = null;
310347
pendingConnection = null;
@@ -395,10 +432,10 @@ private void showOkAlert(final String title, final String detail) {
395432
alertDialog.setTitle(title);
396433
alertDialog.setMessage(detail);
397434
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
398-
public void onClick(DialogInterface dialog, int which) {
399-
dialog.dismiss();
400-
}
401-
});
435+
public void onClick(DialogInterface dialog, int which) {
436+
dialog.dismiss();
437+
}
438+
});
402439
alertDialog.show();
403440
}
404441

@@ -414,6 +451,26 @@ protected void onResume() {
414451
super.onResume();
415452
// The activity has become visible (it is now "resumed").
416453
Log.i(TAG, "%% onResume");
454+
Intent intent = getIntent();
455+
// If reason for resume is that we got an intent designating either an incoming call or message
456+
if (intent.getAction() == "ACTION_INCOMING_CALL") {
457+
ArrayList<RCDevice> list = RCClient.getInstance().listDevices();
458+
if (list.size() != 0) {
459+
RCDevice device = list.get(0);
460+
RCConnection pendingConnection = device.getPendingConnection();
461+
onIncomingConnection(device, pendingConnection);
462+
}
463+
}
464+
if (intent.getAction() == "ACTION_INCOMING_MESSAGE") {
465+
ArrayList<RCDevice> list = RCClient.getInstance().listDevices();
466+
if (list.size() != 0) {
467+
RCDevice device = list.get(0);
468+
RCConnection pendingConnection = device.getPendingConnection();
469+
HashMap<String, String> parms = (HashMap)intent.getSerializableExtra("MESSAGE_PARMS");
470+
String message = (String)intent.getSerializableExtra("MESSAGE");
471+
onIncomingMessage(device, message, parms);
472+
}
473+
}
417474
}
418475
@Override
419476
protected void onPause() {
@@ -433,4 +490,27 @@ protected void onDestroy() {
433490
// The activity is about to be destroyed.
434491
Log.i(TAG, "%% onDestroy");
435492
}
493+
494+
// Callbacks for auio focus change events
495+
public void onAudioFocusChange(int focusChange)
496+
{
497+
Log.i(TAG, "onAudioFocusChange: " + focusChange);
498+
/*
499+
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
500+
// Pause playback
501+
}
502+
else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
503+
// Resume playback or raise it back to normal if we were ducked
504+
}
505+
else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
506+
//am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
507+
audio.abandonAudioFocus(this);
508+
// Stop playback
509+
}
510+
else if (focusChange == AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
511+
// Lower the volume
512+
}
513+
*/
514+
}
515+
436516
}

restcomm.android.client.sdk/src/main/java/org/mobicents/restcomm/android/client/sdk/RCClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public static RCDevice createDevice(String capabilityToken, RCDeviceListener dev
136136
* Retrieve a list of active Devices
137137
* @return List of Devices
138138
*/
139-
public List<RCDevice> listDevices()
139+
public ArrayList<RCDevice> listDevices()
140140
{
141141
//ArrayList<RCDevice> list = new ArrayList<RCDevice>();
142142
return list;

restcomm.android.client.sdk/src/main/java/org/mobicents/restcomm/android/client/sdk/RCConnection.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
* Once an RCConnection (either incoming or outgoing) is established (i.e. RCConnectionStateConnected) media can start flowing over it. DTMF digits can be sent over to
5151
* the remote party using RCConnection.sendDigits() (<b>Not implemented yet</b>). When done with the RCConnection you can disconnect it with RCConnection.disconnect().
5252
*/
53-
public class RCConnection implements SipUAConnectionListener, Parcelable {
53+
public class RCConnection implements SipUAConnectionListener {
5454
/**
5555
* Connection State
5656
*/
@@ -329,6 +329,8 @@ private boolean haveConnectivity()
329329
}
330330
}
331331

332+
// Parcelable stuff (not needed for now -let's keep around in case we use it at some point):
333+
/*
332334
@Override
333335
public int describeContents() {
334336
return 0;
@@ -358,4 +360,5 @@ private RCConnection(Parcel in) {
358360
in.readBooleanArray(one);
359361
incoming = one[0];
360362
}
363+
*/
361364
}

0 commit comments

Comments
 (0)