blob: 5514b446b3066f306f447125008e38d32824a89a [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
tsaichristinec876b492019-12-10 13:47:05 -080031void StateManager::clear() {
32 mStateTrackers.clear();
33}
34
tsaichristine10978642019-09-10 14:12:49 -070035void StateManager::onLogEvent(const LogEvent& event) {
tsaichristine10978642019-09-10 14:12:49 -070036 if (mStateTrackers.find(event.GetTagId()) != mStateTrackers.end()) {
37 mStateTrackers[event.GetTagId()]->onLogEvent(event);
38 }
39}
40
Muhammad Qureshibfc4bdb2020-04-08 06:26:49 -070041void StateManager::registerListener(const int32_t atomId, wp<StateListener> listener) {
tsaichristine7192e0a2019-12-06 02:20:00 -080042 // Check if state tracker already exists.
tsaichristined21aacf2019-10-07 14:47:38 -070043 if (mStateTrackers.find(atomId) == mStateTrackers.end()) {
Muhammad Qureshibfc4bdb2020-04-08 06:26:49 -070044 mStateTrackers[atomId] = new StateTracker(atomId);
tsaichristine10978642019-09-10 14:12:49 -070045 }
tsaichristined21aacf2019-10-07 14:47:38 -070046 mStateTrackers[atomId]->registerListener(listener);
tsaichristine10978642019-09-10 14:12:49 -070047}
48
tsaichristinec876b492019-12-10 13:47:05 -080049void StateManager::unregisterListener(const int32_t atomId, wp<StateListener> listener) {
tsaichristine10978642019-09-10 14:12:49 -070050 std::unique_lock<std::mutex> lock(mMutex);
51
52 // Hold the sp<> until the lock is released so that ~StateTracker() is
53 // not called while the lock is held.
54 sp<StateTracker> toRemove;
55
56 // Unregister listener from correct StateTracker
tsaichristined21aacf2019-10-07 14:47:38 -070057 auto it = mStateTrackers.find(atomId);
tsaichristine10978642019-09-10 14:12:49 -070058 if (it != mStateTrackers.end()) {
59 it->second->unregisterListener(listener);
60
61 // Remove the StateTracker if it has no listeners
62 if (it->second->getListenersCount() == 0) {
63 toRemove = it->second;
64 mStateTrackers.erase(it);
65 }
66 } else {
67 ALOGE("StateManager cannot unregister listener, StateTracker for atom %d does not exist",
tsaichristined21aacf2019-10-07 14:47:38 -070068 atomId);
tsaichristine10978642019-09-10 14:12:49 -070069 }
70 lock.unlock();
71}
72
tsaichristinec876b492019-12-10 13:47:05 -080073bool StateManager::getStateValue(const int32_t atomId, const HashableDimensionKey& key,
tsaichristine69000e62019-10-18 17:34:52 -070074 FieldValue* output) const {
tsaichristine69000e62019-10-18 17:34:52 -070075 auto it = mStateTrackers.find(atomId);
76 if (it != mStateTrackers.end()) {
77 return it->second->getStateValue(key, output);
78 }
79 return false;
tsaichristine10978642019-09-10 14:12:49 -070080}
81
82} // namespace statsd
83} // namespace os
84} // namespace android