1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "observer_handler.h"
17
18 #include "telephony_log_wrapper.h"
19
20 namespace OHOS {
21 namespace Telephony {
ObserverHandler()22 ObserverHandler::ObserverHandler() {}
23
~ObserverHandler()24 ObserverHandler::~ObserverHandler() {}
25
RegObserver(int32_t what,const std::shared_ptr<AppExecFwk::EventHandler> handler)26 void ObserverHandler::RegObserver(int32_t what, const std::shared_ptr<AppExecFwk::EventHandler> handler)
27 {
28 if (handler == nullptr) {
29 TELEPHONY_LOGE("RegObserver failed, handler is nullptr!");
30 return;
31 }
32
33 std::lock_guard<std::mutex> lock(mutex_);
34 auto iter = observerHandlerMap_.find(what);
35 if (iter != observerHandlerMap_.end()) {
36 std::list<std::shared_ptr<OHOS::AppExecFwk::EventHandler>> &handlers = iter->second;
37 auto it = find(handlers.begin(), handlers.end(), handler);
38 if (it == handlers.end()) {
39 handlers.push_back(handler);
40 }
41 TELEPHONY_LOGD("ObserverHandler RegObserver update callback what: %{public}d, list size: %{public}zu", what,
42 handlers.size());
43 } else {
44 TELEPHONY_LOGD("ObserverHandler RegObserver callback what: %{public}d", what);
45 std::list<std::shared_ptr<AppExecFwk::EventHandler>> handlers;
46 handlers.push_back(handler);
47 observerHandlerMap_.emplace(what, handlers);
48 }
49 }
50
RemoveAll()51 void ObserverHandler::RemoveAll()
52 {
53 std::lock_guard<std::mutex> lock(mutex_);
54 observerHandlerMap_.clear();
55 }
56
Remove(int32_t what,const std::shared_ptr<AppExecFwk::EventHandler> handler)57 void ObserverHandler::Remove(int32_t what, const std::shared_ptr<AppExecFwk::EventHandler> handler)
58 {
59 if (handler == nullptr) {
60 TELEPHONY_LOGE("ObserverHandler handler==nullptr");
61 return;
62 }
63
64 std::lock_guard<std::mutex> lock(mutex_);
65 auto iter = observerHandlerMap_.find(what);
66 if (iter != observerHandlerMap_.end()) {
67 std::list<std::shared_ptr<OHOS::AppExecFwk::EventHandler>> &handlers = iter->second;
68 auto it = find(handlers.begin(), handlers.end(), handler);
69 if (it != handlers.end()) {
70 handlers.erase(it);
71 }
72 TELEPHONY_LOGD("ObserverHandler Remove handlers list: %{public}zu", handlers.size());
73 }
74 }
75
NotifyObserver(int32_t what)76 void ObserverHandler::NotifyObserver(int32_t what)
77 {
78 std::lock_guard<std::mutex> lock(mutex_);
79 auto iter = observerHandlerMap_.find(what);
80 if (iter == observerHandlerMap_.end()) {
81 TELEPHONY_LOGE("ObserverHandler NotifyObserver %{public}d not register", what);
82 return;
83 }
84
85 for (auto handler : iter->second) {
86 TELEPHONY_LOGD("handler->SendEvent:%{public}d", what);
87 TelEventHandler::SendTelEvent(handler, what);
88 }
89 }
90 } // namespace Telephony
91 } // namespace OHOS