Methods Summary |
---|
private static void | checkMessageId(int what)
if (what < 0 || what > 0xffff) {
throw new IllegalArgumentException("message id out of range: " + what);
}
|
private static void | checkServiceId(int service)
if (service < 0 || service > 0xffff) {
throw new IllegalArgumentException("service id out of range: " + service);
}
|
public void | close()Closes the transport.
synchronized (mLock) {
if (mOutputBuffer != null) {
if (mThread == null) {
ioClose();
} else {
// If the thread was started then it will be responsible for
// closing the stream when it quits because it may currently
// be in the process of reading from the stream so we can't simply
// shut it down right now.
mThread.quit();
}
mOutputBuffer = null;
}
}
|
private void | dispatchMessageReceived(int service, int what, java.nio.ByteBuffer content)
final Callback callback;
synchronized (mLock) {
callback = mServices.get(service);
}
if (callback != null) {
callback.onMessageReceived(service, what, content);
} else {
mLogger.log("Discarding message " + what
+ " for unregistered service " + service);
}
|
public android.os.Handler | getHandler()Gets the handler on the transport's thread.
return mHandler;
|
public Logger | getLogger()Gets the logger for debugging.
return mLogger;
|
protected abstract void | ioClose()
|
protected abstract int | ioRead(byte[] buffer, int offset, int count)
|
protected abstract void | ioWrite(byte[] buffer, int offset, int count)
|
public void | registerService(int service, com.android.accessorydisplay.common.Transport$Callback callback)Registers a service and provides a callback to receive messages.
checkServiceId(service);
if (callback == null) {
throw new IllegalArgumentException("callback must not be null");
}
synchronized (mLock) {
mServices.put(service, callback);
}
|
public boolean | sendMessage(int service, int what, java.nio.ByteBuffer content)Sends a message.
checkServiceId(service);
checkMessageId(what);
try {
synchronized (mLock) {
if (mOutputBuffer == null) {
mLogger.logError("Send message failed because transport was closed.");
return false;
}
final byte[] outputArray = mOutputBuffer.array();
final int capacity = mOutputBuffer.capacity();
mOutputBuffer.clear();
mOutputBuffer.putShort((short)service);
mOutputBuffer.putShort((short)what);
if (content == null) {
mOutputBuffer.putInt(0);
} else {
final int contentLimit = content.limit();
int contentPosition = content.position();
int contentRemaining = contentLimit - contentPosition;
if (contentRemaining > Protocol.MAX_CONTENT_SIZE) {
throw new IllegalArgumentException("Message content too large: "
+ contentRemaining + " > " + Protocol.MAX_CONTENT_SIZE);
}
mOutputBuffer.putInt(contentRemaining);
while (contentRemaining != 0) {
final int outputAvailable = capacity - mOutputBuffer.position();
if (contentRemaining <= outputAvailable) {
mOutputBuffer.put(content);
break;
}
content.limit(contentPosition + outputAvailable);
mOutputBuffer.put(content);
content.limit(contentLimit);
ioWrite(outputArray, 0, capacity);
contentPosition += outputAvailable;
contentRemaining -= outputAvailable;
mOutputBuffer.clear();
}
}
ioWrite(outputArray, 0, mOutputBuffer.position());
return true;
}
} catch (IOException ex) {
mLogger.logError("Send message failed: " + ex);
return false;
}
|
public void | startReading()Starts reading messages on a separate thread.
synchronized (mLock) {
if (mOutputBuffer == null) {
throw new IllegalStateException("Transport has been closed");
}
mThread = new ReaderThread();
mThread.start();
}
|
public void | unregisterService(int service)Unregisters a service.
checkServiceId(service);
synchronized (mLock) {
mServices.remove(service);
}
|