Skip to content

Commit 8f66921

Browse files
author
Antonis Tsakiridis
committed
Fixed #362: Implement first step of backgrounding: Fixed the issue with the call sound, Fixed the issue for properly hanging up while in the background
1 parent aed0f8e commit 8f66921

File tree

11 files changed

+340
-260
lines changed

11 files changed

+340
-260
lines changed

Examples/restcomm-olympus/.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Examples/restcomm-olympus/app/app.iml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,6 @@
6464
<sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" />
6565
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
6666
<sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
67-
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
68-
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
69-
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
70-
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
71-
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
72-
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
73-
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
74-
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
7567
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
7668
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
7769
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
@@ -80,6 +72,14 @@
8072
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
8173
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
8274
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
75+
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
76+
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
77+
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
78+
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
79+
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
80+
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
81+
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
82+
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
8383
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
8484
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
8585
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/builds" />
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
#Thu Sep 15 14:35:06 EEST 2016
2-
VERSION_CODE=1131
1+
#Fri Sep 16 13:19:58 EEST 2016
2+
VERSION_CODE=1211

Examples/restcomm-olympus/app/src/main/java/org/restcomm/android/olympus/CallActivity.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,17 @@ protected void onStart()
175175
// User requested to disconnect via foreground service notification. At this point the service has already
176176
// disconnected the call, so let's close the call activity
177177
if (getIntent().getAction().equals(RCDevice.ACTION_CALL_DISCONNECT)) {
178-
finish();
178+
Intent intent = new Intent(this, MainActivity.class);
179+
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
180+
intent.setAction(MainActivity.ACTION_DISCONNECTED_BACKGROUND);
181+
startActivity(intent);
179182
}
183+
else {
180184

181-
activityVisible = true;
185+
activityVisible = true;
182186

183-
bindService(new Intent(this, RCDevice.class), this, Context.BIND_AUTO_CREATE);
187+
bindService(new Intent(this, RCDevice.class), this, Context.BIND_AUTO_CREATE);
188+
}
184189
}
185190

186191
@Override

Examples/restcomm-olympus/app/src/main/java/org/restcomm/android/olympus/MainActivity.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public class MainActivity extends AppCompatActivity
7373
private RCConnectivityStatus previousConnectivityStatus = RCConnectivityStatus.RCConnectivityStatusNone;
7474
private static final String APP_VERSION = "Restcomm Android Olympus Client " + BuildConfig.VERSION_NAME + "#" + BuildConfig.VERSION_CODE; //"Restcomm Android Olympus Client 1.0.0-BETA4#20";
7575
FloatingActionButton btnAdd;
76+
public static String ACTION_DISCONNECTED_BACKGROUND = "org.restcomm.android.olympus.ACTION_DISCONNECTED_BACKGROUND";
7677

7778
private static final int CONNECTION_REQUEST = 1;
7879

@@ -174,7 +175,12 @@ protected void onDestroy()
174175
public void onNewIntent(Intent intent)
175176
{
176177
super.onNewIntent(intent);
177-
setIntent(intent);
178+
179+
// We get this intent from CallActivity, when the App is in the background and the user has requested hangup via notification
180+
// In that case we don't wont to interrupt the user from what they are currently doing in the foreground, so we just finish()
181+
if (intent.getAction().equals(ACTION_DISCONNECTED_BACKGROUND)) {
182+
finish();
183+
}
178184
}
179185

180186
// Callbacks for service binding, passed to bindService()
@@ -196,7 +202,7 @@ public void onServiceConnected(ComponentName className, IBinder service)
196202
params.put(RCDevice.ParameterKeys.MEDIA_ICE_USERNAME, prefs.getString(RCDevice.ParameterKeys.MEDIA_ICE_USERNAME, ""));
197203
params.put(RCDevice.ParameterKeys.MEDIA_ICE_PASSWORD, prefs.getString(RCDevice.ParameterKeys.MEDIA_ICE_PASSWORD, ""));
198204
params.put(RCDevice.ParameterKeys.MEDIA_TURN_ENABLED, prefs.getBoolean(RCDevice.ParameterKeys.MEDIA_TURN_ENABLED, true));
199-
params.put(RCDevice.ParameterKeys.SIGNALING_SECURE_ENABLED, prefs.getBoolean(RCDevice.ParameterKeys.SIGNALING_SECURE_ENABLED, false));
205+
params.put(RCDevice.ParameterKeys.SIGNALING_SECURE_ENABLED, prefs.getBoolean(RCDevice.ParameterKeys.SIGNALING_SECURE_ENABLED, true));
200206
// The SDK provides the user with default sounds for calling, ringing, busy (declined) and message, but the user can override them
201207
// by providing their own resource files (i.e. .wav, .mp3, etc) at res/raw passing them with Resource IDs like R.raw.user_provided_calling_sound
202208
//params.put(RCDevice.ParameterKeys.RESOURCE_SOUND_CALLING, R.raw.user_provided_calling_sound);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
#Thu Sep 15 14:35:06 EEST 2016
2-
VERSION_CODE=1142
1+
#Fri Sep 16 14:06:30 EEST 2016
2+
VERSION_CODE=1227

restcomm.android.sdk/restcomm.android.sdk.iml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<module external.linked.project.id=":restcomm.android.sdk" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/../Examples/restcomm-olympus" external.system.id="GRADLE" external.system.module.group="restcomm-olympus" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
2+
<module external.linked.project.id=":restcomm.android.sdk" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/../Examples/restcomm-helloworld" external.system.id="GRADLE" external.system.module.group="restcomm-helloworld" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
33
<component name="FacetManager">
44
<facet type="android-gradle" name="Android-Gradle">
55
<configuration>
@@ -64,14 +64,6 @@
6464
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
6565
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
6666
<sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
67-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
68-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
69-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
70-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
71-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
72-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
73-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
74-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
7567
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
7668
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
7769
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
@@ -80,6 +72,14 @@
8072
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
8173
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
8274
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
75+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
76+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
77+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
78+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
79+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
80+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
81+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
82+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
8383
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/annotations" />
8484
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
8585
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />

restcomm.android.sdk/src/main/java/org/restcomm/android/sdk/MediaClient/AppRTCAudioManager.java

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
/**
7171
* AppRTCAudioManager manages all audio related parts of the AppRTC demo.
7272
*/
73-
public class AppRTCAudioManager {
73+
public class AppRTCAudioManager implements AudioManager.OnAudioFocusChangeListener {
7474
private static final String TAG = "AppRTCAudioManager";
7575

7676
/**
@@ -210,7 +210,7 @@ public void close()
210210
initialized = false;
211211
}
212212

213-
public void startCall()
213+
public void startCallMedia()
214214
{
215215
RCLogger.d(TAG, "startCall");
216216
if (callAudioInitialized) {
@@ -237,7 +237,7 @@ public void run()
237237

238238
// Request audio focus before making any device switch.
239239
audioManager.requestAudioFocus(null, AudioManager.STREAM_VOICE_CALL,
240-
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
240+
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
241241

242242
// Start by setting MODE_IN_COMMUNICATION as default audio mode. It is
243243
// required to be in this mode when playout and/or recording starts for
@@ -260,6 +260,16 @@ public void run()
260260
callAudioInitialized = true;
261261
}
262262

263+
public void requestFocus()
264+
{
265+
266+
}
267+
268+
public void abandonFocus()
269+
{
270+
271+
}
272+
263273
public void endCall()
264274
{
265275
RCLogger.d(TAG, "endCall");
@@ -575,17 +585,29 @@ public void playMessageSound()
575585

576586
public void play(int resid, boolean loop)
577587
{
578-
// Request audio focus before making any device switch.
579-
audioManager.requestAudioFocus(null, AudioManager.STREAM_VOICE_CALL,
580-
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
581-
582588
mediaPlayerWrapper.play(resid, loop);
583589
}
584590

585591
public void stop()
586592
{
587593
mediaPlayerWrapper.stop();
594+
}
588595

589-
audioManager.abandonAudioFocus(null);
596+
public void onAudioFocusChange(int focusChange)
597+
{
598+
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
599+
// Pause playback
600+
RCLogger.i(TAG, "onAudioFocusChange(): AUDIOFOCUS_LOSS_TRANSIENT");
601+
}
602+
else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
603+
// Resume playback
604+
RCLogger.i(TAG, "onAudioFocusChange(): AUDIOFOCUS_GAIN");
605+
}
606+
else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
607+
// Stop playback
608+
//am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
609+
//am.abandonAudioFocus(afChangeListener);
610+
RCLogger.i(TAG, "onAudioFocusChange(): AUDIOFOCUS_GAIN");
611+
}
590612
}
591613
}

restcomm.android.sdk/src/main/java/org/restcomm/android/sdk/MediaClient/MediaPlayerWrapper.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import android.media.AudioManager;
55
import android.media.MediaPlayer;
66

7+
import org.restcomm.android.sdk.util.RCLogger;
8+
79
/*
810
* Notice that I'm doing everything in main thread right now, which is bad practice, but since all audio files are so small it doesn't
911
* seem to cause any issues and I would like to avoid spending the time to make all this work properly asynchronously.
@@ -18,9 +20,10 @@
1820
* - 'PhoneRinging.mp3' by 'acclivity' ( http://www.freesound.org/people/acclivity/ )
1921
*/
2022

21-
public class MediaPlayerWrapper {
23+
public class MediaPlayerWrapper implements MediaPlayer.OnCompletionListener {
2224
private final Context androidContext;
2325
private MediaPlayer mediaPlayer = null;
26+
private static final String TAG = "MediaPlayerWrapper";
2427

2528
MediaPlayerWrapper(Context androidContext)
2629
{
@@ -35,10 +38,18 @@ void play(int resid, boolean loop)
3538
mediaPlayer = null;
3639
}
3740

41+
// Request audio focus before making any device switch.
42+
((AudioManager) androidContext.getSystemService(Context.AUDIO_SERVICE)).requestAudioFocus(null, AudioManager.STREAM_MUSIC,
43+
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
44+
3845
mediaPlayer = MediaPlayer.create(androidContext, resid);
3946
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
4047
mediaPlayer.setLooping(loop);
4148
mediaPlayer.start();
49+
if (!loop) {
50+
// For non-looping sounds we want an event when they are done so that we can abandon focus
51+
mediaPlayer.setOnCompletionListener(this);
52+
}
4253
}
4354

4455
void stop()
@@ -47,6 +58,8 @@ void stop()
4758
mediaPlayer.stop();
4859
mediaPlayer.release();
4960
mediaPlayer = null;
61+
62+
((AudioManager) androidContext.getSystemService(Context.AUDIO_SERVICE)).abandonAudioFocus(null);
5063
}
5164
}
5265

@@ -56,7 +69,19 @@ void close()
5669
mediaPlayer.stop();
5770
mediaPlayer.release();
5871
mediaPlayer = null;
72+
73+
((AudioManager) androidContext.getSystemService(Context.AUDIO_SERVICE)).abandonAudioFocus(null);
5974
}
6075
}
6176

77+
public void onCompletion(MediaPlayer mediaPlayer)
78+
{
79+
RCLogger.i(TAG, "onCompletion()");
80+
((AudioManager) androidContext.getSystemService(Context.AUDIO_SERVICE)).abandonAudioFocus(null);
81+
82+
this.mediaPlayer.stop();
83+
// mediaPlayer.reset();
84+
this.mediaPlayer.release();
85+
this.mediaPlayer = null;
86+
}
6287
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ private RCConnection(Builder builder)
334334
}
335335
peer = builder.peer;
336336

337-
audioManager.startCall();
337+
audioManager.startCallMedia();
338338
/*
339339
if (incoming) {
340340
audioManager.playRingingSound();
@@ -556,7 +556,7 @@ public void setAudioMuted(boolean muted)
556556
audioManager.setMute(muted);
557557
}
558558

559-
device.onMuteChanged(this);
559+
device.onNotificationMuteChanged(this);
560560
}
561561

562562
/**
@@ -717,7 +717,7 @@ public void onCallLocalDisconnectedEvent(String jobId)
717717
public void onCallIncomingCanceledEvent(String jobId)
718718
{
719719
RCLogger.i(TAG, "onCallIncomingCanceledEvent(): jobId: " + jobId);
720-
device.cancelNotificationSoundIfNeeded(this);
720+
device.onNotificationCallCanceled(this);
721721
handleDisconnected(jobId, false);
722722
}
723723

@@ -792,7 +792,7 @@ private void handleDisconnected(String jobId, boolean haveDisconnectedLocally)
792792
disconnectWebrtc();
793793
}
794794

795-
device.stopForegroundNotification(this);
795+
device.onNotificationCallDisconnected(this);
796796

797797
if (listener != null && device.isAttached()) {
798798
listener.onDisconnected(this);
@@ -1482,7 +1482,7 @@ public void run()
14821482
customHeaders = (HashMap<String, String>) callParams.get(ParameterKeys.CONNECTION_CUSTOM_INCOMING_SIP_HEADERS);
14831483
}
14841484
if (device.isAttached()) {
1485-
device.startForegroundNotification(RCConnection.this);
1485+
device.onNotificationCallConnected(RCConnection.this);
14861486
listener.onConnected(RCConnection.this, customHeaders);
14871487
}
14881488
else {

0 commit comments

Comments
 (0)