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