blob: 89ee6c04b922eea60d4dfae5625cfe4acd807018 [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#pragma once
17
tsaichristined21aacf2019-10-07 14:47:38 -070018#include <gtest/gtest_prod.h>
19#include <inttypes.h>
tsaichristine10978642019-09-10 14:12:49 -070020#include <utils/RefBase.h>
tsaichristine10978642019-09-10 14:12:49 -070021
tsaichristined21aacf2019-10-07 14:47:38 -070022#include "HashableDimensionKey.h"
tsaichristine10978642019-09-10 14:12:49 -070023#include "state/StateListener.h"
24#include "state/StateTracker.h"
25
26namespace android {
27namespace os {
28namespace statsd {
29
30class StateManager : public virtual RefBase {
31public:
32 StateManager(){};
33
34 ~StateManager(){};
35
36 // Returns a pointer to the single, shared StateManager object.
37 static StateManager& getInstance();
38
39 // Notifies the correct StateTracker of an event.
40 void onLogEvent(const LogEvent& event);
41
tsaichristined21aacf2019-10-07 14:47:38 -070042 // Returns true if atomId is being tracked and is associated with a state
43 // atom. StateManager notifies the correct StateTracker to register listener.
44 // If the correct StateTracker does not exist, a new StateTracker is created.
45 bool registerListener(int atomId, wp<StateListener> listener);
tsaichristine10978642019-09-10 14:12:49 -070046
47 // Notifies the correct StateTracker to unregister a listener
48 // and removes the tracker if it no longer has any listeners.
tsaichristined21aacf2019-10-07 14:47:38 -070049 void unregisterListener(int atomId, wp<StateListener> listener);
tsaichristine10978642019-09-10 14:12:49 -070050
tsaichristined21aacf2019-10-07 14:47:38 -070051 // Queries the correct StateTracker for the original/un-mapped state value
52 // that is mapped to the given query key.
tsaichristine10978642019-09-10 14:12:49 -070053 // If the StateTracker doesn't exist, returns StateTracker::kStateUnknown.
tsaichristined21aacf2019-10-07 14:47:38 -070054 int getStateValue(int atomId, const HashableDimensionKey& queryKey);
tsaichristine10978642019-09-10 14:12:49 -070055
56 inline int getStateTrackersCount() {
57 std::lock_guard<std::mutex> lock(mMutex);
58 return mStateTrackers.size();
59 }
60
tsaichristined21aacf2019-10-07 14:47:38 -070061 inline int getListenersCount(int atomId) {
tsaichristine10978642019-09-10 14:12:49 -070062 std::lock_guard<std::mutex> lock(mMutex);
tsaichristined21aacf2019-10-07 14:47:38 -070063 if (mStateTrackers.find(atomId) != mStateTrackers.end()) {
64 return mStateTrackers[atomId]->getListenersCount();
tsaichristine10978642019-09-10 14:12:49 -070065 }
66 return -1;
67 }
68
69private:
70 mutable std::mutex mMutex;
71
72 // Maps state atom ids to StateTrackers
73 std::unordered_map<int, sp<StateTracker>> mStateTrackers;
74};
75
76} // namespace statsd
77} // namespace os
78} // namespace android