From b656845fdcd2a59f93ab2eece14d252e62b714ec Mon Sep 17 00:00:00 2001 From: Fredrik Gustafsson Date: Fri, 8 Nov 2024 11:38:44 +0100 Subject: [PATCH] cleanup pandad --- .../java/ai.flow.flowy/ServicePandad.java | 43 +++++++++---------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/android/src/main/java/ai.flow.flowy/ServicePandad.java b/android/src/main/java/ai.flow.flowy/ServicePandad.java index 596d4afe..33ac5265 100644 --- a/android/src/main/java/ai.flow.flowy/ServicePandad.java +++ b/android/src/main/java/ai.flow.flowy/ServicePandad.java @@ -1,5 +1,4 @@ package ai.flow.flowy; -//Java_org_jagheterfredrik_flowapp_ServicePandad_nativeStart import android.app.Service; import android.content.Context; @@ -9,18 +8,11 @@ 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; -import android.os.Bundle; -import android.util.Log; -import org.kivy.android.PythonUtil; - -import java.io.File; import java.util.HashMap; class PandaInstance implements Runnable { @@ -37,7 +29,6 @@ public void run(){ public class ServicePandad extends Service { private static final String TAG = "ServicePandad"; - // Thread for Python code private Thread applicationThread = null; private static final String ACTION_USB_PERMISSION = "ai.flow.flowy.USB_PERMISSION"; @@ -48,10 +39,8 @@ public void onReceive(Context context, Intent intent) { if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) { synchronized (this) { - UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); - if (device == null) { return; } - PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_MUTABLE); - ((UsbManager)context.getSystemService(Context.USB_SERVICE)).requestPermission(device, pendingIntent); + UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); + maybeRequestUSBPermission(usbDevice, context); } } else if (ACTION_USB_PERMISSION.equals(action)) { synchronized (this) { @@ -61,7 +50,6 @@ public void onReceive(Context context, Intent intent) { UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE); UsbDeviceConnection usbDeviceConnection = usbManager.openDevice(device); Log.i(TAG, "Permission granted for serial "+usbDeviceConnection.getSerial()); - // nativeStart(usbDeviceConnection.getFileDescriptor()); PandaInstance pandaInstance = new PandaInstance(usbDeviceConnection.getFileDescriptor()); new Thread(pandaInstance).start(); } @@ -74,6 +62,19 @@ public void onReceive(Context context, Intent intent) { } }; + private void maybeRequestUSBPermission(UsbDevice device, Context context) { + if (device == null) { + Log.w(TAG, "maybeRequestUSBPermission got a null device"); + return; + } + if (device.getVendorId() == 0xbbaa && device.getProductId() == 0xddcc) { + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_MUTABLE); + ((UsbManager) context.getSystemService(Context.USB_SERVICE)).requestPermission(device, pendingIntent); + } else { + Log.w(TAG, "Found a USB device that's not a Panda"); + } + } + public int startType() { return START_NOT_STICKY; } @@ -101,25 +102,20 @@ public int onStartCommand(Intent intent, int flags, int startId) { System.out.println("Flashing Panda"); PythonRunner.run(0, app_root + "/panda/board/obj/"); - // Request permission for newly plugged devices IntentFilter attachFilter = new IntentFilter(); + // Receiver for attached devices, used to request permission when plugging in a device attachFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); + // Receiver for extended permissions, called when the user accepts USB permissions attachFilter.addAction(ACTION_USB_PERMISSION); registerReceiver(usbReceiver, attachFilter, Context.RECEIVER_EXPORTED); // Request permission for already plugged devices UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE); - Intent usbIntent = new Intent(ACTION_USB_PERMISSION); - usbIntent.setPackage(this.getPackageName()); - PendingIntent permissionIntent = PendingIntent.getBroadcast(this, 0, usbIntent, PendingIntent.FLAG_MUTABLE); - - IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); - registerReceiver(usbReceiver, filter, Context.RECEIVER_EXPORTED); HashMap deviceList = manager.getDeviceList(); - System.out.println("Number of devices found: "+deviceList.size()); + Log.i(TAG, "Number of USB devices found: "+deviceList.size()); for (UsbDevice usbDevice : deviceList.values()) { - manager.requestPermission(usbDevice, permissionIntent); + maybeRequestUSBPermission(usbDevice, this); } return startType(); @@ -142,6 +138,7 @@ private void requestUsbPermissions(UsbDevice device) { @Override public void onDestroy() { nativeStop(); + unregisterReceiver(usbReceiver); } // Native part