Commit 6dab0c6d authored by Sylvain Berfini's avatar Sylvain Berfini 🎩
Browse files

Added Android broadcast receiver to reload sound devices when headset is connected/disconnected

parent 5d0915e1
......@@ -20,6 +20,7 @@
package org.linphone.core.tools.audio;
import android.content.Context;
import android.content.IntentFilter;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.AudioManager.OnAudioFocusChangeListener;
......@@ -39,6 +40,7 @@ import java.lang.SecurityException;
import org.linphone.core.Core;
import org.linphone.core.AudioDevice;
import org.linphone.core.tools.Log;
import org.linphone.core.tools.receiver.HeadsetReceiver;
import org.linphone.core.tools.service.CoreManager;
public class AudioHelper implements OnAudioFocusChangeListener {
......@@ -48,11 +50,17 @@ public class AudioHelper implements OnAudioFocusChangeListener {
private MediaPlayer mPlayer;
private int mVolumeBeforeEchoTest;
private AudioDevice mPreviousDefaultOutputAudioDevice;
private HeadsetReceiver mHeadsetReceiver;
public AudioHelper(Context context) {
mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
mRingingRequest = null;
mCallRequest = null;
mHeadsetReceiver = new HeadsetReceiver();
IntentFilter filter = new IntentFilter(AudioManager.ACTION_HEADSET_PLUG);
context.registerReceiver(mHeadsetReceiver, filter);
Log.i("[Audio Helper] Helper created");
}
......
/*
* Copyright (c) 2010-2020 Belledonne Communications SARL.
*
* This file is part of linphone-android
* (see https://www.linphone.org).
* This file is part of Liblinphone.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......
/*
* Copyright (c) 2010-2021 Belledonne Communications SARL.
*
* This file is part of Liblinphone.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.linphone.core.tools.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import org.linphone.core.tools.Log;
import org.linphone.core.tools.service.CoreManager;
public class HeadsetReceiver extends BroadcastReceiver {
public HeadsetReceiver() {
super();
Log.i("[Headset] Headset receiver created");
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i("[Headset] Headset broadcast received");
// https://developer.android.com/reference/android/media/AudioManager#ACTION_HEADSET_PLUG
if (action.equals(AudioManager.ACTION_HEADSET_PLUG)) {
int state = intent.getIntExtra("state", 0);
String name = intent.getStringExtra("name");
int hasMicrophone = intent.getIntExtra("microphone", 0);
if (state == 0) {
Log.i("[Headset] Headset disconnected: " + name);
} else if (state == 1) {
Log.i("[Headset] Headset connected: " + name);
if (hasMicrophone == 1) {
Log.i("[Headset] Headset has a microphone");
}
} else {
Log.w("[Headset] Unknown headset plugged state: " + state);
}
if (CoreManager.isReady()) CoreManager.instance().onHeadsetStateChanged();
}
}
}
\ No newline at end of file
......@@ -276,6 +276,11 @@ public class CoreManager {
mCore.reloadSoundDevices();
}
public void onHeadsetStateChanged() {
Log.i("[Core Manager] Headset state changed, reload sound devices");
mCore.reloadSoundDevices();
}
public void onBackgroundMode() {
Log.i("[Core Manager] App has entered background mode");
if (mCore != null) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment