Skip to content

Commit

Permalink
cleanup pandad
Browse files Browse the repository at this point in the history
  • Loading branch information
jagheterfredrik committed Nov 8, 2024
1 parent 69d26f1 commit b656845
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions android/src/main/java/ai.flow.flowy/ServicePandad.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package ai.flow.flowy;
//Java_org_jagheterfredrik_flowapp_ServicePandad_nativeStart

import android.app.Service;
import android.content.Context;
Expand All @@ -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 {
Expand All @@ -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";
Expand All @@ -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) {
Expand All @@ -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();
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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<String, UsbDevice> 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();
Expand All @@ -142,6 +138,7 @@ private void requestUsbPermissions(UsbDevice device) {
@Override
public void onDestroy() {
nativeStop();
unregisterReceiver(usbReceiver);
}

// Native part
Expand Down

0 comments on commit b656845

Please sign in to comment.