modify behaviour of proximity sensor

use proximity sensor only if it is a voiceOnly call and userSelectedAudioDevice was SPEAKER_PHONE

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2022-02-01 18:20:32 +01:00
parent 338f06dda1
commit a34eb89570
No known key found for this signature in database
GPG Key ID: C793F8B59F43CE7B
3 changed files with 36 additions and 47 deletions

View File

@ -412,7 +412,7 @@ public class CallActivity extends CallBaseActivity {
// Create and audio manager that will take care of audio routing, // Create and audio manager that will take care of audio routing,
// audio modes, audio device enumeration etc. // audio modes, audio device enumeration etc.
audioManager = MagicAudioManager.create(getApplicationContext(), !isVoiceOnlyCall); audioManager = MagicAudioManager.create(getApplicationContext(), isVoiceOnlyCall);
// Store existing audio settings and change audio mode to // Store existing audio settings and change audio mode to
// MODE_IN_COMMUNICATION for best possible VoIP performance. // MODE_IN_COMMUNICATION for best possible VoIP performance.
Log.d(TAG, "Starting the audio manager..."); Log.d(TAG, "Starting the audio manager...");
@ -457,7 +457,7 @@ public class CallActivity extends CallBaseActivity {
public void setAudioOutputChannel(MagicAudioManager.AudioDevice selectedAudioDevice) { public void setAudioOutputChannel(MagicAudioManager.AudioDevice selectedAudioDevice) {
if (audioManager != null) { if (audioManager != null) {
audioManager.selectAudioDevice(selectedAudioDevice); audioManager.selectAudioDevice(selectedAudioDevice);
updateAudioOutputButton(audioManager.getResultingAudioDevice()); updateAudioOutputButton(audioManager.getCurrentAudioDevice());
} }
} }

View File

@ -62,7 +62,7 @@ class AudioOutputDialog(val callActivity: CallActivity) : BottomSheetDialog(call
} }
private fun highlightActiveOutputChannel() { private fun highlightActiveOutputChannel() {
when (callActivity.audioManager?.resultingAudioDevice) { when (callActivity.audioManager?.currentAudioDevice) {
MagicAudioManager.AudioDevice.BLUETOOTH -> { MagicAudioManager.AudioDevice.BLUETOOTH -> {
dialogAudioOutputBinding.audioOutputBluetoothIcon.setColorFilter( dialogAudioOutputBinding.audioOutputBluetoothIcon.setColorFilter(
ContextCompat.getColor( ContextCompat.getColor(

View File

@ -57,7 +57,7 @@ public class MagicAudioManager {
private static final String TAG = "MagicAudioManager"; private static final String TAG = "MagicAudioManager";
private final Context magicContext; private final Context magicContext;
private final MagicBluetoothManager bluetoothManager; private final MagicBluetoothManager bluetoothManager;
private boolean controlSpeakerByProximitySensor; private boolean useProximitySensor;
private AudioManager audioManager; private AudioManager audioManager;
private AudioManagerListener audioManagerListener; private AudioManagerListener audioManagerListener;
private AudioManagerState amState; private AudioManagerState amState;
@ -67,7 +67,7 @@ public class MagicAudioManager {
private boolean hasWiredHeadset = false; private boolean hasWiredHeadset = false;
private AudioDevice userSelectedAudioDevice; private AudioDevice userSelectedAudioDevice;
private AudioDevice resultingAudioDevice; private AudioDevice currentAudioDevice;
private MagicProximitySensor proximitySensor = null; private MagicProximitySensor proximitySensor = null;
@ -90,7 +90,7 @@ public class MagicAudioManager {
powerManagerUtils = new PowerManagerUtils(); powerManagerUtils = new PowerManagerUtils();
powerManagerUtils.updatePhoneState(PowerManagerUtils.PhoneState.WITH_PROXIMITY_SENSOR_LOCK); powerManagerUtils.updatePhoneState(PowerManagerUtils.PhoneState.WITH_PROXIMITY_SENSOR_LOCK);
controlSpeakerByProximitySensor = useProximitySensor; this.useProximitySensor = useProximitySensor;
updateAudioDeviceState(); updateAudioDeviceState();
// Create and initialize the proximity sensor. // Create and initialize the proximity sensor.
@ -118,27 +118,24 @@ public class MagicAudioManager {
* e.g. from "NEAR to FAR" or from "FAR to NEAR". * e.g. from "NEAR to FAR" or from "FAR to NEAR".
*/ */
private void onProximitySensorChangedState() { private void onProximitySensorChangedState() {
if (!useProximitySensor) {
if (!controlSpeakerByProximitySensor) {
return; return;
} }
// The proximity sensor should only be activated when there are exactly two if (userSelectedAudioDevice.equals(AudioDevice.SPEAKER_PHONE)
// available audio devices. && audioDevices.contains(AudioDevice.EARPIECE)
if (audioDevices.size() == 2 && audioDevices.contains(MagicAudioManager.AudioDevice.EARPIECE) && audioDevices.contains(AudioDevice.SPEAKER_PHONE)) {
&& audioDevices.contains(MagicAudioManager.AudioDevice.SPEAKER_PHONE)) {
if (proximitySensor.sensorReportsNearState()) { if (proximitySensor.sensorReportsNearState()) {
// Sensor reports that a "handset is being held up to a person's ear", setAudioDeviceInternal(AudioDevice.EARPIECE);
// or "something is covering the light sensor". Log.d(TAG,"switched to EARPIECE because userSelectedAudioDevice was SPEAKER_PHONE and proximity=near");
setAudioDeviceInternal(MagicAudioManager.AudioDevice.EARPIECE);
EventBus.getDefault().post(new PeerConnectionEvent(PeerConnectionEvent.PeerConnectionEventType EventBus.getDefault().post(new PeerConnectionEvent(PeerConnectionEvent.PeerConnectionEventType
.SENSOR_NEAR, null, null, null, null)); .SENSOR_NEAR, null, null, null, null));
} else { } else {
// Sensor reports that a "handset is removed from a person's ear", or
// "the light sensor is no longer covered".
setAudioDeviceInternal(MagicAudioManager.AudioDevice.SPEAKER_PHONE); setAudioDeviceInternal(MagicAudioManager.AudioDevice.SPEAKER_PHONE);
Log.d(TAG,"switched to SPEAKER_PHONE because userSelectedAudioDevice was SPEAKER_PHONE and proximity=far");
EventBus.getDefault().post(new PeerConnectionEvent(PeerConnectionEvent.PeerConnectionEventType EventBus.getDefault().post(new PeerConnectionEvent(PeerConnectionEvent.PeerConnectionEventType
.SENSOR_FAR, null, null, null, null)); .SENSOR_FAR, null, null, null, null));
@ -226,7 +223,7 @@ public class MagicAudioManager {
// Set initial device states. // Set initial device states.
userSelectedAudioDevice = AudioDevice.NONE; userSelectedAudioDevice = AudioDevice.NONE;
resultingAudioDevice = AudioDevice.NONE; currentAudioDevice = AudioDevice.NONE;
audioDevices.clear(); audioDevices.clear();
// Initialize and start Bluetooth if a BT device is available or initiate // Initialize and start Bluetooth if a BT device is available or initiate
@ -285,21 +282,16 @@ public class MagicAudioManager {
/** /**
* Changes selection of the currently active audio device. * Changes selection of the currently active audio device.
*/ */
private void setAudioDeviceInternal(AudioDevice device) { private void setAudioDeviceInternal(AudioDevice audioDevice) {
Log.d(TAG, "setAudioDeviceInternal(device=" + device + ")"); Log.d(TAG, "setAudioDeviceInternal(device=" + audioDevice + ")");
if (audioDevices.contains(device)) { if (audioDevices.contains(audioDevice)) {
switch (audioDevice) {
switch (device) {
case SPEAKER_PHONE: case SPEAKER_PHONE:
setSpeakerphoneOn(true); setSpeakerphoneOn(true);
break; break;
case EARPIECE: case EARPIECE:
setSpeakerphoneOn(false);
break;
case WIRED_HEADSET: case WIRED_HEADSET:
setSpeakerphoneOn(false);
break;
case BLUETOOTH: case BLUETOOTH:
setSpeakerphoneOn(false); setSpeakerphoneOn(false);
break; break;
@ -307,7 +299,7 @@ public class MagicAudioManager {
Log.e(TAG, "Invalid audio device selection"); Log.e(TAG, "Invalid audio device selection");
break; break;
} }
resultingAudioDevice = device; currentAudioDevice = audioDevice;
} }
} }
@ -322,11 +314,11 @@ public class MagicAudioManager {
userSelectedAudioDevice = device; userSelectedAudioDevice = device;
if (device == AudioDevice.SPEAKER_PHONE) { // if (device == AudioDevice.SPEAKER_PHONE) {
controlSpeakerByProximitySensor = true; // controlSpeakerByProximitySensor = true;
} else { // } else {
controlSpeakerByProximitySensor = false; // controlSpeakerByProximitySensor = false;
} // }
updateAudioDeviceState(); updateAudioDeviceState();
} }
@ -342,9 +334,9 @@ public class MagicAudioManager {
/** /**
* Returns the currently selected audio device. * Returns the currently selected audio device.
*/ */
public AudioDevice getResultingAudioDevice() { public AudioDevice getCurrentAudioDevice() {
ThreadUtils.checkIsOnMainThread(); ThreadUtils.checkIsOnMainThread();
return resultingAudioDevice; return currentAudioDevice;
} }
/** /**
@ -424,12 +416,9 @@ public class MagicAudioManager {
+ "BT state=" + bluetoothManager.getState()); + "BT state=" + bluetoothManager.getState());
Log.d(TAG, "Device status: " Log.d(TAG, "Device status: "
+ "available=" + audioDevices + ", " + "available=" + audioDevices + ", "
+ "resulting(current)=" + resultingAudioDevice + ", " + "current=" + currentAudioDevice + ", "
+ "user selected=" + userSelectedAudioDevice); + "user selected=" + userSelectedAudioDevice);
if (bluetoothManager.getState() == MagicBluetoothManager.State.HEADSET_AVAILABLE if (bluetoothManager.getState() == MagicBluetoothManager.State.HEADSET_AVAILABLE
|| bluetoothManager.getState() == MagicBluetoothManager.State.HEADSET_UNAVAILABLE || bluetoothManager.getState() == MagicBluetoothManager.State.HEADSET_UNAVAILABLE
|| bluetoothManager.getState() == MagicBluetoothManager.State.SCO_DISCONNECTING) { || bluetoothManager.getState() == MagicBluetoothManager.State.SCO_DISCONNECTING) {
@ -514,34 +503,34 @@ public class MagicAudioManager {
// Update selected audio device. // Update selected audio device.
AudioDevice newResultingAudioDevice; AudioDevice newCurrentAudioDevice;
if (bluetoothManager.getState() == MagicBluetoothManager.State.SCO_CONNECTED) { if (bluetoothManager.getState() == MagicBluetoothManager.State.SCO_CONNECTED) {
// If a Bluetooth is connected, then it should be used as output audio // If a Bluetooth is connected, then it should be used as output audio
// device. Note that it is not sufficient that a headset is available; // device. Note that it is not sufficient that a headset is available;
// an active SCO channel must also be up and running. // an active SCO channel must also be up and running.
newResultingAudioDevice = AudioDevice.BLUETOOTH; newCurrentAudioDevice = AudioDevice.BLUETOOTH;
} else if (hasWiredHeadset) { } else if (hasWiredHeadset) {
// If a wired headset is connected, but Bluetooth is not, then wired headset is used as // If a wired headset is connected, but Bluetooth is not, then wired headset is used as
// audio device. // audio device.
newResultingAudioDevice = AudioDevice.WIRED_HEADSET; newCurrentAudioDevice = AudioDevice.WIRED_HEADSET;
} else { } else {
// No wired headset and no Bluetooth, hence the audio-device list can contain speaker // No wired headset and no Bluetooth, hence the audio-device list can contain speaker
// phone (on a tablet), or speaker phone and earpiece (on mobile phone). // phone (on a tablet), or speaker phone and earpiece (on mobile phone).
// |defaultAudioDevice| contains either AudioDevice.SPEAKER_PHONE or AudioDevice.EARPIECE // |defaultAudioDevice| contains either AudioDevice.SPEAKER_PHONE or AudioDevice.EARPIECE
// depending on the user's selection. // depending on the user's selection.
newResultingAudioDevice = userSelectedAudioDevice; newCurrentAudioDevice = userSelectedAudioDevice;
} }
// Switch to new device but only if there has been any changes. // Switch to new device but only if there has been any changes.
if (newResultingAudioDevice != resultingAudioDevice || audioDeviceSetUpdated) { if (newCurrentAudioDevice != currentAudioDevice || audioDeviceSetUpdated) {
// Do the required device switch. // Do the required device switch.
setAudioDeviceInternal(newResultingAudioDevice); setAudioDeviceInternal(newCurrentAudioDevice);
Log.d(TAG, "New device status: " Log.d(TAG, "New device status: "
+ "available=" + audioDevices + ", " + "available=" + audioDevices + ", "
+ "resulting(new)=" + newResultingAudioDevice); + "current(new)=" + newCurrentAudioDevice);
if (audioManagerListener != null) { if (audioManagerListener != null) {
// Notify a listening client that audio device has been changed. // Notify a listening client that audio device has been changed.
audioManagerListener.onAudioDeviceChanged(resultingAudioDevice, audioDevices); audioManagerListener.onAudioDeviceChanged(currentAudioDevice, audioDevices);
} }
} }
Log.d(TAG, "--- updateAudioDeviceState done"); Log.d(TAG, "--- updateAudioDeviceState done");