Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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. |
| 15 | */ |
| 16 | |
| 17 | package com.android.internal.util; |
| 18 | |
| 19 | import android.os.Handler; |
| 20 | import android.os.HandlerThread; |
| 21 | import android.os.Looper; |
| 22 | import android.os.Message; |
Wink Saville | 583eaaa | 2012-04-13 16:11:20 -0700 | [diff] [blame] | 23 | import android.text.TextUtils; |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 24 | import android.util.Log; |
| 25 | |
Wink Saville | 583eaaa | 2012-04-13 16:11:20 -0700 | [diff] [blame] | 26 | import java.io.FileDescriptor; |
| 27 | import java.io.PrintWriter; |
Wink Saville | 54b1b1a | 2015-01-12 11:34:20 -0800 | [diff] [blame] | 28 | import java.io.StringWriter; |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 29 | import java.util.ArrayList; |
Wink Saville | 583eaaa | 2012-04-13 16:11:20 -0700 | [diff] [blame] | 30 | import java.util.Calendar; |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 31 | import java.util.Collection; |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 32 | import java.util.HashMap; |
Rebecca Silberstein | 934535b | 2016-05-13 13:57:34 -0700 | [diff] [blame] | 33 | import java.util.Iterator; |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 34 | import java.util.Vector; |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * {@hide} |
| 38 | * |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 39 | * <p>The state machine defined here is a hierarchical state machine which processes messages |
Wink Saville | 33c54e3 | 2010-11-15 10:50:34 -0800 | [diff] [blame] | 40 | * and can have states arranged hierarchically.</p> |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 41 | * |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 42 | * <p>A state is a <code>State</code> object and must implement |
Wink Saville | 33c54e3 | 2010-11-15 10:50:34 -0800 | [diff] [blame] | 43 | * <code>processMessage</code> and optionally <code>enter/exit/getName</code>. |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 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 |
Kevin Cernekee | e318d8c | 2015-06-18 13:23:24 -0700 | [diff] [blame] | 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> |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 50 | * |
Kevin Cernekee | e318d8c | 2015-06-18 13:23:24 -0700 | [diff] [blame] | 51 | * <p>When a state machine is created, <code>addState</code> is used to build the |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 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> |
Wink Saville | cea056f | 2012-03-26 15:03:16 -0700 | [diff] [blame] | 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 |
Kevin Cernekee | e318d8c | 2015-06-18 13:23:24 -0700 | [diff] [blame] | 57 | * of the StateMachine's Handler, not in the context of the call to start, and they |
Wink Saville | cea056f | 2012-03-26 15:03:16 -0700 | [diff] [blame] | 58 | * will be invoked before any messages are processed. For example, given the simple |
Kevin Cernekee | e318d8c | 2015-06-18 13:23:24 -0700 | [diff] [blame] | 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; |
Wink Saville | cea056f | 2012-03-26 15:03:16 -0700 | [diff] [blame] | 61 | * in our simple state machine below that would initially be mS1.processMessage.</p> |
Kevin Cernekee | e318d8c | 2015-06-18 13:23:24 -0700 | [diff] [blame] | 62 | <pre> |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 63 | mP1 |
| 64 | / \ |
Kevin Cernekee | e318d8c | 2015-06-18 13:23:24 -0700 | [diff] [blame] | 65 | mS2 mS1 ----> initial state |
| 66 | </pre> |
Wink Saville | 33c54e3 | 2010-11-15 10:50:34 -0800 | [diff] [blame] | 67 | * <p>After the state machine is created and started, messages are sent to a state |
Wink Saville | a4f3bec | 2010-05-19 09:11:38 -0700 | [diff] [blame] | 68 | * machine using <code>sendMessage</code> and the messages are created using |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 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> |
Kevin Cernekee | e318d8c | 2015-06-18 13:23:24 -0700 | [diff] [blame] | 72 | * to change the current state to a new state.</p> |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 73 | * |
Kevin Cernekee | e318d8c | 2015-06-18 13:23:24 -0700 | [diff] [blame] | 74 | * <p>Each state in the state machine may have a zero or one parent states. If |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 75 | * a child state is unable to handle a message it may have the message processed |
Kevin Cernekee | e318d8c | 2015-06-18 13:23:24 -0700 | [diff] [blame] | 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> |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 79 | * |
Wink Saville | 33c54e3 | 2010-11-15 10:50:34 -0800 | [diff] [blame] | 80 | * <p>When all processing is completed a state machine may choose to call |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 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 |
Wink Saville | 33c54e3 | 2010-11-15 10:50:34 -0800 | [diff] [blame] | 84 | * machine will cause <code>haltedProcessMessage</code> to be invoked.</p> |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 85 | * |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 86 | * <p>If it is desirable to completely stop the state machine call <code>quit</code> or |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 87 | * <code>quitNow</code>. These will call <code>exit</code> of the current state and its parents, |
Kevin Cernekee | e318d8c | 2015-06-18 13:23:24 -0700 | [diff] [blame] | 88 | * call <code>onQuitting</code> and then exit Thread/Loopers.</p> |
Wink Saville | 1b8b98b | 2010-03-11 11:49:54 -0800 | [diff] [blame] | 89 | * |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 90 | * <p>In addition to <code>processMessage</code> each <code>State</code> has |
Kevin Cernekee | e318d8c | 2015-06-18 13:23:24 -0700 | [diff] [blame] | 91 | * an <code>enter</code> method and <code>exit</code> method which may be overridden.</p> |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 92 | * |
Wink Saville | 33c54e3 | 2010-11-15 10:50:34 -0800 | [diff] [blame] | 93 | * <p>Since the states are arranged in a hierarchy transitioning to a new state |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 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 |
Wink Saville | 33c54e3 | 2010-11-15 10:50:34 -0800 | [diff] [blame] | 100 | * are entered.</p> |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 101 | * |
Wink Saville | 33c54e3 | 2010-11-15 10:50:34 -0800 | [diff] [blame] | 102 | * <p>Two other methods that states can use are <code>deferMessage</code> and |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 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 |
Wink Saville | 33c54e3 | 2010-11-15 10:50:34 -0800 | [diff] [blame] | 110 | * these are protected and may only be invoked from within a state machine.</p> |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 111 | * |
Wink Saville | 33c54e3 | 2010-11-15 10:50:34 -0800 | [diff] [blame] | 112 | * <p>To illustrate some of these properties we'll use state machine with an 8 |
| 113 | * state hierarchy:</p> |
Kevin Cernekee | e318d8c | 2015-06-18 13:23:24 -0700 | [diff] [blame] | 114 | <pre> |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 115 | mP0 |
| 116 | / \ |
| 117 | mP1 mS0 |
| 118 | / \ |
| 119 | mS2 mS1 |
| 120 | / \ \ |
Kevin Cernekee | e318d8c | 2015-06-18 13:23:24 -0700 | [diff] [blame] | 121 | mS3 mS4 mS5 ---> initial state |
| 122 | </pre> |
Wink Saville | 33c54e3 | 2010-11-15 10:50:34 -0800 | [diff] [blame] | 123 | * <p>After starting mS5 the list of active states is mP0, mP1, mS1 and mS5. |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 124 | * So the order of calling processMessage when a message is received is mS5, |
Wink Saville | a4f3bec | 2010-05-19 09:11:38 -0700 | [diff] [blame] | 125 | * mS1, mP1, mP0 assuming each processMessage indicates it can't handle this |
Wink Saville | 33c54e3 | 2010-11-15 10:50:34 -0800 | [diff] [blame] | 126 | * message by returning false or NOT_HANDLED.</p> |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 127 | * |
Wink Saville | 33c54e3 | 2010-11-15 10:50:34 -0800 | [diff] [blame] | 128 | * <p>Now assume mS5.processMessage receives a message it can handle, and during |
Wink Saville | a4f3bec | 2010-05-19 09:11:38 -0700 | [diff] [blame] | 129 | * the handling determines the machine should change states. It could call |
| 130 | * transitionTo(mS4) and return true or HANDLED. Immediately after returning from |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 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 |
Wink Saville | 33c54e3 | 2010-11-15 10:50:34 -0800 | [diff] [blame] | 134 | * when the next message is received mS4.processMessage will be invoked.</p> |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 135 | * |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 136 | * <p>Now for some concrete examples, here is the canonical HelloWorld as a state machine. |
Wink Saville | 33c54e3 | 2010-11-15 10:50:34 -0800 | [diff] [blame] | 137 | * It responds with "Hello World" being printed to the log for every message.</p> |
Kevin Cernekee | e318d8c | 2015-06-18 13:23:24 -0700 | [diff] [blame] | 138 | <pre> |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 139 | class HelloWorld extends StateMachine { |
| 140 | HelloWorld(String name) { |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 141 | super(name); |
| 142 | addState(mState1); |
| 143 | setInitialState(mState1); |
| 144 | } |
| 145 | |
| 146 | public static HelloWorld makeHelloWorld() { |
| 147 | HelloWorld hw = new HelloWorld("hw"); |
| 148 | hw.start(); |
| 149 | return hw; |
| 150 | } |
| 151 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 152 | class State1 extends State { |
Joe Onorato | 0b6232d | 2010-09-16 11:55:35 -0400 | [diff] [blame] | 153 | @Override public boolean processMessage(Message message) { |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 154 | log("Hello World"); |
Wink Saville | a4f3bec | 2010-05-19 09:11:38 -0700 | [diff] [blame] | 155 | return HANDLED; |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | State1 mState1 = new State1(); |
| 159 | } |
| 160 | |
| 161 | void testHelloWorld() { |
| 162 | HelloWorld hw = makeHelloWorld(); |
| 163 | hw.sendMessage(hw.obtainMessage()); |
| 164 | } |
Kevin Cernekee | e318d8c | 2015-06-18 13:23:24 -0700 | [diff] [blame] | 165 | </pre> |
Wink Saville | 33c54e3 | 2010-11-15 10:50:34 -0800 | [diff] [blame] | 166 | * <p>A more interesting state machine is one with four states |
| 167 | * with two independent parent states.</p> |
Kevin Cernekee | e318d8c | 2015-06-18 13:23:24 -0700 | [diff] [blame] | 168 | <pre> |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 169 | mP1 mP2 |
| 170 | / \ |
| 171 | mS2 mS1 |
Kevin Cernekee | e318d8c | 2015-06-18 13:23:24 -0700 | [diff] [blame] | 172 | </pre> |
Wink Saville | 33c54e3 | 2010-11-15 10:50:34 -0800 | [diff] [blame] | 173 | * <p>Here is a description of this state machine using pseudo code.</p> |
Kevin Cernekee | e318d8c | 2015-06-18 13:23:24 -0700 | [diff] [blame] | 174 | <pre> |
Wink Saville | 33c54e3 | 2010-11-15 10:50:34 -0800 | [diff] [blame] | 175 | state mP1 { |
| 176 | enter { log("mP1.enter"); } |
| 177 | exit { log("mP1.exit"); } |
| 178 | on msg { |
| 179 | CMD_2 { |
| 180 | send(CMD_3); |
| 181 | defer(msg); |
Kevin Cernekee | e318d8c | 2015-06-18 13:23:24 -0700 | [diff] [blame] | 182 | transitionTo(mS2); |
Wink Saville | 33c54e3 | 2010-11-15 10:50:34 -0800 | [diff] [blame] | 183 | return HANDLED; |
| 184 | } |
| 185 | return NOT_HANDLED; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | INITIAL |
| 190 | state mS1 parent mP1 { |
| 191 | enter { log("mS1.enter"); } |
| 192 | exit { log("mS1.exit"); } |
| 193 | on msg { |
| 194 | CMD_1 { |
| 195 | transitionTo(mS1); |
| 196 | return HANDLED; |
| 197 | } |
| 198 | return NOT_HANDLED; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | state mS2 parent mP1 { |
| 203 | enter { log("mS2.enter"); } |
| 204 | exit { log("mS2.exit"); } |
| 205 | on msg { |
| 206 | CMD_2 { |
| 207 | send(CMD_4); |
| 208 | return HANDLED; |
| 209 | } |
| 210 | CMD_3 { |
| 211 | defer(msg); |
| 212 | transitionTo(mP2); |
| 213 | return HANDLED; |
| 214 | } |
| 215 | return NOT_HANDLED; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | state mP2 { |
| 220 | enter { |
| 221 | log("mP2.enter"); |
| 222 | send(CMD_5); |
| 223 | } |
| 224 | exit { log("mP2.exit"); } |
| 225 | on msg { |
| 226 | CMD_3, CMD_4 { return HANDLED; } |
| 227 | CMD_5 { |
| 228 | transitionTo(HaltingState); |
| 229 | return HANDLED; |
| 230 | } |
| 231 | return NOT_HANDLED; |
| 232 | } |
| 233 | } |
Kevin Cernekee | e318d8c | 2015-06-18 13:23:24 -0700 | [diff] [blame] | 234 | </pre> |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 235 | * <p>The implementation is below and also in StateMachineTest:</p> |
Kevin Cernekee | e318d8c | 2015-06-18 13:23:24 -0700 | [diff] [blame] | 236 | <pre> |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 237 | class Hsm1 extends StateMachine { |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 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; |
| 243 | |
| 244 | public static Hsm1 makeHsm1() { |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 245 | log("makeHsm1 E"); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 246 | Hsm1 sm = new Hsm1("hsm1"); |
| 247 | sm.start(); |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 248 | log("makeHsm1 X"); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 249 | return sm; |
| 250 | } |
| 251 | |
| 252 | Hsm1(String name) { |
| 253 | super(name); |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 254 | log("ctor E"); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 255 | |
| 256 | // Add states, use indentation to show hierarchy |
| 257 | addState(mP1); |
| 258 | addState(mS1, mP1); |
| 259 | addState(mS2, mP1); |
| 260 | addState(mP2); |
| 261 | |
| 262 | // Set the initial state |
| 263 | setInitialState(mS1); |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 264 | log("ctor X"); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 265 | } |
| 266 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 267 | class P1 extends State { |
Joe Onorato | 0b6232d | 2010-09-16 11:55:35 -0400 | [diff] [blame] | 268 | @Override public void enter() { |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 269 | log("mP1.enter"); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 270 | } |
Joe Onorato | 0b6232d | 2010-09-16 11:55:35 -0400 | [diff] [blame] | 271 | @Override public boolean processMessage(Message message) { |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 272 | boolean retVal; |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 273 | log("mP1.processMessage what=" + message.what); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 274 | switch(message.what) { |
| 275 | case CMD_2: |
| 276 | // CMD_2 will arrive in mS2 before CMD_3 |
| 277 | sendMessage(obtainMessage(CMD_3)); |
| 278 | deferMessage(message); |
| 279 | transitionTo(mS2); |
Wink Saville | a4f3bec | 2010-05-19 09:11:38 -0700 | [diff] [blame] | 280 | retVal = HANDLED; |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 281 | break; |
| 282 | default: |
| 283 | // Any message we don't understand in this state invokes unhandledMessage |
Wink Saville | a4f3bec | 2010-05-19 09:11:38 -0700 | [diff] [blame] | 284 | retVal = NOT_HANDLED; |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 285 | break; |
| 286 | } |
| 287 | return retVal; |
| 288 | } |
Joe Onorato | 0b6232d | 2010-09-16 11:55:35 -0400 | [diff] [blame] | 289 | @Override public void exit() { |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 290 | log("mP1.exit"); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 294 | class S1 extends State { |
Joe Onorato | 0b6232d | 2010-09-16 11:55:35 -0400 | [diff] [blame] | 295 | @Override public void enter() { |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 296 | log("mS1.enter"); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 297 | } |
Joe Onorato | 0b6232d | 2010-09-16 11:55:35 -0400 | [diff] [blame] | 298 | @Override public boolean processMessage(Message message) { |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 299 | log("S1.processMessage what=" + message.what); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 300 | if (message.what == CMD_1) { |
| 301 | // Transition to ourself to show that enter/exit is called |
| 302 | transitionTo(mS1); |
Wink Saville | a4f3bec | 2010-05-19 09:11:38 -0700 | [diff] [blame] | 303 | return HANDLED; |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 304 | } else { |
| 305 | // Let parent process all other messages |
Wink Saville | a4f3bec | 2010-05-19 09:11:38 -0700 | [diff] [blame] | 306 | return NOT_HANDLED; |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 307 | } |
| 308 | } |
Joe Onorato | 0b6232d | 2010-09-16 11:55:35 -0400 | [diff] [blame] | 309 | @Override public void exit() { |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 310 | log("mS1.exit"); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 314 | class S2 extends State { |
Joe Onorato | 0b6232d | 2010-09-16 11:55:35 -0400 | [diff] [blame] | 315 | @Override public void enter() { |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 316 | log("mS2.enter"); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 317 | } |
Joe Onorato | 0b6232d | 2010-09-16 11:55:35 -0400 | [diff] [blame] | 318 | @Override public boolean processMessage(Message message) { |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 319 | boolean retVal; |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 320 | log("mS2.processMessage what=" + message.what); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 321 | switch(message.what) { |
| 322 | case(CMD_2): |
| 323 | sendMessage(obtainMessage(CMD_4)); |
Wink Saville | a4f3bec | 2010-05-19 09:11:38 -0700 | [diff] [blame] | 324 | retVal = HANDLED; |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 325 | break; |
| 326 | case(CMD_3): |
| 327 | deferMessage(message); |
| 328 | transitionTo(mP2); |
Wink Saville | a4f3bec | 2010-05-19 09:11:38 -0700 | [diff] [blame] | 329 | retVal = HANDLED; |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 330 | break; |
| 331 | default: |
Wink Saville | a4f3bec | 2010-05-19 09:11:38 -0700 | [diff] [blame] | 332 | retVal = NOT_HANDLED; |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 333 | break; |
| 334 | } |
| 335 | return retVal; |
| 336 | } |
Joe Onorato | 0b6232d | 2010-09-16 11:55:35 -0400 | [diff] [blame] | 337 | @Override public void exit() { |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 338 | log("mS2.exit"); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 342 | class P2 extends State { |
Joe Onorato | 0b6232d | 2010-09-16 11:55:35 -0400 | [diff] [blame] | 343 | @Override public void enter() { |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 344 | log("mP2.enter"); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 345 | sendMessage(obtainMessage(CMD_5)); |
| 346 | } |
Joe Onorato | 0b6232d | 2010-09-16 11:55:35 -0400 | [diff] [blame] | 347 | @Override public boolean processMessage(Message message) { |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 348 | log("P2.processMessage what=" + message.what); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 349 | switch(message.what) { |
| 350 | case(CMD_3): |
| 351 | break; |
| 352 | case(CMD_4): |
| 353 | break; |
| 354 | case(CMD_5): |
| 355 | transitionToHaltingState(); |
| 356 | break; |
| 357 | } |
Wink Saville | a4f3bec | 2010-05-19 09:11:38 -0700 | [diff] [blame] | 358 | return HANDLED; |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 359 | } |
Joe Onorato | 0b6232d | 2010-09-16 11:55:35 -0400 | [diff] [blame] | 360 | @Override public void exit() { |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 361 | log("mP2.exit"); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
Joe Onorato | 0b6232d | 2010-09-16 11:55:35 -0400 | [diff] [blame] | 365 | @Override |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 366 | void onHalting() { |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 367 | log("halting"); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 368 | synchronized (this) { |
| 369 | this.notifyAll(); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | P1 mP1 = new P1(); |
| 374 | S1 mS1 = new S1(); |
| 375 | S2 mS2 = new S2(); |
| 376 | P2 mP2 = new P2(); |
| 377 | } |
Kevin Cernekee | e318d8c | 2015-06-18 13:23:24 -0700 | [diff] [blame] | 378 | </pre> |
Wink Saville | 33c54e3 | 2010-11-15 10:50:34 -0800 | [diff] [blame] | 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> |
Kevin Cernekee | e318d8c | 2015-06-18 13:23:24 -0700 | [diff] [blame] | 381 | <pre> |
Wink Saville | 33c54e3 | 2010-11-15 10:50:34 -0800 | [diff] [blame] | 382 | Hsm1 hsm = makeHsm1(); |
| 383 | synchronize(hsm) { |
| 384 | hsm.sendMessage(obtainMessage(hsm.CMD_1)); |
| 385 | hsm.sendMessage(obtainMessage(hsm.CMD_2)); |
| 386 | try { |
| 387 | // wait for the messages to be handled |
| 388 | hsm.wait(); |
| 389 | } catch (InterruptedException e) { |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 390 | loge("exception while waiting " + e.getMessage()); |
Wink Saville | 33c54e3 | 2010-11-15 10:50:34 -0800 | [diff] [blame] | 391 | } |
| 392 | } |
Kevin Cernekee | e318d8c | 2015-06-18 13:23:24 -0700 | [diff] [blame] | 393 | </pre> |
Wink Saville | 33c54e3 | 2010-11-15 10:50:34 -0800 | [diff] [blame] | 394 | * <p>The output is:</p> |
Kevin Cernekee | e318d8c | 2015-06-18 13:23:24 -0700 | [diff] [blame] | 395 | <pre> |
Wink Saville | 33c54e3 | 2010-11-15 10:50:34 -0800 | [diff] [blame] | 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 |
Kevin Cernekee | e318d8c | 2015-06-18 13:23:24 -0700 | [diff] [blame] | 419 | </pre> |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 420 | */ |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 421 | public class StateMachine { |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 422 | // Name of the state machine and used as logging tag |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 423 | private String mName; |
| 424 | |
Wink Saville | a4f3bec | 2010-05-19 09:11:38 -0700 | [diff] [blame] | 425 | /** Message.what value when quitting */ |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 426 | private static final int SM_QUIT_CMD = -1; |
Wink Saville | 1b8b98b | 2010-03-11 11:49:54 -0800 | [diff] [blame] | 427 | |
Wink Saville | a4f3bec | 2010-05-19 09:11:38 -0700 | [diff] [blame] | 428 | /** Message.what value when initializing */ |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 429 | private static final int SM_INIT_CMD = -2; |
Wink Saville | a4f3bec | 2010-05-19 09:11:38 -0700 | [diff] [blame] | 430 | |
| 431 | /** |
| 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 |
| 435 | */ |
| 436 | public static final boolean HANDLED = true; |
| 437 | |
| 438 | /** |
| 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 |
| 442 | */ |
| 443 | public static final boolean NOT_HANDLED = false; |
| 444 | |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 445 | /** |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 446 | * StateMachine logging record. |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 447 | * {@hide} |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 448 | */ |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 449 | public static class LogRec { |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 450 | private StateMachine mSm; |
Wink Saville | 583eaaa | 2012-04-13 16:11:20 -0700 | [diff] [blame] | 451 | private long mTime; |
| 452 | private int mWhat; |
| 453 | private String mInfo; |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 454 | private IState mState; |
| 455 | private IState mOrgState; |
| 456 | private IState mDstState; |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 457 | |
| 458 | /** |
| 459 | * Constructor |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 460 | * |
| 461 | * @param msg |
Irfan Sheriff | ef8da9f | 2012-11-29 09:42:13 -0800 | [diff] [blame] | 462 | * @param state the state which handled the message |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 463 | * @param orgState is the first state the received the message but |
| 464 | * did not processes the message. |
Irfan Sheriff | ef8da9f | 2012-11-29 09:42:13 -0800 | [diff] [blame] | 465 | * @param transToState is the state that was transitioned to after the message was |
| 466 | * processed. |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 467 | */ |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 468 | LogRec(StateMachine sm, Message msg, String info, IState state, IState orgState, |
| 469 | IState transToState) { |
| 470 | update(sm, msg, info, state, orgState, transToState); |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Update the information in the record. |
| 475 | * @param state that handled the message |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 476 | * @param orgState is the first state the received the message |
| 477 | * @param dstState is the state that was the transition target when logging |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 478 | */ |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 479 | public void update(StateMachine sm, Message msg, String info, IState state, IState orgState, |
| 480 | IState dstState) { |
| 481 | mSm = sm; |
Wink Saville | 583eaaa | 2012-04-13 16:11:20 -0700 | [diff] [blame] | 482 | mTime = System.currentTimeMillis(); |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 483 | mWhat = (msg != null) ? msg.what : 0; |
Wink Saville | 583eaaa | 2012-04-13 16:11:20 -0700 | [diff] [blame] | 484 | mInfo = info; |
| 485 | mState = state; |
| 486 | mOrgState = orgState; |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 487 | mDstState = dstState; |
Wink Saville | 583eaaa | 2012-04-13 16:11:20 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | /** |
| 491 | * @return time stamp |
| 492 | */ |
| 493 | public long getTime() { |
| 494 | return mTime; |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * @return msg.what |
| 499 | */ |
| 500 | public long getWhat() { |
| 501 | return mWhat; |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | /** |
| 505 | * @return the command that was executing |
| 506 | */ |
Wink Saville | 583eaaa | 2012-04-13 16:11:20 -0700 | [diff] [blame] | 507 | public String getInfo() { |
| 508 | return mInfo; |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | /** |
| 512 | * @return the state that handled this message |
| 513 | */ |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 514 | public IState getState() { |
Wink Saville | 583eaaa | 2012-04-13 16:11:20 -0700 | [diff] [blame] | 515 | return mState; |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /** |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 519 | * @return the state destination state if a transition is occurring or null if none. |
| 520 | */ |
| 521 | public IState getDestState() { |
| 522 | return mDstState; |
| 523 | } |
| 524 | |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 525 | /** |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 526 | * @return the original state that received the message. |
| 527 | */ |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 528 | public IState getOriginalState() { |
Wink Saville | 583eaaa | 2012-04-13 16:11:20 -0700 | [diff] [blame] | 529 | return mOrgState; |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 532 | @Override |
| 533 | public String toString() { |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 534 | StringBuilder sb = new StringBuilder(); |
Wink Saville | 583eaaa | 2012-04-13 16:11:20 -0700 | [diff] [blame] | 535 | sb.append("time="); |
| 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)); |
Irfan Sheriff | ef8da9f | 2012-11-29 09:42:13 -0800 | [diff] [blame] | 539 | sb.append(" processed="); |
Wink Saville | 583eaaa | 2012-04-13 16:11:20 -0700 | [diff] [blame] | 540 | sb.append(mState == null ? "<null>" : mState.getName()); |
Irfan Sheriff | ef8da9f | 2012-11-29 09:42:13 -0800 | [diff] [blame] | 541 | sb.append(" org="); |
Wink Saville | 583eaaa | 2012-04-13 16:11:20 -0700 | [diff] [blame] | 542 | sb.append(mOrgState == null ? "<null>" : mOrgState.getName()); |
Irfan Sheriff | ef8da9f | 2012-11-29 09:42:13 -0800 | [diff] [blame] | 543 | sb.append(" dest="); |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 544 | sb.append(mDstState == null ? "<null>" : mDstState.getName()); |
Wink Saville | 583eaaa | 2012-04-13 16:11:20 -0700 | [diff] [blame] | 545 | sb.append(" what="); |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 546 | String what = mSm != null ? mSm.getWhatToString(mWhat) : ""; |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 547 | if (TextUtils.isEmpty(what)) { |
| 548 | sb.append(mWhat); |
| 549 | sb.append("(0x"); |
| 550 | sb.append(Integer.toHexString(mWhat)); |
| 551 | sb.append(")"); |
| 552 | } else { |
| 553 | sb.append(what); |
| 554 | } |
Wink Saville | ff4fcdb | 2013-02-24 07:21:45 -0800 | [diff] [blame] | 555 | if (!TextUtils.isEmpty(mInfo)) { |
Wink Saville | 583eaaa | 2012-04-13 16:11:20 -0700 | [diff] [blame] | 556 | sb.append(" "); |
| 557 | sb.append(mInfo); |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 558 | } |
Wink Saville | 583eaaa | 2012-04-13 16:11:20 -0700 | [diff] [blame] | 559 | return sb.toString(); |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | |
| 563 | /** |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 564 | * A list of log records including messages recently processed by the state machine. |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 565 | * |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 566 | * The class maintains a list of log records including messages |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 567 | * recently processed. The list is finite and may be set in the |
| 568 | * constructor or by calling setSize. The public interface also |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 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. |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 573 | */ |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 574 | private static class LogRecords { |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 575 | |
| 576 | private static final int DEFAULT_SIZE = 20; |
| 577 | |
Irfan Sheriff | ef8da9f | 2012-11-29 09:42:13 -0800 | [diff] [blame] | 578 | private Vector<LogRec> mLogRecVector = new Vector<LogRec>(); |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 579 | private int mMaxSize = DEFAULT_SIZE; |
| 580 | private int mOldestIndex = 0; |
| 581 | private int mCount = 0; |
Irfan Sheriff | ef8da9f | 2012-11-29 09:42:13 -0800 | [diff] [blame] | 582 | private boolean mLogOnlyTransitions = false; |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 583 | |
| 584 | /** |
Wink Saville | 583eaaa | 2012-04-13 16:11:20 -0700 | [diff] [blame] | 585 | * private constructor use add |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 586 | */ |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 587 | private LogRecords() { |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | /** |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 591 | * Set size of messages to maintain and clears all current records. |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 592 | * |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 593 | * @param maxSize number of records to maintain at anyone time. |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 594 | */ |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 595 | synchronized void setSize(int maxSize) { |
Samuel Tan | 2326e9b | 2016-04-15 13:06:12 -0700 | [diff] [blame] | 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 |
| 598 | // expected. |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 599 | mMaxSize = maxSize; |
Samuel Tan | 2326e9b | 2016-04-15 13:06:12 -0700 | [diff] [blame] | 600 | mOldestIndex = 0; |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 601 | mCount = 0; |
Irfan Sheriff | ef8da9f | 2012-11-29 09:42:13 -0800 | [diff] [blame] | 602 | mLogRecVector.clear(); |
| 603 | } |
| 604 | |
| 605 | synchronized void setLogOnlyTransitions(boolean enable) { |
| 606 | mLogOnlyTransitions = enable; |
| 607 | } |
| 608 | |
| 609 | synchronized boolean logOnlyTransitions() { |
| 610 | return mLogOnlyTransitions; |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | /** |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 614 | * @return the number of recent records. |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 615 | */ |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 616 | synchronized int size() { |
Irfan Sheriff | ef8da9f | 2012-11-29 09:42:13 -0800 | [diff] [blame] | 617 | return mLogRecVector.size(); |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | /** |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 621 | * @return the total number of records processed since size was set. |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 622 | */ |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 623 | synchronized int count() { |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 624 | return mCount; |
| 625 | } |
| 626 | |
| 627 | /** |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 628 | * Clear the list of records. |
Jaikumar Ganesh | 6f9a616 | 2011-11-28 13:13:02 -0800 | [diff] [blame] | 629 | */ |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 630 | synchronized void cleanup() { |
Irfan Sheriff | ef8da9f | 2012-11-29 09:42:13 -0800 | [diff] [blame] | 631 | mLogRecVector.clear(); |
Jaikumar Ganesh | 6f9a616 | 2011-11-28 13:13:02 -0800 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | /** |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 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. |
| 638 | */ |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 639 | synchronized LogRec get(int index) { |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 640 | int nextIndex = mOldestIndex + index; |
| 641 | if (nextIndex >= mMaxSize) { |
| 642 | nextIndex -= mMaxSize; |
| 643 | } |
| 644 | if (nextIndex >= size()) { |
| 645 | return null; |
| 646 | } else { |
Irfan Sheriff | ef8da9f | 2012-11-29 09:42:13 -0800 | [diff] [blame] | 647 | return mLogRecVector.get(nextIndex); |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * Add a processed message. |
| 653 | * |
Wink Saville | 583eaaa | 2012-04-13 16:11:20 -0700 | [diff] [blame] | 654 | * @param msg |
| 655 | * @param messageInfo to be stored |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 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. |
Irfan Sheriff | ef8da9f | 2012-11-29 09:42:13 -0800 | [diff] [blame] | 659 | * @param transToState is the state that was transitioned to after the message was |
| 660 | * processed. |
| 661 | * |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 662 | */ |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 663 | synchronized void add(StateMachine sm, Message msg, String messageInfo, IState state, |
Wink Saville | ff4fcdb | 2013-02-24 07:21:45 -0800 | [diff] [blame] | 664 | IState orgState, IState transToState) { |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 665 | mCount += 1; |
Irfan Sheriff | ef8da9f | 2012-11-29 09:42:13 -0800 | [diff] [blame] | 666 | if (mLogRecVector.size() < mMaxSize) { |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 667 | mLogRecVector.add(new LogRec(sm, msg, messageInfo, state, orgState, transToState)); |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 668 | } else { |
Irfan Sheriff | ef8da9f | 2012-11-29 09:42:13 -0800 | [diff] [blame] | 669 | LogRec pmi = mLogRecVector.get(mOldestIndex); |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 670 | mOldestIndex += 1; |
| 671 | if (mOldestIndex >= mMaxSize) { |
| 672 | mOldestIndex = 0; |
| 673 | } |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 674 | pmi.update(sm, msg, messageInfo, state, orgState, transToState); |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | } |
| 678 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 679 | private static class SmHandler extends Handler { |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 680 | |
Wink Saville | 03812c7 | 2013-04-16 13:21:00 -0700 | [diff] [blame] | 681 | /** true if StateMachine has quit */ |
| 682 | private boolean mHasQuit = false; |
| 683 | |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 684 | /** The debug flag */ |
| 685 | private boolean mDbg = false; |
| 686 | |
Wink Saville | cea056f | 2012-03-26 15:03:16 -0700 | [diff] [blame] | 687 | /** The SmHandler object, identifies that message is internal */ |
| 688 | private static final Object mSmHandlerObj = new Object(); |
Wink Saville | 1b8b98b | 2010-03-11 11:49:54 -0800 | [diff] [blame] | 689 | |
Wink Saville | a4f3bec | 2010-05-19 09:11:38 -0700 | [diff] [blame] | 690 | /** The current message */ |
| 691 | private Message mMsg; |
| 692 | |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 693 | /** A list of log records including messages this state machine has processed */ |
| 694 | private LogRecords mLogRecords = new LogRecords(); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 695 | |
| 696 | /** true if construction of the state machine has not been completed */ |
| 697 | private boolean mIsConstructionCompleted; |
| 698 | |
| 699 | /** Stack used to manage the current hierarchy of states */ |
| 700 | private StateInfo mStateStack[]; |
| 701 | |
| 702 | /** Top of mStateStack */ |
| 703 | private int mStateStackTopIndex = -1; |
| 704 | |
| 705 | /** A temporary stack used to manage the state stack */ |
| 706 | private StateInfo mTempStateStack[]; |
| 707 | |
| 708 | /** The top of the mTempStateStack */ |
| 709 | private int mTempStateStackCount; |
| 710 | |
| 711 | /** State used when state machine is halted */ |
| 712 | private HaltingState mHaltingState = new HaltingState(); |
| 713 | |
Wink Saville | 1b8b98b | 2010-03-11 11:49:54 -0800 | [diff] [blame] | 714 | /** State used when state machine is quitting */ |
| 715 | private QuittingState mQuittingState = new QuittingState(); |
| 716 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 717 | /** Reference to the StateMachine */ |
| 718 | private StateMachine mSm; |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 719 | |
| 720 | /** |
| 721 | * Information about a state. |
| 722 | * Used to maintain the hierarchy. |
| 723 | */ |
| 724 | private class StateInfo { |
| 725 | /** The state */ |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 726 | State state; |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 727 | |
| 728 | /** The parent of this state, null if there is no parent */ |
| 729 | StateInfo parentStateInfo; |
| 730 | |
| 731 | /** True when the state has been entered and on the stack */ |
| 732 | boolean active; |
| 733 | |
| 734 | /** |
| 735 | * Convert StateInfo to string |
| 736 | */ |
| 737 | @Override |
| 738 | public String toString() { |
Wink Saville | ff4fcdb | 2013-02-24 07:21:45 -0800 | [diff] [blame] | 739 | return "state=" + state.getName() + ",active=" + active + ",parent=" |
| 740 | + ((parentStateInfo == null) ? "null" : parentStateInfo.state.getName()); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 741 | } |
| 742 | } |
| 743 | |
| 744 | /** The map of all of the states in the state machine */ |
Wink Saville | ff4fcdb | 2013-02-24 07:21:45 -0800 | [diff] [blame] | 745 | private HashMap<State, StateInfo> mStateInfo = new HashMap<State, StateInfo>(); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 746 | |
| 747 | /** The initial state that will process the first message */ |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 748 | private State mInitialState; |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 749 | |
| 750 | /** The destination state when transitionTo has been invoked */ |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 751 | private State mDestState; |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 752 | |
Mitchell Wills | 07e317c | 2016-08-11 11:05:03 -0700 | [diff] [blame] | 753 | /** |
| 754 | * Indicates if a transition is in progress |
| 755 | * |
| 756 | * This will be true for all calls of State.exit and all calls of State.enter except for the |
| 757 | * last enter call for the current destination state. |
| 758 | */ |
| 759 | private boolean mTransitionInProgress = false; |
| 760 | |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 761 | /** The list of deferred messages */ |
| 762 | private ArrayList<Message> mDeferredMessages = new ArrayList<Message>(); |
| 763 | |
| 764 | /** |
| 765 | * State entered when transitionToHaltingState is called. |
| 766 | */ |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 767 | private class HaltingState extends State { |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 768 | @Override |
| 769 | public boolean processMessage(Message msg) { |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 770 | mSm.haltedProcessMessage(msg); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 771 | return true; |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | /** |
Wink Saville | 1b8b98b | 2010-03-11 11:49:54 -0800 | [diff] [blame] | 776 | * State entered when a valid quit message is handled. |
| 777 | */ |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 778 | private class QuittingState extends State { |
Wink Saville | 1b8b98b | 2010-03-11 11:49:54 -0800 | [diff] [blame] | 779 | @Override |
| 780 | public boolean processMessage(Message msg) { |
Wink Saville | a4f3bec | 2010-05-19 09:11:38 -0700 | [diff] [blame] | 781 | return NOT_HANDLED; |
Wink Saville | 1b8b98b | 2010-03-11 11:49:54 -0800 | [diff] [blame] | 782 | } |
| 783 | } |
| 784 | |
| 785 | /** |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 786 | * Handle messages sent to the state machine by calling |
| 787 | * the current state's processMessage. It also handles |
| 788 | * the enter/exit calls and placing any deferred messages |
| 789 | * back onto the queue when transitioning to a new state. |
| 790 | */ |
| 791 | @Override |
| 792 | public final void handleMessage(Message msg) { |
Wink Saville | 03812c7 | 2013-04-16 13:21:00 -0700 | [diff] [blame] | 793 | if (!mHasQuit) { |
Hall Liu | 74d7d0f | 2016-01-04 15:17:53 -0800 | [diff] [blame] | 794 | if (mSm != null && msg.what != SM_INIT_CMD && msg.what != SM_QUIT_CMD) { |
Brad Ebinger | 355f110 | 2015-12-14 08:46:49 -0800 | [diff] [blame] | 795 | mSm.onPreHandleMessage(msg); |
| 796 | } |
| 797 | |
Wink Saville | 03812c7 | 2013-04-16 13:21:00 -0700 | [diff] [blame] | 798 | if (mDbg) mSm.log("handleMessage: E msg.what=" + msg.what); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 799 | |
Wink Saville | 03812c7 | 2013-04-16 13:21:00 -0700 | [diff] [blame] | 800 | /** Save the current message */ |
| 801 | mMsg = msg; |
Wink Saville | a4f3bec | 2010-05-19 09:11:38 -0700 | [diff] [blame] | 802 | |
Wink Saville | 03812c7 | 2013-04-16 13:21:00 -0700 | [diff] [blame] | 803 | /** State that processed the message */ |
| 804 | State msgProcessedState = null; |
| 805 | if (mIsConstructionCompleted) { |
| 806 | /** Normal path */ |
| 807 | msgProcessedState = processMsg(msg); |
| 808 | } else if (!mIsConstructionCompleted && (mMsg.what == SM_INIT_CMD) |
| 809 | && (mMsg.obj == mSmHandlerObj)) { |
| 810 | /** Initial one time path. */ |
| 811 | mIsConstructionCompleted = true; |
| 812 | invokeEnterMethods(0); |
| 813 | } else { |
| 814 | throw new RuntimeException("StateMachine.handleMessage: " |
| 815 | + "The start method not called, received msg: " + msg); |
| 816 | } |
| 817 | performTransitions(msgProcessedState, msg); |
| 818 | |
| 819 | // We need to check if mSm == null here as we could be quitting. |
| 820 | if (mDbg && mSm != null) mSm.log("handleMessage: X"); |
Brad Ebinger | 355f110 | 2015-12-14 08:46:49 -0800 | [diff] [blame] | 821 | |
Hall Liu | 74d7d0f | 2016-01-04 15:17:53 -0800 | [diff] [blame] | 822 | if (mSm != null && msg.what != SM_INIT_CMD && msg.what != SM_QUIT_CMD) { |
Brad Ebinger | 355f110 | 2015-12-14 08:46:49 -0800 | [diff] [blame] | 823 | mSm.onPostHandleMessage(msg); |
| 824 | } |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 825 | } |
Wink Saville | e7be6a8 | 2010-03-18 17:03:30 -0700 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | /** |
| 829 | * Do any transitions |
Irfan Sheriff | ef8da9f | 2012-11-29 09:42:13 -0800 | [diff] [blame] | 830 | * @param msgProcessedState is the state that processed the message |
Wink Saville | e7be6a8 | 2010-03-18 17:03:30 -0700 | [diff] [blame] | 831 | */ |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 832 | private void performTransitions(State msgProcessedState, Message msg) { |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 833 | /** |
| 834 | * If transitionTo has been called, exit and then enter |
Wink Saville | e7be6a8 | 2010-03-18 17:03:30 -0700 | [diff] [blame] | 835 | * the appropriate states. We loop on this to allow |
| 836 | * enter and exit methods to use transitionTo. |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 837 | */ |
Irfan Sheriff | ef8da9f | 2012-11-29 09:42:13 -0800 | [diff] [blame] | 838 | State orgState = mStateStack[mStateStackTopIndex].state; |
| 839 | |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 840 | /** |
| 841 | * Record whether message needs to be logged before we transition and |
| 842 | * and we won't log special messages SM_INIT_CMD or SM_QUIT_CMD which |
| 843 | * always set msg.obj to the handler. |
| 844 | */ |
Wink Saville | f643069 | 2013-02-11 16:16:02 -0800 | [diff] [blame] | 845 | boolean recordLogMsg = mSm.recordLogRec(mMsg) && (msg.obj != mSmHandlerObj); |
Irfan Sheriff | ef8da9f | 2012-11-29 09:42:13 -0800 | [diff] [blame] | 846 | |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 847 | if (mLogRecords.logOnlyTransitions()) { |
| 848 | /** Record only if there is a transition */ |
| 849 | if (mDestState != null) { |
| 850 | mLogRecords.add(mSm, mMsg, mSm.getLogRecString(mMsg), msgProcessedState, |
| 851 | orgState, mDestState); |
| 852 | } |
| 853 | } else if (recordLogMsg) { |
| 854 | /** Record message */ |
Wink Saville | ff4fcdb | 2013-02-24 07:21:45 -0800 | [diff] [blame] | 855 | mLogRecords.add(mSm, mMsg, mSm.getLogRecString(mMsg), msgProcessedState, orgState, |
| 856 | mDestState); |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 857 | } |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 858 | |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 859 | State destState = mDestState; |
| 860 | if (destState != null) { |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 861 | /** |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 862 | * Process the transitions including transitions in the enter/exit methods |
Wink Saville | e7be6a8 | 2010-03-18 17:03:30 -0700 | [diff] [blame] | 863 | */ |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 864 | while (true) { |
| 865 | if (mDbg) mSm.log("handleMessage: new destination call exit/enter"); |
| 866 | |
| 867 | /** |
| 868 | * Determine the states to exit and enter and return the |
| 869 | * common ancestor state of the enter/exit states. Then |
| 870 | * invoke the exit methods then the enter methods. |
| 871 | */ |
| 872 | StateInfo commonStateInfo = setupTempStateStackWithStatesToEnter(destState); |
Mitchell Wills | 07e317c | 2016-08-11 11:05:03 -0700 | [diff] [blame] | 873 | // flag is cleared in invokeEnterMethods before entering the target state |
| 874 | mTransitionInProgress = true; |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 875 | invokeExitMethods(commonStateInfo); |
| 876 | int stateStackEnteringIndex = moveTempStateStackToStateStack(); |
| 877 | invokeEnterMethods(stateStackEnteringIndex); |
| 878 | |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 879 | /** |
| 880 | * Since we have transitioned to a new state we need to have |
| 881 | * any deferred messages moved to the front of the message queue |
| 882 | * so they will be processed before any other messages in the |
| 883 | * message queue. |
| 884 | */ |
| 885 | moveDeferredMessageAtFrontOfQueue(); |
| 886 | |
| 887 | if (destState != mDestState) { |
| 888 | // A new mDestState so continue looping |
| 889 | destState = mDestState; |
| 890 | } else { |
| 891 | // No change in mDestState so we're done |
| 892 | break; |
| 893 | } |
| 894 | } |
Wink Saville | e7be6a8 | 2010-03-18 17:03:30 -0700 | [diff] [blame] | 895 | mDestState = null; |
Wink Saville | e7be6a8 | 2010-03-18 17:03:30 -0700 | [diff] [blame] | 896 | } |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 897 | |
Wink Saville | e7be6a8 | 2010-03-18 17:03:30 -0700 | [diff] [blame] | 898 | /** |
| 899 | * After processing all transitions check and |
| 900 | * see if the last transition was to quit or halt. |
| 901 | */ |
| 902 | if (destState != null) { |
| 903 | if (destState == mQuittingState) { |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 904 | /** |
| 905 | * Call onQuitting to let subclasses cleanup. |
| 906 | */ |
| 907 | mSm.onQuitting(); |
Jaikumar Ganesh | 6f9a616 | 2011-11-28 13:13:02 -0800 | [diff] [blame] | 908 | cleanupAfterQuitting(); |
Wink Saville | e7be6a8 | 2010-03-18 17:03:30 -0700 | [diff] [blame] | 909 | } else if (destState == mHaltingState) { |
| 910 | /** |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 911 | * Call onHalting() if we've transitioned to the halting |
Wink Saville | e7be6a8 | 2010-03-18 17:03:30 -0700 | [diff] [blame] | 912 | * state. All subsequent messages will be processed in |
| 913 | * in the halting state which invokes haltedProcessMessage(msg); |
| 914 | */ |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 915 | mSm.onHalting(); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 916 | } |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 917 | } |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | /** |
Jaikumar Ganesh | 6f9a616 | 2011-11-28 13:13:02 -0800 | [diff] [blame] | 921 | * Cleanup all the static variables and the looper after the SM has been quit. |
| 922 | */ |
| 923 | private final void cleanupAfterQuitting() { |
Jaikumar Ganesh | 6f9a616 | 2011-11-28 13:13:02 -0800 | [diff] [blame] | 924 | if (mSm.mSmThread != null) { |
| 925 | // If we made the thread then quit looper which stops the thread. |
| 926 | getLooper().quit(); |
| 927 | mSm.mSmThread = null; |
| 928 | } |
| 929 | |
| 930 | mSm.mSmHandler = null; |
| 931 | mSm = null; |
| 932 | mMsg = null; |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 933 | mLogRecords.cleanup(); |
Jaikumar Ganesh | 6f9a616 | 2011-11-28 13:13:02 -0800 | [diff] [blame] | 934 | mStateStack = null; |
| 935 | mTempStateStack = null; |
| 936 | mStateInfo.clear(); |
| 937 | mInitialState = null; |
| 938 | mDestState = null; |
| 939 | mDeferredMessages.clear(); |
Wink Saville | 03812c7 | 2013-04-16 13:21:00 -0700 | [diff] [blame] | 940 | mHasQuit = true; |
Jaikumar Ganesh | 6f9a616 | 2011-11-28 13:13:02 -0800 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | /** |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 944 | * Complete the construction of the state machine. |
| 945 | */ |
| 946 | private final void completeConstruction() { |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 947 | if (mDbg) mSm.log("completeConstruction: E"); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 948 | |
| 949 | /** |
| 950 | * Determine the maximum depth of the state hierarchy |
| 951 | * so we can allocate the state stacks. |
| 952 | */ |
| 953 | int maxDepth = 0; |
| 954 | for (StateInfo si : mStateInfo.values()) { |
| 955 | int depth = 0; |
| 956 | for (StateInfo i = si; i != null; depth++) { |
| 957 | i = i.parentStateInfo; |
| 958 | } |
| 959 | if (maxDepth < depth) { |
| 960 | maxDepth = depth; |
| 961 | } |
| 962 | } |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 963 | if (mDbg) mSm.log("completeConstruction: maxDepth=" + maxDepth); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 964 | |
| 965 | mStateStack = new StateInfo[maxDepth]; |
| 966 | mTempStateStack = new StateInfo[maxDepth]; |
| 967 | setupInitialStateStack(); |
| 968 | |
Wink Saville | cea056f | 2012-03-26 15:03:16 -0700 | [diff] [blame] | 969 | /** Sending SM_INIT_CMD message to invoke enter methods asynchronously */ |
| 970 | sendMessageAtFrontOfQueue(obtainMessage(SM_INIT_CMD, mSmHandlerObj)); |
Wink Saville | e7be6a8 | 2010-03-18 17:03:30 -0700 | [diff] [blame] | 971 | |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 972 | if (mDbg) mSm.log("completeConstruction: X"); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | /** |
| 976 | * Process the message. If the current state doesn't handle |
| 977 | * it, call the states parent and so on. If it is never handled then |
| 978 | * call the state machines unhandledMessage method. |
Irfan Sheriff | ef8da9f | 2012-11-29 09:42:13 -0800 | [diff] [blame] | 979 | * @return the state that processed the message |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 980 | */ |
Irfan Sheriff | ef8da9f | 2012-11-29 09:42:13 -0800 | [diff] [blame] | 981 | private final State processMsg(Message msg) { |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 982 | StateInfo curStateInfo = mStateStack[mStateStackTopIndex]; |
| 983 | if (mDbg) { |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 984 | mSm.log("processMsg: " + curStateInfo.state.getName()); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 985 | } |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 986 | |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 987 | if (isQuit(msg)) { |
| 988 | transitionTo(mQuittingState); |
| 989 | } else { |
| 990 | while (!curStateInfo.state.processMessage(msg)) { |
| 991 | /** |
| 992 | * Not processed |
| 993 | */ |
| 994 | curStateInfo = curStateInfo.parentStateInfo; |
| 995 | if (curStateInfo == null) { |
| 996 | /** |
| 997 | * No parents left so it's not handled |
| 998 | */ |
| 999 | mSm.unhandledMessage(msg); |
| 1000 | break; |
| 1001 | } |
| 1002 | if (mDbg) { |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 1003 | mSm.log("processMsg: " + curStateInfo.state.getName()); |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 1004 | } |
| 1005 | } |
Wink Saville | ff4fcdb | 2013-02-24 07:21:45 -0800 | [diff] [blame] | 1006 | } |
Irfan Sheriff | ef8da9f | 2012-11-29 09:42:13 -0800 | [diff] [blame] | 1007 | return (curStateInfo != null) ? curStateInfo.state : null; |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | /** |
| 1011 | * Call the exit method for each state from the top of stack |
| 1012 | * up to the common ancestor state. |
| 1013 | */ |
| 1014 | private final void invokeExitMethods(StateInfo commonStateInfo) { |
Wink Saville | ff4fcdb | 2013-02-24 07:21:45 -0800 | [diff] [blame] | 1015 | while ((mStateStackTopIndex >= 0) |
| 1016 | && (mStateStack[mStateStackTopIndex] != commonStateInfo)) { |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1017 | State curState = mStateStack[mStateStackTopIndex].state; |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 1018 | if (mDbg) mSm.log("invokeExitMethods: " + curState.getName()); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1019 | curState.exit(); |
| 1020 | mStateStack[mStateStackTopIndex].active = false; |
| 1021 | mStateStackTopIndex -= 1; |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | /** |
| 1026 | * Invoke the enter method starting at the entering index to top of state stack |
| 1027 | */ |
| 1028 | private final void invokeEnterMethods(int stateStackEnteringIndex) { |
| 1029 | for (int i = stateStackEnteringIndex; i <= mStateStackTopIndex; i++) { |
Mitchell Wills | 07e317c | 2016-08-11 11:05:03 -0700 | [diff] [blame] | 1030 | if (stateStackEnteringIndex == mStateStackTopIndex) { |
| 1031 | // Last enter state for transition |
| 1032 | mTransitionInProgress = false; |
| 1033 | } |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 1034 | if (mDbg) mSm.log("invokeEnterMethods: " + mStateStack[i].state.getName()); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1035 | mStateStack[i].state.enter(); |
| 1036 | mStateStack[i].active = true; |
| 1037 | } |
Mitchell Wills | 07e317c | 2016-08-11 11:05:03 -0700 | [diff] [blame] | 1038 | mTransitionInProgress = false; // ensure flag set to false if no methods called |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | /** |
| 1042 | * Move the deferred message to the front of the message queue. |
| 1043 | */ |
| 1044 | private final void moveDeferredMessageAtFrontOfQueue() { |
| 1045 | /** |
| 1046 | * The oldest messages on the deferred list must be at |
| 1047 | * the front of the queue so start at the back, which |
| 1048 | * as the most resent message and end with the oldest |
| 1049 | * messages at the front of the queue. |
| 1050 | */ |
Wink Saville | ff4fcdb | 2013-02-24 07:21:45 -0800 | [diff] [blame] | 1051 | for (int i = mDeferredMessages.size() - 1; i >= 0; i--) { |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1052 | Message curMsg = mDeferredMessages.get(i); |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 1053 | if (mDbg) mSm.log("moveDeferredMessageAtFrontOfQueue; what=" + curMsg.what); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1054 | sendMessageAtFrontOfQueue(curMsg); |
| 1055 | } |
| 1056 | mDeferredMessages.clear(); |
| 1057 | } |
| 1058 | |
| 1059 | /** |
| 1060 | * Move the contents of the temporary stack to the state stack |
| 1061 | * reversing the order of the items on the temporary stack as |
| 1062 | * they are moved. |
| 1063 | * |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1064 | * @return index into mStateStack where entering needs to start |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1065 | */ |
| 1066 | private final int moveTempStateStackToStateStack() { |
| 1067 | int startingIndex = mStateStackTopIndex + 1; |
| 1068 | int i = mTempStateStackCount - 1; |
| 1069 | int j = startingIndex; |
| 1070 | while (i >= 0) { |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 1071 | if (mDbg) mSm.log("moveTempStackToStateStack: i=" + i + ",j=" + j); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1072 | mStateStack[j] = mTempStateStack[i]; |
| 1073 | j += 1; |
| 1074 | i -= 1; |
| 1075 | } |
| 1076 | |
| 1077 | mStateStackTopIndex = j - 1; |
| 1078 | if (mDbg) { |
Wink Saville | ff4fcdb | 2013-02-24 07:21:45 -0800 | [diff] [blame] | 1079 | mSm.log("moveTempStackToStateStack: X mStateStackTop=" + mStateStackTopIndex |
| 1080 | + ",startingIndex=" + startingIndex + ",Top=" |
| 1081 | + mStateStack[mStateStackTopIndex].state.getName()); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1082 | } |
| 1083 | return startingIndex; |
| 1084 | } |
| 1085 | |
| 1086 | /** |
| 1087 | * Setup the mTempStateStack with the states we are going to enter. |
| 1088 | * |
| 1089 | * This is found by searching up the destState's ancestors for a |
| 1090 | * state that is already active i.e. StateInfo.active == true. |
| 1091 | * The destStae and all of its inactive parents will be on the |
| 1092 | * TempStateStack as the list of states to enter. |
| 1093 | * |
| 1094 | * @return StateInfo of the common ancestor for the destState and |
| 1095 | * current state or null if there is no common parent. |
| 1096 | */ |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1097 | private final StateInfo setupTempStateStackWithStatesToEnter(State destState) { |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1098 | /** |
| 1099 | * Search up the parent list of the destination state for an active |
| 1100 | * state. Use a do while() loop as the destState must always be entered |
| 1101 | * even if it is active. This can happen if we are exiting/entering |
| 1102 | * the current state. |
| 1103 | */ |
| 1104 | mTempStateStackCount = 0; |
| 1105 | StateInfo curStateInfo = mStateInfo.get(destState); |
| 1106 | do { |
| 1107 | mTempStateStack[mTempStateStackCount++] = curStateInfo; |
| 1108 | curStateInfo = curStateInfo.parentStateInfo; |
| 1109 | } while ((curStateInfo != null) && !curStateInfo.active); |
| 1110 | |
| 1111 | if (mDbg) { |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 1112 | mSm.log("setupTempStateStackWithStatesToEnter: X mTempStateStackCount=" |
Wink Saville | ff4fcdb | 2013-02-24 07:21:45 -0800 | [diff] [blame] | 1113 | + mTempStateStackCount + ",curStateInfo: " + curStateInfo); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1114 | } |
| 1115 | return curStateInfo; |
| 1116 | } |
| 1117 | |
| 1118 | /** |
| 1119 | * Initialize StateStack to mInitialState. |
| 1120 | */ |
| 1121 | private final void setupInitialStateStack() { |
| 1122 | if (mDbg) { |
Wink Saville | ff4fcdb | 2013-02-24 07:21:45 -0800 | [diff] [blame] | 1123 | mSm.log("setupInitialStateStack: E mInitialState=" + mInitialState.getName()); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | StateInfo curStateInfo = mStateInfo.get(mInitialState); |
| 1127 | for (mTempStateStackCount = 0; curStateInfo != null; mTempStateStackCount++) { |
| 1128 | mTempStateStack[mTempStateStackCount] = curStateInfo; |
| 1129 | curStateInfo = curStateInfo.parentStateInfo; |
| 1130 | } |
| 1131 | |
| 1132 | // Empty the StateStack |
| 1133 | mStateStackTopIndex = -1; |
| 1134 | |
| 1135 | moveTempStateStackToStateStack(); |
| 1136 | } |
| 1137 | |
| 1138 | /** |
Wink Saville | a4f3bec | 2010-05-19 09:11:38 -0700 | [diff] [blame] | 1139 | * @return current message |
| 1140 | */ |
| 1141 | private final Message getCurrentMessage() { |
| 1142 | return mMsg; |
| 1143 | } |
| 1144 | |
| 1145 | /** |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1146 | * @return current state |
| 1147 | */ |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1148 | private final IState getCurrentState() { |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1149 | return mStateStack[mStateStackTopIndex].state; |
| 1150 | } |
| 1151 | |
| 1152 | /** |
| 1153 | * Add a new state to the state machine. Bottom up addition |
| 1154 | * of states is allowed but the same state may only exist |
| 1155 | * in one hierarchy. |
| 1156 | * |
| 1157 | * @param state the state to add |
| 1158 | * @param parent the parent of state |
| 1159 | * @return stateInfo for this state |
| 1160 | */ |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1161 | private final StateInfo addState(State state, State parent) { |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1162 | if (mDbg) { |
Wink Saville | ff4fcdb | 2013-02-24 07:21:45 -0800 | [diff] [blame] | 1163 | mSm.log("addStateInternal: E state=" + state.getName() + ",parent=" |
| 1164 | + ((parent == null) ? "" : parent.getName())); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1165 | } |
| 1166 | StateInfo parentStateInfo = null; |
| 1167 | if (parent != null) { |
| 1168 | parentStateInfo = mStateInfo.get(parent); |
| 1169 | if (parentStateInfo == null) { |
| 1170 | // Recursively add our parent as it's not been added yet. |
| 1171 | parentStateInfo = addState(parent, null); |
| 1172 | } |
| 1173 | } |
| 1174 | StateInfo stateInfo = mStateInfo.get(state); |
| 1175 | if (stateInfo == null) { |
| 1176 | stateInfo = new StateInfo(); |
| 1177 | mStateInfo.put(state, stateInfo); |
| 1178 | } |
| 1179 | |
| 1180 | // Validate that we aren't adding the same state in two different hierarchies. |
Wink Saville | ff4fcdb | 2013-02-24 07:21:45 -0800 | [diff] [blame] | 1181 | if ((stateInfo.parentStateInfo != null) |
| 1182 | && (stateInfo.parentStateInfo != parentStateInfo)) { |
| 1183 | throw new RuntimeException("state already added"); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1184 | } |
| 1185 | stateInfo.state = state; |
| 1186 | stateInfo.parentStateInfo = parentStateInfo; |
| 1187 | stateInfo.active = false; |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 1188 | if (mDbg) mSm.log("addStateInternal: X stateInfo: " + stateInfo); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1189 | return stateInfo; |
| 1190 | } |
| 1191 | |
| 1192 | /** |
Hall Liu | b222d20 | 2016-11-21 17:29:02 -0800 | [diff] [blame] | 1193 | * Remove a state from the state machine. Will not remove the state if it is currently |
| 1194 | * active or if it has any children in the hierarchy. |
| 1195 | * @param state the state to remove |
| 1196 | */ |
| 1197 | private void removeState(State state) { |
| 1198 | StateInfo stateInfo = mStateInfo.get(state); |
| 1199 | if (stateInfo == null || stateInfo.active) { |
| 1200 | return; |
| 1201 | } |
| 1202 | boolean isParent = mStateInfo.values().stream() |
| 1203 | .filter(si -> si.parentStateInfo == stateInfo) |
| 1204 | .findAny() |
| 1205 | .isPresent(); |
| 1206 | if (isParent) { |
| 1207 | return; |
| 1208 | } |
| 1209 | mStateInfo.remove(state); |
| 1210 | } |
| 1211 | |
| 1212 | /** |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1213 | * Constructor |
| 1214 | * |
| 1215 | * @param looper for dispatching messages |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1216 | * @param sm the hierarchical state machine |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1217 | */ |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1218 | private SmHandler(Looper looper, StateMachine sm) { |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1219 | super(looper); |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1220 | mSm = sm; |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1221 | |
| 1222 | addState(mHaltingState, null); |
Wink Saville | 1b8b98b | 2010-03-11 11:49:54 -0800 | [diff] [blame] | 1223 | addState(mQuittingState, null); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1224 | } |
| 1225 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1226 | /** @see StateMachine#setInitialState(State) */ |
| 1227 | private final void setInitialState(State initialState) { |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 1228 | if (mDbg) mSm.log("setInitialState: initialState=" + initialState.getName()); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1229 | mInitialState = initialState; |
| 1230 | } |
| 1231 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1232 | /** @see StateMachine#transitionTo(IState) */ |
| 1233 | private final void transitionTo(IState destState) { |
Mitchell Wills | 07e317c | 2016-08-11 11:05:03 -0700 | [diff] [blame] | 1234 | if (mTransitionInProgress) { |
| 1235 | Log.wtf(mSm.mName, "transitionTo called while transition already in progress to " + |
| 1236 | mDestState + ", new target state=" + destState); |
| 1237 | } |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1238 | mDestState = (State) destState; |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 1239 | if (mDbg) mSm.log("transitionTo: destState=" + mDestState.getName()); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1240 | } |
| 1241 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1242 | /** @see StateMachine#deferMessage(Message) */ |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1243 | private final void deferMessage(Message msg) { |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 1244 | if (mDbg) mSm.log("deferMessage: msg=" + msg.what); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1245 | |
| 1246 | /* Copy the "msg" to "newMsg" as "msg" will be recycled */ |
| 1247 | Message newMsg = obtainMessage(); |
| 1248 | newMsg.copyFrom(msg); |
| 1249 | |
| 1250 | mDeferredMessages.add(newMsg); |
| 1251 | } |
| 1252 | |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 1253 | /** @see StateMachine#quit() */ |
Wink Saville | 1b8b98b | 2010-03-11 11:49:54 -0800 | [diff] [blame] | 1254 | private final void quit() { |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 1255 | if (mDbg) mSm.log("quit:"); |
Wink Saville | cea056f | 2012-03-26 15:03:16 -0700 | [diff] [blame] | 1256 | sendMessage(obtainMessage(SM_QUIT_CMD, mSmHandlerObj)); |
Wink Saville | 1b8b98b | 2010-03-11 11:49:54 -0800 | [diff] [blame] | 1257 | } |
| 1258 | |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 1259 | /** @see StateMachine#quitNow() */ |
| 1260 | private final void quitNow() { |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 1261 | if (mDbg) mSm.log("quitNow:"); |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 1262 | sendMessageAtFrontOfQueue(obtainMessage(SM_QUIT_CMD, mSmHandlerObj)); |
| 1263 | } |
| 1264 | |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 1265 | /** Validate that the message was sent by quit or quitNow. */ |
Wink Saville | 1b8b98b | 2010-03-11 11:49:54 -0800 | [diff] [blame] | 1266 | private final boolean isQuit(Message msg) { |
Wink Saville | cea056f | 2012-03-26 15:03:16 -0700 | [diff] [blame] | 1267 | return (msg.what == SM_QUIT_CMD) && (msg.obj == mSmHandlerObj); |
Wink Saville | 1b8b98b | 2010-03-11 11:49:54 -0800 | [diff] [blame] | 1268 | } |
| 1269 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1270 | /** @see StateMachine#isDbg() */ |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1271 | private final boolean isDbg() { |
| 1272 | return mDbg; |
| 1273 | } |
| 1274 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1275 | /** @see StateMachine#setDbg(boolean) */ |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1276 | private final void setDbg(boolean dbg) { |
| 1277 | mDbg = dbg; |
| 1278 | } |
| 1279 | |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1280 | } |
| 1281 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1282 | private SmHandler mSmHandler; |
| 1283 | private HandlerThread mSmThread; |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1284 | |
| 1285 | /** |
| 1286 | * Initialize. |
| 1287 | * |
| 1288 | * @param looper for this state machine |
| 1289 | * @param name of the state machine |
| 1290 | */ |
Wink Saville | f0f566e | 2010-03-11 14:03:50 -0800 | [diff] [blame] | 1291 | private void initStateMachine(String name, Looper looper) { |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1292 | mName = name; |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1293 | mSmHandler = new SmHandler(looper, this); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | /** |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1297 | * Constructor creates a StateMachine with its own thread. |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1298 | * |
| 1299 | * @param name of the state machine |
| 1300 | */ |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1301 | protected StateMachine(String name) { |
| 1302 | mSmThread = new HandlerThread(name); |
| 1303 | mSmThread.start(); |
| 1304 | Looper looper = mSmThread.getLooper(); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1305 | |
Wink Saville | f0f566e | 2010-03-11 14:03:50 -0800 | [diff] [blame] | 1306 | initStateMachine(name, looper); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | /** |
Ken Wakasa | f76a50c | 2012-03-09 19:56:35 +0900 | [diff] [blame] | 1310 | * Constructor creates a StateMachine using the looper. |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1311 | * |
| 1312 | * @param name of the state machine |
| 1313 | */ |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1314 | protected StateMachine(String name, Looper looper) { |
Wink Saville | f0f566e | 2010-03-11 14:03:50 -0800 | [diff] [blame] | 1315 | initStateMachine(name, looper); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | /** |
Wink Saville | 24d248a | 2013-03-17 14:26:21 -0700 | [diff] [blame] | 1319 | * Constructor creates a StateMachine using the handler. |
| 1320 | * |
| 1321 | * @param name of the state machine |
| 1322 | */ |
| 1323 | protected StateMachine(String name, Handler handler) { |
| 1324 | initStateMachine(name, handler.getLooper()); |
| 1325 | } |
| 1326 | |
| 1327 | /** |
Brad Ebinger | 0c71404 | 2015-12-02 17:41:45 -0800 | [diff] [blame] | 1328 | * Notifies subclass that the StateMachine handler is about to process the Message msg |
| 1329 | * @param msg The message that is being handled |
| 1330 | */ |
| 1331 | protected void onPreHandleMessage(Message msg) { |
| 1332 | } |
| 1333 | |
| 1334 | /** |
| 1335 | * Notifies subclass that the StateMachine handler has finished processing the Message msg and |
| 1336 | * has possibly transitioned to a new state. |
| 1337 | * @param msg The message that is being handled |
| 1338 | */ |
| 1339 | protected void onPostHandleMessage(Message msg) { |
| 1340 | } |
| 1341 | |
| 1342 | /** |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1343 | * Add a new state to the state machine |
| 1344 | * @param state the state to add |
| 1345 | * @param parent the parent of state |
| 1346 | */ |
Mitchell Wills | 009ae99 | 2016-08-09 13:33:20 -0700 | [diff] [blame] | 1347 | public final void addState(State state, State parent) { |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1348 | mSmHandler.addState(state, parent); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1349 | } |
Wink Saville | a4f3bec | 2010-05-19 09:11:38 -0700 | [diff] [blame] | 1350 | |
| 1351 | /** |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1352 | * Add a new state to the state machine, parent will be null |
| 1353 | * @param state to add |
| 1354 | */ |
Mitchell Wills | 009ae99 | 2016-08-09 13:33:20 -0700 | [diff] [blame] | 1355 | public final void addState(State state) { |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1356 | mSmHandler.addState(state, null); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | /** |
Hall Liu | b222d20 | 2016-11-21 17:29:02 -0800 | [diff] [blame] | 1360 | * Removes a state from the state machine, unless it is currently active or if it has children. |
| 1361 | * @param state state to remove |
| 1362 | */ |
| 1363 | public final void removeState(State state) { |
| 1364 | mSmHandler.removeState(state); |
| 1365 | } |
| 1366 | |
| 1367 | /** |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1368 | * Set the initial state. This must be invoked before |
| 1369 | * and messages are sent to the state machine. |
| 1370 | * |
| 1371 | * @param initialState is the state which will receive the first message. |
| 1372 | */ |
Mitchell Wills | 009ae99 | 2016-08-09 13:33:20 -0700 | [diff] [blame] | 1373 | public final void setInitialState(State initialState) { |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1374 | mSmHandler.setInitialState(initialState); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1375 | } |
| 1376 | |
| 1377 | /** |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1378 | * @return current message |
| 1379 | */ |
Mitchell Wills | 009ae99 | 2016-08-09 13:33:20 -0700 | [diff] [blame] | 1380 | public final Message getCurrentMessage() { |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1381 | // mSmHandler can be null if the state machine has quit. |
| 1382 | SmHandler smh = mSmHandler; |
| 1383 | if (smh == null) return null; |
| 1384 | return smh.getCurrentMessage(); |
| 1385 | } |
| 1386 | |
| 1387 | /** |
| 1388 | * @return current state |
| 1389 | */ |
Mitchell Wills | 009ae99 | 2016-08-09 13:33:20 -0700 | [diff] [blame] | 1390 | public final IState getCurrentState() { |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1391 | // mSmHandler can be null if the state machine has quit. |
| 1392 | SmHandler smh = mSmHandler; |
| 1393 | if (smh == null) return null; |
| 1394 | return smh.getCurrentState(); |
| 1395 | } |
| 1396 | |
| 1397 | /** |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1398 | * transition to destination state. Upon returning |
| 1399 | * from processMessage the current state's exit will |
| 1400 | * be executed and upon the next message arriving |
| 1401 | * destState.enter will be invoked. |
| 1402 | * |
Isaac Levy | d2fe04b | 2011-07-22 08:48:26 -0700 | [diff] [blame] | 1403 | * this function can also be called inside the enter function of the |
| 1404 | * previous transition target, but the behavior is undefined when it is |
| 1405 | * called mid-way through a previous transition (for example, calling this |
| 1406 | * in the enter() routine of a intermediate node when the current transition |
| 1407 | * target is one of the nodes descendants). |
| 1408 | * |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1409 | * @param destState will be the state that receives the next message. |
| 1410 | */ |
Mitchell Wills | 009ae99 | 2016-08-09 13:33:20 -0700 | [diff] [blame] | 1411 | public final void transitionTo(IState destState) { |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1412 | mSmHandler.transitionTo(destState); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | /** |
| 1416 | * transition to halt state. Upon returning |
| 1417 | * from processMessage we will exit all current |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 1418 | * states, execute the onHalting() method and then |
| 1419 | * for all subsequent messages haltedProcessMessage |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1420 | * will be called. |
| 1421 | */ |
Mitchell Wills | 009ae99 | 2016-08-09 13:33:20 -0700 | [diff] [blame] | 1422 | public final void transitionToHaltingState() { |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1423 | mSmHandler.transitionTo(mSmHandler.mHaltingState); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1424 | } |
| 1425 | |
| 1426 | /** |
| 1427 | * Defer this message until next state transition. |
| 1428 | * Upon transitioning all deferred messages will be |
| 1429 | * placed on the queue and reprocessed in the original |
| 1430 | * order. (i.e. The next state the oldest messages will |
| 1431 | * be processed first) |
| 1432 | * |
| 1433 | * @param msg is deferred until the next transition. |
| 1434 | */ |
Mitchell Wills | 009ae99 | 2016-08-09 13:33:20 -0700 | [diff] [blame] | 1435 | public final void deferMessage(Message msg) { |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1436 | mSmHandler.deferMessage(msg); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1437 | } |
| 1438 | |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1439 | /** |
| 1440 | * Called when message wasn't handled |
| 1441 | * |
| 1442 | * @param msg that couldn't be handled. |
| 1443 | */ |
| 1444 | protected void unhandledMessage(Message msg) { |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 1445 | if (mSmHandler.mDbg) loge(" - unhandledMessage: msg.what=" + msg.what); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | /** |
| 1449 | * Called for any message that is received after |
| 1450 | * transitionToHalting is called. |
| 1451 | */ |
| 1452 | protected void haltedProcessMessage(Message msg) { |
| 1453 | } |
| 1454 | |
| 1455 | /** |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 1456 | * This will be called once after handling a message that called |
| 1457 | * transitionToHalting. All subsequent messages will invoke |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1458 | * {@link StateMachine#haltedProcessMessage(Message)} |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1459 | */ |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 1460 | protected void onHalting() { |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | /** |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 1464 | * This will be called once after a quit message that was NOT handled by |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1465 | * the derived StateMachine. The StateMachine will stop and any subsequent messages will be |
| 1466 | * ignored. In addition, if this StateMachine created the thread, the thread will |
Wink Saville | d305948 | 2011-04-11 11:51:28 -0700 | [diff] [blame] | 1467 | * be stopped after this method returns. |
Wink Saville | 1b8b98b | 2010-03-11 11:49:54 -0800 | [diff] [blame] | 1468 | */ |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 1469 | protected void onQuitting() { |
Wink Saville | 1b8b98b | 2010-03-11 11:49:54 -0800 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | /** |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1473 | * @return the name |
| 1474 | */ |
| 1475 | public final String getName() { |
| 1476 | return mName; |
| 1477 | } |
| 1478 | |
| 1479 | /** |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 1480 | * Set number of log records to maintain and clears all current records. |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1481 | * |
| 1482 | * @param maxSize number of messages to maintain at anyone time. |
| 1483 | */ |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 1484 | public final void setLogRecSize(int maxSize) { |
| 1485 | mSmHandler.mLogRecords.setSize(maxSize); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1486 | } |
| 1487 | |
| 1488 | /** |
Irfan Sheriff | ef8da9f | 2012-11-29 09:42:13 -0800 | [diff] [blame] | 1489 | * Set to log only messages that cause a state transition |
| 1490 | * |
| 1491 | * @param enable {@code true} to enable, {@code false} to disable |
| 1492 | */ |
| 1493 | public final void setLogOnlyTransitions(boolean enable) { |
| 1494 | mSmHandler.mLogRecords.setLogOnlyTransitions(enable); |
| 1495 | } |
| 1496 | |
| 1497 | /** |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 1498 | * @return number of log records |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1499 | */ |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 1500 | public final int getLogRecSize() { |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1501 | // mSmHandler can be null if the state machine has quit. |
| 1502 | SmHandler smh = mSmHandler; |
| 1503 | if (smh == null) return 0; |
| 1504 | return smh.mLogRecords.size(); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | /** |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 1508 | * @return the total number of records processed |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1509 | */ |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 1510 | public final int getLogRecCount() { |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1511 | // mSmHandler can be null if the state machine has quit. |
| 1512 | SmHandler smh = mSmHandler; |
| 1513 | if (smh == null) return 0; |
| 1514 | return smh.mLogRecords.count(); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1515 | } |
| 1516 | |
| 1517 | /** |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1518 | * @return a log record, or null if index is out of range |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1519 | */ |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 1520 | public final LogRec getLogRec(int index) { |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1521 | // mSmHandler can be null if the state machine has quit. |
| 1522 | SmHandler smh = mSmHandler; |
| 1523 | if (smh == null) return null; |
| 1524 | return smh.mLogRecords.get(index); |
| 1525 | } |
| 1526 | |
| 1527 | /** |
| 1528 | * @return a copy of LogRecs as a collection |
| 1529 | */ |
| 1530 | public final Collection<LogRec> copyLogRecs() { |
| 1531 | Vector<LogRec> vlr = new Vector<LogRec>(); |
| 1532 | SmHandler smh = mSmHandler; |
| 1533 | if (smh != null) { |
| 1534 | for (LogRec lr : smh.mLogRecords.mLogRecVector) { |
| 1535 | vlr.add(lr); |
| 1536 | } |
| 1537 | } |
| 1538 | return vlr; |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 1539 | } |
| 1540 | |
| 1541 | /** |
| 1542 | * Add the string to LogRecords. |
| 1543 | * |
| 1544 | * @param string |
| 1545 | */ |
Mitchell Wills | 009ae99 | 2016-08-09 13:33:20 -0700 | [diff] [blame] | 1546 | public void addLogRec(String string) { |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1547 | // mSmHandler can be null if the state machine has quit. |
| 1548 | SmHandler smh = mSmHandler; |
| 1549 | if (smh == null) return; |
| 1550 | smh.mLogRecords.add(this, smh.getCurrentMessage(), string, smh.getCurrentState(), |
| 1551 | smh.mStateStack[smh.mStateStackTopIndex].state, smh.mDestState); |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | /** |
| 1555 | * @return true if msg should be saved in the log, default is true. |
| 1556 | */ |
| 1557 | protected boolean recordLogRec(Message msg) { |
| 1558 | return true; |
| 1559 | } |
| 1560 | |
| 1561 | /** |
| 1562 | * Return a string to be logged by LogRec, default |
| 1563 | * is an empty string. Override if additional information is desired. |
| 1564 | * |
| 1565 | * @param msg that was processed |
| 1566 | * @return information to be logged as a String |
| 1567 | */ |
| 1568 | protected String getLogRecString(Message msg) { |
| 1569 | return ""; |
| 1570 | } |
| 1571 | |
| 1572 | /** |
| 1573 | * @return the string for msg.what |
| 1574 | */ |
| 1575 | protected String getWhatToString(int what) { |
| 1576 | return null; |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | /** |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1580 | * @return Handler, maybe null if state machine has quit. |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1581 | */ |
| 1582 | public final Handler getHandler() { |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1583 | return mSmHandler; |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | /** |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1587 | * Get a message and set Message.target state machine handler. |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1588 | * |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 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. |
| 1593 | * |
| 1594 | * @return A Message object from the global pool |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1595 | */ |
Wink Saville | ff4fcdb | 2013-02-24 07:21:45 -0800 | [diff] [blame] | 1596 | public final Message obtainMessage() { |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1597 | return Message.obtain(mSmHandler); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | /** |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1601 | * Get a message and set Message.target state machine handler, what. |
| 1602 | * |
| 1603 | * Note: The handler can be null if the state machine has quit, |
| 1604 | * which means target will be null and may cause a AndroidRuntimeException |
| 1605 | * in MessageQueue#enqueMessage if sent directly or if sent using |
| 1606 | * StateMachine#sendMessage the message will just be ignored. |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1607 | * |
| 1608 | * @param what is the assigned to Message.what. |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1609 | * @return A Message object from the global pool |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1610 | */ |
| 1611 | public final Message obtainMessage(int what) { |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1612 | return Message.obtain(mSmHandler, what); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | /** |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1616 | * Get a message and set Message.target state machine handler, |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1617 | * what and obj. |
| 1618 | * |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1619 | * Note: The handler can be null if the state machine has quit, |
| 1620 | * which means target will be null and may cause a AndroidRuntimeException |
| 1621 | * in MessageQueue#enqueMessage if sent directly or if sent using |
| 1622 | * StateMachine#sendMessage the message will just be ignored. |
| 1623 | * |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1624 | * @param what is the assigned to Message.what. |
| 1625 | * @param obj is assigned to Message.obj. |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1626 | * @return A Message object from the global pool |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1627 | */ |
Wink Saville | ff4fcdb | 2013-02-24 07:21:45 -0800 | [diff] [blame] | 1628 | public final Message obtainMessage(int what, Object obj) { |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1629 | return Message.obtain(mSmHandler, what, obj); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1630 | } |
| 1631 | |
Wink Saville | 91fbd56 | 2010-03-17 17:12:43 -0700 | [diff] [blame] | 1632 | /** |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1633 | * Get a message and set Message.target state machine handler, |
Irfan Sheriff | 96e6bdb | 2010-06-25 09:44:27 -0700 | [diff] [blame] | 1634 | * what, arg1 and arg2 |
| 1635 | * |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1636 | * Note: The handler can be null if the state machine has quit, |
| 1637 | * which means target will be null and may cause a AndroidRuntimeException |
| 1638 | * in MessageQueue#enqueMessage if sent directly or if sent using |
| 1639 | * StateMachine#sendMessage the message will just be ignored. |
| 1640 | * |
Irfan Sheriff | 96e6bdb | 2010-06-25 09:44:27 -0700 | [diff] [blame] | 1641 | * @param what is assigned to Message.what |
| 1642 | * @param arg1 is assigned to Message.arg1 |
Wink Saville | 8b0db52 | 2013-03-14 13:23:19 -0700 | [diff] [blame] | 1643 | * @return A Message object from the global pool |
| 1644 | */ |
| 1645 | public final Message obtainMessage(int what, int arg1) { |
| 1646 | // use this obtain so we don't match the obtain(h, what, Object) method |
| 1647 | return Message.obtain(mSmHandler, what, arg1, 0); |
| 1648 | } |
| 1649 | |
| 1650 | /** |
| 1651 | * Get a message and set Message.target state machine handler, |
| 1652 | * what, arg1 and arg2 |
| 1653 | * |
| 1654 | * Note: The handler can be null if the state machine has quit, |
| 1655 | * which means target will be null and may cause a AndroidRuntimeException |
| 1656 | * in MessageQueue#enqueMessage if sent directly or if sent using |
| 1657 | * StateMachine#sendMessage the message will just be ignored. |
| 1658 | * |
| 1659 | * @param what is assigned to Message.what |
| 1660 | * @param arg1 is assigned to Message.arg1 |
Irfan Sheriff | 96e6bdb | 2010-06-25 09:44:27 -0700 | [diff] [blame] | 1661 | * @param arg2 is assigned to Message.arg2 |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1662 | * @return A Message object from the global pool |
Irfan Sheriff | 96e6bdb | 2010-06-25 09:44:27 -0700 | [diff] [blame] | 1663 | */ |
Wink Saville | ff4fcdb | 2013-02-24 07:21:45 -0800 | [diff] [blame] | 1664 | public final Message obtainMessage(int what, int arg1, int arg2) { |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1665 | return Message.obtain(mSmHandler, what, arg1, arg2); |
Irfan Sheriff | 96e6bdb | 2010-06-25 09:44:27 -0700 | [diff] [blame] | 1666 | } |
| 1667 | |
| 1668 | /** |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1669 | * Get a message and set Message.target state machine handler, |
Irfan Sheriff | 96e6bdb | 2010-06-25 09:44:27 -0700 | [diff] [blame] | 1670 | * what, arg1, arg2 and obj |
| 1671 | * |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1672 | * Note: The handler can be null if the state machine has quit, |
| 1673 | * which means target will be null and may cause a AndroidRuntimeException |
| 1674 | * in MessageQueue#enqueMessage if sent directly or if sent using |
| 1675 | * StateMachine#sendMessage the message will just be ignored. |
| 1676 | * |
Irfan Sheriff | 96e6bdb | 2010-06-25 09:44:27 -0700 | [diff] [blame] | 1677 | * @param what is assigned to Message.what |
| 1678 | * @param arg1 is assigned to Message.arg1 |
| 1679 | * @param arg2 is assigned to Message.arg2 |
| 1680 | * @param obj is assigned to Message.obj |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1681 | * @return A Message object from the global pool |
Irfan Sheriff | 96e6bdb | 2010-06-25 09:44:27 -0700 | [diff] [blame] | 1682 | */ |
Wink Saville | ff4fcdb | 2013-02-24 07:21:45 -0800 | [diff] [blame] | 1683 | public final Message obtainMessage(int what, int arg1, int arg2, Object obj) { |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1684 | return Message.obtain(mSmHandler, what, arg1, arg2, obj); |
Irfan Sheriff | 96e6bdb | 2010-06-25 09:44:27 -0700 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | /** |
Wink Saville | 91fbd56 | 2010-03-17 17:12:43 -0700 | [diff] [blame] | 1688 | * Enqueue a message to this state machine. |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1689 | * |
| 1690 | * Message is ignored if state machine has quit. |
Wink Saville | 91fbd56 | 2010-03-17 17:12:43 -0700 | [diff] [blame] | 1691 | */ |
Rebecca Silberstein | 934535b | 2016-05-13 13:57:34 -0700 | [diff] [blame] | 1692 | public void sendMessage(int what) { |
Jaikumar Ganesh | a544d46 | 2011-12-07 14:52:01 -0800 | [diff] [blame] | 1693 | // mSmHandler can be null if the state machine has quit. |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1694 | SmHandler smh = mSmHandler; |
| 1695 | if (smh == null) return; |
Jaikumar Ganesh | a544d46 | 2011-12-07 14:52:01 -0800 | [diff] [blame] | 1696 | |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1697 | smh.sendMessage(obtainMessage(what)); |
Wink Saville | 91fbd56 | 2010-03-17 17:12:43 -0700 | [diff] [blame] | 1698 | } |
| 1699 | |
| 1700 | /** |
| 1701 | * Enqueue a message to this state machine. |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1702 | * |
| 1703 | * Message is ignored if state machine has quit. |
Wink Saville | 91fbd56 | 2010-03-17 17:12:43 -0700 | [diff] [blame] | 1704 | */ |
Rebecca Silberstein | 934535b | 2016-05-13 13:57:34 -0700 | [diff] [blame] | 1705 | public void sendMessage(int what, Object obj) { |
Jaikumar Ganesh | a544d46 | 2011-12-07 14:52:01 -0800 | [diff] [blame] | 1706 | // mSmHandler can be null if the state machine has quit. |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1707 | SmHandler smh = mSmHandler; |
| 1708 | if (smh == null) return; |
Jaikumar Ganesh | a544d46 | 2011-12-07 14:52:01 -0800 | [diff] [blame] | 1709 | |
Wink Saville | ff4fcdb | 2013-02-24 07:21:45 -0800 | [diff] [blame] | 1710 | smh.sendMessage(obtainMessage(what, obj)); |
Wink Saville | 91fbd56 | 2010-03-17 17:12:43 -0700 | [diff] [blame] | 1711 | } |
| 1712 | |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1713 | /** |
| 1714 | * Enqueue a message to this state machine. |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1715 | * |
| 1716 | * Message is ignored if state machine has quit. |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1717 | */ |
Rebecca Silberstein | 934535b | 2016-05-13 13:57:34 -0700 | [diff] [blame] | 1718 | public void sendMessage(int what, int arg1) { |
Wink Saville | 8b0db52 | 2013-03-14 13:23:19 -0700 | [diff] [blame] | 1719 | // mSmHandler can be null if the state machine has quit. |
| 1720 | SmHandler smh = mSmHandler; |
| 1721 | if (smh == null) return; |
| 1722 | |
| 1723 | smh.sendMessage(obtainMessage(what, arg1)); |
| 1724 | } |
| 1725 | |
| 1726 | /** |
| 1727 | * Enqueue a message to this state machine. |
| 1728 | * |
| 1729 | * Message is ignored if state machine has quit. |
| 1730 | */ |
Rebecca Silberstein | 934535b | 2016-05-13 13:57:34 -0700 | [diff] [blame] | 1731 | public void sendMessage(int what, int arg1, int arg2) { |
Wink Saville | 8b0db52 | 2013-03-14 13:23:19 -0700 | [diff] [blame] | 1732 | // mSmHandler can be null if the state machine has quit. |
| 1733 | SmHandler smh = mSmHandler; |
| 1734 | if (smh == null) return; |
| 1735 | |
| 1736 | smh.sendMessage(obtainMessage(what, arg1, arg2)); |
| 1737 | } |
| 1738 | |
| 1739 | /** |
| 1740 | * Enqueue a message to this state machine. |
| 1741 | * |
| 1742 | * Message is ignored if state machine has quit. |
| 1743 | */ |
Rebecca Silberstein | 934535b | 2016-05-13 13:57:34 -0700 | [diff] [blame] | 1744 | public void sendMessage(int what, int arg1, int arg2, Object obj) { |
Wink Saville | 4753cd2 | 2013-03-06 13:41:23 -0800 | [diff] [blame] | 1745 | // mSmHandler can be null if the state machine has quit. |
| 1746 | SmHandler smh = mSmHandler; |
| 1747 | if (smh == null) return; |
| 1748 | |
| 1749 | smh.sendMessage(obtainMessage(what, arg1, arg2, obj)); |
| 1750 | } |
| 1751 | |
| 1752 | /** |
| 1753 | * Enqueue a message to this state machine. |
| 1754 | * |
| 1755 | * Message is ignored if state machine has quit. |
| 1756 | */ |
Rebecca Silberstein | 934535b | 2016-05-13 13:57:34 -0700 | [diff] [blame] | 1757 | public void sendMessage(Message msg) { |
Jaikumar Ganesh | a544d46 | 2011-12-07 14:52:01 -0800 | [diff] [blame] | 1758 | // mSmHandler can be null if the state machine has quit. |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1759 | SmHandler smh = mSmHandler; |
| 1760 | if (smh == null) return; |
Jaikumar Ganesh | a544d46 | 2011-12-07 14:52:01 -0800 | [diff] [blame] | 1761 | |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1762 | smh.sendMessage(msg); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1763 | } |
| 1764 | |
| 1765 | /** |
| 1766 | * Enqueue a message to this state machine after a delay. |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1767 | * |
| 1768 | * Message is ignored if state machine has quit. |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1769 | */ |
Rebecca Silberstein | 934535b | 2016-05-13 13:57:34 -0700 | [diff] [blame] | 1770 | public void sendMessageDelayed(int what, long delayMillis) { |
Jaikumar Ganesh | a544d46 | 2011-12-07 14:52:01 -0800 | [diff] [blame] | 1771 | // mSmHandler can be null if the state machine has quit. |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1772 | SmHandler smh = mSmHandler; |
| 1773 | if (smh == null) return; |
Jaikumar Ganesh | a544d46 | 2011-12-07 14:52:01 -0800 | [diff] [blame] | 1774 | |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1775 | smh.sendMessageDelayed(obtainMessage(what), delayMillis); |
Wink Saville | 91fbd56 | 2010-03-17 17:12:43 -0700 | [diff] [blame] | 1776 | } |
| 1777 | |
| 1778 | /** |
| 1779 | * Enqueue a message to this state machine after a delay. |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1780 | * |
| 1781 | * Message is ignored if state machine has quit. |
Wink Saville | 91fbd56 | 2010-03-17 17:12:43 -0700 | [diff] [blame] | 1782 | */ |
Rebecca Silberstein | 934535b | 2016-05-13 13:57:34 -0700 | [diff] [blame] | 1783 | public void sendMessageDelayed(int what, Object obj, long delayMillis) { |
Jaikumar Ganesh | a544d46 | 2011-12-07 14:52:01 -0800 | [diff] [blame] | 1784 | // mSmHandler can be null if the state machine has quit. |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1785 | SmHandler smh = mSmHandler; |
| 1786 | if (smh == null) return; |
Jaikumar Ganesh | a544d46 | 2011-12-07 14:52:01 -0800 | [diff] [blame] | 1787 | |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1788 | smh.sendMessageDelayed(obtainMessage(what, obj), delayMillis); |
Wink Saville | 91fbd56 | 2010-03-17 17:12:43 -0700 | [diff] [blame] | 1789 | } |
| 1790 | |
| 1791 | /** |
| 1792 | * Enqueue a message to this state machine after a delay. |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1793 | * |
| 1794 | * Message is ignored if state machine has quit. |
Wink Saville | 91fbd56 | 2010-03-17 17:12:43 -0700 | [diff] [blame] | 1795 | */ |
Rebecca Silberstein | 934535b | 2016-05-13 13:57:34 -0700 | [diff] [blame] | 1796 | public void sendMessageDelayed(int what, int arg1, long delayMillis) { |
Wink Saville | 8b0db52 | 2013-03-14 13:23:19 -0700 | [diff] [blame] | 1797 | // mSmHandler can be null if the state machine has quit. |
| 1798 | SmHandler smh = mSmHandler; |
| 1799 | if (smh == null) return; |
| 1800 | |
| 1801 | smh.sendMessageDelayed(obtainMessage(what, arg1), delayMillis); |
| 1802 | } |
| 1803 | |
| 1804 | /** |
| 1805 | * Enqueue a message to this state machine after a delay. |
| 1806 | * |
| 1807 | * Message is ignored if state machine has quit. |
| 1808 | */ |
Rebecca Silberstein | 934535b | 2016-05-13 13:57:34 -0700 | [diff] [blame] | 1809 | public void sendMessageDelayed(int what, int arg1, int arg2, long delayMillis) { |
Wink Saville | 8b0db52 | 2013-03-14 13:23:19 -0700 | [diff] [blame] | 1810 | // mSmHandler can be null if the state machine has quit. |
| 1811 | SmHandler smh = mSmHandler; |
| 1812 | if (smh == null) return; |
| 1813 | |
| 1814 | smh.sendMessageDelayed(obtainMessage(what, arg1, arg2), delayMillis); |
| 1815 | } |
| 1816 | |
| 1817 | /** |
| 1818 | * Enqueue a message to this state machine after a delay. |
| 1819 | * |
| 1820 | * Message is ignored if state machine has quit. |
| 1821 | */ |
Rebecca Silberstein | 934535b | 2016-05-13 13:57:34 -0700 | [diff] [blame] | 1822 | public void sendMessageDelayed(int what, int arg1, int arg2, Object obj, |
Wink Saville | 4753cd2 | 2013-03-06 13:41:23 -0800 | [diff] [blame] | 1823 | long delayMillis) { |
| 1824 | // mSmHandler can be null if the state machine has quit. |
| 1825 | SmHandler smh = mSmHandler; |
| 1826 | if (smh == null) return; |
| 1827 | |
| 1828 | smh.sendMessageDelayed(obtainMessage(what, arg1, arg2, obj), delayMillis); |
| 1829 | } |
| 1830 | |
| 1831 | /** |
| 1832 | * Enqueue a message to this state machine after a delay. |
| 1833 | * |
| 1834 | * Message is ignored if state machine has quit. |
| 1835 | */ |
Rebecca Silberstein | 934535b | 2016-05-13 13:57:34 -0700 | [diff] [blame] | 1836 | public void sendMessageDelayed(Message msg, long delayMillis) { |
Jaikumar Ganesh | a544d46 | 2011-12-07 14:52:01 -0800 | [diff] [blame] | 1837 | // mSmHandler can be null if the state machine has quit. |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1838 | SmHandler smh = mSmHandler; |
| 1839 | if (smh == null) return; |
Jaikumar Ganesh | a544d46 | 2011-12-07 14:52:01 -0800 | [diff] [blame] | 1840 | |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1841 | smh.sendMessageDelayed(msg, delayMillis); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | /** |
| 1845 | * Enqueue a message to the front of the queue for this state machine. |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1846 | * Protected, may only be called by instances of StateMachine. |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1847 | * |
| 1848 | * Message is ignored if state machine has quit. |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1849 | */ |
Wink Saville | 8b0db52 | 2013-03-14 13:23:19 -0700 | [diff] [blame] | 1850 | protected final void sendMessageAtFrontOfQueue(int what) { |
| 1851 | // mSmHandler can be null if the state machine has quit. |
| 1852 | SmHandler smh = mSmHandler; |
| 1853 | if (smh == null) return; |
| 1854 | |
| 1855 | smh.sendMessageAtFrontOfQueue(obtainMessage(what)); |
| 1856 | } |
| 1857 | |
| 1858 | /** |
| 1859 | * Enqueue a message to the front of the queue for this state machine. |
| 1860 | * Protected, may only be called by instances of StateMachine. |
| 1861 | * |
| 1862 | * Message is ignored if state machine has quit. |
| 1863 | */ |
Wink Saville | 91fbd56 | 2010-03-17 17:12:43 -0700 | [diff] [blame] | 1864 | protected final void sendMessageAtFrontOfQueue(int what, Object obj) { |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1865 | // mSmHandler can be null if the state machine has quit. |
| 1866 | SmHandler smh = mSmHandler; |
| 1867 | if (smh == null) return; |
| 1868 | |
| 1869 | smh.sendMessageAtFrontOfQueue(obtainMessage(what, obj)); |
Wink Saville | 91fbd56 | 2010-03-17 17:12:43 -0700 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | /** |
| 1873 | * Enqueue a message to the front of the queue for this state machine. |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1874 | * Protected, may only be called by instances of StateMachine. |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1875 | * |
| 1876 | * Message is ignored if state machine has quit. |
Wink Saville | 91fbd56 | 2010-03-17 17:12:43 -0700 | [diff] [blame] | 1877 | */ |
Wink Saville | 8b0db52 | 2013-03-14 13:23:19 -0700 | [diff] [blame] | 1878 | protected final void sendMessageAtFrontOfQueue(int what, int arg1) { |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1879 | // mSmHandler can be null if the state machine has quit. |
| 1880 | SmHandler smh = mSmHandler; |
| 1881 | if (smh == null) return; |
| 1882 | |
Wink Saville | 8b0db52 | 2013-03-14 13:23:19 -0700 | [diff] [blame] | 1883 | smh.sendMessageAtFrontOfQueue(obtainMessage(what, arg1)); |
| 1884 | } |
| 1885 | |
| 1886 | |
| 1887 | /** |
| 1888 | * Enqueue a message to the front of the queue for this state machine. |
| 1889 | * Protected, may only be called by instances of StateMachine. |
| 1890 | * |
| 1891 | * Message is ignored if state machine has quit. |
| 1892 | */ |
| 1893 | protected final void sendMessageAtFrontOfQueue(int what, int arg1, int arg2) { |
| 1894 | // mSmHandler can be null if the state machine has quit. |
| 1895 | SmHandler smh = mSmHandler; |
| 1896 | if (smh == null) return; |
| 1897 | |
| 1898 | smh.sendMessageAtFrontOfQueue(obtainMessage(what, arg1, arg2)); |
Wink Saville | 91fbd56 | 2010-03-17 17:12:43 -0700 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | /** |
| 1902 | * Enqueue a message to the front of the queue for this state machine. |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1903 | * Protected, may only be called by instances of StateMachine. |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1904 | * |
| 1905 | * Message is ignored if state machine has quit. |
Wink Saville | 91fbd56 | 2010-03-17 17:12:43 -0700 | [diff] [blame] | 1906 | */ |
Wink Saville | 4753cd2 | 2013-03-06 13:41:23 -0800 | [diff] [blame] | 1907 | protected final void sendMessageAtFrontOfQueue(int what, int arg1, int arg2, Object obj) { |
| 1908 | // mSmHandler can be null if the state machine has quit. |
| 1909 | SmHandler smh = mSmHandler; |
| 1910 | if (smh == null) return; |
| 1911 | |
| 1912 | smh.sendMessageAtFrontOfQueue(obtainMessage(what, arg1, arg2, obj)); |
| 1913 | } |
| 1914 | |
| 1915 | /** |
| 1916 | * Enqueue a message to the front of the queue for this state machine. |
| 1917 | * Protected, may only be called by instances of StateMachine. |
| 1918 | * |
| 1919 | * Message is ignored if state machine has quit. |
| 1920 | */ |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1921 | protected final void sendMessageAtFrontOfQueue(Message msg) { |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1922 | // mSmHandler can be null if the state machine has quit. |
| 1923 | SmHandler smh = mSmHandler; |
| 1924 | if (smh == null) return; |
| 1925 | |
| 1926 | smh.sendMessageAtFrontOfQueue(msg); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 1927 | } |
| 1928 | |
| 1929 | /** |
Jaikumar Ganesh | aa4b235 | 2010-10-14 09:36:39 -0700 | [diff] [blame] | 1930 | * Removes a message from the message queue. |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1931 | * Protected, may only be called by instances of StateMachine. |
Jaikumar Ganesh | aa4b235 | 2010-10-14 09:36:39 -0700 | [diff] [blame] | 1932 | */ |
| 1933 | protected final void removeMessages(int what) { |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1934 | // mSmHandler can be null if the state machine has quit. |
| 1935 | SmHandler smh = mSmHandler; |
| 1936 | if (smh == null) return; |
| 1937 | |
| 1938 | smh.removeMessages(what); |
| 1939 | } |
| 1940 | |
| 1941 | /** |
Ajay Panicker | c211978 | 2015-08-26 14:06:34 -0700 | [diff] [blame] | 1942 | * Removes a message from the deferred messages queue. |
| 1943 | */ |
| 1944 | protected final void removeDeferredMessages(int what) { |
| 1945 | SmHandler smh = mSmHandler; |
| 1946 | if (smh == null) return; |
| 1947 | |
| 1948 | Iterator<Message> iterator = smh.mDeferredMessages.iterator(); |
| 1949 | while (iterator.hasNext()) { |
| 1950 | Message msg = iterator.next(); |
| 1951 | if (msg.what == what) iterator.remove(); |
| 1952 | } |
| 1953 | } |
| 1954 | |
| 1955 | /** |
Amit Mahajan | 8ed715e | 2015-11-03 10:06:03 -0800 | [diff] [blame] | 1956 | * Check if there are any pending messages with code 'what' in deferred messages queue. |
| 1957 | */ |
| 1958 | protected final boolean hasDeferredMessages(int what) { |
| 1959 | SmHandler smh = mSmHandler; |
| 1960 | if (smh == null) return false; |
| 1961 | |
| 1962 | Iterator<Message> iterator = smh.mDeferredMessages.iterator(); |
| 1963 | while (iterator.hasNext()) { |
| 1964 | Message msg = iterator.next(); |
| 1965 | if (msg.what == what) return true; |
| 1966 | } |
| 1967 | |
| 1968 | return false; |
| 1969 | } |
| 1970 | |
| 1971 | /** |
| 1972 | * Check if there are any pending posts of messages with code 'what' in |
| 1973 | * the message queue. This does NOT check messages in deferred message queue. |
| 1974 | */ |
| 1975 | protected final boolean hasMessages(int what) { |
| 1976 | SmHandler smh = mSmHandler; |
| 1977 | if (smh == null) return false; |
| 1978 | |
| 1979 | return smh.hasMessages(what); |
| 1980 | } |
| 1981 | |
| 1982 | /** |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1983 | * Validate that the message was sent by |
| 1984 | * {@link StateMachine#quit} or {@link StateMachine#quitNow}. |
| 1985 | * */ |
| 1986 | protected final boolean isQuit(Message msg) { |
| 1987 | // mSmHandler can be null if the state machine has quit. |
| 1988 | SmHandler smh = mSmHandler; |
| 1989 | if (smh == null) return msg.what == SM_QUIT_CMD; |
| 1990 | |
| 1991 | return smh.isQuit(msg); |
Jaikumar Ganesh | aa4b235 | 2010-10-14 09:36:39 -0700 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | /** |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 1995 | * Quit the state machine after all currently queued up messages are processed. |
Wink Saville | 1b8b98b | 2010-03-11 11:49:54 -0800 | [diff] [blame] | 1996 | */ |
Mitchell Wills | 009ae99 | 2016-08-09 13:33:20 -0700 | [diff] [blame] | 1997 | public final void quit() { |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 1998 | // mSmHandler can be null if the state machine is already stopped. |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 1999 | SmHandler smh = mSmHandler; |
| 2000 | if (smh == null) return; |
Jaikumar Ganesh | a544d46 | 2011-12-07 14:52:01 -0800 | [diff] [blame] | 2001 | |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 2002 | smh.quit(); |
Wink Saville | 1b8b98b | 2010-03-11 11:49:54 -0800 | [diff] [blame] | 2003 | } |
| 2004 | |
| 2005 | /** |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 2006 | * Quit the state machine immediately all currently queued messages will be discarded. |
Wink Saville | 1b8b98b | 2010-03-11 11:49:54 -0800 | [diff] [blame] | 2007 | */ |
Mitchell Wills | 009ae99 | 2016-08-09 13:33:20 -0700 | [diff] [blame] | 2008 | public final void quitNow() { |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 2009 | // mSmHandler can be null if the state machine is already stopped. |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 2010 | SmHandler smh = mSmHandler; |
| 2011 | if (smh == null) return; |
Wink Saville | 1b8b98b | 2010-03-11 11:49:54 -0800 | [diff] [blame] | 2012 | |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 2013 | smh.quitNow(); |
Wink Saville | 583eaaa | 2012-04-13 16:11:20 -0700 | [diff] [blame] | 2014 | } |
| 2015 | |
| 2016 | /** |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 2017 | * @return if debugging is enabled |
| 2018 | */ |
| 2019 | public boolean isDbg() { |
Jaikumar Ganesh | a544d46 | 2011-12-07 14:52:01 -0800 | [diff] [blame] | 2020 | // mSmHandler can be null if the state machine has quit. |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 2021 | SmHandler smh = mSmHandler; |
| 2022 | if (smh == null) return false; |
Jaikumar Ganesh | a544d46 | 2011-12-07 14:52:01 -0800 | [diff] [blame] | 2023 | |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 2024 | return smh.isDbg(); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | /** |
| 2028 | * Set debug enable/disabled. |
| 2029 | * |
| 2030 | * @param dbg is true to enable debugging. |
| 2031 | */ |
| 2032 | public void setDbg(boolean dbg) { |
Jaikumar Ganesh | a544d46 | 2011-12-07 14:52:01 -0800 | [diff] [blame] | 2033 | // mSmHandler can be null if the state machine has quit. |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 2034 | SmHandler smh = mSmHandler; |
| 2035 | if (smh == null) return; |
Jaikumar Ganesh | a544d46 | 2011-12-07 14:52:01 -0800 | [diff] [blame] | 2036 | |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 2037 | smh.setDbg(dbg); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 2038 | } |
| 2039 | |
| 2040 | /** |
| 2041 | * Start the state machine. |
| 2042 | */ |
| 2043 | public void start() { |
Jaikumar Ganesh | a544d46 | 2011-12-07 14:52:01 -0800 | [diff] [blame] | 2044 | // mSmHandler can be null if the state machine has quit. |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 2045 | SmHandler smh = mSmHandler; |
| 2046 | if (smh == null) return; |
Jaikumar Ganesh | a544d46 | 2011-12-07 14:52:01 -0800 | [diff] [blame] | 2047 | |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 2048 | /** Send the complete construction message */ |
Wink Saville | efcc3d3 | 2013-01-30 11:21:22 -0800 | [diff] [blame] | 2049 | smh.completeConstruction(); |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 2050 | } |
Wink Saville | 583eaaa | 2012-04-13 16:11:20 -0700 | [diff] [blame] | 2051 | |
| 2052 | /** |
| 2053 | * Dump the current state. |
| 2054 | * |
| 2055 | * @param fd |
| 2056 | * @param pw |
| 2057 | * @param args |
| 2058 | */ |
| 2059 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Wink Saville | 54b1b1a | 2015-01-12 11:34:20 -0800 | [diff] [blame] | 2060 | // Cannot just invoke pw.println(this.toString()) because if the |
| 2061 | // resulting string is to long it won't be displayed. |
| 2062 | pw.println(getName() + ":"); |
| 2063 | pw.println(" total records=" + getLogRecCount()); |
| 2064 | for (int i = 0; i < getLogRecSize(); i++) { |
| 2065 | pw.println(" rec[" + i + "]: " + getLogRec(i).toString()); |
| 2066 | pw.flush(); |
| 2067 | } |
| 2068 | pw.println("curState=" + getCurrentState().getName()); |
Mike Lockwood | 726d4de | 2014-10-28 14:06:28 -0700 | [diff] [blame] | 2069 | } |
| 2070 | |
| 2071 | @Override |
| 2072 | public String toString() { |
Wink Saville | 54b1b1a | 2015-01-12 11:34:20 -0800 | [diff] [blame] | 2073 | StringWriter sr = new StringWriter(); |
| 2074 | PrintWriter pr = new PrintWriter(sr); |
| 2075 | dump(null, pr, null); |
| 2076 | pr.flush(); |
| 2077 | pr.close(); |
| 2078 | return sr.toString(); |
Wink Saville | 583eaaa | 2012-04-13 16:11:20 -0700 | [diff] [blame] | 2079 | } |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 2080 | |
Wink Saville | ff4fcdb | 2013-02-24 07:21:45 -0800 | [diff] [blame] | 2081 | /** |
| 2082 | * Log with debug and add to the LogRecords. |
| 2083 | * |
| 2084 | * @param s is string log |
| 2085 | */ |
| 2086 | protected void logAndAddLogRec(String s) { |
| 2087 | addLogRec(s); |
| 2088 | log(s); |
| 2089 | } |
| 2090 | |
| 2091 | /** |
| 2092 | * Log with debug |
| 2093 | * |
| 2094 | * @param s is string log |
| 2095 | */ |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 2096 | protected void log(String s) { |
| 2097 | Log.d(mName, s); |
| 2098 | } |
| 2099 | |
Wink Saville | ff4fcdb | 2013-02-24 07:21:45 -0800 | [diff] [blame] | 2100 | /** |
| 2101 | * Log with debug attribute |
| 2102 | * |
| 2103 | * @param s is string log |
| 2104 | */ |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 2105 | protected void logd(String s) { |
| 2106 | Log.d(mName, s); |
| 2107 | } |
| 2108 | |
Wink Saville | ff4fcdb | 2013-02-24 07:21:45 -0800 | [diff] [blame] | 2109 | /** |
| 2110 | * Log with verbose attribute |
| 2111 | * |
| 2112 | * @param s is string log |
| 2113 | */ |
| 2114 | protected void logv(String s) { |
| 2115 | Log.v(mName, s); |
| 2116 | } |
| 2117 | |
| 2118 | /** |
| 2119 | * Log with info attribute |
| 2120 | * |
| 2121 | * @param s is string log |
| 2122 | */ |
| 2123 | protected void logi(String s) { |
| 2124 | Log.i(mName, s); |
| 2125 | } |
| 2126 | |
| 2127 | /** |
| 2128 | * Log with warning attribute |
| 2129 | * |
| 2130 | * @param s is string log |
| 2131 | */ |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 2132 | protected void logw(String s) { |
| 2133 | Log.w(mName, s); |
| 2134 | } |
| 2135 | |
Wink Saville | ff4fcdb | 2013-02-24 07:21:45 -0800 | [diff] [blame] | 2136 | /** |
| 2137 | * Log with error attribute |
| 2138 | * |
| 2139 | * @param s is string log |
| 2140 | */ |
Wink Saville | 58c73c3 | 2013-01-28 10:52:34 -0800 | [diff] [blame] | 2141 | protected void loge(String s) { |
| 2142 | Log.e(mName, s); |
| 2143 | } |
Wink Saville | ff4fcdb | 2013-02-24 07:21:45 -0800 | [diff] [blame] | 2144 | |
| 2145 | /** |
| 2146 | * Log with error attribute |
| 2147 | * |
| 2148 | * @param s is string log |
| 2149 | * @param e is a Throwable which logs additional information. |
| 2150 | */ |
| 2151 | protected void loge(String s, Throwable e) { |
| 2152 | Log.e(mName, s, e); |
| 2153 | } |
Wink Saville | fc5b480 | 2009-12-08 21:22:24 -0800 | [diff] [blame] | 2154 | } |