Fields Summary |
---|
private static final String | TAGLog tag |
private static final boolean | DBGEnable to turn on debugging |
private static final int | BASE |
public static final int | CMD_CHANNEL_HALF_CONNECTEDCommand sent when the channel is half connected. Half connected
means that the channel can be used to send commends to the destination
but the destination is unaware that the channel exists. The first
command sent to the destination is typically CMD_CHANNEL_FULL_CONNECTION if
it is desired to establish a long term connection, but any command maybe
sent.
msg.arg1 == 0 : STATUS_SUCCESSFUL
1 : STATUS_BINDING_UNSUCCESSFUL
msg.obj == the AsyncChannel
msg.replyTo == dstMessenger if successful |
public static final int | CMD_CHANNEL_FULL_CONNECTIONCommand typically sent when after receiving the CMD_CHANNEL_HALF_CONNECTED.
This is used to initiate a long term connection with the destination and
typically the destination will reply with CMD_CHANNEL_FULLY_CONNECTED.
msg.replyTo = srcMessenger. |
public static final int | CMD_CHANNEL_FULLY_CONNECTEDCommand typically sent after the destination receives a CMD_CHANNEL_FULL_CONNECTION.
This signifies the acceptance or rejection of the channel by the sender.
msg.arg1 == 0 : Accept connection
: All other values signify the destination rejected the connection
and {@link AsyncChannel#disconnect} would typically be called. |
public static final int | CMD_CHANNEL_DISCONNECTCommand sent when one side or the other wishes to disconnect. The sender
may or may not be able to receive a reply depending upon the protocol and
the state of the connection. The receiver should call {@link AsyncChannel#disconnect}
to close its side of the channel and it will receive a CMD_CHANNEL_DISCONNECTED
when the channel is closed.
msg.replyTo = messenger that is disconnecting |
public static final int | CMD_CHANNEL_DISCONNECTEDCommand sent when the channel becomes disconnected. This is sent when the
channel is forcibly disconnected by the system or as a reply to CMD_CHANNEL_DISCONNECT.
msg.arg1 == 0 : STATUS_SUCCESSFUL
1 : STATUS_BINDING_UNSUCCESSFUL
2 : STATUS_SEND_UNSUCCESSFUL
: All other values signify failure and the channel state is indeterminate
msg.obj == the AsyncChannel
msg.replyTo = messenger disconnecting or null if it was never connected. |
private static final int | CMD_TO_STRING_COUNT |
private static String[] | sCmdToString |
public static final int | STATUS_SUCCESSFULSuccessful status always 0, !0 is an unsuccessful status |
public static final int | STATUS_BINDING_UNSUCCESSFULError attempting to bind on a connect |
public static final int | STATUS_SEND_UNSUCCESSFULError attempting to send a message |
public static final int | STATUS_FULL_CONNECTION_REFUSED_ALREADY_CONNECTEDCMD_FULLY_CONNECTED refused because a connection already exists |
public static final int | STATUS_REMOTE_DISCONNECTIONError indicating abnormal termination of destination messenger |
private AsyncChannelConnection | mConnectionService connection |
private android.content.Context | mSrcContextContext for source |
private android.os.Handler | mSrcHandlerHandler for source |
private android.os.Messenger | mSrcMessengerMessenger for source |
private android.os.Messenger | mDstMessengerMessenger for destination |
private DeathMonitor | mDeathMonitorDeath Monitor for destination messenger |
Methods Summary |
---|
protected static java.lang.String | cmdToString(int cmd)
sCmdToString[CMD_CHANNEL_HALF_CONNECTED - BASE] = "CMD_CHANNEL_HALF_CONNECTED";
sCmdToString[CMD_CHANNEL_FULL_CONNECTION - BASE] = "CMD_CHANNEL_FULL_CONNECTION";
sCmdToString[CMD_CHANNEL_FULLY_CONNECTED - BASE] = "CMD_CHANNEL_FULLY_CONNECTED";
sCmdToString[CMD_CHANNEL_DISCONNECT - BASE] = "CMD_CHANNEL_DISCONNECT";
sCmdToString[CMD_CHANNEL_DISCONNECTED - BASE] = "CMD_CHANNEL_DISCONNECTED";
cmd -= BASE;
if ((cmd >= 0) && (cmd < sCmdToString.length)) {
return sCmdToString[cmd];
} else {
return null;
}
|
public void | connect(android.content.Context srcContext, android.os.Handler srcHandler, android.os.Handler dstHandler)Connect two local Handlers.
connect(srcContext, srcHandler, new Messenger(dstHandler));
|
public void | connect(AsyncService srcAsyncService, android.os.Messenger dstMessenger)Connect service and messenger.
Sends a CMD_CHANNEL_HALF_CONNECTED message to srcAsyncService when complete.
msg.arg1 = status
msg.obj = the AsyncChannel
connect(srcAsyncService, srcAsyncService.getHandler(), dstMessenger);
|
public void | connect(android.content.Context srcContext, android.os.Handler srcHandler, java.lang.String dstPackageName, java.lang.String dstClassName)Connect handler to named package/class.
Sends a CMD_CHANNEL_HALF_CONNECTED message to srcHandler when complete.
msg.arg1 = status
msg.obj = the AsyncChannel
if (DBG) log("connect srcHandler to dst Package & class E");
final class ConnectAsync implements Runnable {
Context mSrcCtx;
Handler mSrcHdlr;
String mDstPackageName;
String mDstClassName;
ConnectAsync(Context srcContext, Handler srcHandler, String dstPackageName,
String dstClassName) {
mSrcCtx = srcContext;
mSrcHdlr = srcHandler;
mDstPackageName = dstPackageName;
mDstClassName = dstClassName;
}
@Override
public void run() {
int result = connectSrcHandlerToPackageSync(mSrcCtx, mSrcHdlr, mDstPackageName,
mDstClassName);
replyHalfConnected(result);
}
}
ConnectAsync ca = new ConnectAsync(srcContext, srcHandler, dstPackageName, dstClassName);
new Thread(ca).start();
if (DBG) log("connect srcHandler to dst Package & class X");
|
public void | connect(android.content.Context srcContext, android.os.Handler srcHandler, java.lang.Class klass)Connect handler to a class
Sends a CMD_CHANNEL_HALF_CONNECTED message to srcHandler when complete.
msg.arg1 = status
msg.obj = the AsyncChannel
connect(srcContext, srcHandler, klass.getPackage().getName(), klass.getName());
|
public void | connect(android.content.Context srcContext, android.os.Handler srcHandler, android.os.Messenger dstMessenger)Connect handler and messenger.
Sends a CMD_CHANNEL_HALF_CONNECTED message to srcHandler when complete.
msg.arg1 = status
msg.obj = the AsyncChannel
if (DBG) log("connect srcHandler to the dstMessenger E");
// We are connected
connected(srcContext, srcHandler, dstMessenger);
// Tell source we are half connected
replyHalfConnected(STATUS_SUCCESSFUL);
if (DBG) log("connect srcHandler to the dstMessenger X");
|
public int | connectSrcHandlerToPackageSync(android.content.Context srcContext, android.os.Handler srcHandler, java.lang.String dstPackageName, java.lang.String dstClassName)Connect handler to named package/class synchronously.
if (DBG) log("connect srcHandler to dst Package & class E");
mConnection = new AsyncChannelConnection();
/* Initialize the source information */
mSrcContext = srcContext;
mSrcHandler = srcHandler;
mSrcMessenger = new Messenger(srcHandler);
/*
* Initialize destination information to null they will
* be initialized when the AsyncChannelConnection#onServiceConnected
* is called
*/
mDstMessenger = null;
/* Send intent to create the connection */
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName(dstPackageName, dstClassName);
boolean result = srcContext.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
if (DBG) log("connect srcHandler to dst Package & class X result=" + result);
return result ? STATUS_SUCCESSFUL : STATUS_BINDING_UNSUCCESSFUL;
|
public int | connectSync(android.content.Context srcContext, android.os.Handler srcHandler, android.os.Messenger dstMessenger)Connect a handler to Messenger synchronously.
if (DBG) log("halfConnectSync srcHandler to the dstMessenger E");
// We are connected
connected(srcContext, srcHandler, dstMessenger);
if (DBG) log("halfConnectSync srcHandler to the dstMessenger X");
return STATUS_SUCCESSFUL;
|
public int | connectSync(android.content.Context srcContext, android.os.Handler srcHandler, android.os.Handler dstHandler)connect two local Handlers synchronously.
return connectSync(srcContext, srcHandler, new Messenger(dstHandler));
|
public void | connected(android.content.Context srcContext, android.os.Handler srcHandler, android.os.Messenger dstMessenger)Connect handler to messenger. This method is typically called
when a server receives a CMD_CHANNEL_FULL_CONNECTION request
and initializes the internal instance variables to allow communication
with the dstMessenger.
if (DBG) log("connected srcHandler to the dstMessenger E");
// Initialize source fields
mSrcContext = srcContext;
mSrcHandler = srcHandler;
mSrcMessenger = new Messenger(mSrcHandler);
// Initialize destination fields
mDstMessenger = dstMessenger;
if (DBG) log("connected srcHandler to the dstMessenger X");
|
public void | disconnect()Disconnect
if ((mConnection != null) && (mSrcContext != null)) {
mSrcContext.unbindService(mConnection);
mConnection = null;
}
try {
// Send the DISCONNECTED, although it may not be received
// but its the best we can do.
Message msg = Message.obtain();
msg.what = CMD_CHANNEL_DISCONNECTED;
msg.replyTo = mSrcMessenger;
mDstMessenger.send(msg);
} catch(Exception e) {
}
// Tell source we're disconnected.
if (mSrcHandler != null) {
replyDisconnected(STATUS_SUCCESSFUL);
mSrcHandler = null;
}
// Unlink only when bindService isn't used
if (mConnection == null && mDstMessenger != null && mDeathMonitor!= null) {
mDstMessenger.getBinder().unlinkToDeath(mDeathMonitor, 0);
mDeathMonitor = null;
}
|
public void | disconnected()To close the connection call when handler receives CMD_CHANNEL_DISCONNECTED
mSrcContext = null;
mSrcHandler = null;
mSrcMessenger = null;
mDstMessenger = null;
mDeathMonitor = null;
mConnection = null;
|
public int | fullyConnectSync(android.content.Context srcContext, android.os.Handler srcHandler, android.os.Handler dstHandler)Fully connect two local Handlers synchronously.
int status = connectSync(srcContext, srcHandler, dstHandler);
if (status == STATUS_SUCCESSFUL) {
Message response = sendMessageSynchronously(CMD_CHANNEL_FULL_CONNECTION);
status = response.arg1;
}
return status;
|
private static void | log(java.lang.String s)Log the string.
Slog.d(TAG, s);
|
private void | replyDisconnected(int status)Reply to the src handler that we are disconnected
see: CMD_CHANNEL_DISCONNECTED for message contents
Message msg = mSrcHandler.obtainMessage(CMD_CHANNEL_DISCONNECTED);
msg.arg1 = status;
msg.obj = this;
msg.replyTo = mDstMessenger;
mSrcHandler.sendMessage(msg);
|
private void | replyHalfConnected(int status)Reply to the src handler that we're half connected.
see: CMD_CHANNEL_HALF_CONNECTED for message contents
Message msg = mSrcHandler.obtainMessage(CMD_CHANNEL_HALF_CONNECTED);
msg.arg1 = status;
msg.obj = this;
msg.replyTo = mDstMessenger;
/*
* Link to death only when bindService isn't used.
*/
if (mConnection == null) {
mDeathMonitor = new DeathMonitor();
try {
mDstMessenger.getBinder().linkToDeath(mDeathMonitor, 0);
} catch (RemoteException e) {
mDeathMonitor = null;
// Override status to indicate failure
msg.arg1 = STATUS_BINDING_UNSUCCESSFUL;
}
}
mSrcHandler.sendMessage(msg);
|
public void | replyToMessage(android.os.Message srcMsg, android.os.Message dstMsg)Reply to srcMsg sending dstMsg
try {
dstMsg.replyTo = mSrcMessenger;
srcMsg.replyTo.send(dstMsg);
} catch (RemoteException e) {
log("TODO: handle replyToMessage RemoteException" + e);
e.printStackTrace();
}
|
public void | replyToMessage(android.os.Message srcMsg, int what)Reply to srcMsg
Message msg = Message.obtain();
msg.what = what;
replyToMessage(srcMsg, msg);
|
public void | replyToMessage(android.os.Message srcMsg, int what, int arg1)Reply to srcMsg
Message msg = Message.obtain();
msg.what = what;
msg.arg1 = arg1;
replyToMessage(srcMsg, msg);
|
public void | replyToMessage(android.os.Message srcMsg, int what, int arg1, int arg2)Reply to srcMsg
Message msg = Message.obtain();
msg.what = what;
msg.arg1 = arg1;
msg.arg2 = arg2;
replyToMessage(srcMsg, msg);
|
public void | replyToMessage(android.os.Message srcMsg, int what, int arg1, int arg2, java.lang.Object obj)Reply to srcMsg
Message msg = Message.obtain();
msg.what = what;
msg.arg1 = arg1;
msg.arg2 = arg2;
msg.obj = obj;
replyToMessage(srcMsg, msg);
|
public void | replyToMessage(android.os.Message srcMsg, int what, java.lang.Object obj)Reply to srcMsg
Message msg = Message.obtain();
msg.what = what;
msg.obj = obj;
replyToMessage(srcMsg, msg);
|
public void | sendMessage(android.os.Message msg)Send a message to the destination handler.
msg.replyTo = mSrcMessenger;
try {
mDstMessenger.send(msg);
} catch (RemoteException e) {
replyDisconnected(STATUS_SEND_UNSUCCESSFUL);
}
|
public void | sendMessage(int what)Send a message to the destination handler
Message msg = Message.obtain();
msg.what = what;
sendMessage(msg);
|
public void | sendMessage(int what, int arg1)Send a message to the destination handler
Message msg = Message.obtain();
msg.what = what;
msg.arg1 = arg1;
sendMessage(msg);
|
public void | sendMessage(int what, int arg1, int arg2)Send a message to the destination handler
Message msg = Message.obtain();
msg.what = what;
msg.arg1 = arg1;
msg.arg2 = arg2;
sendMessage(msg);
|
public void | sendMessage(int what, int arg1, int arg2, java.lang.Object obj)Send a message to the destination handler
Message msg = Message.obtain();
msg.what = what;
msg.arg1 = arg1;
msg.arg2 = arg2;
msg.obj = obj;
sendMessage(msg);
|
public void | sendMessage(int what, java.lang.Object obj)Send a message to the destination handler
Message msg = Message.obtain();
msg.what = what;
msg.obj = obj;
sendMessage(msg);
|
public android.os.Message | sendMessageSynchronously(android.os.Message msg)Send the Message synchronously.
Message resultMsg = SyncMessenger.sendMessageSynchronously(mDstMessenger, msg);
return resultMsg;
|
public android.os.Message | sendMessageSynchronously(int what)Send the Message synchronously.
Message msg = Message.obtain();
msg.what = what;
Message resultMsg = sendMessageSynchronously(msg);
return resultMsg;
|
public android.os.Message | sendMessageSynchronously(int what, int arg1)Send the Message synchronously.
Message msg = Message.obtain();
msg.what = what;
msg.arg1 = arg1;
Message resultMsg = sendMessageSynchronously(msg);
return resultMsg;
|
public android.os.Message | sendMessageSynchronously(int what, int arg1, int arg2)Send the Message synchronously.
Message msg = Message.obtain();
msg.what = what;
msg.arg1 = arg1;
msg.arg2 = arg2;
Message resultMsg = sendMessageSynchronously(msg);
return resultMsg;
|
public android.os.Message | sendMessageSynchronously(int what, int arg1, int arg2, java.lang.Object obj)Send the Message synchronously.
Message msg = Message.obtain();
msg.what = what;
msg.arg1 = arg1;
msg.arg2 = arg2;
msg.obj = obj;
Message resultMsg = sendMessageSynchronously(msg);
return resultMsg;
|
public android.os.Message | sendMessageSynchronously(int what, java.lang.Object obj)Send the Message synchronously.
Message msg = Message.obtain();
msg.what = what;
msg.obj = obj;
Message resultMsg = sendMessageSynchronously(msg);
return resultMsg;
|