2 * Copyright (C) 2009 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.internal.util;
19 import android.os.Handler;
20 import android.os.HandlerThread;
21 import android.os.Looper;
22 import android.os.Message;
23 import android.text.TextUtils;
24 import android.util.Log;
26 import java.io.FileDescriptor;
27 import java.io.PrintWriter;
28 import java.io.StringWriter;
29 import java.util.ArrayList;
30 import java.util.Calendar;
31 import java.util.Collection;
32 import java.util.HashMap;
33 import java.util.Iterator;
34 import java.util.Vector;
39 * <p>The state machine defined here is a hierarchical state machine which processes messages
40 * and can have states arranged hierarchically.</p>
42 * <p>A state is a <code>State</code> object and must implement
43 * <code>processMessage</code> and optionally <code>enter/exit/getName</code>.
44 * The enter/exit methods are equivalent to the construction and destruction
45 * in Object Oriented programming and are used to perform initialization and
46 * cleanup of the state respectively. The <code>getName</code> method returns the
47 * name of the state; the default implementation returns the class name. It may be
48 * desirable to have <code>getName</code> return the the state instance name instead,
49 * in particular if a particular state class has multiple instances.</p>
51 * <p>When a state machine is created, <code>addState</code> is used to build the
52 * hierarchy and <code>setInitialState</code> is used to identify which of these
53 * is the initial state. After construction the programmer calls <code>start</code>
54 * which initializes and starts the state machine. The first action the StateMachine
55 * is to the invoke <code>enter</code> for all of the initial state's hierarchy,
56 * starting at its eldest parent. The calls to enter will be done in the context
57 * of the StateMachine's Handler, not in the context of the call to start, and they
58 * will be invoked before any messages are processed. For example, given the simple
59 * state machine below, mP1.enter will be invoked and then mS1.enter. Finally,
60 * messages sent to the state machine will be processed by the current state;
61 * in our simple state machine below that would initially be mS1.processMessage.</p>
65 mS2 mS1 ----> initial state
67 * <p>After the state machine is created and started, messages are sent to a state
68 * machine using <code>sendMessage</code> and the messages are created using
69 * <code>obtainMessage</code>. When the state machine receives a message the
70 * current state's <code>processMessage</code> is invoked. In the above example
71 * mS1.processMessage will be invoked first. The state may use <code>transitionTo</code>
72 * to change the current state to a new state.</p>
74 * <p>Each state in the state machine may have a zero or one parent states. If
75 * a child state is unable to handle a message it may have the message processed
76 * by its parent by returning false or NOT_HANDLED. If a message is not handled by
77 * a child state or any of its ancestors, <code>unhandledMessage</code> will be invoked
78 * to give one last chance for the state machine to process the message.</p>
80 * <p>When all processing is completed a state machine may choose to call
81 * <code>transitionToHaltingState</code>. When the current <code>processingMessage</code>
82 * returns the state machine will transfer to an internal <code>HaltingState</code>
83 * and invoke <code>halting</code>. Any message subsequently received by the state
84 * machine will cause <code>haltedProcessMessage</code> to be invoked.</p>
86 * <p>If it is desirable to completely stop the state machine call <code>quit</code> or
87 * <code>quitNow</code>. These will call <code>exit</code> of the current state and its parents,
88 * call <code>onQuitting</code> and then exit Thread/Loopers.</p>
90 * <p>In addition to <code>processMessage</code> each <code>State</code> has
91 * an <code>enter</code> method and <code>exit</code> method which may be overridden.</p>
93 * <p>Since the states are arranged in a hierarchy transitioning to a new state
94 * causes current states to be exited and new states to be entered. To determine
95 * the list of states to be entered/exited the common parent closest to
96 * the current state is found. We then exit from the current state and its
97 * parent's up to but not including the common parent state and then enter all
98 * of the new states below the common parent down to the destination state.
99 * If there is no common parent all states are exited and then the new states
102 * <p>Two other methods that states can use are <code>deferMessage</code> and
103 * <code>sendMessageAtFrontOfQueue</code>. The <code>sendMessageAtFrontOfQueue</code> sends
104 * a message but places it on the front of the queue rather than the back. The
105 * <code>deferMessage</code> causes the message to be saved on a list until a
106 * transition is made to a new state. At which time all of the deferred messages
107 * will be put on the front of the state machine queue with the oldest message
108 * at the front. These will then be processed by the new current state before
109 * any other messages that are on the queue or might be added later. Both of
110 * these are protected and may only be invoked from within a state machine.</p>
112 * <p>To illustrate some of these properties we'll use state machine with an 8
113 * state hierarchy:</p>
121 mS3 mS4 mS5 ---> initial state
123 * <p>After starting mS5 the list of active states is mP0, mP1, mS1 and mS5.
124 * So the order of calling processMessage when a message is received is mS5,
125 * mS1, mP1, mP0 assuming each processMessage indicates it can't handle this
126 * message by returning false or NOT_HANDLED.</p>
128 * <p>Now assume mS5.processMessage receives a message it can handle, and during
129 * the handling determines the machine should change states. It could call
130 * transitionTo(mS4) and return true or HANDLED. Immediately after returning from
131 * processMessage the state machine runtime will find the common parent,
132 * which is mP1. It will then call mS5.exit, mS1.exit, mS2.enter and then
133 * mS4.enter. The new list of active states is mP0, mP1, mS2 and mS4. So
134 * when the next message is received mS4.processMessage will be invoked.</p>
136 * <p>Now for some concrete examples, here is the canonical HelloWorld as a state machine.
137 * It responds with "Hello World" being printed to the log for every message.</p>
139 class HelloWorld extends StateMachine {
140 HelloWorld(String name) {
143 setInitialState(mState1);
146 public static HelloWorld makeHelloWorld() {
147 HelloWorld hw = new HelloWorld("hw");
152 class State1 extends State {
153 @Override public boolean processMessage(Message message) {
158 State1 mState1 = new State1();
161 void testHelloWorld() {
162 HelloWorld hw = makeHelloWorld();
163 hw.sendMessage(hw.obtainMessage());
166 * <p>A more interesting state machine is one with four states
167 * with two independent parent states.</p>
173 * <p>Here is a description of this state machine using pseudo code.</p>
176 enter { log("mP1.enter"); }
177 exit { log("mP1.exit"); }
190 state mS1 parent mP1 {
191 enter { log("mS1.enter"); }
192 exit { log("mS1.exit"); }
202 state mS2 parent mP1 {
203 enter { log("mS2.enter"); }
204 exit { log("mS2.exit"); }
224 exit { log("mP2.exit"); }
226 CMD_3, CMD_4 { return HANDLED; }
228 transitionTo(HaltingState);
235 * <p>The implementation is below and also in StateMachineTest:</p>
237 class Hsm1 extends StateMachine {
238 public static final int CMD_1 = 1;
239 public static final int CMD_2 = 2;
240 public static final int CMD_3 = 3;
241 public static final int CMD_4 = 4;
242 public static final int CMD_5 = 5;
244 public static Hsm1 makeHsm1() {
246 Hsm1 sm = new Hsm1("hsm1");
256 // Add states, use indentation to show hierarchy
262 // Set the initial state
263 setInitialState(mS1);
267 class P1 extends State {
268 @Override public void enter() {
271 @Override public boolean processMessage(Message message) {
273 log("mP1.processMessage what=" + message.what);
274 switch(message.what) {
276 // CMD_2 will arrive in mS2 before CMD_3
277 sendMessage(obtainMessage(CMD_3));
278 deferMessage(message);
283 // Any message we don't understand in this state invokes unhandledMessage
284 retVal = NOT_HANDLED;
289 @Override public void exit() {
294 class S1 extends State {
295 @Override public void enter() {
298 @Override public boolean processMessage(Message message) {
299 log("S1.processMessage what=" + message.what);
300 if (message.what == CMD_1) {
301 // Transition to ourself to show that enter/exit is called
305 // Let parent process all other messages
309 @Override public void exit() {
314 class S2 extends State {
315 @Override public void enter() {
318 @Override public boolean processMessage(Message message) {
320 log("mS2.processMessage what=" + message.what);
321 switch(message.what) {
323 sendMessage(obtainMessage(CMD_4));
327 deferMessage(message);
332 retVal = NOT_HANDLED;
337 @Override public void exit() {
342 class P2 extends State {
343 @Override public void enter() {
345 sendMessage(obtainMessage(CMD_5));
347 @Override public boolean processMessage(Message message) {
348 log("P2.processMessage what=" + message.what);
349 switch(message.what) {
355 transitionToHaltingState();
360 @Override public void exit() {
368 synchronized (this) {
379 * <p>If this is executed by sending two messages CMD_1 and CMD_2
380 * (Note the synchronize is only needed because we use hsm.wait())</p>
382 Hsm1 hsm = makeHsm1();
384 hsm.sendMessage(obtainMessage(hsm.CMD_1));
385 hsm.sendMessage(obtainMessage(hsm.CMD_2));
387 // wait for the messages to be handled
389 } catch (InterruptedException e) {
390 loge("exception while waiting " + e.getMessage());
394 * <p>The output is:</p>
396 D/hsm1 ( 1999): makeHsm1 E
397 D/hsm1 ( 1999): ctor E
398 D/hsm1 ( 1999): ctor X
399 D/hsm1 ( 1999): mP1.enter
400 D/hsm1 ( 1999): mS1.enter
401 D/hsm1 ( 1999): makeHsm1 X
402 D/hsm1 ( 1999): mS1.processMessage what=1
403 D/hsm1 ( 1999): mS1.exit
404 D/hsm1 ( 1999): mS1.enter
405 D/hsm1 ( 1999): mS1.processMessage what=2
406 D/hsm1 ( 1999): mP1.processMessage what=2
407 D/hsm1 ( 1999): mS1.exit
408 D/hsm1 ( 1999): mS2.enter
409 D/hsm1 ( 1999): mS2.processMessage what=2
410 D/hsm1 ( 1999): mS2.processMessage what=3
411 D/hsm1 ( 1999): mS2.exit
412 D/hsm1 ( 1999): mP1.exit
413 D/hsm1 ( 1999): mP2.enter
414 D/hsm1 ( 1999): mP2.processMessage what=3
415 D/hsm1 ( 1999): mP2.processMessage what=4
416 D/hsm1 ( 1999): mP2.processMessage what=5
417 D/hsm1 ( 1999): mP2.exit
418 D/hsm1 ( 1999): halting
421 public class StateMachine {
422 // Name of the state machine and used as logging tag
423 private String mName;
425 /** Message.what value when quitting */
426 private static final int SM_QUIT_CMD = -1;
428 /** Message.what value when initializing */
429 private static final int SM_INIT_CMD = -2;
432 * Convenience constant that maybe returned by processMessage
433 * to indicate the the message was processed and is not to be
434 * processed by parent states
436 public static final boolean HANDLED = true;
439 * Convenience constant that maybe returned by processMessage
440 * to indicate the the message was NOT processed and is to be
441 * processed by parent states
443 public static final boolean NOT_HANDLED = false;
446 * StateMachine logging record.
449 public static class LogRec {
450 private StateMachine mSm;
453 private String mInfo;
454 private IState mState;
455 private IState mOrgState;
456 private IState mDstState;
462 * @param state the state which handled the message
463 * @param orgState is the first state the received the message but
464 * did not processes the message.
465 * @param transToState is the state that was transitioned to after the message was
468 LogRec(StateMachine sm, Message msg, String info, IState state, IState orgState,
469 IState transToState) {
470 update(sm, msg, info, state, orgState, transToState);
474 * Update the information in the record.
475 * @param state that handled the message
476 * @param orgState is the first state the received the message
477 * @param dstState is the state that was the transition target when logging
479 public void update(StateMachine sm, Message msg, String info, IState state, IState orgState,
482 mTime = System.currentTimeMillis();
483 mWhat = (msg != null) ? msg.what : 0;
486 mOrgState = orgState;
487 mDstState = dstState;
493 public long getTime() {
500 public long getWhat() {
505 * @return the command that was executing
507 public String getInfo() {
512 * @return the state that handled this message
514 public IState getState() {
519 * @return the state destination state if a transition is occurring or null if none.
521 public IState getDestState() {
526 * @return the original state that received the message.
528 public IState getOriginalState() {
533 public String toString() {
534 StringBuilder sb = new StringBuilder();
536 Calendar c = Calendar.getInstance();
537 c.setTimeInMillis(mTime);
538 sb.append(String.format("%tm-%td %tH:%tM:%tS.%tL", c, c, c, c, c, c));
539 sb.append(" processed=");
540 sb.append(mState == null ? "<null>" : mState.getName());
542 sb.append(mOrgState == null ? "<null>" : mOrgState.getName());
544 sb.append(mDstState == null ? "<null>" : mDstState.getName());
546 String what = mSm != null ? mSm.getWhatToString(mWhat) : "";
547 if (TextUtils.isEmpty(what)) {
550 sb.append(Integer.toHexString(mWhat));
555 if (!TextUtils.isEmpty(mInfo)) {
559 return sb.toString();
564 * A list of log records including messages recently processed by the state machine.
566 * The class maintains a list of log records including messages
567 * recently processed. The list is finite and may be set in the
568 * constructor or by calling setSize. The public interface also
569 * includes size which returns the number of recent records,
570 * count which is the number of records processed since the
571 * the last setSize, get which returns a record and
572 * add which adds a record.
574 private static class LogRecords {
576 private static final int DEFAULT_SIZE = 20;
578 private Vector<LogRec> mLogRecVector = new Vector<LogRec>();
579 private int mMaxSize = DEFAULT_SIZE;
580 private int mOldestIndex = 0;
581 private int mCount = 0;
582 private boolean mLogOnlyTransitions = false;
585 * private constructor use add
587 private LogRecords() {
591 * Set size of messages to maintain and clears all current records.
593 * @param maxSize number of records to maintain at anyone time.
595 synchronized void setSize(int maxSize) {
596 // TODO: once b/28217358 is fixed, add unit tests to verify that these variables are
597 // cleared after calling this method, and that subsequent calls to get() function as
602 mLogRecVector.clear();
605 synchronized void setLogOnlyTransitions(boolean enable) {
606 mLogOnlyTransitions = enable;
609 synchronized boolean logOnlyTransitions() {
610 return mLogOnlyTransitions;
614 * @return the number of recent records.
616 synchronized int size() {
617 return mLogRecVector.size();
621 * @return the total number of records processed since size was set.
623 synchronized int count() {
628 * Clear the list of records.
630 synchronized void cleanup() {
631 mLogRecVector.clear();
635 * @return the information on a particular record. 0 is the oldest
636 * record and size()-1 is the newest record. If the index is to
637 * large null is returned.
639 synchronized LogRec get(int index) {
640 int nextIndex = mOldestIndex + index;
641 if (nextIndex >= mMaxSize) {
642 nextIndex -= mMaxSize;
644 if (nextIndex >= size()) {
647 return mLogRecVector.get(nextIndex);
652 * Add a processed message.
655 * @param messageInfo to be stored
656 * @param state that handled the message
657 * @param orgState is the first state the received the message but
658 * did not processes the message.
659 * @param transToState is the state that was transitioned to after the message was
663 synchronized void add(StateMachine sm, Message msg, String messageInfo, IState state,
664 IState orgState, IState transToState) {
666 if (mLogRecVector.size() < mMaxSize) {
667 mLogRecVector.add(new LogRec(sm, msg, messageInfo, state, orgState, transToState));
669 LogRec pmi = mLogRecVector.get(mOldestIndex);
671 if (mOldestIndex >= mMaxSize) {
674 pmi.update(sm, msg, messageInfo, state, orgState, transToState);
679 private static class SmHandler extends Handler {
681 /** true if StateMachine has quit */
682 private boolean mHasQuit = false;
684 /** The debug flag */
685 private boolean mDbg = false;
687 /** The SmHandler object, identifies that message is internal */
688 private static final Object mSmHandlerObj = new Object();
690 /** The current message */
691 private Message mMsg;
693 /** A list of log records including messages this state machine has processed */
694 private LogRecords mLogRecords = new LogRecords();
696 /** true if construction of the state machine has not been completed */
697 private boolean mIsConstructionCompleted;
699 /** Stack used to manage the current hierarchy of states */
700 private StateInfo mStateStack[];
702 /** Top of mStateStack */
703 private int mStateStackTopIndex = -1;
705 /** A temporary stack used to manage the state stack */
706 private StateInfo mTempStateStack[];
708 /** The top of the mTempStateStack */
709 private int mTempStateStackCount;
711 /** State used when state machine is halted */
712 private HaltingState mHaltingState = new HaltingState();
714 /** State used when state machine is quitting */
715 private QuittingState mQuittingState = new QuittingState();
717 /** Reference to the StateMachine */
718 private StateMachine mSm;
721 * Information about a state.
722 * Used to maintain the hierarchy.
724 private class StateInfo {
728 /** The parent of this state, null if there is no parent */
729 StateInfo parentStateInfo;
731 /** True when the state has been entered and on the stack */
735 * Convert StateInfo to string
738 public String toString() {
739 return "state=" + state.getName() + ",active=" + active + ",parent="
740 + ((parentStateInfo == null) ? "null" : parentStateInfo.state.getName());
744 /** The map of all of the states in the state machine */
745 private HashMap<State, StateInfo> mStateInfo = new HashMap<State, StateInfo>();
747 /** The initial state that will process the first message */
748 private State mInitialState;
750 /** The destination state when transitionTo has been invoked */
751 private State mDestState;
753 /** The list of deferred messages */
754 private ArrayList<Message> mDeferredMessages = new ArrayList<Message>();
757 * State entered when transitionToHaltingState is called.
759 private class HaltingState extends State {
761 public boolean processMessage(Message msg) {
762 mSm.haltedProcessMessage(msg);
768 * State entered when a valid quit message is handled.
770 private class QuittingState extends State {
772 public boolean processMessage(Message msg) {
778 * Handle messages sent to the state machine by calling
779 * the current state's processMessage. It also handles
780 * the enter/exit calls and placing any deferred messages
781 * back onto the queue when transitioning to a new state.
784 public final void handleMessage(Message msg) {
786 if (mSm != null && msg.what != SM_INIT_CMD && msg.what != SM_QUIT_CMD) {
787 mSm.onPreHandleMessage(msg);
790 if (mDbg) mSm.log("handleMessage: E msg.what=" + msg.what);
792 /** Save the current message */
795 /** State that processed the message */
796 State msgProcessedState = null;
797 if (mIsConstructionCompleted) {
799 msgProcessedState = processMsg(msg);
800 } else if (!mIsConstructionCompleted && (mMsg.what == SM_INIT_CMD)
801 && (mMsg.obj == mSmHandlerObj)) {
802 /** Initial one time path. */
803 mIsConstructionCompleted = true;
804 invokeEnterMethods(0);
806 throw new RuntimeException("StateMachine.handleMessage: "
807 + "The start method not called, received msg: " + msg);
809 performTransitions(msgProcessedState, msg);
811 // We need to check if mSm == null here as we could be quitting.
812 if (mDbg && mSm != null) mSm.log("handleMessage: X");
814 if (mSm != null && msg.what != SM_INIT_CMD && msg.what != SM_QUIT_CMD) {
815 mSm.onPostHandleMessage(msg);
822 * @param msgProcessedState is the state that processed the message
824 private void performTransitions(State msgProcessedState, Message msg) {
826 * If transitionTo has been called, exit and then enter
827 * the appropriate states. We loop on this to allow
828 * enter and exit methods to use transitionTo.
830 State orgState = mStateStack[mStateStackTopIndex].state;
833 * Record whether message needs to be logged before we transition and
834 * and we won't log special messages SM_INIT_CMD or SM_QUIT_CMD which
835 * always set msg.obj to the handler.
837 boolean recordLogMsg = mSm.recordLogRec(mMsg) && (msg.obj != mSmHandlerObj);
839 if (mLogRecords.logOnlyTransitions()) {
840 /** Record only if there is a transition */
841 if (mDestState != null) {
842 mLogRecords.add(mSm, mMsg, mSm.getLogRecString(mMsg), msgProcessedState,
843 orgState, mDestState);
845 } else if (recordLogMsg) {
846 /** Record message */
847 mLogRecords.add(mSm, mMsg, mSm.getLogRecString(mMsg), msgProcessedState, orgState,
851 State destState = mDestState;
852 if (destState != null) {
854 * Process the transitions including transitions in the enter/exit methods
857 if (mDbg) mSm.log("handleMessage: new destination call exit/enter");
860 * Determine the states to exit and enter and return the
861 * common ancestor state of the enter/exit states. Then
862 * invoke the exit methods then the enter methods.
864 StateInfo commonStateInfo = setupTempStateStackWithStatesToEnter(destState);
865 invokeExitMethods(commonStateInfo);
866 int stateStackEnteringIndex = moveTempStateStackToStateStack();
867 invokeEnterMethods(stateStackEnteringIndex);
870 * Since we have transitioned to a new state we need to have
871 * any deferred messages moved to the front of the message queue
872 * so they will be processed before any other messages in the
875 moveDeferredMessageAtFrontOfQueue();
877 if (destState != mDestState) {
878 // A new mDestState so continue looping
879 destState = mDestState;
881 // No change in mDestState so we're done
889 * After processing all transitions check and
890 * see if the last transition was to quit or halt.
892 if (destState != null) {
893 if (destState == mQuittingState) {
895 * Call onQuitting to let subclasses cleanup.
898 cleanupAfterQuitting();
899 } else if (destState == mHaltingState) {
901 * Call onHalting() if we've transitioned to the halting
902 * state. All subsequent messages will be processed in
903 * in the halting state which invokes haltedProcessMessage(msg);
911 * Cleanup all the static variables and the looper after the SM has been quit.
913 private final void cleanupAfterQuitting() {
914 if (mSm.mSmThread != null) {
915 // If we made the thread then quit looper which stops the thread.
917 mSm.mSmThread = null;
920 mSm.mSmHandler = null;
923 mLogRecords.cleanup();
925 mTempStateStack = null;
927 mInitialState = null;
929 mDeferredMessages.clear();
934 * Complete the construction of the state machine.
936 private final void completeConstruction() {
937 if (mDbg) mSm.log("completeConstruction: E");
940 * Determine the maximum depth of the state hierarchy
941 * so we can allocate the state stacks.
944 for (StateInfo si : mStateInfo.values()) {
946 for (StateInfo i = si; i != null; depth++) {
947 i = i.parentStateInfo;
949 if (maxDepth < depth) {
953 if (mDbg) mSm.log("completeConstruction: maxDepth=" + maxDepth);
955 mStateStack = new StateInfo[maxDepth];
956 mTempStateStack = new StateInfo[maxDepth];
957 setupInitialStateStack();
959 /** Sending SM_INIT_CMD message to invoke enter methods asynchronously */
960 sendMessageAtFrontOfQueue(obtainMessage(SM_INIT_CMD, mSmHandlerObj));
962 if (mDbg) mSm.log("completeConstruction: X");
966 * Process the message. If the current state doesn't handle
967 * it, call the states parent and so on. If it is never handled then
968 * call the state machines unhandledMessage method.
969 * @return the state that processed the message
971 private final State processMsg(Message msg) {
972 StateInfo curStateInfo = mStateStack[mStateStackTopIndex];
974 mSm.log("processMsg: " + curStateInfo.state.getName());
978 transitionTo(mQuittingState);
980 while (!curStateInfo.state.processMessage(msg)) {
984 curStateInfo = curStateInfo.parentStateInfo;
985 if (curStateInfo == null) {
987 * No parents left so it's not handled
989 mSm.unhandledMessage(msg);
993 mSm.log("processMsg: " + curStateInfo.state.getName());
997 return (curStateInfo != null) ? curStateInfo.state : null;
1001 * Call the exit method for each state from the top of stack
1002 * up to the common ancestor state.
1004 private final void invokeExitMethods(StateInfo commonStateInfo) {
1005 while ((mStateStackTopIndex >= 0)
1006 && (mStateStack[mStateStackTopIndex] != commonStateInfo)) {
1007 State curState = mStateStack[mStateStackTopIndex].state;
1008 if (mDbg) mSm.log("invokeExitMethods: " + curState.getName());
1010 mStateStack[mStateStackTopIndex].active = false;
1011 mStateStackTopIndex -= 1;
1016 * Invoke the enter method starting at the entering index to top of state stack
1018 private final void invokeEnterMethods(int stateStackEnteringIndex) {
1019 for (int i = stateStackEnteringIndex; i <= mStateStackTopIndex; i++) {
1020 if (mDbg) mSm.log("invokeEnterMethods: " + mStateStack[i].state.getName());
1021 mStateStack[i].state.enter();
1022 mStateStack[i].active = true;
1027 * Move the deferred message to the front of the message queue.
1029 private final void moveDeferredMessageAtFrontOfQueue() {
1031 * The oldest messages on the deferred list must be at
1032 * the front of the queue so start at the back, which
1033 * as the most resent message and end with the oldest
1034 * messages at the front of the queue.
1036 for (int i = mDeferredMessages.size() - 1; i >= 0; i--) {
1037 Message curMsg = mDeferredMessages.get(i);
1038 if (mDbg) mSm.log("moveDeferredMessageAtFrontOfQueue; what=" + curMsg.what);
1039 sendMessageAtFrontOfQueue(curMsg);
1041 mDeferredMessages.clear();
1045 * Move the contents of the temporary stack to the state stack
1046 * reversing the order of the items on the temporary stack as
1049 * @return index into mStateStack where entering needs to start
1051 private final int moveTempStateStackToStateStack() {
1052 int startingIndex = mStateStackTopIndex + 1;
1053 int i = mTempStateStackCount - 1;
1054 int j = startingIndex;
1056 if (mDbg) mSm.log("moveTempStackToStateStack: i=" + i + ",j=" + j);
1057 mStateStack[j] = mTempStateStack[i];
1062 mStateStackTopIndex = j - 1;
1064 mSm.log("moveTempStackToStateStack: X mStateStackTop=" + mStateStackTopIndex
1065 + ",startingIndex=" + startingIndex + ",Top="
1066 + mStateStack[mStateStackTopIndex].state.getName());
1068 return startingIndex;
1072 * Setup the mTempStateStack with the states we are going to enter.
1074 * This is found by searching up the destState's ancestors for a
1075 * state that is already active i.e. StateInfo.active == true.
1076 * The destStae and all of its inactive parents will be on the
1077 * TempStateStack as the list of states to enter.
1079 * @return StateInfo of the common ancestor for the destState and
1080 * current state or null if there is no common parent.
1082 private final StateInfo setupTempStateStackWithStatesToEnter(State destState) {
1084 * Search up the parent list of the destination state for an active
1085 * state. Use a do while() loop as the destState must always be entered
1086 * even if it is active. This can happen if we are exiting/entering
1087 * the current state.
1089 mTempStateStackCount = 0;
1090 StateInfo curStateInfo = mStateInfo.get(destState);
1092 mTempStateStack[mTempStateStackCount++] = curStateInfo;
1093 curStateInfo = curStateInfo.parentStateInfo;
1094 } while ((curStateInfo != null) && !curStateInfo.active);
1097 mSm.log("setupTempStateStackWithStatesToEnter: X mTempStateStackCount="
1098 + mTempStateStackCount + ",curStateInfo: " + curStateInfo);
1100 return curStateInfo;
1104 * Initialize StateStack to mInitialState.
1106 private final void setupInitialStateStack() {
1108 mSm.log("setupInitialStateStack: E mInitialState=" + mInitialState.getName());
1111 StateInfo curStateInfo = mStateInfo.get(mInitialState);
1112 for (mTempStateStackCount = 0; curStateInfo != null; mTempStateStackCount++) {
1113 mTempStateStack[mTempStateStackCount] = curStateInfo;
1114 curStateInfo = curStateInfo.parentStateInfo;
1117 // Empty the StateStack
1118 mStateStackTopIndex = -1;
1120 moveTempStateStackToStateStack();
1124 * @return current message
1126 private final Message getCurrentMessage() {
1131 * @return current state
1133 private final IState getCurrentState() {
1134 return mStateStack[mStateStackTopIndex].state;
1138 * Add a new state to the state machine. Bottom up addition
1139 * of states is allowed but the same state may only exist
1142 * @param state the state to add
1143 * @param parent the parent of state
1144 * @return stateInfo for this state
1146 private final StateInfo addState(State state, State parent) {
1148 mSm.log("addStateInternal: E state=" + state.getName() + ",parent="
1149 + ((parent == null) ? "" : parent.getName()));
1151 StateInfo parentStateInfo = null;
1152 if (parent != null) {
1153 parentStateInfo = mStateInfo.get(parent);
1154 if (parentStateInfo == null) {
1155 // Recursively add our parent as it's not been added yet.
1156 parentStateInfo = addState(parent, null);
1159 StateInfo stateInfo = mStateInfo.get(state);
1160 if (stateInfo == null) {
1161 stateInfo = new StateInfo();
1162 mStateInfo.put(state, stateInfo);
1165 // Validate that we aren't adding the same state in two different hierarchies.
1166 if ((stateInfo.parentStateInfo != null)
1167 && (stateInfo.parentStateInfo != parentStateInfo)) {
1168 throw new RuntimeException("state already added");
1170 stateInfo.state = state;
1171 stateInfo.parentStateInfo = parentStateInfo;
1172 stateInfo.active = false;
1173 if (mDbg) mSm.log("addStateInternal: X stateInfo: " + stateInfo);
1180 * @param looper for dispatching messages
1181 * @param sm the hierarchical state machine
1183 private SmHandler(Looper looper, StateMachine sm) {
1187 addState(mHaltingState, null);
1188 addState(mQuittingState, null);
1191 /** @see StateMachine#setInitialState(State) */
1192 private final void setInitialState(State initialState) {
1193 if (mDbg) mSm.log("setInitialState: initialState=" + initialState.getName());
1194 mInitialState = initialState;
1197 /** @see StateMachine#transitionTo(IState) */
1198 private final void transitionTo(IState destState) {
1199 mDestState = (State) destState;
1200 if (mDbg) mSm.log("transitionTo: destState=" + mDestState.getName());
1203 /** @see StateMachine#deferMessage(Message) */
1204 private final void deferMessage(Message msg) {
1205 if (mDbg) mSm.log("deferMessage: msg=" + msg.what);
1207 /* Copy the "msg" to "newMsg" as "msg" will be recycled */
1208 Message newMsg = obtainMessage();
1209 newMsg.copyFrom(msg);
1211 mDeferredMessages.add(newMsg);
1214 /** @see StateMachine#quit() */
1215 private final void quit() {
1216 if (mDbg) mSm.log("quit:");
1217 sendMessage(obtainMessage(SM_QUIT_CMD, mSmHandlerObj));
1220 /** @see StateMachine#quitNow() */
1221 private final void quitNow() {
1222 if (mDbg) mSm.log("quitNow:");
1223 sendMessageAtFrontOfQueue(obtainMessage(SM_QUIT_CMD, mSmHandlerObj));
1226 /** Validate that the message was sent by quit or quitNow. */
1227 private final boolean isQuit(Message msg) {
1228 return (msg.what == SM_QUIT_CMD) && (msg.obj == mSmHandlerObj);
1231 /** @see StateMachine#isDbg() */
1232 private final boolean isDbg() {
1236 /** @see StateMachine#setDbg(boolean) */
1237 private final void setDbg(boolean dbg) {
1243 private SmHandler mSmHandler;
1244 private HandlerThread mSmThread;
1249 * @param looper for this state machine
1250 * @param name of the state machine
1252 private void initStateMachine(String name, Looper looper) {
1254 mSmHandler = new SmHandler(looper, this);
1258 * Constructor creates a StateMachine with its own thread.
1260 * @param name of the state machine
1262 protected StateMachine(String name) {
1263 mSmThread = new HandlerThread(name);
1265 Looper looper = mSmThread.getLooper();
1267 initStateMachine(name, looper);
1271 * Constructor creates a StateMachine using the looper.
1273 * @param name of the state machine
1275 protected StateMachine(String name, Looper looper) {
1276 initStateMachine(name, looper);
1280 * Constructor creates a StateMachine using the handler.
1282 * @param name of the state machine
1284 protected StateMachine(String name, Handler handler) {
1285 initStateMachine(name, handler.getLooper());
1289 * Notifies subclass that the StateMachine handler is about to process the Message msg
1290 * @param msg The message that is being handled
1292 protected void onPreHandleMessage(Message msg) {
1296 * Notifies subclass that the StateMachine handler has finished processing the Message msg and
1297 * has possibly transitioned to a new state.
1298 * @param msg The message that is being handled
1300 protected void onPostHandleMessage(Message msg) {
1304 * Add a new state to the state machine
1305 * @param state the state to add
1306 * @param parent the parent of state
1308 protected final void addState(State state, State parent) {
1309 mSmHandler.addState(state, parent);
1313 * Add a new state to the state machine, parent will be null
1314 * @param state to add
1316 protected final void addState(State state) {
1317 mSmHandler.addState(state, null);
1321 * Set the initial state. This must be invoked before
1322 * and messages are sent to the state machine.
1324 * @param initialState is the state which will receive the first message.
1326 protected final void setInitialState(State initialState) {
1327 mSmHandler.setInitialState(initialState);
1331 * @return current message
1333 protected final Message getCurrentMessage() {
1334 // mSmHandler can be null if the state machine has quit.
1335 SmHandler smh = mSmHandler;
1336 if (smh == null) return null;
1337 return smh.getCurrentMessage();
1341 * @return current state
1343 protected final IState getCurrentState() {
1344 // mSmHandler can be null if the state machine has quit.
1345 SmHandler smh = mSmHandler;
1346 if (smh == null) return null;
1347 return smh.getCurrentState();
1351 * transition to destination state. Upon returning
1352 * from processMessage the current state's exit will
1353 * be executed and upon the next message arriving
1354 * destState.enter will be invoked.
1356 * this function can also be called inside the enter function of the
1357 * previous transition target, but the behavior is undefined when it is
1358 * called mid-way through a previous transition (for example, calling this
1359 * in the enter() routine of a intermediate node when the current transition
1360 * target is one of the nodes descendants).
1362 * @param destState will be the state that receives the next message.
1364 protected final void transitionTo(IState destState) {
1365 mSmHandler.transitionTo(destState);
1369 * transition to halt state. Upon returning
1370 * from processMessage we will exit all current
1371 * states, execute the onHalting() method and then
1372 * for all subsequent messages haltedProcessMessage
1375 protected final void transitionToHaltingState() {
1376 mSmHandler.transitionTo(mSmHandler.mHaltingState);
1380 * Defer this message until next state transition.
1381 * Upon transitioning all deferred messages will be
1382 * placed on the queue and reprocessed in the original
1383 * order. (i.e. The next state the oldest messages will
1384 * be processed first)
1386 * @param msg is deferred until the next transition.
1388 protected final void deferMessage(Message msg) {
1389 mSmHandler.deferMessage(msg);
1393 * Called when message wasn't handled
1395 * @param msg that couldn't be handled.
1397 protected void unhandledMessage(Message msg) {
1398 if (mSmHandler.mDbg) loge(" - unhandledMessage: msg.what=" + msg.what);
1402 * Called for any message that is received after
1403 * transitionToHalting is called.
1405 protected void haltedProcessMessage(Message msg) {
1409 * This will be called once after handling a message that called
1410 * transitionToHalting. All subsequent messages will invoke
1411 * {@link StateMachine#haltedProcessMessage(Message)}
1413 protected void onHalting() {
1417 * This will be called once after a quit message that was NOT handled by
1418 * the derived StateMachine. The StateMachine will stop and any subsequent messages will be
1419 * ignored. In addition, if this StateMachine created the thread, the thread will
1420 * be stopped after this method returns.
1422 protected void onQuitting() {
1428 public final String getName() {
1433 * Set number of log records to maintain and clears all current records.
1435 * @param maxSize number of messages to maintain at anyone time.
1437 public final void setLogRecSize(int maxSize) {
1438 mSmHandler.mLogRecords.setSize(maxSize);
1442 * Set to log only messages that cause a state transition
1444 * @param enable {@code true} to enable, {@code false} to disable
1446 public final void setLogOnlyTransitions(boolean enable) {
1447 mSmHandler.mLogRecords.setLogOnlyTransitions(enable);
1451 * @return number of log records
1453 public final int getLogRecSize() {
1454 // mSmHandler can be null if the state machine has quit.
1455 SmHandler smh = mSmHandler;
1456 if (smh == null) return 0;
1457 return smh.mLogRecords.size();
1461 * @return the total number of records processed
1463 public final int getLogRecCount() {
1464 // mSmHandler can be null if the state machine has quit.
1465 SmHandler smh = mSmHandler;
1466 if (smh == null) return 0;
1467 return smh.mLogRecords.count();
1471 * @return a log record, or null if index is out of range
1473 public final LogRec getLogRec(int index) {
1474 // mSmHandler can be null if the state machine has quit.
1475 SmHandler smh = mSmHandler;
1476 if (smh == null) return null;
1477 return smh.mLogRecords.get(index);
1481 * @return a copy of LogRecs as a collection
1483 public final Collection<LogRec> copyLogRecs() {
1484 Vector<LogRec> vlr = new Vector<LogRec>();
1485 SmHandler smh = mSmHandler;
1487 for (LogRec lr : smh.mLogRecords.mLogRecVector) {
1495 * Add the string to LogRecords.
1499 protected void addLogRec(String string) {
1500 // mSmHandler can be null if the state machine has quit.
1501 SmHandler smh = mSmHandler;
1502 if (smh == null) return;
1503 smh.mLogRecords.add(this, smh.getCurrentMessage(), string, smh.getCurrentState(),
1504 smh.mStateStack[smh.mStateStackTopIndex].state, smh.mDestState);
1508 * @return true if msg should be saved in the log, default is true.
1510 protected boolean recordLogRec(Message msg) {
1515 * Return a string to be logged by LogRec, default
1516 * is an empty string. Override if additional information is desired.
1518 * @param msg that was processed
1519 * @return information to be logged as a String
1521 protected String getLogRecString(Message msg) {
1526 * @return the string for msg.what
1528 protected String getWhatToString(int what) {
1533 * @return Handler, maybe null if state machine has quit.
1535 public final Handler getHandler() {
1540 * Get a message and set Message.target state machine handler.
1542 * Note: The handler can be null if the state machine has quit,
1543 * which means target will be null and may cause a AndroidRuntimeException
1544 * in MessageQueue#enqueMessage if sent directly or if sent using
1545 * StateMachine#sendMessage the message will just be ignored.
1547 * @return A Message object from the global pool
1549 public final Message obtainMessage() {
1550 return Message.obtain(mSmHandler);
1554 * Get a message and set Message.target state machine handler, what.
1556 * Note: The handler can be null if the state machine has quit,
1557 * which means target will be null and may cause a AndroidRuntimeException
1558 * in MessageQueue#enqueMessage if sent directly or if sent using
1559 * StateMachine#sendMessage the message will just be ignored.
1561 * @param what is the assigned to Message.what.
1562 * @return A Message object from the global pool
1564 public final Message obtainMessage(int what) {
1565 return Message.obtain(mSmHandler, what);
1569 * Get a message and set Message.target state machine handler,
1572 * Note: The handler can be null if the state machine has quit,
1573 * which means target will be null and may cause a AndroidRuntimeException
1574 * in MessageQueue#enqueMessage if sent directly or if sent using
1575 * StateMachine#sendMessage the message will just be ignored.
1577 * @param what is the assigned to Message.what.
1578 * @param obj is assigned to Message.obj.
1579 * @return A Message object from the global pool
1581 public final Message obtainMessage(int what, Object obj) {
1582 return Message.obtain(mSmHandler, what, obj);
1586 * Get a message and set Message.target state machine handler,
1587 * what, arg1 and arg2
1589 * Note: The handler can be null if the state machine has quit,
1590 * which means target will be null and may cause a AndroidRuntimeException
1591 * in MessageQueue#enqueMessage if sent directly or if sent using
1592 * StateMachine#sendMessage the message will just be ignored.
1594 * @param what is assigned to Message.what
1595 * @param arg1 is assigned to Message.arg1
1596 * @return A Message object from the global pool
1598 public final Message obtainMessage(int what, int arg1) {
1599 // use this obtain so we don't match the obtain(h, what, Object) method
1600 return Message.obtain(mSmHandler, what, arg1, 0);
1604 * Get a message and set Message.target state machine handler,
1605 * what, arg1 and arg2
1607 * Note: The handler can be null if the state machine has quit,
1608 * which means target will be null and may cause a AndroidRuntimeException
1609 * in MessageQueue#enqueMessage if sent directly or if sent using
1610 * StateMachine#sendMessage the message will just be ignored.
1612 * @param what is assigned to Message.what
1613 * @param arg1 is assigned to Message.arg1
1614 * @param arg2 is assigned to Message.arg2
1615 * @return A Message object from the global pool
1617 public final Message obtainMessage(int what, int arg1, int arg2) {
1618 return Message.obtain(mSmHandler, what, arg1, arg2);
1622 * Get a message and set Message.target state machine handler,
1623 * what, arg1, arg2 and obj
1625 * Note: The handler can be null if the state machine has quit,
1626 * which means target will be null and may cause a AndroidRuntimeException
1627 * in MessageQueue#enqueMessage if sent directly or if sent using
1628 * StateMachine#sendMessage the message will just be ignored.
1630 * @param what is assigned to Message.what
1631 * @param arg1 is assigned to Message.arg1
1632 * @param arg2 is assigned to Message.arg2
1633 * @param obj is assigned to Message.obj
1634 * @return A Message object from the global pool
1636 public final Message obtainMessage(int what, int arg1, int arg2, Object obj) {
1637 return Message.obtain(mSmHandler, what, arg1, arg2, obj);
1641 * Enqueue a message to this state machine.
1643 * Message is ignored if state machine has quit.
1645 public void sendMessage(int what) {
1646 // mSmHandler can be null if the state machine has quit.
1647 SmHandler smh = mSmHandler;
1648 if (smh == null) return;
1650 smh.sendMessage(obtainMessage(what));
1654 * Enqueue a message to this state machine.
1656 * Message is ignored if state machine has quit.
1658 public void sendMessage(int what, Object obj) {
1659 // mSmHandler can be null if the state machine has quit.
1660 SmHandler smh = mSmHandler;
1661 if (smh == null) return;
1663 smh.sendMessage(obtainMessage(what, obj));
1667 * Enqueue a message to this state machine.
1669 * Message is ignored if state machine has quit.
1671 public void sendMessage(int what, int arg1) {
1672 // mSmHandler can be null if the state machine has quit.
1673 SmHandler smh = mSmHandler;
1674 if (smh == null) return;
1676 smh.sendMessage(obtainMessage(what, arg1));
1680 * Enqueue a message to this state machine.
1682 * Message is ignored if state machine has quit.
1684 public void sendMessage(int what, int arg1, int arg2) {
1685 // mSmHandler can be null if the state machine has quit.
1686 SmHandler smh = mSmHandler;
1687 if (smh == null) return;
1689 smh.sendMessage(obtainMessage(what, arg1, arg2));
1693 * Enqueue a message to this state machine.
1695 * Message is ignored if state machine has quit.
1697 public void sendMessage(int what, int arg1, int arg2, Object obj) {
1698 // mSmHandler can be null if the state machine has quit.
1699 SmHandler smh = mSmHandler;
1700 if (smh == null) return;
1702 smh.sendMessage(obtainMessage(what, arg1, arg2, obj));
1706 * Enqueue a message to this state machine.
1708 * Message is ignored if state machine has quit.
1710 public void sendMessage(Message msg) {
1711 // mSmHandler can be null if the state machine has quit.
1712 SmHandler smh = mSmHandler;
1713 if (smh == null) return;
1715 smh.sendMessage(msg);
1719 * Enqueue a message to this state machine after a delay.
1721 * Message is ignored if state machine has quit.
1723 public void sendMessageDelayed(int what, long delayMillis) {
1724 // mSmHandler can be null if the state machine has quit.
1725 SmHandler smh = mSmHandler;
1726 if (smh == null) return;
1728 smh.sendMessageDelayed(obtainMessage(what), delayMillis);
1732 * Enqueue a message to this state machine after a delay.
1734 * Message is ignored if state machine has quit.
1736 public void sendMessageDelayed(int what, Object obj, long delayMillis) {
1737 // mSmHandler can be null if the state machine has quit.
1738 SmHandler smh = mSmHandler;
1739 if (smh == null) return;
1741 smh.sendMessageDelayed(obtainMessage(what, obj), delayMillis);
1745 * Enqueue a message to this state machine after a delay.
1747 * Message is ignored if state machine has quit.
1749 public void sendMessageDelayed(int what, int arg1, long delayMillis) {
1750 // mSmHandler can be null if the state machine has quit.
1751 SmHandler smh = mSmHandler;
1752 if (smh == null) return;
1754 smh.sendMessageDelayed(obtainMessage(what, arg1), delayMillis);
1758 * Enqueue a message to this state machine after a delay.
1760 * Message is ignored if state machine has quit.
1762 public void sendMessageDelayed(int what, int arg1, int arg2, long delayMillis) {
1763 // mSmHandler can be null if the state machine has quit.
1764 SmHandler smh = mSmHandler;
1765 if (smh == null) return;
1767 smh.sendMessageDelayed(obtainMessage(what, arg1, arg2), delayMillis);
1771 * Enqueue a message to this state machine after a delay.
1773 * Message is ignored if state machine has quit.
1775 public void sendMessageDelayed(int what, int arg1, int arg2, Object obj,
1777 // mSmHandler can be null if the state machine has quit.
1778 SmHandler smh = mSmHandler;
1779 if (smh == null) return;
1781 smh.sendMessageDelayed(obtainMessage(what, arg1, arg2, obj), delayMillis);
1785 * Enqueue a message to this state machine after a delay.
1787 * Message is ignored if state machine has quit.
1789 public void sendMessageDelayed(Message msg, long delayMillis) {
1790 // mSmHandler can be null if the state machine has quit.
1791 SmHandler smh = mSmHandler;
1792 if (smh == null) return;
1794 smh.sendMessageDelayed(msg, delayMillis);
1798 * Enqueue a message to the front of the queue for this state machine.
1799 * Protected, may only be called by instances of StateMachine.
1801 * Message is ignored if state machine has quit.
1803 protected final void sendMessageAtFrontOfQueue(int what) {
1804 // mSmHandler can be null if the state machine has quit.
1805 SmHandler smh = mSmHandler;
1806 if (smh == null) return;
1808 smh.sendMessageAtFrontOfQueue(obtainMessage(what));
1812 * Enqueue a message to the front of the queue for this state machine.
1813 * Protected, may only be called by instances of StateMachine.
1815 * Message is ignored if state machine has quit.
1817 protected final void sendMessageAtFrontOfQueue(int what, Object obj) {
1818 // mSmHandler can be null if the state machine has quit.
1819 SmHandler smh = mSmHandler;
1820 if (smh == null) return;
1822 smh.sendMessageAtFrontOfQueue(obtainMessage(what, obj));
1826 * Enqueue a message to the front of the queue for this state machine.
1827 * Protected, may only be called by instances of StateMachine.
1829 * Message is ignored if state machine has quit.
1831 protected final void sendMessageAtFrontOfQueue(int what, int arg1) {
1832 // mSmHandler can be null if the state machine has quit.
1833 SmHandler smh = mSmHandler;
1834 if (smh == null) return;
1836 smh.sendMessageAtFrontOfQueue(obtainMessage(what, arg1));
1841 * Enqueue a message to the front of the queue for this state machine.
1842 * Protected, may only be called by instances of StateMachine.
1844 * Message is ignored if state machine has quit.
1846 protected final void sendMessageAtFrontOfQueue(int what, int arg1, int arg2) {
1847 // mSmHandler can be null if the state machine has quit.
1848 SmHandler smh = mSmHandler;
1849 if (smh == null) return;
1851 smh.sendMessageAtFrontOfQueue(obtainMessage(what, arg1, arg2));
1855 * Enqueue a message to the front of the queue for this state machine.
1856 * Protected, may only be called by instances of StateMachine.
1858 * Message is ignored if state machine has quit.
1860 protected final void sendMessageAtFrontOfQueue(int what, int arg1, int arg2, Object obj) {
1861 // mSmHandler can be null if the state machine has quit.
1862 SmHandler smh = mSmHandler;
1863 if (smh == null) return;
1865 smh.sendMessageAtFrontOfQueue(obtainMessage(what, arg1, arg2, obj));
1869 * Enqueue a message to the front of the queue for this state machine.
1870 * Protected, may only be called by instances of StateMachine.
1872 * Message is ignored if state machine has quit.
1874 protected final void sendMessageAtFrontOfQueue(Message msg) {
1875 // mSmHandler can be null if the state machine has quit.
1876 SmHandler smh = mSmHandler;
1877 if (smh == null) return;
1879 smh.sendMessageAtFrontOfQueue(msg);
1883 * Removes a message from the message queue.
1884 * Protected, may only be called by instances of StateMachine.
1886 protected final void removeMessages(int what) {
1887 // mSmHandler can be null if the state machine has quit.
1888 SmHandler smh = mSmHandler;
1889 if (smh == null) return;
1891 smh.removeMessages(what);
1895 * Removes a message from the deferred messages queue.
1897 protected final void removeDeferredMessages(int what) {
1898 SmHandler smh = mSmHandler;
1899 if (smh == null) return;
1901 Iterator<Message> iterator = smh.mDeferredMessages.iterator();
1902 while (iterator.hasNext()) {
1903 Message msg = iterator.next();
1904 if (msg.what == what) iterator.remove();
1909 * Check if there are any pending messages with code 'what' in deferred messages queue.
1911 protected final boolean hasDeferredMessages(int what) {
1912 SmHandler smh = mSmHandler;
1913 if (smh == null) return false;
1915 Iterator<Message> iterator = smh.mDeferredMessages.iterator();
1916 while (iterator.hasNext()) {
1917 Message msg = iterator.next();
1918 if (msg.what == what) return true;
1925 * Check if there are any pending posts of messages with code 'what' in
1926 * the message queue. This does NOT check messages in deferred message queue.
1928 protected final boolean hasMessages(int what) {
1929 SmHandler smh = mSmHandler;
1930 if (smh == null) return false;
1932 return smh.hasMessages(what);
1936 * Validate that the message was sent by
1937 * {@link StateMachine#quit} or {@link StateMachine#quitNow}.
1939 protected final boolean isQuit(Message msg) {
1940 // mSmHandler can be null if the state machine has quit.
1941 SmHandler smh = mSmHandler;
1942 if (smh == null) return msg.what == SM_QUIT_CMD;
1944 return smh.isQuit(msg);
1948 * Quit the state machine after all currently queued up messages are processed.
1950 protected final void quit() {
1951 // mSmHandler can be null if the state machine is already stopped.
1952 SmHandler smh = mSmHandler;
1953 if (smh == null) return;
1959 * Quit the state machine immediately all currently queued messages will be discarded.
1961 protected final void quitNow() {
1962 // mSmHandler can be null if the state machine is already stopped.
1963 SmHandler smh = mSmHandler;
1964 if (smh == null) return;
1970 * @return if debugging is enabled
1972 public boolean isDbg() {
1973 // mSmHandler can be null if the state machine has quit.
1974 SmHandler smh = mSmHandler;
1975 if (smh == null) return false;
1981 * Set debug enable/disabled.
1983 * @param dbg is true to enable debugging.
1985 public void setDbg(boolean dbg) {
1986 // mSmHandler can be null if the state machine has quit.
1987 SmHandler smh = mSmHandler;
1988 if (smh == null) return;
1994 * Start the state machine.
1996 public void start() {
1997 // mSmHandler can be null if the state machine has quit.
1998 SmHandler smh = mSmHandler;
1999 if (smh == null) return;
2001 /** Send the complete construction message */
2002 smh.completeConstruction();
2006 * Dump the current state.
2012 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2013 // Cannot just invoke pw.println(this.toString()) because if the
2014 // resulting string is to long it won't be displayed.
2015 pw.println(getName() + ":");
2016 pw.println(" total records=" + getLogRecCount());
2017 for (int i = 0; i < getLogRecSize(); i++) {
2018 pw.println(" rec[" + i + "]: " + getLogRec(i).toString());
2021 pw.println("curState=" + getCurrentState().getName());
2025 public String toString() {
2026 StringWriter sr = new StringWriter();
2027 PrintWriter pr = new PrintWriter(sr);
2028 dump(null, pr, null);
2031 return sr.toString();
2035 * Log with debug and add to the LogRecords.
2037 * @param s is string log
2039 protected void logAndAddLogRec(String s) {
2047 * @param s is string log
2049 protected void log(String s) {
2054 * Log with debug attribute
2056 * @param s is string log
2058 protected void logd(String s) {
2063 * Log with verbose attribute
2065 * @param s is string log
2067 protected void logv(String s) {
2072 * Log with info attribute
2074 * @param s is string log
2076 protected void logi(String s) {
2081 * Log with warning attribute
2083 * @param s is string log
2085 protected void logw(String s) {
2090 * Log with error attribute
2092 * @param s is string log
2094 protected void loge(String s) {
2099 * Log with error attribute
2101 * @param s is string log
2102 * @param e is a Throwable which logs additional information.
2104 protected void loge(String s, Throwable e) {