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 auto iter = observerHandlerMap_.find(what);
29 if (iter != observerHandlerMap_.end()) {
30 std::list<std::shared_ptr<OHOS::AppExecFwk::EventHandler>> &handlers = iter->second;
31 auto it = find(handlers.begin(), handlers.end(), handler);
32 if (it == handlers.end()) {
33 handlers.push_back(handler);
34 }
35 TELEPHONY_LOGI("ObserverHandler RegObserver update callback what: %{public}d, list size: %{public}zu", what,
36 handlers.size());
37 } else {
38 TELEPHONY_LOGI("ObserverHandler RegObserver callback what: %{public}d", what);
39 std::list<std::shared_ptr<AppExecFwk::EventHandler>> handlers;
40 handlers.push_back(handler);
41 observerHandlerMap_.emplace(what, handlers);
42 }
43 }
44
RemoveAll()45 void ObserverHandler::RemoveAll()
46 {
47 observerHandlerMap_.clear();
48 }
49
Remove(int32_t what,const std::shared_ptr<AppExecFwk::EventHandler> handler)50 void ObserverHandler::Remove(int32_t what, const std::shared_ptr<AppExecFwk::EventHandler> handler)
51 {
52 if (handler == nullptr) {
53 TELEPHONY_LOGE("ObserverHandler handler==nullptr");
54 return;
55 }
56
57 auto iter = observerHandlerMap_.find(what);
58 if (iter != observerHandlerMap_.end()) {
59 std::list<std::shared_ptr<OHOS::AppExecFwk::EventHandler>> &handlers = iter->second;
60 auto it = find(handlers.begin(), handlers.end(), handler);
61 if (it != handlers.end()) {
62 handlers.erase(it);
63 }
64 TELEPHONY_LOGI("ObserverHandler Remove handlers list: %{public}zu", handlers.size());
65 }
66 }
67
NotifyObserver(int32_t what)68 void ObserverHandler::NotifyObserver(int32_t what)
69 {
70 auto iter = observerHandlerMap_.find(what);
71 if (iter == observerHandlerMap_.end()) {
72 TELEPHONY_LOGE("ObserverHandler NotifyObserver %{public}d not register", what);
73 return;
74 }
75
76 for (auto handler : iter->second) {
77 TELEPHONY_LOGI("handler->SendEvent:%{public}d", what);
78 handler->SendEvent(what);
79 }
80 }
81 } // namespace Telephony
82 } // namespace OHOS