tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 18 | //#include <utils/Log.h> |
| 19 | #include <utils/RefBase.h> |
| 20 | #include "HashableDimensionKey.h" |
| 21 | |
| 22 | #include "state/StateListener.h" |
| 23 | #include "state/StateTracker.h" |
| 24 | |
| 25 | namespace android { |
| 26 | namespace os { |
| 27 | namespace statsd { |
| 28 | |
| 29 | class StateManager : public virtual RefBase { |
| 30 | public: |
| 31 | StateManager(){}; |
| 32 | |
| 33 | ~StateManager(){}; |
| 34 | |
| 35 | // Returns a pointer to the single, shared StateManager object. |
| 36 | static StateManager& getInstance(); |
| 37 | |
| 38 | // Notifies the correct StateTracker of an event. |
| 39 | void onLogEvent(const LogEvent& event); |
| 40 | |
| 41 | // Returns true if stateAtomId is the id of a state atom and notifies the |
| 42 | // correct StateTracker to register the listener. If the correct |
| 43 | // StateTracker does not exist, a new StateTracker is created. |
| 44 | bool registerListener(int stateAtomId, wp<StateListener> listener); |
| 45 | |
| 46 | // Notifies the correct StateTracker to unregister a listener |
| 47 | // and removes the tracker if it no longer has any listeners. |
| 48 | void unregisterListener(int stateAtomId, wp<StateListener> listener); |
| 49 | |
| 50 | // Queries the correct StateTracker for the state that is mapped to the given |
| 51 | // query key. |
| 52 | // If the StateTracker doesn't exist, returns StateTracker::kStateUnknown. |
| 53 | int getState(int stateAtomId, const HashableDimensionKey& queryKey); |
| 54 | |
| 55 | inline int getStateTrackersCount() { |
| 56 | std::lock_guard<std::mutex> lock(mMutex); |
| 57 | return mStateTrackers.size(); |
| 58 | } |
| 59 | |
| 60 | inline int getListenersCount(int stateAtomId) { |
| 61 | std::lock_guard<std::mutex> lock(mMutex); |
| 62 | if (mStateTrackers.find(stateAtomId) != mStateTrackers.end()) { |
| 63 | return mStateTrackers[stateAtomId]->getListenersCount(); |
| 64 | } |
| 65 | return -1; |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | mutable std::mutex mMutex; |
| 70 | |
| 71 | // Maps state atom ids to StateTrackers |
| 72 | std::unordered_map<int, sp<StateTracker>> mStateTrackers; |
| 73 | }; |
| 74 | |
| 75 | } // namespace statsd |
| 76 | } // namespace os |
| 77 | } // namespace android |