在Android系统中,经常使用状态机来处理不同状态下的行为动作。状态机是将对象的状态与行为封装在一起;可以解决庞大的分支语句带来程序阅读性差和不便于进行扩展问题,使整个结构变得更加清晰明了,降低程序管理的复杂性提高灵活度。Android系统的StateMachine机制是一个State模式的应用,StateMachine是一个分层处理消息的状态机,并且是能够有分层排列状态。
StateMachine的构造函数都是protected类型,不能实例化,都是由其子类进行初始化操作。StateMachine有两个重载的构造函数,一个是通过指定消息循环对象来构造状态机
protected StateMachine(String name, Looper looper) { initStateMachine(name, looper); }
另一个则是直接启动一个消息循环线程来构造一个状态机
protected StateMachine(String name) { //启动一个消息循环线程 mSmThread = new HandlerThread(name); mSmThread.start(); Looper looper = mSmThread.getLooper(); initStateMachine(name, looper); }这两种构造函数都会通过initStateMachine函数来初始化该状态机
private void initStateMachine(String name, Looper looper) { mName = name; mSmHandler = new SmHandler(looper, this); }初始化过程比较简单,就是将状态机名称保存到成员变量mName中,同时创建SmHandler对象,SmHandler是一个Handle对象,用于派发消息。
状态机中的每个状态使用State来封装,对于每个状态的信息又采用StateInfo来描述
StateMachine三个内部类:
1.ProcessedMessageInfo:保存已处理消息的信息;
2.ProcessedMessages:存储StateMachine最近处理的一些消息,需要保存最近处理的消息条数默认20,可以用户自己设定最大数目;
3.SmHandle是消息处理派发和状态控制切换的核心,运行在单独的线程上;
SmHandle成员变量定义:
ProcessedMessages用于保存已处理过的消息,mStateStack和mTempStateStack是一个数组栈,用于保存状态机中的链式状态关系。
mStateInfo定义为一个Hash链表,用于保存添加的所有状态。mInitialState保存初始状态,mDestState保存切换的目的状态。
protected final void addState(State state, State parent) { mSmHandler.addState(state, parent); }直接通过mSmHandler来完成状态添加过程
private final StateInfo addState(State state, State parent) { if (mDbg) { Log.d(TAG, "addStateInternal: E state=" + state.getName() + ",parent=" + ((parent == null) ? "" : parent.getName())); } StateInfo parentStateInfo = null; if (parent != null) { parentStateInfo = mStateInfo.get(parent); if (parentStateInfo == null) { // Recursively add our parent as it's not been added yet. parentStateInfo = addState(parent, null); } } StateInfo stateInfo = mStateInfo.get(state); if (stateInfo == null) { stateInfo = new StateInfo(); mStateInfo.put(state, stateInfo); } // Validate that we aren't adding the same state in two different hierarchies. if ((stateInfo.parentStateInfo != null) && (stateInfo.parentStateInfo != parentStateInfo)) { throw new RuntimeException("state already added"); } stateInfo.state = state; stateInfo.parentStateInfo = parentStateInfo; stateInfo.active = false; if (mDbg) Log.d(TAG, "addStateInternal: X stateInfo: " + stateInfo); return stateInfo; }状态添加过程其实就是为每个State创建相应的StateInfo对象,通过该对象来建立各个状态之间的关系,并且一个State-StateInfo键值对的方式保存到mStateInfo Hash表中。StateInfo就是包装State组成一个Node,建立State的父子关系;mStateInfo =new HashMap<State, StateInfo>();用来保存State Machine中的所有State,可以按照树形层次结构组织状态机中的所有状态
sm.addState(S0,null); sm.addState(S1,S0); sm.addState(S2,S0); sm.addState(S3,S1); sm.addState(S4,S1); sm.addState(S5,S2); sm.addState(S6,S2); sm.addState(S7,S2); setInitialState(S4); //设置初始状态1.添加根节点状态过程
private final StateInfo addState(S0,null) { StateInfo parentStateInfo = null; //状态S0还未加入状态机中 StateInfo stateInfo = mStateInfo.get(state); if (stateInfo == null) { //创建State详细信息对象 stateInfo = new StateInfo(); //将S0加入状态机中 mStateInfo.put(state, stateInfo); } //设置状态S0的状态信息 stateInfo.state = state; //S0的父节点为null stateInfo.parentStateInfo = parentStateInfo; stateInfo.active = false; return stateInfo; }2.添加树枝节点状态过程
private final StateInfo addState(State state, State parent) { StateInfo parentStateInfo = null; //状态S0在前面已经加入状态机中了 if (parent != null) { //获取S0详细信息 StateInfo parentStateInfo = mStateInfo.get(parent); //因为S0已经存在于状态机中,因此这里不为空 if (parentStateInfo == null) { //当前状态父状态未加入到StateMachine中,递归先加入其父State parentStateInfo = addState(parent, null); } } //从状态机中得到S1的状态信息,由于S1还为加入状态机,因此得到的结果为空 StateInfo stateInfo = mStateInfo.get(state); if (stateInfo == null) { //创建State详细信息对象 stateInfo = new StateInfo(); //将S1加入状态机中 mStateInfo.put(state, stateInfo); } //S1的状态信息刚创建,还没有为其设置父节点,因此其父节点为空 if ((stateInfo.parentStateInfo != null) && (stateInfo.parentStateInfo != parentStateInfo)) { throw new RuntimeException("state already added"); } //设置状态S1的状态信息 stateInfo.state = state; //S1的父节点为S0 stateInfo.parentStateInfo = parentStateInfo; stateInfo.active = false; return stateInfo; }对于其他树枝节点的添加过程类似,这里不在介绍,最后保存在mStateInfo表中的所有状态之间就建立了以下树形关系:
public void start() { // mSmHandler can be null if the state machine has quit. if (mSmHandler == null) return; /** Send the complete construction message */ mSmHandler.completeConstruction(); }调用mSmHandler的completeConstruction函数来完成状态机的构造完成处理
private final void completeConstruction() { if (mDbg) Log.d(TAG, "completeConstruction: E"); //查找状态树的深度 int maxDepth = 0; for (StateInfo si : mStateInfo.values()) { int depth = 0; //根据父子关系计算树枝层次数 for (StateInfo i = si; i != null; depth++) { i = i.parentStateInfo; } if (maxDepth < depth) { maxDepth = depth; } } if (mDbg) Log.d(TAG, "completeConstruction: maxDepth=" + maxDepth); //创建mStateStack,mTempStateStack状态栈 mStateStack = new StateInfo[maxDepth]; mTempStateStack = new StateInfo[maxDepth]; //设置状态堆栈 setupInitialStateStack(); /** Sending SM_INIT_CMD message to invoke enter methods asynchronously */ sendMessageAtFrontOfQueue(obtainMessage(SM_INIT_CMD, mSmHandlerObj)); if (mDbg) Log.d(TAG, "completeConstruction: X"); }计算状态树的最大深度方法:
private final void setupInitialStateStack() { //在mStateInfo中取得初始状态mInitialState对应的StateInfo StateInfo curStateInfo = mStateInfo.get(mInitialState); //从初始状态mInitialState开始根据父子关系填充mTempStateStack堆栈 for (mTempStateStackCount = 0; curStateInfo != null; mTempStateStackCount++) { mTempStateStack[mTempStateStackCount] = curStateInfo; curStateInfo = curStateInfo.parentStateInfo; } // Empty the StateStack mStateStackTopIndex = -1; //将mTempStateStack中的状态按反序方式移动到mStateStack栈中 moveTempStateStackToStateStack(); }
private final int moveTempStateStackToStateStack() { //startingIndex= 0 int startingIndex = mStateStackTopIndex + 1; int i = mTempStateStackCount - 1; int j = startingIndex; while (i >= 0) { if (mDbg) Log.d(TAG, "moveTempStackToStateStack: i=" + i + ",j=" + j); mStateStack[j] = mTempStateStack[i]; j += 1; i -= 1; } mStateStackTopIndex = j - 1; return startingIndex; }mStateStack={S0,S1,S4}
sendMessageAtFrontOfQueue(obtainMessage(SM_INIT_CMD, mSmHandlerObj))该消息对应的处理如下:
else if (!mIsConstructionCompleted &&(mMsg.what == SM_INIT_CMD) && (mMsg.obj == mSmHandlerObj)) { mIsConstructionCompleted = true; invokeEnterMethods(0); } performTransitions();消息处理过程首先调用invokeEnterMethods函数将mStateStack栈中的所有状态设置为激活状态,同时调用每一个状态的enter()函数
private final void invokeEnterMethods(int stateStackEnteringIndex) { for (int i = stateStackEnteringIndex; i <= mStateStackTopIndex; i++) { if (mDbg) Log.d(TAG, "invokeEnterMethods: " + mStateStack[i].state.getName()); mStateStack[i].state.enter(); mStateStack[i].active = true; } }最后调用performTransitions函数来切换状态,同时设置mIsConstructionCompleted为true,表示状态机已经启动完成,SmHandler在以后的消息处理过程中就不在重新启动状态机了。
private synchronized void performTransitions() { while (mDestState != null){ //当前状态切换了 存在于mStateStack中的State需要改变 //仍然按照链式父子关系来存储 //先从当前状态S3找到 最近的被激活的parent状态S0 //未被激活的全部保存起来(S3,S1) 返回S0 StateInfo commonStateInfo = setupTempStateStackWithStatesToEnter(destState); //将mStateStack中 不属于当前状态(S3), //关系链上的State(S5,S2)退出(执行exit方法) invokeExitMethods(commonStateInfo); //将S3关系链 加入到栈中(S3,S1) int stateStackEnteringIndex = moveTempStateStackToStateStack(); //将新加入到mStateStack中 未被激活的State激活(S3,S1) invokeEnterMethods(stateStackEnteringIndex); //将延迟的消息移动到消息队列的前面,以便快速得到处理 moveDeferredMessageAtFrontOfQueue(); } }
private final StateInfo setupTempStateStackWithStatesToEnter(State destState) { mTempStateStackCount = 0; StateInfo curStateInfo = mStateInfo.get(destState); do { mTempStateStack[mTempStateStackCount++] = curStateInfo; if (curStateInfo != null) { curStateInfo = curStateInfo.parentStateInfo; } } while ((curStateInfo != null) && !curStateInfo.active); return curStateInfo; }然后弹出mStateStack中保存的原始状态
private final void invokeExitMethods(StateInfo commonStateInfo) { while ((mStateStackTopIndex >= 0) && (mStateStack[mStateStackTopIndex] != commonStateInfo)) { State curState = mStateStack[mStateStackTopIndex].state; if (mDbg) Log.d(TAG, "invokeExitMethods: " + curState.getName()); curState.exit(); mStateStack[mStateStackTopIndex].active = false; mStateStackTopIndex -= 1; } }将新建立的状态节点链表保存到mStateStack栈中
private final int moveTempStateStackToStateStack() { //startingIndex= 0 int startingIndex = mStateStackTopIndex + 1; int i = mTempStateStackCount - 1; int j = startingIndex; while (i >= 0) { if (mDbg) Log.d(TAG, "moveTempStackToStateStack: i=" + i + ",j=" + j); mStateStack[j] = mTempStateStack[i]; j += 1; i -= 1; } mStateStackTopIndex = j - 1; return startingIndex; }初始化入栈的所有新状态,并设置为激活状态
private final void invokeEnterMethods(int stateStackEnteringIndex) { for (int i = stateStackEnteringIndex; i <= mStateStackTopIndex; i++) { if (mDbg) Log.d(TAG, "invokeEnterMethods: " + mStateStack[i].state.getName()); mStateStack[i].state.enter(); mStateStack[i].active = true; } }如何设置目标状态呢?StateMachine提供了transitionTo函数来切换状态
protected final void transitionTo(IState destState) { mSmHandler.transitionTo(destState); }该函数直接调用SmHandler的transitionTo函数来实现,SmHandler的transitionTo函数定义如下:
private final void transitionTo(IState destState) { mDestState = (State) destState; if (mDbg) Log.d(TAG, "transitionTo: destState=" + mDestState.getName()); }这里只是简单地设置了mDestState变量,并未真正更新状态栈 mStateStack,在前面介绍了SmHandler在每次处理消息时都会自动更新一次mStateStack,无论mDestState变量值是否改变。由此可知目标状态的设置与状态栈的更新是异步的。
public final void sendMessage(int what) { // mSmHandler can be null if the state machine has quit. if (mSmHandler == null) return; mSmHandler.sendMessage(obtainMessage(what)); }SmHandler将处理通过SmHandler发送的消息,处理过程如下:
public final void handleMessage(Message msg) { /** Save the current message */ mMsg = msg; if (mIsConstructionCompleted) { //派发当前消息到state中去处理 processMsg(msg); } else if (!mIsConstructionCompleted && (mMsg.what == SM_INIT_CMD) && (mMsg.obj == mSmHandlerObj)) { /** Initial one time path. */ mIsConstructionCompleted = true; invokeEnterMethods(0); } else { throw new RuntimeException("StateMachine.handleMessage: " + "The start method not called, received msg: " + msg); } //消息处理完毕更新mStateStack performTransitions(); }
private final void processMsg(Message msg) { StateInfo curStateInfo = mStateStack[mStateStackTopIndex]; //如果当前状态未处理该消息 while (!curStateInfo.state.processMessage(msg)) { //将消息传给当前状态的父节点处理 curStateInfo = curStateInfo.parentStateInfo; if (curStateInfo == null) { //当前状态无父几点,则丢弃该消息 mSm.unhandledMessage(msg); if (isQuit(msg)) { transitionTo(mQuittingState); } break; } } //记录处理过的消息 if (mSm.recordProcessedMessage(msg)) { if (curStateInfo != null) { State orgState = mStateStack[mStateStackTopIndex].state; mProcessedMessages.add(msg, mSm.getMessageInfo(msg), curStateInfo.state,orgState); } else { mProcessedMessages.add(msg, mSm.getMessageInfo(msg), null, null); } } }