1.hierarchical state machine pattern原理。
http://www.360doc.com/content/10/1105/15/4238731_66855208.shtml
2.android.os.handlerstatemachine(android 2.0中提供)
是
com.android.internal.util.hierarchicalstatemachine(android 2.2.1 froyo中提供)
的簡化版。
hierarchicalstatemachine源代碼參照:
http://blog.chinaunix.net/space.php?uid=17151843&do=blog&id=142162
3. hierarchicalstatemachine是一個機器,這個機器是含有狀态的機器,
機器的每個狀态都可以有父狀态,如果目前狀态需要父狀态作處理, 那麼讓該狀态的processmessage傳回false
4. 目前狀态和它的父狀态們放在mstatestack數組中, active是用來記錄stateinfo包含的state是否是active,在後面計算目前狀态和目标狀态的共同父狀态 過程中使用。
所有狀态和狀态的stateinfo的鍵值對存放在hashmap mstateinfo中。
/** the map of all of the states in the state machine */
private hashmap<hierarchicalstate, stateinfo> mstateinfo =
new hashmap<hierarchicalstate, stateinfo>();
/** stack used to manage the current hierarchy of states */
private stateinfo mstatestack[
] ;
5. 轉換狀态:performtransitions()
a. 尋找目前狀态和目标狀态的共同父狀态,從目标狀态開始循環查詢父狀态的active是否是true,如果是active,說明是目前狀态和目标狀态的父狀态,并依次放入數組mtempstatestack中。
b. 從目前狀态到共同父狀态(不包括本身)依次調用exit,并将active設定為false,将mstatestacktopindex設定成mstatestack數組的個數。
c. 反序将mtempstatestack中的元素拷貝到mstatestack中,并重新設定mstatestacktopindex,并傳回第一個需要調用enter的state的
index startingindex
d. 從共同父狀态到目标狀态依次調用enter
e. 移動arrayliist mdeferredmessages中的deffered message, 從尾部開始将message加入到messagequeue中的第一個,這樣,最先加入mdeferredmessages中的message第一個得到執行。并且deffered message比普通message先執行。