diff options
Diffstat (limited to 'core/java/com')
| -rw-r--r-- | core/java/com/android/internal/util/AsyncChannel.java | 209 | ||||
| -rw-r--r-- | core/java/com/android/internal/util/IState.java (renamed from core/java/com/android/internal/util/HierarchicalState.java) | 51 | ||||
| -rw-r--r-- | core/java/com/android/internal/util/ProcessedMessages.java | 198 | ||||
| -rw-r--r-- | core/java/com/android/internal/util/Protocol.java | 15 | ||||
| -rw-r--r-- | core/java/com/android/internal/util/State.java | 74 | ||||
| -rw-r--r-- | core/java/com/android/internal/util/StateMachine.java (renamed from core/java/com/android/internal/util/HierarchicalStateMachine.java) | 429 | ||||
| -rwxr-xr-x[-rw-r--r--] | core/java/com/google/android/mms/pdu/PduParser.java | 74 | ||||
| -rw-r--r-- | core/java/com/google/android/mms/util/PduCache.java | 2 |
8 files changed, 608 insertions, 444 deletions
diff --git a/core/java/com/android/internal/util/AsyncChannel.java b/core/java/com/android/internal/util/AsyncChannel.java index 101dd91..3973344 100644 --- a/core/java/com/android/internal/util/AsyncChannel.java +++ b/core/java/com/android/internal/util/AsyncChannel.java @@ -44,16 +44,16 @@ import java.util.Stack; * In this usage model there is no need for the destination to * use the connect methods. The typical sequence of operations is:</p> *<ol> - * <li>Client calls AsyncChannel#connect</li> - * <li>Client receives CMD_CHANNEL_HALF_CONNECTED from AsyncChannel</li> + * <li>Client calls AsyncChannel#connectSync or Asynchronously:</li> + * <ol>For an asynchronous half connection client calls AsyncChannel#connect.</ol> + * <li>Client receives CMD_CHANNEL_HALF_CONNECTED from AsyncChannel</li> + * </ol> * <li><code>comm-loop:</code></li> - * <li>Client calls AsyncChannel#sendMessage(msgX)</li> - * <li>Server receives and processes msgX</li> - * <li>Server optionally calls AsyncChannel#replyToMessage(msgY) - * and if sent Client receives and processes msgY</li> + * <li>Client calls AsyncChannel#sendMessage</li> + * <li>Server processes messages and optionally replies using AsyncChannel#replyToMessage * <li>Loop to <code>comm-loop</code> until done</li> - * <li>When done Client calls {@link AsyncChannel#disconnect(int)}</li> - * <li>Client receives CMD_CHANNEL_DISCONNECTED from AsyncChannel</li> + * <li>When done Client calls {@link AsyncChannel#disconnect}</li> + * <li>Client/Server receives CMD_CHANNEL_DISCONNECTED from AsyncChannel</li> *</ol> *<br/> * <p>A second usage model is where the server/destination needs to know @@ -62,21 +62,26 @@ import java.util.Stack; * different state for each client. In this model the server will also * use the connect methods. The typical sequence of operation is:</p> *<ol> - * <li>Client calls AsyncChannel#connect</li> - * <li>Client receives CMD_CHANNEL_HALF_CONNECTED from AsyncChannel</li> - * <li>Client calls AsyncChannel#sendMessage(CMD_CHANNEL_FULL_CONNECTION)</li> + * <li>Client calls AsyncChannel#fullyConnectSync or Asynchronously:<li> + * <ol>For an asynchronous full connection it calls AsyncChannel#connect</li> + * <li>Client receives CMD_CHANNEL_HALF_CONNECTED from AsyncChannel</li> + * <li>Client calls AsyncChannel#sendMessage(CMD_CHANNEL_FULL_CONNECTION)</li> + * </ol> * <li>Server receives CMD_CHANNEL_FULL_CONNECTION</li> - * <li>Server calls AsyncChannel#connect</li> - * <li>Server receives CMD_CHANNEL_HALF_CONNECTED from AsyncChannel</li> + * <li>Server calls AsyncChannel#connected</li> * <li>Server sends AsyncChannel#sendMessage(CMD_CHANNEL_FULLY_CONNECTED)</li> * <li>Client receives CMD_CHANNEL_FULLY_CONNECTED</li> * <li><code>comm-loop:</code></li> * <li>Client/Server uses AsyncChannel#sendMessage/replyToMessage * to communicate and perform work</li> * <li>Loop to <code>comm-loop</code> until done</li> - * <li>When done Client/Server calls {@link AsyncChannel#disconnect(int)}</li> + * <li>When done Client/Server calls {@link AsyncChannel#disconnect}</li> * <li>Client/Server receives CMD_CHANNEL_DISCONNECTED from AsyncChannel</li> *</ol> + * + * TODO: Consider simplifying where we have connect and fullyConnect with only one response + * message RSP_CHANNEL_CONNECT instead of two, CMD_CHANNEL_HALF_CONNECTED and + * CMD_CHANNEL_FULLY_CONNECTED. We'd also change CMD_CHANNEL_FULL_CONNECTION to REQ_CHANNEL_CONNECT. */ public class AsyncChannel { /** Log tag */ @@ -85,6 +90,8 @@ public class AsyncChannel { /** Enable to turn on debugging */ private static final boolean DBG = false; + private static final int BASE = Protocol.BASE_SYSTEM_ASYNC_CHANNEL; + /** * Command sent when the channel is half connected. Half connected * means that the channel can be used to send commends to the destination @@ -98,7 +105,7 @@ public class AsyncChannel { * msg.obj == the AsyncChannel * msg.replyTo == dstMessenger if successful */ - public static final int CMD_CHANNEL_HALF_CONNECTED = -1; + public static final int CMD_CHANNEL_HALF_CONNECTED = BASE + 0; /** * Command typically sent when after receiving the CMD_CHANNEL_HALF_CONNECTED. @@ -107,7 +114,7 @@ public class AsyncChannel { * * msg.replyTo = srcMessenger. */ - public static final int CMD_CHANNEL_FULL_CONNECTION = -2; + public static final int CMD_CHANNEL_FULL_CONNECTION = BASE + 1; /** * Command typically sent after the destination receives a CMD_CHANNEL_FULL_CONNECTION. @@ -115,31 +122,33 @@ public class AsyncChannel { * * msg.arg1 == 0 : Accept connection * : All other values signify the destination rejected the connection - * and {@link AsyncChannel#disconnect(int)} would typically be called. + * and {@link AsyncChannel#disconnect} would typically be called. */ - public static final int CMD_CHANNEL_FULLY_CONNECTED = -3; + public static final int CMD_CHANNEL_FULLY_CONNECTED = BASE + 2; /** * Command 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(int)} + * 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_DISCONNECT = -4; + public static final int CMD_CHANNEL_DISCONNECT = BASE + 3; /** * Command 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. */ - public static final int CMD_CHANNEL_DISCONNECTED = -5; + public static final int CMD_CHANNEL_DISCONNECTED = BASE + 4; /** Successful status always 0, !0 is an unsuccessful status */ public static final int STATUS_SUCCESSFUL = 0; @@ -147,6 +156,12 @@ public class AsyncChannel { /** Error attempting to bind on a connect */ public static final int STATUS_BINDING_UNSUCCESSFUL = 1; + /** Error attempting to send a message */ + public static final int STATUS_SEND_UNSUCCESSFUL = 2; + + /** CMD_FULLY_CONNECTED refused because a connection already exists*/ + public static final int STATUS_FULL_CONNECTION_REFUSED_ALREADY_CONNECTED = 3; + /** Service connection */ private AsyncChannelConnection mConnection; @@ -169,9 +184,7 @@ public class AsyncChannel { } /** - * Connect handler to named package/class. - * - * Sends a CMD_CHANNEL_HALF_CONNECTED message to srcHandler when complete. + * Connect handler to named package/class synchronously. * * @param srcContext is the context of the source * @param srcHandler is the hander to receive CONNECTED & DISCONNECTED @@ -179,8 +192,10 @@ public class AsyncChannel { * @param dstPackageName is the destination package name * @param dstClassName is the fully qualified class name (i.e. contains * package name) + * + * @return STATUS_SUCCESSFUL on success any other value is an error. */ - private void connectSrcHandlerToPackage( + public int connectSrcHandlerToPackageSync( Context srcContext, Handler srcHandler, String dstPackageName, String dstClassName) { if (DBG) log("connect srcHandler to dst Package & class E"); @@ -202,11 +217,61 @@ public class AsyncChannel { Intent intent = new Intent(Intent.ACTION_MAIN); intent.setClassName(dstPackageName, dstClassName); boolean result = srcContext.bindService(intent, mConnection, Context.BIND_AUTO_CREATE); - if (!result) { - replyHalfConnected(STATUS_BINDING_UNSUCCESSFUL); - } - if (DBG) log("connect srcHandler to dst Package & class X result=" + result); + return result ? STATUS_SUCCESSFUL : STATUS_BINDING_UNSUCCESSFUL; + } + + /** + * Connect a handler to Messenger synchronously. + * + * @param srcContext is the context of the source + * @param srcHandler is the hander to receive CONNECTED & DISCONNECTED + * messages + * @param dstMessenger is the hander to send messages to. + * + * @return STATUS_SUCCESSFUL on success any other value is an error. + */ + public int connectSync(Context srcContext, Handler srcHandler, Messenger dstMessenger) { + 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; + } + + /** + * connect two local Handlers synchronously. + * + * @param srcContext is the context of the source + * @param srcHandler is the hander to receive CONNECTED & DISCONNECTED + * messages + * @param dstHandler is the hander to send messages to. + * + * @return STATUS_SUCCESSFUL on success any other value is an error. + */ + public int connectSync(Context srcContext, Handler srcHandler, Handler dstHandler) { + return connectSync(srcContext, srcHandler, new Messenger(dstHandler)); + } + + /** + * Fully connect two local Handlers synchronously. + * + * @param srcContext is the context of the source + * @param srcHandler is the hander to receive CONNECTED & DISCONNECTED + * messages + * @param dstHandler is the hander to send messages to. + * + * @return STATUS_SUCCESSFUL on success any other value is an error. + */ + public int fullyConnectSync(Context srcContext, Handler srcHandler, Handler dstHandler) { + int status = connectSync(srcContext, srcHandler, dstHandler); + if (status == STATUS_SUCCESSFUL) { + Message response = sendMessageSynchronously(CMD_CHANNEL_FULL_CONNECTION); + status = response.arg1; + } + return status; } /** @@ -241,8 +306,11 @@ public class AsyncChannel { mDstClassName = dstClassName; } + @Override public void run() { - connectSrcHandlerToPackage(mSrcCtx, mSrcHdlr, mDstPackageName, mDstClassName); + int result = connectSrcHandlerToPackageSync(mSrcCtx, mSrcHdlr, mDstPackageName, + mDstClassName); + replyHalfConnected(result); } } @@ -281,6 +349,28 @@ public class AsyncChannel { public void connect(Context srcContext, Handler srcHandler, Messenger dstMessenger) { 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"); + } + + /** + * 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. + * + * @param srcContext + * @param srcHandler + * @param dstMessenger + */ + public void connected(Context srcContext, Handler srcHandler, Messenger dstMessenger) { + if (DBG) log("connected srcHandler to the dstMessenger E"); + // Initialize source fields mSrcContext = srcContext; mSrcHandler = srcHandler; @@ -289,21 +379,12 @@ public class AsyncChannel { // Initialize destination fields mDstMessenger = dstMessenger; - if (DBG) log("tell source we are half connected"); - - // Tell source we are half connected - replyHalfConnected(STATUS_SUCCESSFUL); - - if (DBG) log("connect srcHandler to the dstMessenger X"); + if (DBG) log("connected srcHandler to the dstMessenger X"); } /** * Connect two local Handlers. * - * Sends a CMD_CHANNEL_HALF_CONNECTED message to srcHandler when complete. - * msg.arg1 = status - * msg.obj = the AsyncChannel - * * @param srcContext is the context of the source * @param srcHandler is the hander to receive CONNECTED & DISCONNECTED * messages @@ -331,6 +412,7 @@ public class AsyncChannel { * To close the connection call when handler receives CMD_CHANNEL_DISCONNECTED */ public void disconnected() { + mSrcContext = null; mSrcHandler = null; mSrcMessenger = null; mDstMessenger = null; @@ -341,15 +423,11 @@ public class AsyncChannel { * Disconnect */ public void disconnect() { - if (mConnection != null) { + if ((mConnection != null) && (mSrcContext != null)) { mSrcContext.unbindService(mConnection); } if (mSrcHandler != null) { - Message msg = mSrcHandler.obtainMessage(CMD_CHANNEL_DISCONNECTED); - msg.arg1 = STATUS_SUCCESSFUL; - msg.obj = this; - msg.replyTo = mDstMessenger; - mSrcHandler.sendMessage(msg); + replyDisconnected(STATUS_SUCCESSFUL); } } @@ -363,7 +441,7 @@ public class AsyncChannel { try { mDstMessenger.send(msg); } catch (RemoteException e) { - log("TODO: handle sendMessage RemoteException" + e); + replyDisconnected(STATUS_SEND_UNSUCCESSFUL); } } @@ -444,6 +522,7 @@ public class AsyncChannel { */ public void replyToMessage(Message srcMsg, Message dstMsg) { try { + dstMsg.replyTo = mSrcMessenger; srcMsg.replyTo.send(dstMsg); } catch (RemoteException e) { log("TODO: handle replyToMessage RemoteException" + e); @@ -694,10 +773,14 @@ public class AsyncChannel { private static Message sendMessageSynchronously(Messenger dstMessenger, Message msg) { SyncMessenger sm = SyncMessenger.obtain(); try { - msg.replyTo = sm.mMessenger; - dstMessenger.send(msg); - synchronized (sm.mHandler.mLockObject) { - sm.mHandler.mLockObject.wait(); + if (dstMessenger != null && msg != null) { + msg.replyTo = sm.mMessenger; + synchronized (sm.mHandler.mLockObject) { + dstMessenger.send(msg); + sm.mHandler.mLockObject.wait(); + } + } else { + sm.mHandler.mResultMsg = null; } } catch (InterruptedException e) { sm.mHandler.mResultMsg = null; @@ -712,6 +795,7 @@ public class AsyncChannel { /** * Reply to the src handler that we're half connected. + * see: CMD_CHANNEL_HALF_CONNECTED for message contents * * @param status to be stored in msg.arg1 */ @@ -724,23 +808,36 @@ public class AsyncChannel { } /** + * Reply to the src handler that we are disconnected + * see: CMD_CHANNEL_DISCONNECTED for message contents + * + * @param status to be stored in msg.arg1 + */ + private void replyDisconnected(int status) { + Message msg = mSrcHandler.obtainMessage(CMD_CHANNEL_DISCONNECTED); + msg.arg1 = status; + msg.obj = this; + msg.replyTo = mDstMessenger; + mSrcHandler.sendMessage(msg); + } + + + /** * ServiceConnection to receive call backs. */ class AsyncChannelConnection implements ServiceConnection { AsyncChannelConnection() { } + @Override public void onServiceConnected(ComponentName className, IBinder service) { mDstMessenger = new Messenger(service); replyHalfConnected(STATUS_SUCCESSFUL); } + @Override public void onServiceDisconnected(ComponentName className) { - Message msg = mSrcHandler.obtainMessage(CMD_CHANNEL_DISCONNECTED); - msg.arg1 = STATUS_SUCCESSFUL; - msg.obj = AsyncChannel.this; - msg.replyTo = mDstMessenger; - mSrcHandler.sendMessage(msg); + replyDisconnected(STATUS_SUCCESSFUL); } } diff --git a/core/java/com/android/internal/util/HierarchicalState.java b/core/java/com/android/internal/util/IState.java index b37f46c..056f8e9 100644 --- a/core/java/com/android/internal/util/HierarchicalState.java +++ b/core/java/com/android/internal/util/IState.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2009 The Android Open Source Project + * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,21 +21,29 @@ import android.os.Message; /** * {@hide} * - * The class for implementing states in a HierarchicalStateMachine + * The interface for implementing states in a {@link StateMachine} */ -public class HierarchicalState { +public interface IState { /** - * Constructor + * Returned by processMessage to indicate the the message was processed. */ - protected HierarchicalState() { - } + static final boolean HANDLED = true; + + /** + * Returned by processMessage to indicate the the message was NOT processed. + */ + static final boolean NOT_HANDLED = false; /** * Called when a state is entered. */ - protected void enter() { - } + void enter(); + + /** + * Called when a state is exited. + */ + void exit(); /** * Called when a message is to be processed by the @@ -49,28 +57,15 @@ public class HierarchicalState { * be processed until this routine returns. * * @param msg to process - * @return true if processing has completed and false - * if the parent state's processMessage should - * be invoked. - */ - protected boolean processMessage(Message msg) { - return false; - } - - /** - * Called when a state is exited. + * @return HANDLED if processing has completed and NOT_HANDLED + * if the message wasn't processed. */ - protected void exit() { - } + boolean processMessage(Message msg); /** - * @return name of state, but default returns the states - * class name. An instance name would be better but requiring - * it seems unnecessary. + * Name of State for debugging purposes. + * + * @return name of state. */ - public String getName() { - String name = getClass().getName(); - int lastDollar = name.lastIndexOf('$'); - return name.substring(lastDollar + 1); - } + String getName(); } diff --git a/core/java/com/android/internal/util/ProcessedMessages.java b/core/java/com/android/internal/util/ProcessedMessages.java deleted file mode 100644 index 244474e..0000000 --- a/core/java/com/android/internal/util/ProcessedMessages.java +++ /dev/null @@ -1,198 +0,0 @@ -/** - * Copyright (C) 2009 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.internal.util; - -import android.os.Message; - -import java.util.Vector; - -/** - * {@hide} - * - * A list of messages recently processed by the state machine. - * - * The class maintains a list of messages that have been most - * recently processed. The list is finite and may be set in the - * constructor or by calling setSize. The public interface also - * includes size which returns the number of recent messages, - * count which is the number of message processed since the - * the last setSize, get which returns a processed message and - * add which adds a processed messaged. - */ -public class ProcessedMessages { - - public static final int DEFAULT_SIZE = 20; - - /** - * The information maintained for a processed message. - */ - public class Info { - private int what; - private HierarchicalState state; - private HierarchicalState orgState; - - /** - * Constructor - * @param message - * @param state that handled the message - * @param orgState is the first state the received the message but - * did not processes the message. - */ - Info(Message message, HierarchicalState state, HierarchicalState orgState) { - update(message, state, orgState); - } - - /** - * Update the information in the record. - * @param state that handled the message - * @param orgState is the first state the received the message but - * did not processes the message. - */ - public void update(Message message, HierarchicalState state, HierarchicalState orgState) { - this.what = message.what; - this.state = state; - this.orgState = orgState; - } - - /** - * @return the command that was executing - */ - public int getWhat() { - return what; - } - - /** - * @return the state that handled this message - */ - public HierarchicalState getState() { - return state; - } - - /** - * @return the original state that received the message. - */ - public HierarchicalState getOriginalState() { - return orgState; - } - - /** - * @return as string - */ - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("what="); - sb.append(what); - sb.append(" state="); - sb.append(cn(state)); - sb.append(" orgState="); - sb.append(cn(orgState)); - return sb.toString(); - } - - /** - * @return an objects class name - */ - private String cn(Object n) { - if (n == null) { - return "null"; - } else { - String name = n.getClass().getName(); - int lastDollar = name.lastIndexOf('$'); - return name.substring(lastDollar + 1); - } - } - } - - private Vector<Info> mMessages = new Vector<Info>(); - private int mMaxSize = DEFAULT_SIZE; - private int mOldestIndex = 0; - private int mCount = 0; - - /** - * Constructor - */ - ProcessedMessages() { - } - - ProcessedMessages(int maxSize) { - setSize(maxSize); - } - - /** - * Set size of messages to maintain and clears all current messages. - * - * @param maxSize number of messages to maintain at anyone time. - */ - void setSize(int maxSize) { - mMaxSize = maxSize; - mCount = 0; - mMessages.clear(); - } - - /** - * @return the number of recent messages. - */ - int size() { - return mMessages.size(); - } - - /** - * @return the total number of messages processed since size was set. - */ - int count() { - return mCount; - } - - /** - * @return the information on a particular record. 0 is the oldest - * record and size()-1 is the newest record. If the index is to - * large null is returned. - */ - Info get(int index) { - int nextIndex = mOldestIndex + index; - if (nextIndex >= mMaxSize) { - nextIndex -= mMaxSize; - } - if (nextIndex >= size()) { - return null; - } else { - return mMessages.get(nextIndex); - } - } - - /** - * Add a processed message. - * - * @param message - * @param state that handled the message - * @param orgState is the first state the received the message but - * did not processes the message. - */ - void add(Message message, HierarchicalState state, HierarchicalState orgState) { - mCount += 1; - if (mMessages.size() < mMaxSize) { - mMessages.add(new Info(message, state, orgState)); - } else { - Info info = mMessages.get(mOldestIndex); - mOldestIndex += 1; - if (mOldestIndex >= mMaxSize) { - mOldestIndex = 0; - } - info.update(message, state, orgState); - } - } -} diff --git a/core/java/com/android/internal/util/Protocol.java b/core/java/com/android/internal/util/Protocol.java index 2689f09..2e7ec58 100644 --- a/core/java/com/android/internal/util/Protocol.java +++ b/core/java/com/android/internal/util/Protocol.java @@ -29,9 +29,18 @@ package com.android.internal.util; * {@hide} */ public class Protocol { - public static final int MAX_MESSAGE = 0x0000FFFF; + public static final int MAX_MESSAGE = 0x0000FFFF; + + /** Base reserved for system */ + public static final int BASE_SYSTEM_RESERVED = 0x00010000; + public static final int BASE_SYSTEM_ASYNC_CHANNEL = 0x00011000; + + /** Non system protocols */ + public static final int BASE_WIFI = 0x00020000; + public static final int BASE_DHCP = 0x00030000; + public static final int BASE_DATA_CONNECTION = 0x00040000; + public static final int BASE_DATA_CONNECTION_AC = 0x00041000; + public static final int BASE_DATA_CONNECTION_TRACKER = 0x00050000; - public static final int BASE_WIFI = 0x00010000; - public static final int BASE_DHCP = 0x00020000; //TODO: define all used protocols } diff --git a/core/java/com/android/internal/util/State.java b/core/java/com/android/internal/util/State.java new file mode 100644 index 0000000..3eadff5 --- /dev/null +++ b/core/java/com/android/internal/util/State.java @@ -0,0 +1,74 @@ +/** + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.internal.util; + +import android.os.Message; + +/** + * {@hide} + * + * The class for implementing states in a StateMachine + */ +public class State implements IState { + + /** + * Constructor + */ + protected State() { + } + + /* (non-Javadoc) + * @see com.android.internal.util.IState#enter() + */ + @Override + public void enter() { + } + + /* (non-Javadoc) + * @see com.android.internal.util.IState#exit() + */ + @Override + public void exit() { + } + + /* (non-Javadoc) + * @see com.android.internal.util.IState#processMessage(android.os.Message) + */ + @Override + public boolean processMessage(Message msg) { + return false; + } + + /** + * Name of State for debugging purposes. + * + * This default implementation returns the class name, returning + * the instance name would better in cases where a State class + * is used for multiple states. But normally there is one class per + * state and the class name is sufficient and easy to get. You may + * want to provide a setName or some other mechanism for setting + * another name if the class name is not appropriate. + * + * @see com.android.internal.util.IState#processMessage(android.os.Message) + */ + @Override + public String getName() { + String name = getClass().getName(); + int lastDollar = name.lastIndexOf('$'); + return name.substring(lastDollar + 1); + } +} diff --git a/core/java/com/android/internal/util/HierarchicalStateMachine.java b/core/java/com/android/internal/util/StateMachine.java index f43f459..cbe72dd 100644 --- a/core/java/com/android/internal/util/HierarchicalStateMachine.java +++ b/core/java/com/android/internal/util/StateMachine.java @@ -24,14 +24,15 @@ import android.util.Log; import java.util.ArrayList; import java.util.HashMap; +import java.util.Vector; /** * {@hide} * - * <p>A hierarchical state machine is a state machine which processes messages + * <p>The state machine defined here is a hierarchical state machine which processes messages * and can have states arranged hierarchically.</p> * - * <p>A state is a <code>HierarchicalState</code> object and must implement + * <p>A state is a <code>State</code> object and must implement * <code>processMessage</code> and optionally <code>enter/exit/getName</code>. * The enter/exit methods are equivalent to the construction and destruction * in Object Oriented programming and are used to perform initialization and @@ -75,7 +76,7 @@ import java.util.HashMap; * will exit the current state and its parent and then exit from the controlling thread * and no further messages will be processed.</p> * - * <p>In addition to <code>processMessage</code> each <code>HierarchicalState</code> has + * <p>In addition to <code>processMessage</code> each <code>State</code> has * an <code>enter</code> method and <code>exit</exit> method which may be overridden.</p> * * <p>Since the states are arranged in a hierarchy transitioning to a new state @@ -121,11 +122,11 @@ import java.util.HashMap; * mS4.enter. The new list of active states is mP0, mP1, mS2 and mS4. So * when the next message is received mS4.processMessage will be invoked.</p> * - * <p>Now for some concrete examples, here is the canonical HelloWorld as an HSM. + * <p>Now for some concrete examples, here is the canonical HelloWorld as a state machine. * It responds with "Hello World" being printed to the log for every message.</p> <code> -class HelloWorld extends HierarchicalStateMachine { - Hsm1(String name) { +class HelloWorld extends StateMachine { + HelloWorld(String name) { super(name); addState(mState1); setInitialState(mState1); @@ -137,7 +138,7 @@ class HelloWorld extends HierarchicalStateMachine { return hw; } - class State1 extends HierarchicalState { + class State1 extends State { @Override public boolean processMessage(Message message) { Log.d(TAG, "Hello World"); return HANDLED; @@ -220,9 +221,9 @@ state mP2 { } } </code> - * <p>The implementation is below and also in HierarchicalStateMachineTest:</p> + * <p>The implementation is below and also in StateMachineTest:</p> <code> -class Hsm1 extends HierarchicalStateMachine { +class Hsm1 extends StateMachine { private static final String TAG = "hsm1"; public static final int CMD_1 = 1; @@ -254,7 +255,7 @@ class Hsm1 extends HierarchicalStateMachine { Log.d(TAG, "ctor X"); } - class P1 extends HierarchicalState { + class P1 extends State { @Override public void enter() { Log.d(TAG, "mP1.enter"); } @@ -281,7 +282,7 @@ class Hsm1 extends HierarchicalStateMachine { } } - class S1 extends HierarchicalState { + class S1 extends State { @Override public void enter() { Log.d(TAG, "mS1.enter"); } @@ -301,7 +302,7 @@ class Hsm1 extends HierarchicalStateMachine { } } - class S2 extends HierarchicalState { + class S2 extends State { @Override public void enter() { Log.d(TAG, "mS2.enter"); } @@ -329,7 +330,7 @@ class Hsm1 extends HierarchicalStateMachine { } } - class P2 extends HierarchicalState { + class P2 extends State { @Override public void enter() { Log.d(TAG, "mP2.enter"); sendMessage(obtainMessage(CMD_5)); @@ -408,16 +409,16 @@ D/hsm1 ( 1999): mP2.exit D/hsm1 ( 1999): halting </code> */ -public class HierarchicalStateMachine { +public class StateMachine { - private static final String TAG = "HierarchicalStateMachine"; + private static final String TAG = "StateMachine"; private String mName; /** Message.what value when quitting */ - public static final int HSM_QUIT_CMD = -1; + public static final int SM_QUIT_CMD = -1; /** Message.what value when initializing */ - public static final int HSM_INIT_CMD = -1; + public static final int SM_INIT_CMD = -1; /** * Convenience constant that maybe returned by processMessage @@ -433,7 +434,181 @@ public class HierarchicalStateMachine { */ public static final boolean NOT_HANDLED = false; - private static class HsmHandler extends Handler { + /** + * {@hide} + * + * The information maintained for a processed message. + */ + public static class ProcessedMessageInfo { + private int what; + private State state; + private State orgState; + + /** + * Constructor + * @param message + * @param state that handled the message + * @param orgState is the first state the received the message but + * did not processes the message. + */ + ProcessedMessageInfo(Message message, State state, State orgState) { + update(message, state, orgState); + } + + /** + * Update the information in the record. + * @param state that handled the message + * @param orgState is the first state the received the message but + * did not processes the message. + */ + public void update(Message message, State state, State orgState) { + this.what = message.what; + this.state = state; + this.orgState = orgState; + } + + /** + * @return the command that was executing + */ + public int getWhat() { + return what; + } + + /** + * @return the state that handled this message + */ + public State getState() { + return state; + } + + /** + * @return the original state that received the message. + */ + public State getOriginalState() { + return orgState; + } + + /** + * @return as string + */ + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("what="); + sb.append(what); + sb.append(" state="); + sb.append(cn(state)); + sb.append(" orgState="); + sb.append(cn(orgState)); + return sb.toString(); + } + + /** + * @return an objects class name + */ + private String cn(Object n) { + if (n == null) { + return "null"; + } else { + String name = n.getClass().getName(); + int lastDollar = name.lastIndexOf('$'); + return name.substring(lastDollar + 1); + } + } + } + + /** + * A list of messages recently processed by the state machine. + * + * The class maintains a list of messages that have been most + * recently processed. The list is finite and may be set in the + * constructor or by calling setSize. The public interface also + * includes size which returns the number of recent messages, + * count which is the number of message processed since the + * the last setSize, get which returns a processed message and + * add which adds a processed messaged. + */ + private static class ProcessedMessages { + + private static final int DEFAULT_SIZE = 20; + + private Vector<ProcessedMessageInfo> mMessages = new Vector<ProcessedMessageInfo>(); + private int mMaxSize = DEFAULT_SIZE; + private int mOldestIndex = 0; + private int mCount = 0; + + /** + * Constructor + */ + ProcessedMessages() { + } + + /** + * Set size of messages to maintain and clears all current messages. + * + * @param maxSize number of messages to maintain at anyone time. + */ + void setSize(int maxSize) { + mMaxSize = maxSize; + mCount = 0; + mMessages.clear(); + } + + /** + * @return the number of recent messages. + */ + int size() { + return mMessages.size(); + } + + /** + * @return the total number of messages processed since size was set. + */ + int count() { + return mCount; + } + + /** + * @return the information on a particular record. 0 is the oldest + * record and size()-1 is the newest record. If the index is to + * large null is returned. + */ + ProcessedMessageInfo get(int index) { + int nextIndex = mOldestIndex + index; + if (nextIndex >= mMaxSize) { + nextIndex -= mMaxSize; + } + if (nextIndex >= size()) { + return null; + } else { + return mMessages.get(nextIndex); + } + } + + /** + * Add a processed message. + * + * @param message + * @param state that handled the message + * @param orgState is the first state the received the message but + * did not processes the message. + */ + void add(Message message, State state, State orgState) { + mCount += 1; + if (mMessages.size() < mMaxSize) { + mMessages.add(new ProcessedMessageInfo(message, state, orgState)); + } else { + ProcessedMessageInfo pmi = mMessages.get(mOldestIndex); + mOldestIndex += 1; + if (mOldestIndex >= mMaxSize) { + mOldestIndex = 0; + } + pmi.update(message, state, orgState); + } + } + } + + private static class SmHandler extends Handler { /** The debug flag */ private boolean mDbg = false; @@ -441,9 +616,6 @@ public class HierarchicalStateMachine { /** The quit object */ private static final Object mQuitObj = new Object(); - /** The initialization message */ - private static final Message mInitMsg = null; - /** The current message */ private Message mMsg; @@ -471,8 +643,8 @@ public class HierarchicalStateMachine { /** State used when state machine is quitting */ private QuittingState mQuittingState = new QuittingState(); - /** Reference to the HierarchicalStateMachine */ - private HierarchicalStateMachine mHsm; + /** Reference to the StateMachine */ + private StateMachine mSm; /** * Information about a state. @@ -480,7 +652,7 @@ public class HierarchicalStateMachine { */ private class StateInfo { /** The state */ - HierarchicalState state; + State state; /** The parent of this state, null if there is no parent */ StateInfo parentStateInfo; @@ -500,14 +672,14 @@ public class HierarchicalStateMachine { } /** The map of all of the states in the state machine */ - private HashMap<HierarchicalState, StateInfo> mStateInfo = - new HashMap<HierarchicalState, StateInfo>(); + private HashMap<State, StateInfo> mStateInfo = + new HashMap<State, StateInfo>(); /** The initial state that will process the first message */ - private HierarchicalState mInitialState; + private State mInitialState; /** The destination state when transitionTo has been invoked */ - private HierarchicalState mDestState; + private State mDestState; /** The list of deferred messages */ private ArrayList<Message> mDeferredMessages = new ArrayList<Message>(); @@ -515,10 +687,10 @@ public class HierarchicalStateMachine { /** * State entered when transitionToHaltingState is called. */ - private class HaltingState extends HierarchicalState { + private class HaltingState extends State { @Override public boolean processMessage(Message msg) { - mHsm.haltedProcessMessage(msg); + mSm.haltedProcessMessage(msg); return true; } } @@ -526,7 +698,7 @@ public class HierarchicalStateMachine { /** * State entered when a valid quit message is handled. */ - private class QuittingState extends HierarchicalState { + private class QuittingState extends State { @Override public boolean processMessage(Message msg) { return NOT_HANDLED; @@ -573,7 +745,7 @@ public class HierarchicalStateMachine { * the appropriate states. We loop on this to allow * enter and exit methods to use transitionTo. */ - HierarchicalState destState = null; + State destState = null; while (mDestState != null) { if (mDbg) Log.d(TAG, "handleMessage: new destination call exit"); @@ -613,10 +785,11 @@ public class HierarchicalStateMachine { /** * We are quitting so ignore all messages. */ - mHsm.quitting(); - if (mHsm.mHsmThread != null) { - // If we made the thread then quit looper + mSm.quitting(); + if (mSm.mSmThread != null) { + // If we made the thread then quit looper which stops the thread. getLooper().quit(); + mSm.mSmThread = null; } } else if (destState == mHaltingState) { /** @@ -624,7 +797,7 @@ public class HierarchicalStateMachine { * state. All subsequent messages will be processed in * in the halting state which invokes haltedProcessMessage(msg); */ - mHsm.halting(); + mSm.halting(); } } } @@ -660,7 +833,7 @@ public class HierarchicalStateMachine { * starting at the first entry. */ mIsConstructionCompleted = true; - mMsg = obtainMessage(HSM_INIT_CMD); + mMsg = obtainMessage(SM_INIT_CMD); invokeEnterMethods(0); /** @@ -690,7 +863,7 @@ public class HierarchicalStateMachine { /** * No parents left so it's not handled */ - mHsm.unhandledMessage(msg); + mSm.unhandledMessage(msg); if (isQuit(msg)) { transitionTo(mQuittingState); } @@ -705,7 +878,7 @@ public class HierarchicalStateMachine { * Record that we processed the message */ if (curStateInfo != null) { - HierarchicalState orgState = mStateStack[mStateStackTopIndex].state; + State orgState = mStateStack[mStateStackTopIndex].state; mProcessedMessages.add(msg, curStateInfo.state, orgState); } else { mProcessedMessages.add(msg, null, null); @@ -719,7 +892,7 @@ public class HierarchicalStateMachine { private final void invokeExitMethods(StateInfo commonStateInfo) { while ((mStateStackTopIndex >= 0) && (mStateStack[mStateStackTopIndex] != commonStateInfo)) { - HierarchicalState curState = mStateStack[mStateStackTopIndex].state; + State curState = mStateStack[mStateStackTopIndex].state; if (mDbg) Log.d(TAG, "invokeExitMethods: " + curState.getName()); curState.exit(); mStateStack[mStateStackTopIndex].active = false; @@ -761,7 +934,7 @@ public class HierarchicalStateMachine { * reversing the order of the items on the temporary stack as * they are moved. * - * @return index into mStateState where entering needs to start + * @return index into mStateStack where entering needs to start */ private final int moveTempStateStackToStateStack() { int startingIndex = mStateStackTopIndex + 1; @@ -794,7 +967,7 @@ public class HierarchicalStateMachine { * @return StateInfo of the common ancestor for the destState and * current state or null if there is no common parent. */ - private final StateInfo setupTempStateStackWithStatesToEnter(HierarchicalState destState) { + private final StateInfo setupTempStateStackWithStatesToEnter(State destState) { /** * Search up the parent list of the destination state for an active * state. Use a do while() loop as the destState must always be entered @@ -846,7 +1019,7 @@ public class HierarchicalStateMachine { /** * @return current state */ - private final HierarchicalState getCurrentState() { + private final IState getCurrentState() { return mStateStack[mStateStackTopIndex].state; } @@ -859,7 +1032,7 @@ public class HierarchicalStateMachine { * @param parent the parent of state * @return stateInfo for this state */ - private final StateInfo addState(HierarchicalState state, HierarchicalState parent) { + private final StateInfo addState(State state, State parent) { if (mDbg) { Log.d(TAG, "addStateInternal: E state=" + state.getName() + ",parent=" + ((parent == null) ? "" : parent.getName())); @@ -894,29 +1067,29 @@ public class HierarchicalStateMachine { * Constructor * * @param looper for dispatching messages - * @param hsm the hierarchical state machine + * @param sm the hierarchical state machine */ - private HsmHandler(Looper looper, HierarchicalStateMachine hsm) { + private SmHandler(Looper looper, StateMachine sm) { super(looper); - mHsm = hsm; + mSm = sm; addState(mHaltingState, null); addState(mQuittingState, null); } - /** @see HierarchicalStateMachine#setInitialState(HierarchicalState) */ - private final void setInitialState(HierarchicalState initialState) { + /** @see StateMachine#setInitialState(State) */ + private final void setInitialState(State initialState) { if (mDbg) Log.d(TAG, "setInitialState: initialState" + initialState.getName()); mInitialState = initialState; } - /** @see HierarchicalStateMachine#transitionTo(HierarchicalState) */ - private final void transitionTo(HierarchicalState destState) { - if (mDbg) Log.d(TAG, "StateMachine.transitionTo EX destState" + destState.getName()); - mDestState = destState; + /** @see StateMachine#transitionTo(IState) */ + private final void transitionTo(IState destState) { + mDestState = (State) destState; + if (mDbg) Log.d(TAG, "StateMachine.transitionTo EX destState" + mDestState.getName()); } - /** @see HierarchicalStateMachine#deferMessage(Message) */ + /** @see StateMachine#deferMessage(Message) */ private final void deferMessage(Message msg) { if (mDbg) Log.d(TAG, "deferMessage: msg=" + msg.what); @@ -927,51 +1100,51 @@ public class HierarchicalStateMachine { mDeferredMessages.add(newMsg); } - /** @see HierarchicalStateMachine#deferMessage(Message) */ + /** @see StateMachine#deferMessage(Message) */ private final void quit() { if (mDbg) Log.d(TAG, "quit:"); - sendMessage(obtainMessage(HSM_QUIT_CMD, mQuitObj)); + sendMessage(obtainMessage(SM_QUIT_CMD, mQuitObj)); } - /** @see HierarchicalStateMachine#isQuit(Message) */ + /** @see StateMachine#isQuit(Message) */ private final boolean isQuit(Message msg) { - return (msg.what == HSM_QUIT_CMD) && (msg.obj == mQuitObj); + return (msg.what == SM_QUIT_CMD) && (msg.obj == mQuitObj); } - /** @see HierarchicalStateMachine#isDbg() */ + /** @see StateMachine#isDbg() */ private final boolean isDbg() { return mDbg; } - /** @see HierarchicalStateMachine#setDbg(boolean) */ + /** @see StateMachine#setDbg(boolean) */ private final void setDbg(boolean dbg) { mDbg = dbg; } - /** @see HierarchicalStateMachine#setProcessedMessagesSize(int) */ + /** @see StateMachine#setProcessedMessagesSize(int) */ private final void setProcessedMessagesSize(int maxSize) { mProcessedMessages.setSize(maxSize); } - /** @see HierarchicalStateMachine#getProcessedMessagesSize() */ + /** @see StateMachine#getProcessedMessagesSize() */ private final int getProcessedMessagesSize() { return mProcessedMessages.size(); } - /** @see HierarchicalStateMachine#getProcessedMessagesCount() */ + /** @see StateMachine#getProcessedMessagesCount() */ private final int getProcessedMessagesCount() { return mProcessedMessages.count(); } - /** @see HierarchicalStateMachine#getProcessedMessage(int) */ - private final ProcessedMessages.Info getProcessedMessage(int index) { + /** @see StateMachine#getProcessedMessageInfo(int) */ + private final ProcessedMessageInfo getProcessedMessageInfo(int index) { return mProcessedMessages.get(index); } } - private HsmHandler mHsmHandler; - private HandlerThread mHsmThread; + private SmHandler mSmHandler; + private HandlerThread mSmThread; /** * Initialize. @@ -981,28 +1154,28 @@ public class HierarchicalStateMachine { */ private void initStateMachine(String name, Looper looper) { mName = name; - mHsmHandler = new HsmHandler(looper, this); + mSmHandler = new SmHandler(looper, this); } /** - * Constructor creates an HSM with its own thread. + * Constructor creates a StateMachine with its own thread. * * @param name of the state machine */ - protected HierarchicalStateMachine(String name) { - mHsmThread = new HandlerThread(name); - mHsmThread.start(); - Looper looper = mHsmThread.getLooper(); + protected StateMachine(String name) { + mSmThread = new HandlerThread(name); + mSmThread.start(); + Looper looper = mSmThread.getLooper(); initStateMachine(name, looper); } /** - * Constructor creates an HSMStateMachine using the looper. + * Constructor creates an StateMachine using the looper. * * @param name of the state machine */ - protected HierarchicalStateMachine(String name, Looper looper) { + protected StateMachine(String name, Looper looper) { initStateMachine(name, looper); } @@ -1011,30 +1184,30 @@ public class HierarchicalStateMachine { * @param state the state to add * @param parent the parent of state */ - protected final void addState(HierarchicalState state, HierarchicalState parent) { - mHsmHandler.addState(state, parent); + protected final void addState(State state, State parent) { + mSmHandler.addState(state, parent); } /** * @return current message */ protected final Message getCurrentMessage() { - return mHsmHandler.getCurrentMessage(); + return mSmHandler.getCurrentMessage(); } /** * @return current state */ - protected final HierarchicalState getCurrentState() { - return mHsmHandler.getCurrentState(); + protected final IState getCurrentState() { + return mSmHandler.getCurrentState(); } /** * Add a new state to the state machine, parent will be null * @param state to add */ - protected final void addState(HierarchicalState state) { - mHsmHandler.addState(state, null); + protected final void addState(State state) { + mSmHandler.addState(state, null); } /** @@ -1043,8 +1216,8 @@ public class HierarchicalStateMachine { * * @param initialState is the state which will receive the first message. */ - protected final void setInitialState(HierarchicalState initialState) { - mHsmHandler.setInitialState(initialState); + protected final void setInitialState(State initialState) { + mSmHandler.setInitialState(initialState); } /** @@ -1055,8 +1228,8 @@ public class HierarchicalStateMachine { * * @param destState will be the state that receives the next message. */ - protected final void transitionTo(HierarchicalState destState) { - mHsmHandler.transitionTo(destState); + protected final void transitionTo(IState destState) { + mSmHandler.transitionTo(destState); } /** @@ -1067,7 +1240,7 @@ public class HierarchicalStateMachine { * will be called. */ protected final void transitionToHaltingState() { - mHsmHandler.transitionTo(mHsmHandler.mHaltingState); + mSmHandler.transitionTo(mSmHandler.mHaltingState); } /** @@ -1080,7 +1253,7 @@ public class HierarchicalStateMachine { * @param msg is deferred until the next transition. */ protected final void deferMessage(Message msg) { - mHsmHandler.deferMessage(msg); + mSmHandler.deferMessage(msg); } @@ -1090,9 +1263,7 @@ public class HierarchicalStateMachine { * @param msg that couldn't be handled. */ protected void unhandledMessage(Message msg) { - if (false) { - Log.e(TAG, mName + " - unhandledMessage: msg.what=" + msg.what); - } + if (mSmHandler.mDbg) Log.e(TAG, mName + " - unhandledMessage: msg.what=" + msg.what); } /** @@ -1103,16 +1274,18 @@ public class HierarchicalStateMachine { } /** - * Called after the message that called transitionToHalting - * is called and should be overridden by StateMachine's that - * call transitionToHalting. + * This will be called once after handling a message that called + * transitionToHalting. All subsequent messages will invoke + * {@link StateMachine#haltedProcessMessage(Message)} */ protected void halting() { } /** - * Called after the quitting message was NOT handled and - * just before the quit actually occurs. + * This will be called once after a quit message that was NOT handled by + * the derived StateMachine. The StateMachine will stop and any subsequent messages will be + * ignored. In addition, if this StateMachine created the thread, the thread will + * be stopped after this method returns. */ protected void quitting() { } @@ -1130,35 +1303,35 @@ public class HierarchicalStateMachine { * @param maxSize number of messages to maintain at anyone time. */ public final void setProcessedMessagesSize(int maxSize) { - mHsmHandler.setProcessedMessagesSize(maxSize); + mSmHandler.setProcessedMessagesSize(maxSize); } /** * @return number of messages processed */ public final int getProcessedMessagesSize() { - return mHsmHandler.getProcessedMessagesSize(); + return mSmHandler.getProcessedMessagesSize(); } /** * @return the total number of messages processed */ public final int getProcessedMessagesCount() { - return mHsmHandler.getProcessedMessagesCount(); + return mSmHandler.getProcessedMessagesCount(); } /** - * @return a processed message + * @return a processed message information */ - public final ProcessedMessages.Info getProcessedMessage(int index) { - return mHsmHandler.getProcessedMessage(index); + public final ProcessedMessageInfo getProcessedMessageInfo(int index) { + return mSmHandler.getProcessedMessageInfo(index); } /** * @return Handler */ public final Handler getHandler() { - return mHsmHandler; + return mSmHandler; } /** @@ -1168,7 +1341,7 @@ public class HierarchicalStateMachine { */ public final Message obtainMessage() { - return Message.obtain(mHsmHandler); + return Message.obtain(mSmHandler); } /** @@ -1178,7 +1351,7 @@ public class HierarchicalStateMachine { * @return message */ public final Message obtainMessage(int what) { - return Message.obtain(mHsmHandler, what); + return Message.obtain(mSmHandler, what); } /** @@ -1191,7 +1364,7 @@ public class HierarchicalStateMachine { */ public final Message obtainMessage(int what, Object obj) { - return Message.obtain(mHsmHandler, what, obj); + return Message.obtain(mSmHandler, what, obj); } /** @@ -1205,7 +1378,7 @@ public class HierarchicalStateMachine { */ public final Message obtainMessage(int what, int arg1, int arg2) { - return Message.obtain(mHsmHandler, what, arg1, arg2); + return Message.obtain(mSmHandler, what, arg1, arg2); } /** @@ -1220,107 +1393,107 @@ public class HierarchicalStateMachine { */ public final Message obtainMessage(int what, int arg1, int arg2, Object obj) { - return Message.obtain(mHsmHandler, what, arg1, arg2, obj); + return Message.obtain(mSmHandler, what, arg1, arg2, obj); } /** * Enqueue a message to this state machine. */ public final void sendMessage(int what) { - mHsmHandler.sendMessage(obtainMessage(what)); + mSmHandler.sendMessage(obtainMessage(what)); } /** * Enqueue a message to this state machine. */ public final void sendMessage(int what, Object obj) { - mHsmHandler.sendMessage(obtainMessage(what,obj)); + mSmHandler.sendMessage(obtainMessage(what,obj)); } /** * Enqueue a message to this state machine. */ public final void sendMessage(Message msg) { - mHsmHandler.sendMessage(msg); + mSmHandler.sendMessage(msg); } /** * Enqueue a message to this state machine after a delay. */ public final void sendMessageDelayed(int what, long delayMillis) { - mHsmHandler.sendMessageDelayed(obtainMessage(what), delayMillis); + mSmHandler.sendMessageDelayed(obtainMessage(what), delayMillis); } /** * Enqueue a message to this state machine after a delay. */ public final void sendMessageDelayed(int what, Object obj, long delayMillis) { - mHsmHandler.sendMessageDelayed(obtainMessage(what, obj), delayMillis); + mSmHandler.sendMessageDelayed(obtainMessage(what, obj), delayMillis); } /** * Enqueue a message to this state machine after a delay. */ public final void sendMessageDelayed(Message msg, long delayMillis) { - mHsmHandler.sendMessageDelayed(msg, delayMillis); + mSmHandler.sendMessageDelayed(msg, delayMillis); } /** * Enqueue a message to the front of the queue for this state machine. - * Protected, may only be called by instances of HierarchicalStateMachine. + * Protected, may only be called by instances of StateMachine. */ protected final void sendMessageAtFrontOfQueue(int what, Object obj) { - mHsmHandler.sendMessageAtFrontOfQueue(obtainMessage(what, obj)); + mSmHandler.sendMessageAtFrontOfQueue(obtainMessage(what, obj)); } /** * Enqueue a message to the front of the queue for this state machine. - * Protected, may only be called by instances of HierarchicalStateMachine. + * Protected, may only be called by instances of StateMachine. */ protected final void sendMessageAtFrontOfQueue(int what) { - mHsmHandler.sendMessageAtFrontOfQueue(obtainMessage(what)); + mSmHandler.sendMessageAtFrontOfQueue(obtainMessage(what)); } /** * Enqueue a message to the front of the queue for this state machine. - * Protected, may only be called by instances of HierarchicalStateMachine. + * Protected, may only be called by instances of StateMachine. */ protected final void sendMessageAtFrontOfQueue(Message msg) { - mHsmHandler.sendMessageAtFrontOfQueue(msg); + mSmHandler.sendMessageAtFrontOfQueue(msg); } /** * Removes a message from the message queue. - * Protected, may only be called by instances of HierarchicalStateMachine. + * Protected, may only be called by instances of StateMachine. */ protected final void removeMessages(int what) { - mHsmHandler.removeMessages(what); + mSmHandler.removeMessages(what); } /** * Conditionally quit the looper and stop execution. * - * This sends the HSM_QUIT_MSG to the state machine and + * This sends the SM_QUIT_MSG to the state machine and * if not handled by any state's processMessage then the * state machine will be stopped and no further messages * will be processed. */ public final void quit() { - mHsmHandler.quit(); + mSmHandler.quit(); } /** * @return ture if msg is quit */ protected final boolean isQuit(Message msg) { - return mHsmHandler.isQuit(msg); + return mSmHandler.isQuit(msg); } /** * @return if debugging is enabled */ public boolean isDbg() { - return mHsmHandler.isDbg(); + return mSmHandler.isDbg(); } /** @@ -1329,7 +1502,7 @@ public class HierarchicalStateMachine { * @param dbg is true to enable debugging. */ public void setDbg(boolean dbg) { - mHsmHandler.setDbg(dbg); + mSmHandler.setDbg(dbg); } /** @@ -1337,6 +1510,6 @@ public class HierarchicalStateMachine { */ public void start() { /** Send the complete construction message */ - mHsmHandler.completeConstruction(); + mSmHandler.completeConstruction(); } } diff --git a/core/java/com/google/android/mms/pdu/PduParser.java b/core/java/com/google/android/mms/pdu/PduParser.java index 8edfe52..3f185aa 100644..100755 --- a/core/java/com/google/android/mms/pdu/PduParser.java +++ b/core/java/com/google/android/mms/pdu/PduParser.java @@ -29,6 +29,8 @@ import java.io.UnsupportedEncodingException; import java.util.Arrays; import java.util.HashMap; +import android.content.res.Resources; + public class PduParser { /** * The next are WAP values defined in WSP specification. @@ -1557,43 +1559,55 @@ public class PduParser { * Attachment = <Octet 129> * Inline = <Octet 130> */ - int len = parseValueLength(pduDataStream); - pduDataStream.mark(1); - int thisStartPos = pduDataStream.available(); - int thisEndPos = 0; - int value = pduDataStream.read(); - - if (value == PduPart.P_DISPOSITION_FROM_DATA ) { - part.setContentDisposition(PduPart.DISPOSITION_FROM_DATA); - } else if (value == PduPart.P_DISPOSITION_ATTACHMENT) { - part.setContentDisposition(PduPart.DISPOSITION_ATTACHMENT); - } else if (value == PduPart.P_DISPOSITION_INLINE) { - part.setContentDisposition(PduPart.DISPOSITION_INLINE); - } else { - pduDataStream.reset(); - /* Token-text */ - part.setContentDisposition(parseWapString(pduDataStream, TYPE_TEXT_STRING)); - } - /* get filename parameter and skip other parameters */ - thisEndPos = pduDataStream.available(); - if (thisStartPos - thisEndPos < len) { - value = pduDataStream.read(); - if (value == PduPart.P_FILENAME) { //filename is text-string - part.setFilename(parseWapString(pduDataStream, TYPE_TEXT_STRING)); + /* + * some carrier mmsc servers do not support content_disposition + * field correctly + */ + boolean contentDisposition = Resources.getSystem().getBoolean(com + .android.internal.R.bool.config_mms_content_disposition_support); + + if (contentDisposition) { + int len = parseValueLength(pduDataStream); + pduDataStream.mark(1); + int thisStartPos = pduDataStream.available(); + int thisEndPos = 0; + int value = pduDataStream.read(); + + if (value == PduPart.P_DISPOSITION_FROM_DATA ) { + part.setContentDisposition(PduPart.DISPOSITION_FROM_DATA); + } else if (value == PduPart.P_DISPOSITION_ATTACHMENT) { + part.setContentDisposition(PduPart.DISPOSITION_ATTACHMENT); + } else if (value == PduPart.P_DISPOSITION_INLINE) { + part.setContentDisposition(PduPart.DISPOSITION_INLINE); + } else { + pduDataStream.reset(); + /* Token-text */ + part.setContentDisposition(parseWapString(pduDataStream + , TYPE_TEXT_STRING)); } - /* skip other parameters */ + /* get filename parameter and skip other parameters */ thisEndPos = pduDataStream.available(); if (thisStartPos - thisEndPos < len) { - int last = len - (thisStartPos - thisEndPos); - byte[] temp = new byte[last]; - pduDataStream.read(temp, 0, last); + value = pduDataStream.read(); + if (value == PduPart.P_FILENAME) { //filename is text-string + part.setFilename(parseWapString(pduDataStream + , TYPE_TEXT_STRING)); + } + + /* skip other parameters */ + thisEndPos = pduDataStream.available(); + if (thisStartPos - thisEndPos < len) { + int last = len - (thisStartPos - thisEndPos); + byte[] temp = new byte[last]; + pduDataStream.read(temp, 0, last); + } } - } - tempPos = pduDataStream.available(); - lastLen = length - (startPos - tempPos); + tempPos = pduDataStream.available(); + lastLen = length - (startPos - tempPos); + } break; default: if (LOCAL_LOGV) { diff --git a/core/java/com/google/android/mms/util/PduCache.java b/core/java/com/google/android/mms/util/PduCache.java index 7c3fad7..866ca1e 100644 --- a/core/java/com/google/android/mms/util/PduCache.java +++ b/core/java/com/google/android/mms/util/PduCache.java @@ -235,7 +235,7 @@ public final class PduCache extends AbstractCache<Uri, PduCacheEntry> { } private void removeFromMessageBoxes(Uri key, PduCacheEntry entry) { - HashSet<Uri> msgBox = mThreads.get(entry.getMessageBox()); + HashSet<Uri> msgBox = mThreads.get(Long.valueOf(entry.getMessageBox())); if (msgBox != null) { msgBox.remove(key); } |
