• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "switch_event_input_subscribe_manager.h"
17 
18 #include <cinttypes>
19 
20 #include "bytrace_adapter.h"
21 #include "define_multimodal.h"
22 #include "error_multimodal.h"
23 #include "multimodal_event_handler.h"
24 
25 namespace OHOS {
26 namespace MMI {
27 namespace {
28 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "SwitchEventInputSubscribeManager" };
29 constexpr int32_t INVALID_SUBSCRIBE_ID = -1;
30 } // namespace
31 int32_t SwitchEventInputSubscribeManager::subscribeManagerId_ = 0;
32 
SwitchEventInputSubscribeManager()33 SwitchEventInputSubscribeManager::SwitchEventInputSubscribeManager() {}
~SwitchEventInputSubscribeManager()34 SwitchEventInputSubscribeManager::~SwitchEventInputSubscribeManager() {}
35 
SubscribeSwitchEvent(std::function<void (std::shared_ptr<SwitchEvent>)> callback)36 int32_t SwitchEventInputSubscribeManager::SubscribeSwitchEvent(
37     std::function<void(std::shared_ptr<SwitchEvent>)> callback)
38 {
39     CALL_INFO_TRACE;
40     CHKPR(callback, INVALID_SUBSCRIBE_ID);
41 
42     std::lock_guard<std::mutex> guard(mtx_);
43     if (!MMIEventHdl.InitClient()) {
44         MMI_HILOGE("Client init failed");
45         return INVALID_SUBSCRIBE_ID;
46     }
47     if (SwitchEventInputSubscribeManager::subscribeManagerId_ >= INT_MAX) {
48         MMI_HILOGE("The subscribeId has reached the upper limit, cannot continue the subscription");
49         return INVALID_SUBSCRIBE_ID;
50     }
51     int32_t subscribeId = SwitchEventInputSubscribeManager::subscribeManagerId_;
52     ++SwitchEventInputSubscribeManager::subscribeManagerId_;
53     subscribeInfos_[subscribeId] = callback;
54     MMIEventHdl.SubscribeSwitchEvent(subscribeId);
55     MMI_HILOGI("subscribeId:%{public}d,", subscribeId);
56 
57     return subscribeId;
58 }
59 
UnsubscribeSwitchEvent(int32_t subscribeId)60 int32_t SwitchEventInputSubscribeManager::UnsubscribeSwitchEvent(int32_t subscribeId)
61 {
62     CALL_INFO_TRACE;
63     if (subscribeId < 0) {
64         MMI_HILOGE("The subscribe id is less than 0");
65         return RET_ERR;
66     }
67 
68     std::lock_guard<std::mutex> guard(mtx_);
69     if (!MMIEventHdl.InitClient()) {
70         MMI_HILOGE("Client init failed");
71         return INVALID_SUBSCRIBE_ID;
72     }
73     if (subscribeInfos_.empty()) {
74         MMI_HILOGE("The subscribeInfos is empty");
75         return RET_ERR;
76     }
77 
78     auto it = subscribeInfos_.find(subscribeId);
79     if (it != subscribeInfos_.end()) {
80         if (MMIEventHdl.UnsubscribeSwitchEvent(subscribeId) != RET_OK) {
81             MMI_HILOGE("Leave, unsubscribe switch event failed");
82             return RET_ERR;
83         }
84         subscribeInfos_.erase(it);
85         return RET_OK;
86     }
87 
88     return RET_ERR;
89 }
90 
OnSubscribeSwitchEventCallback(std::shared_ptr<SwitchEvent> event,int32_t subscribeId)91 int32_t SwitchEventInputSubscribeManager::OnSubscribeSwitchEventCallback(std::shared_ptr<SwitchEvent> event,
92     int32_t subscribeId)
93 {
94     CHK_PID_AND_TID();
95     CHKPR(event, ERROR_NULL_POINTER);
96     if (subscribeId < 0) {
97         MMI_HILOGE("Leave, the subscribe id is less than 0");
98         return RET_ERR;
99     }
100     std::function<void(std::shared_ptr<SwitchEvent>)> callback = nullptr;
101     std::lock_guard<std::mutex> guard(mtx_);
102     auto it = subscribeInfos_.find(subscribeId);
103     if (it != subscribeInfos_.end()) {
104         callback = it->second;
105     }
106     CHKPR(callback, ERROR_NULL_POINTER);
107     callback(event);
108     MMI_HILOGD("Switch event id:%{public}d switchValue:%{public}d", subscribeId, event->GetSwitchValue());
109     return RET_OK;
110 }
111 
OnConnected()112 void SwitchEventInputSubscribeManager::OnConnected()
113 {
114     CALL_DEBUG_ENTER;
115     if (subscribeInfos_.empty()) {
116         MMI_HILOGD("Leave, subscribeInfos_ is empty");
117         return;
118     }
119     for (auto it = subscribeInfos_.begin(); it != subscribeInfos_.end(); ++it) {
120         if (MMIEventHdl.SubscribeSwitchEvent(it->first) != RET_OK) {
121             MMI_HILOGE("Subscribe switch event failed");
122         }
123     }
124 }
125 
126 } // namespace MMI
127 } // namespace OHOS