Skip to content

Commit

Permalink
Fixes #68: Add workaround for incoming media, so that the MS announce…
Browse files Browse the repository at this point in the history
…ments 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
  • Loading branch information
atsakiridis committed Jul 6, 2015
1 parent 16f27cf commit 84c7f55
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
//import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.preference.PreferenceManager;
Expand All @@ -29,12 +31,13 @@
import org.mobicents.restcomm.android.client.sdk.RCDeviceListener;
import org.mobicents.restcomm.android.client.sdk.RCPresenceEvent;

import java.util.ArrayList;
import java.util.HashMap;


public class MainActivity extends Activity implements RCDeviceListener, RCConnectionListener,
OnClickListener, SharedPreferences.OnSharedPreferenceChangeListener, OnCheckedChangeListener,
MediaPlayer.OnPreparedListener {
MediaPlayer.OnPreparedListener, AudioManager.OnAudioFocusChangeListener {

SharedPreferences prefs;
private RCDevice device;
Expand All @@ -44,6 +47,7 @@ public class MainActivity extends Activity implements RCDeviceListener, RCConnec
MediaPlayer ringingPlayer;
MediaPlayer callingPlayer;
MediaPlayer messagePlayer;
AudioManager audioManager;

// UI elements
Button btnRegister;
Expand Down Expand Up @@ -115,14 +119,21 @@ public void onError(Exception exception)

cbMuted.setEnabled(false);

// volume control should be by default 'music' which will control the ringing sounds and 'voice call' when within a call
setVolumeControlStream(AudioManager.STREAM_MUSIC);
// Setup Media (notice that I'm not preparing the media as create does that implicitly plus
// I'm not ever stopping a player -instead I'm pausing so no additional preparation is needed
// there either. We might need to revisit this at some point though
ringingPlayer = MediaPlayer.create(getApplicationContext(), R.raw.ringing);
ringingPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
ringingPlayer.setLooping(true);
callingPlayer = MediaPlayer.create(getApplicationContext(), R.raw.calling);
callingPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
callingPlayer.setLooping(true);
messagePlayer = MediaPlayer.create(getApplicationContext(), R.raw.message);
messagePlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
}

@Override
Expand Down Expand Up @@ -168,12 +179,16 @@ public void onClick(View view) {
pendingConnection.accept();
connection = this.pendingConnection;
ringingPlayer.pause();
// Abandon audio focus when playback complete
audioManager.abandonAudioFocus(this);
}
} else if (view.getId() == R.id.button_decline) {
if (pendingConnection != null) {
pendingConnection.reject();
pendingConnection = null;
ringingPlayer.pause();
// Abandon audio focus when playback complete
audioManager.abandonAudioFocus(this);
}
} else if (view.getId() == R.id.button_cancel) {
if (connection == null) {
Expand All @@ -184,6 +199,8 @@ public void onClick(View view) {
connection = null;
pendingConnection = null;
callingPlayer.pause();
// Abandon audio focus when playback complete
audioManager.abandonAudioFocus(this);
}
} else if (view.getId() == R.id.button_send) {
HashMap<String, String> sendParams = new HashMap<String, String>();
Expand All @@ -197,7 +214,10 @@ public void onClick(View view) {
*/
txtWall.append("Me: " + txtMessage.getText().toString() + "\n");

messagePlayer.start();
int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
messagePlayer.start();
}
}
}
}
Expand Down Expand Up @@ -248,37 +268,48 @@ public void onPresenceChanged(RCDevice device, RCPresenceEvent presenceEvent)
public void onIncomingConnection(RCDevice device, RCConnection connection)
{
Log.i(TAG, "Connection arrived");
ringingPlayer.start();
int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
ringingPlayer.start();
}
pendingConnection = connection;
}

public void onIncomingMessage(RCDevice device, String message, HashMap<String, String> parameters)
{
Log.i(TAG, "Message arrived: " + message);
int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
messagePlayer.start();
}
/* put new text on top
String text = txtWall.getText().toString();
String newText = parameters.get("username") + ": " + message + "\n" + text;
txtWall.setText(newText, TextView.BufferType.EDITABLE);
*/
// put new text on the bottom
txtWall.append(parameters.get("username") + ": " + message + "\n");

messagePlayer.start();
}

// RCConnection Listeners
public void onConnecting(RCConnection connection)
{
Log.i(TAG, "RCConnection connecting");
callingPlayer.start();
int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
callingPlayer.start();
}
}

public void onConnected(RCConnection connection) {
Log.i(TAG, "RCConnection connected");
cbMuted.setEnabled(true);
if (!connection.isIncoming()) {
callingPlayer.pause();
// Abandon audio focus when playback complete
audioManager.abandonAudioFocus(this);
}
setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
}

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

this.connection = null;
pendingConnection = null;
setVolumeControlStream(AudioManager.STREAM_MUSIC);
}

public void onCancelled(RCConnection connection) {
Expand All @@ -297,6 +329,8 @@ public void onCancelled(RCConnection connection) {
else {
callingPlayer.pause();
}
// Abandon audio focus when playback complete
audioManager.abandonAudioFocus(this);

this.connection = null;
pendingConnection = null;
Expand All @@ -305,6 +339,9 @@ public void onCancelled(RCConnection connection) {
public void onDeclined(RCConnection connection) {
Log.i(TAG, "RCConnection declined");
callingPlayer.pause();
// Abandon audio focus when playback complete
audioManager.abandonAudioFocus(this);


this.connection = null;
pendingConnection = null;
Expand Down Expand Up @@ -395,10 +432,10 @@ private void showOkAlert(final String title, final String detail) {
alertDialog.setTitle(title);
alertDialog.setMessage(detail);
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}

Expand All @@ -414,6 +451,26 @@ protected void onResume() {
super.onResume();
// The activity has become visible (it is now "resumed").
Log.i(TAG, "%% onResume");
Intent intent = getIntent();
// If reason for resume is that we got an intent designating either an incoming call or message
if (intent.getAction() == "ACTION_INCOMING_CALL") {
ArrayList<RCDevice> list = RCClient.getInstance().listDevices();
if (list.size() != 0) {
RCDevice device = list.get(0);
RCConnection pendingConnection = device.getPendingConnection();
onIncomingConnection(device, pendingConnection);
}
}
if (intent.getAction() == "ACTION_INCOMING_MESSAGE") {
ArrayList<RCDevice> list = RCClient.getInstance().listDevices();
if (list.size() != 0) {
RCDevice device = list.get(0);
RCConnection pendingConnection = device.getPendingConnection();
HashMap<String, String> parms = (HashMap)intent.getSerializableExtra("MESSAGE_PARMS");
String message = (String)intent.getSerializableExtra("MESSAGE");
onIncomingMessage(device, message, parms);
}
}
}
@Override
protected void onPause() {
Expand All @@ -433,4 +490,27 @@ protected void onDestroy() {
// The activity is about to be destroyed.
Log.i(TAG, "%% onDestroy");
}

// Callbacks for auio focus change events
public void onAudioFocusChange(int focusChange)
{
Log.i(TAG, "onAudioFocusChange: " + focusChange);
/*
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
// Pause playback
}
else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
// Resume playback or raise it back to normal if we were ducked
}
else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
//am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
audio.abandonAudioFocus(this);
// Stop playback
}
else if (focusChange == AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
// Lower the volume
}
*/
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public static RCDevice createDevice(String capabilityToken, RCDeviceListener dev
* Retrieve a list of active Devices
* @return List of Devices
*/
public List<RCDevice> listDevices()
public ArrayList<RCDevice> listDevices()
{
//ArrayList<RCDevice> list = new ArrayList<RCDevice>();
return list;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
* 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
* the remote party using RCConnection.sendDigits() (<b>Not implemented yet</b>). When done with the RCConnection you can disconnect it with RCConnection.disconnect().
*/
public class RCConnection implements SipUAConnectionListener, Parcelable {
public class RCConnection implements SipUAConnectionListener {
/**
* Connection State
*/
Expand Down Expand Up @@ -329,6 +329,8 @@ private boolean haveConnectivity()
}
}

// Parcelable stuff (not needed for now -let's keep around in case we use it at some point):
/*
@Override
public int describeContents() {
return 0;
Expand Down Expand Up @@ -358,4 +360,5 @@ private RCConnection(Parcel in) {
in.readBooleanArray(one);
incoming = one[0];
}
*/
}
Loading

0 comments on commit 84c7f55

Please sign in to comment.