FileDocCategorySizeDatePackage
SourceActivity.javaAPI DocAndroid 5.1 API8733Thu Mar 12 22:22:42 GMT 2015com.android.accessorydisplay.source

SourceActivity

public class SourceActivity extends android.app.Activity

Fields Summary
private static final String
TAG
private static final String
ACTION_USB_ACCESSORY_PERMISSION
private static final String
MANUFACTURER
private static final String
MODEL
private android.hardware.usb.UsbManager
mUsbManager
private AccessoryReceiver
mReceiver
private android.widget.TextView
mLogTextView
private com.android.accessorydisplay.common.Logger
mLogger
private Presenter
mPresenter
private boolean
mConnected
private android.hardware.usb.UsbAccessory
mAccessory
private UsbAccessoryStreamTransport
mTransport
private DisplaySourceService
mDisplaySourceService
Constructors Summary
Methods Summary
private voidconnect(android.hardware.usb.UsbAccessory accessory)

        if (!isSink(accessory)) {
            mLogger.log("Not connecting to USB accessory because it is not an accessory display sink: "
                    + accessory);
            return;
        }

        if (mConnected) {
            disconnect();
        }

        // Check whether we have permission to access the accessory.
        if (!mUsbManager.hasPermission(accessory)) {
            mLogger.log("Prompting the user for access to the accessory.");
            Intent intent = new Intent(ACTION_USB_ACCESSORY_PERMISSION);
            intent.setPackage(getPackageName());
            PendingIntent pendingIntent = PendingIntent.getBroadcast(
                    this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
            mUsbManager.requestPermission(accessory, pendingIntent);
            return;
        }

        // Open the accessory.
        ParcelFileDescriptor fd = mUsbManager.openAccessory(accessory);
        if (fd == null) {
            mLogger.logError("Could not obtain accessory connection.");
            return;
        }

        // All set.
        mLogger.log("Connected.");
        mConnected = true;
        mAccessory = accessory;
        mTransport = new UsbAccessoryStreamTransport(mLogger, fd);
        startServices();
        mTransport.startReading();
    
private voiddisconnect()

        mLogger.log("Disconnecting from accessory: " + mAccessory);
        stopServices();

        mLogger.log("Disconnected.");
        mConnected = false;
        mAccessory = null;
        if (mTransport != null) {
            mTransport.close();
            mTransport = null;
        }
    
private static booleanisSink(android.hardware.usb.UsbAccessory accessory)

        return MANUFACTURER.equals(accessory.getManufacturer())
                && MODEL.equals(accessory.getModel());
    
private voidonAccessoryAttached(android.hardware.usb.UsbAccessory accessory)

        mLogger.log("USB accessory attached: " + accessory);
        if (!mConnected) {
            connect(accessory);
        }
    
private voidonAccessoryDetached(android.hardware.usb.UsbAccessory accessory)

        mLogger.log("USB accessory detached: " + accessory);
        if (mConnected && accessory.equals(mAccessory)) {
            disconnect();
        }
    
protected voidonCreate(android.os.Bundle savedInstanceState)


    
        
        super.onCreate(savedInstanceState);

        mUsbManager = (UsbManager)getSystemService(Context.USB_SERVICE);

        setContentView(R.layout.source_activity);

        mLogTextView = (TextView) findViewById(R.id.logTextView);
        mLogTextView.setMovementMethod(ScrollingMovementMethod.getInstance());
        mLogger = new TextLogger();
        mPresenter = new Presenter();

        mLogger.log("Waiting for accessory display sink to be attached to USB...");

        IntentFilter filter = new IntentFilter();
        filter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
        filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
        filter.addAction(ACTION_USB_ACCESSORY_PERMISSION);
        mReceiver = new AccessoryReceiver();
        registerReceiver(mReceiver, filter);

        Intent intent = getIntent();
        if (intent.getAction().equals(UsbManager.ACTION_USB_ACCESSORY_ATTACHED)) {
            UsbAccessory accessory =
                    (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
            if (accessory != null) {
                onAccessoryAttached(accessory);
            }
        } else {
            UsbAccessory[] accessories = mUsbManager.getAccessoryList();
            if (accessories != null) {
                for (UsbAccessory accessory : accessories) {
                    onAccessoryAttached(accessory);
                }
            }
        }
    
protected voidonDestroy()

        super.onDestroy();

        unregisterReceiver(mReceiver);
    
protected voidonPause()

        super.onPause();
    
protected voidonResume()

        super.onResume();

        //new DemoPresentation(this, getWindowManager().getDefaultDisplay()).show();
    
private voidstartServices()

        mDisplaySourceService = new DisplaySourceService(this, mTransport, mPresenter);
        mDisplaySourceService.start();
    
private voidstopServices()

        if (mDisplaySourceService != null) {
            mDisplaySourceService.stop();
            mDisplaySourceService = null;
        }