blob: a3059c5b52ac836d8fdd2c0ab9491d4d406dc2ae [file] [log] [blame]
tsaichristine10978642019-09-10 14:12:49 -07001/*
2 * Copyright (C) 2019, 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#define DEBUG false // STOPSHIP if true
18#include "Log.h"
19
20#include "StateManager.h"
21
22namespace android {
23namespace os {
24namespace statsd {
25
26StateManager& StateManager::getInstance() {
27 static StateManager sStateManager;
28 return sStateManager;
29}
30
31void StateManager::onLogEvent(const LogEvent& event) {
32 std::lock_guard<std::mutex> lock(mMutex);
33 if (mStateTrackers.find(event.GetTagId()) != mStateTrackers.end()) {
34 mStateTrackers[event.GetTagId()]->onLogEvent(event);
35 }
36}
37
38bool StateManager::registerListener(int stateAtomId, wp<StateListener> listener) {
39 std::lock_guard<std::mutex> lock(mMutex);
40
41 // Check if state tracker already exists
42 if (mStateTrackers.find(stateAtomId) == mStateTrackers.end()) {
43 // Create a new state tracker iff atom is a state atom
44 auto it = android::util::AtomsInfo::kStateAtomsFieldOptions.find(stateAtomId);
45 if (it != android::util::AtomsInfo::kStateAtomsFieldOptions.end()) {
46 mStateTrackers[stateAtomId] = new StateTracker(stateAtomId, it->second);
47 } else {
48 ALOGE("StateManager cannot register listener, Atom %d is not a state atom",
49 stateAtomId);
50 return false;
51 }
52 }
53 mStateTrackers[stateAtomId]->registerListener(listener);
54 return true;
55}
56
57void StateManager::unregisterListener(int stateAtomId, wp<StateListener> listener) {
58 std::unique_lock<std::mutex> lock(mMutex);
59
60 // Hold the sp<> until the lock is released so that ~StateTracker() is
61 // not called while the lock is held.
62 sp<StateTracker> toRemove;
63
64 // Unregister listener from correct StateTracker
65 auto it = mStateTrackers.find(stateAtomId);
66 if (it != mStateTrackers.end()) {
67 it->second->unregisterListener(listener);
68
69 // Remove the StateTracker if it has no listeners
70 if (it->second->getListenersCount() == 0) {
71 toRemove = it->second;
72 mStateTrackers.erase(it);
73 }
74 } else {
75 ALOGE("StateManager cannot unregister listener, StateTracker for atom %d does not exist",
76 stateAtomId);
77 }
78 lock.unlock();
79}
80
81int StateManager::getState(int stateAtomId, const HashableDimensionKey& key) {
82 std::lock_guard<std::mutex> lock(mMutex);
83 if (mStateTrackers.find(stateAtomId) != mStateTrackers.end()) {
84 return mStateTrackers[stateAtomId]->getState(key);
85 }
86
87 return StateTracker::kStateUnknown;
88}
89
90} // namespace statsd
91} // namespace os
92} // namespace android