From 65b73c10af9d90a6296d01f58771c224e72f66f7 Mon Sep 17 00:00:00 2001 From: Fernando <100nandoo@gmail.com> Date: Wed, 2 Jul 2025 09:31:59 +0800 Subject: [PATCH 1/6] only apply volume on apple dongle & cleanup ui --- app/build.gradle | 4 +- .../libusbAndroidTest/MainActivity.java | 37 +++++-- app/src/main/res/layout/activity_main.xml | 98 ++++++++----------- app/src/main/res/values/dimens.xml | 5 + app/src/main/res/values/strings.xml | 6 ++ 5 files changed, 86 insertions(+), 64 deletions(-) create mode 100644 app/src/main/res/values/dimens.xml diff --git a/app/build.gradle b/app/build.gradle index 14d651c..7d9a931 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -9,8 +9,8 @@ android { applicationId 'com.example.libusbAndroidTest' minSdk 28 targetSdk 30 - versionCode 1 - versionName "1.0" + versionCode 2 + versionName "1.1" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" externalNativeBuild { diff --git a/app/src/main/java/com/example/libusbAndroidTest/MainActivity.java b/app/src/main/java/com/example/libusbAndroidTest/MainActivity.java index ed3ef9f..4799d10 100644 --- a/app/src/main/java/com/example/libusbAndroidTest/MainActivity.java +++ b/app/src/main/java/com/example/libusbAndroidTest/MainActivity.java @@ -41,7 +41,7 @@ public class MainActivity extends AppCompatActivity { private ActivityMainBinding binding; private UsbManager usbManager; - private TextView tv; + private TextView tvDeviceName; private EditText volInput; private CheckBox autoApply; @@ -52,6 +52,13 @@ public class MainActivity extends AppCompatActivity { private int deviceDescriptor = -1; private static String deviceName; + // in Hex 0x5AC is apple vendor id + private static final String APPLE_VENDOR_ID = "1452"; + + // in Hex 0x110A is apple dongle product id + private static final String APPLE_DONGLE_PRODUCT_ID = "4362"; + + private static final String TAG = "USB DAC Volume Adjustment" ; private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION"; @@ -65,7 +72,7 @@ public void onReceive(Context context, Intent intent) { UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) { - if(device != null){ + if(device != null && isAppleDongle(device)){ connectDevice(device); } } @@ -77,8 +84,20 @@ public void onReceive(Context context, Intent intent) { } }; + private boolean isAppleDongle(UsbDevice device){ + return device.getVendorId() == Integer.parseInt(APPLE_VENDOR_ID) && device.getProductId() == Integer.parseInt(APPLE_DONGLE_PRODUCT_ID); + } + protected void connectDevice(UsbDevice device) { + Log.d("UsbDevice", "device: " + device.getDeviceName() + " " + device.getVendorId() + " " + device.getProductId()); + boolean isAppleDongle = isAppleDongle(device); + + String vendorId = "0x" + Integer.toHexString(device.getVendorId()).toUpperCase(); + String productId = "0x" + Integer.toHexString(device.getProductId()).toUpperCase(); + String deviceVendorIdAndProductId = isAppleDongle ? "Apple Dongle" : + "vendorId: " + vendorId + " productId:" + productId; + tvDeviceName.setText(deviceVendorIdAndProductId); UsbInterface intf = device.getInterface(0); UsbEndpoint endpoint = intf.getEndpoint(0); UsbDeviceConnection connection = usbManager.openDevice(device); @@ -88,9 +107,11 @@ protected void connectDevice(UsbDevice device) deviceName = initializeNativeDevice(fileDescriptor); deviceDescriptor = fileDescriptor; - if(autoApply.isChecked() && quitAfterApply.isChecked()){ + if(autoApply.isChecked()){ setDeviceVolume(fileDescriptor); - finishAndRemoveTask(); + if(quitAfterApply.isChecked()){ + finishAndRemoveTask(); + } } } @@ -99,7 +120,9 @@ protected void checkUsbDevices() PendingIntent permissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0); HashMap deviceList = usbManager.getDeviceList(); + for (UsbDevice device : deviceList.values()) { + if(!isAppleDongle(device)) return; if(usbManager.hasPermission(device)) { connectDevice(device); @@ -116,14 +139,14 @@ protected void onCreate(Bundle savedInstanceState) { binding = ActivityMainBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); - tv = binding.sampleText; + tvDeviceName = binding.deviceName; volInput = binding.volume; autoApply = binding.autoApply; quitAfterApply = binding.quitAfterApply; recordPermissionButton = binding.recordPermissionButton; SharedPreferences settings = getApplicationContext().getSharedPreferences("myPrefs", 0); - volInput.setText(settings.getString("volume", "0000")); + volInput.setText(settings.getString("volume", "007f")); autoApply.setChecked(settings.getBoolean("autoApply", false)); quitAfterApply.setChecked(settings.getBoolean("quitAfterApply", false)); @@ -147,7 +170,7 @@ public void applyButtonPressed(View view){ String volume = volInput.getText().toString(); if(deviceDescriptor < 0){ - tv.setBackgroundColor(Color.RED); + tvDeviceName.setBackgroundColor(Color.RED); return; } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 14ee2dc..436478c 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -6,57 +6,15 @@ android:layout_height="match_parent" tools:context=".MainActivity"> -