Skip to content

Commit

Permalink
Listen to USB device de/attach event. (#1961)
Browse files Browse the repository at this point in the history
* Listen to USB device de/attach event.

With listening to USB device de/attach event, disconnect
test can well sync on the peripherals plug state.
  • Loading branch information
flamme authored Feb 8, 2024
1 parent 5b2a750 commit 86165b8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
4 changes: 2 additions & 2 deletions apps/OboeTester/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId = "com.mobileer.oboetester"
minSdkVersion 23
targetSdkVersion 34
versionCode 80
versionName "2.5.9"
versionCode 81
versionName "2.5.10"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
Expand Down Expand Up @@ -49,6 +54,7 @@ public class TestDisconnectActivity extends TestAudioActivity {
private volatile boolean mTestFailed;
private volatile boolean mSkipTest;
private volatile int mPlugCount;
private volatile int mUsbDeviceAttachedCount;
private volatile int mPlugState;
private volatile int mPlugMicrophone;
private BroadcastReceiver mPluginReceiver = new PluginBroadcastReceiver();
Expand All @@ -64,20 +70,68 @@ public class TestDisconnectActivity extends TestAudioActivity {
public class PluginBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
mPlugMicrophone = intent.getIntExtra("microphone", -1);
mPlugState = intent.getIntExtra("state", -1);
mPlugCount++;
switch (intent.getAction()) {
case Intent.ACTION_HEADSET_PLUG: {
mPlugMicrophone = intent.getIntExtra("microphone", -1);
mPlugState = intent.getIntExtra("state", -1);
mPlugCount++;
} break;
case UsbManager.ACTION_USB_DEVICE_ATTACHED:
case UsbManager.ACTION_USB_DEVICE_DETACHED: {
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
final boolean hasAudioPlayback =
containsAudioStreamingInterface(device, UsbConstants.USB_DIR_OUT);
final boolean hasAudioCapture =
containsAudioStreamingInterface(device, UsbConstants.USB_DIR_IN);
if (hasAudioPlayback || hasAudioCapture) {
mPlugState =
intent.getAction() == UsbManager.ACTION_USB_DEVICE_ATTACHED ? 1 : 0;
mUsbDeviceAttachedCount++;
mPlugMicrophone = hasAudioCapture ? 1 : 0;
}
} break;
default:
break;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
String message = "HEADSET_PLUG #" + mPlugCount
+ ", USB_DEVICE_DE/ATTACHED #" + mUsbDeviceAttachedCount
+ ", mic = " + mPlugMicrophone
+ ", state = " + mPlugState;
mPlugTextView.setText(message);
log(message);
}
});
}

private static final int AUDIO_STREAMING_SUB_CLASS = 2;

/**
* Figure out if an UsbDevice contains audio input/output streaming interface or not.
*
* @param device the given UsbDevice
* @param direction the direction of the audio streaming interface
* @return true if the UsbDevice contains the audio input/output streaming interface.
*/
private boolean containsAudioStreamingInterface(UsbDevice device, int direction) {
final int interfaceCount = device.getInterfaceCount();
for (int i = 0; i < interfaceCount; ++i) {
UsbInterface usbInterface = device.getInterface(i);
if (usbInterface.getInterfaceClass() != UsbConstants.USB_CLASS_AUDIO
&& usbInterface.getInterfaceSubclass() != AUDIO_STREAMING_SUB_CLASS) {
continue;
}
final int endpointCount = usbInterface.getEndpointCount();
for (int j = 0; j < endpointCount; ++j) {
if (usbInterface.getEndpoint(j).getDirection() == direction) {
return true;
}
}
}
return false;
}
}

@Override
Expand Down Expand Up @@ -152,6 +206,8 @@ public void run() {
public void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
this.registerReceiver(mPluginReceiver, filter);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
android:id="@+id/text_plug_events"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="1"
android:lines="2"
android:text="plug #"
android:textSize="18sp"
android:textStyle="bold"
Expand Down

0 comments on commit 86165b8

Please sign in to comment.