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 |
Methods Summary |
---|
private void | connect(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 void | disconnect()
mLogger.log("Disconnecting from accessory: " + mAccessory);
stopServices();
mLogger.log("Disconnected.");
mConnected = false;
mAccessory = null;
if (mTransport != null) {
mTransport.close();
mTransport = null;
}
|
private static boolean | isSink(android.hardware.usb.UsbAccessory accessory)
return MANUFACTURER.equals(accessory.getManufacturer())
&& MODEL.equals(accessory.getModel());
|
private void | onAccessoryAttached(android.hardware.usb.UsbAccessory accessory)
mLogger.log("USB accessory attached: " + accessory);
if (!mConnected) {
connect(accessory);
}
|
private void | onAccessoryDetached(android.hardware.usb.UsbAccessory accessory)
mLogger.log("USB accessory detached: " + accessory);
if (mConnected && accessory.equals(mAccessory)) {
disconnect();
}
|
protected void | onCreate(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 void | onDestroy()
super.onDestroy();
unregisterReceiver(mReceiver);
|
protected void | onPause()
super.onPause();
|
protected void | onResume()
super.onResume();
//new DemoPresentation(this, getWindowManager().getDefaultDisplay()).show();
|
private void | startServices()
mDisplaySourceService = new DisplaySourceService(this, mTransport, mPresenter);
mDisplaySourceService.start();
|
private void | stopServices()
if (mDisplaySourceService != null) {
mDisplaySourceService.stop();
mDisplaySourceService = null;
}
|