diff --git a/android/app/build.gradle b/android/app/build.gradle index 9d9786084e..6d775e35c2 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -6,7 +6,7 @@ android { defaultConfig { applicationId "com.rusefi.app" - minSdkVersion 17 + minSdkVersion 21 targetSdkVersion 30 versionCode 1 versionName "1.0" @@ -22,6 +22,7 @@ android { } dependencies { + implementation files('lib/dfu_java.jar') implementation 'com.github.mik3y:usb-serial-for-android:v2.2.3' implementation fileTree(dir: "libs", include: ["*.jar"]) implementation 'androidx.appcompat:appcompat:1.1.0' diff --git a/android/app/lib/dfu_java.jar b/android/app/lib/dfu_java.jar new file mode 100644 index 0000000000..6414960bf6 Binary files /dev/null and b/android/app/lib/dfu_java.jar differ diff --git a/android/app/src/main/java/com/rusefi/app/rusEFI.java b/android/app/src/main/java/com/rusefi/app/rusEFI.java index 021c93ed0f..bc3cbe52ae 100644 --- a/android/app/src/main/java/com/rusefi/app/rusEFI.java +++ b/android/app/src/main/java/com/rusefi/app/rusEFI.java @@ -17,8 +17,11 @@ package com.rusefi.app; import android.app.Activity; +import android.app.PendingIntent; +import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.hardware.usb.UsbDevice; import android.hardware.usb.UsbDeviceConnection; import android.hardware.usb.UsbManager; @@ -32,6 +35,7 @@ import com.hoho.android.usbserial.driver.UsbSerialDriver; import com.hoho.android.usbserial.driver.UsbSerialPort; import com.hoho.android.usbserial.driver.UsbSerialProber; import com.hoho.android.usbserial.util.SerialInputOutputManager; +import com.rusefi.dfu.android.DfuDeviceLocator; import java.io.IOException; import java.util.Date; @@ -42,27 +46,68 @@ public class rusEFI extends Activity { private static final int VENDOR_ST = 0x0483; private static final int ST_CDC = 0x5740; + private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION"; + /* UI elements */ private TextView mStatusView; private TextView mResultView; + private UsbManager usbManager; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_usb); + usbManager = (UsbManager) getSystemService(Context.USB_SERVICE); + + mStatusView = (TextView) findViewById(R.id.text_status); mResultView = (TextView) findViewById(R.id.text_result); + IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); + registerReceiver(mUsbReceiver, filter); mStatusView.setText("Hello"); handleButton(); } + private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() { + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + if (ACTION_USB_PERMISSION.equals(action)) { + synchronized (this) { + if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) { + UsbDevice dfuDevice = DfuDeviceLocator.findDevice(usbManager); + new DfuDeviceLocator().openDfu(usbManager, dfuDevice); +// if (device != null) { +// //call method to set up device communication +// } +// } else { + //txtInfo.append("permission denied for device " + device); + } + } + } + } + }; + private void handleButton() { mResultView.append("rusEFI app v0.0000001\n"); - UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE); + + UsbDevice dfuDevice = DfuDeviceLocator.findDevice(usbManager); + + if (usbManager.hasPermission(dfuDevice)) { + new DfuDeviceLocator().openDfu(usbManager, dfuDevice); + } else { + PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0); + usbManager.requestPermission(dfuDevice, mPermissionIntent); + } + + + if (1 == 1) + return; + // listDevices(manager); @@ -70,7 +115,7 @@ public class rusEFI extends Activity { customTable.addProduct(VENDOR_ST, ST_CDC, CdcAcmSerialDriver.class); UsbSerialProber prober = new UsbSerialProber(customTable); - List availableDrivers = prober.findAllDrivers(manager); + List availableDrivers = prober.findAllDrivers(usbManager); if (availableDrivers.isEmpty()) { mStatusView.setText("Not connected"); mResultView.append("No devices " + new Date()); @@ -79,7 +124,7 @@ public class rusEFI extends Activity { mStatusView.setText("rusEFI: " + availableDrivers.size() + " device(s)"); UsbSerialDriver driver = availableDrivers.get(0); - UsbDeviceConnection connection = manager.openDevice(driver.getDevice()); + UsbDeviceConnection connection = usbManager.openDevice(driver.getDevice()); if (connection == null) { // add UsbManager.requestPermission(driver.getDevice(), ..) handling here mStatusView.setText("Unable to open");