package com.openxc.remote.sources.usb;

import android.app.Activity;

import android.content.Intent;

import android.hardware.usb.UsbManager;

import android.util.Log;

public class UsbDeviceAttachmentActivity extends Activity {
    private final static String TAG = "UsbDeviceAttachmentActivity";

    @Override
    public void onResume() {
        super.onResume();

        Intent intent = getIntent();
        Log.d(TAG, "Resumed with intent: " + intent);
        String action = intent.getAction();

        if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
            Intent refreshDeviceIntent = new Intent(
                    UsbVehicleDataSource.ACTION_USB_DEVICE_ATTACHED);
            sendBroadcast(refreshDeviceIntent);
        }
        finish();
    }
}

@@ -166,592 +166,1453 @@
 g;%0A%0A
-public class UsbDeviceAttachmentActivity extends Activity %7B%0A    private final static String TAG = %22UsbDeviceAttachmentActivity%22;%0A%0A    @Override%0A    public void onResume() %7B%0A        super.onResume();%0A%0A        Intent intent = getIntent();%0A        Log.d(TAG, %22Resumed with intent: %22 + intent);%0A        String action = intent.getAction();%0A%0A        if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) %7B%0A            Intent refreshDeviceIntent = new Intent(%0A                    UsbVehicleDataSource.ACTION_USB_DEVICE_ATTACHED)
+/**%0A *%0A * The UsbDeviceActivity is a proxy to listen for USB_DEVICE_ATTACHED intents.%0A *%0A * Android unfortunately doesn't provide a way to specify USB device filters for%0A * the ATTACHED intent programatically - it must be in the AndroidManifest. This%0A * means we have no way to link the correct device filters with the actual%0A * instance of the BroadcastReceiver (an object inside of UsbVehicleDataSource),%0A * since we are doing something besides just starting an activity based on the%0A * intent.%0A *%0A * This seems like an oversight by the Android developers - they only envisioned%0A * you using the ATTACHED signal to auto-start your application.%0A *%0A * This Activity is a workaround that listens for the intent. Originally it%0A * rebroadcast a custom version that the true listener can actually receive, but%0A * that is actually unneccessary - when the activity's blank screen opens and%0A * immediately closes (invisble to the user), it causes the active OpenXC app to%0A * pause. That currently means the background services all get destroyed and%0A * recreated when the activity exits and the previous activity regains focus.%0A *%0A * After the services restart, the USB devices is reconnected.%0A */%0Apublic class UsbDeviceAttachmentActivity extends Activity %7B private final static%0A    String TAG = %22UsbDeviceAttachmentActivity%22
 ;%0A
+%0A
     
-        sendBroadcast(refreshDeviceIntent);%0A        %7D
+@Override%0A    public void onResume() %7B%0A        super.onResume();%0A        Log.d(TAG, %22USB device proxy listener woke up%22);
 %0A   

Document the UsbDeviceAttachmentActivity proxy listener.