Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,10 @@ public class CommunicationDeviceView extends LinearLayout {
private boolean mScoStateReceiverRegistered = false;
private CommunicationDeviceSpinner mDeviceSpinner;
private int mScoState;
private CommDeviceSniffer mCommDeviceSniffer = new CommDeviceSniffer();;

protected class CommDeviceSniffer extends NativeSniffer {
@Override
public void updateStatusText() {
showCommDeviceStatus();
}
}
private AudioManager.OnCommunicationDeviceChangedListener mCommDeviceListener;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't mCommDeviceListener sufficient to get device callbacks ? Why do we explicitly need speakerphone? Is there any scenario where we need speaker change callbacks which are not sent as part of mCommunicationDeviceChangedListener ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right. Explicitly setting the speakerphone on is strongly discouraged. I can clean this up as a follow-up PR

private BroadcastReceiver mSpeakerphoneStateReceiver;
private boolean mSpeakerphoneStateReceiverRegistered = false;

public CommunicationDeviceView(Context context) {
super(context);
Expand Down Expand Up @@ -107,6 +103,19 @@ public void onReceive(Context context, Intent intent) {
}
};

mSpeakerphoneStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
showCommDeviceStatus();
}
};

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
mCommDeviceListener = (deviceInfo) -> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If communication device listener is used, it looks there is no need to get speaker phone state receiver

showCommDeviceStatus();
};
}

mDeviceSpinner = (CommunicationDeviceSpinner) findViewById(R.id.comm_devices_spinner);
mDeviceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
Expand Down Expand Up @@ -137,17 +146,28 @@ public void onNothingSelected(AdapterView<?> parent) {

public void onStart() {
registerScoStateReceiver();
mCommDeviceSniffer.startSniffer();
registerSpeakerphoneStateReceiver();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (mCommDeviceListener != null) {
mAudioManager.addOnCommunicationDeviceChangedListener(getContext().getMainExecutor(),
mCommDeviceListener);
}
}
}


public void onStop() {
mCommDeviceSniffer.stopSniffer();
mSpeakerphoneCheckbox.setChecked(false);
setSpeakerPhoneOn(false);
mScoCheckbox.setChecked(false);
mAudioManager.stopBluetoothSco();
unregisterScoStateReceiver();
unregisterSpeakerphoneStateReceiver();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (mCommDeviceListener != null) {
mAudioManager.removeOnCommunicationDeviceChangedListener(mCommDeviceListener);
}
}
}

public void onSetSpeakerphoneOn(View view) {
Expand Down Expand Up @@ -208,4 +228,18 @@ private synchronized void unregisterScoStateReceiver() {
}
}

private synchronized void registerSpeakerphoneStateReceiver() {
if (!mSpeakerphoneStateReceiverRegistered) {
getContext().registerReceiver(mSpeakerphoneStateReceiver,
new IntentFilter("android.media.action.SPEAKERPHONE_STATE_CHANGED"));
mSpeakerphoneStateReceiverRegistered = true;
}
}

private synchronized void unregisterSpeakerphoneStateReceiver() {
if (mSpeakerphoneStateReceiverRegistered) {
getContext().unregisterReceiver(mSpeakerphoneStateReceiver);
mSpeakerphoneStateReceiverRegistered = false;
}
}
}