AHierarchicalStateMachine.cpp revision 5ff1dd576bb93c45b44088a51544a18fc43ebf58
1/*
2 * Copyright (C) 2010 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#include <media/stagefright/foundation/AHierarchicalStateMachine.h>
18
19#include <media/stagefright/foundation/ADebug.h>
20#include <media/stagefright/foundation/AMessage.h>
21#include <utils/Vector.h>
22
23namespace android {
24
25AState::AState(const sp<AState> &parentState)
26    : mParentState(parentState) {
27}
28
29AState::~AState() {
30}
31
32sp<AState> AState::parentState() {
33    return mParentState;
34}
35
36void AState::stateEntered() {
37}
38
39void AState::stateExited() {
40}
41
42////////////////////////////////////////////////////////////////////////////////
43
44AHierarchicalStateMachine::AHierarchicalStateMachine() {
45}
46
47AHierarchicalStateMachine::~AHierarchicalStateMachine() {
48}
49
50void AHierarchicalStateMachine::onMessageReceived(const sp<AMessage> &msg) {
51    sp<AState> save = mState;
52
53    sp<AState> cur = mState;
54    while (cur != NULL && !cur->onMessageReceived(msg)) {
55        // If you claim not to have handled the message you shouldn't
56        // have called setState...
57        CHECK(save == mState);
58
59        cur = cur->parentState();
60    }
61
62    if (cur != NULL) {
63        return;
64    }
65
66    ALOGW("Warning message %s unhandled in root state.",
67         msg->debugString().c_str());
68}
69
70void AHierarchicalStateMachine::changeState(const sp<AState> &state) {
71    if (state == mState) {
72        // Quick exit for the easy case.
73        return;
74    }
75
76    Vector<sp<AState> > A;
77    sp<AState> cur = mState;
78    for (;;) {
79        A.push(cur);
80        if (cur == NULL) {
81            break;
82        }
83        cur = cur->parentState();
84    }
85
86    Vector<sp<AState> > B;
87    cur = state;
88    for (;;) {
89        B.push(cur);
90        if (cur == NULL) {
91            break;
92        }
93        cur = cur->parentState();
94    }
95
96    // Remove the common tail.
97    while (A.size() > 0 && B.size() > 0 && A.top() == B.top()) {
98        A.pop();
99        B.pop();
100    }
101
102    mState = state;
103
104    for (size_t i = 0; i < A.size(); ++i) {
105        A.editItemAt(i)->stateExited();
106    }
107
108    for (size_t i = B.size(); i-- > 0;) {
109        B.editItemAt(i)->stateEntered();
110    }
111}
112
113}  // namespace android
114