-
Notifications
You must be signed in to change notification settings - Fork 617
OboeTester: Use callbacks for showCommDeviceStatus #2318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
robertwu1
wants to merge
1
commit into
main
Choose a base branch
from
robertwu/usecallbacksforshowCommDeviceStatus
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
| private BroadcastReceiver mSpeakerphoneStateReceiver; | ||
| private boolean mSpeakerphoneStateReceiverRegistered = false; | ||
|
|
||
| public CommunicationDeviceView(Context context) { | ||
| super(context); | ||
|
|
@@ -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) -> { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
|
@@ -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) { | ||
|
|
@@ -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; | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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