• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "long_press_event_subscribe_manager.h"
17 
18 #include "define_multimodal.h"
19 #include "error_multimodal.h"
20 #include "multimodal_event_handler.h"
21 
22 #undef MMI_LOG_TAG
23 #define MMI_LOG_TAG "LongPressEventSubscribeManager"
24 
25 namespace OHOS {
26 namespace MMI {
27 namespace {
28 constexpr int32_t INVALID_SUBSCRIBE_ID { -1 };
29 constexpr int32_t MAX_FINGER_COUNT { 2 };
30 constexpr int32_t MAX_DURATION { 3000 };
31 } // namespace
32 int32_t LongPressEventSubscribeManager::subscribeManagerId_ = 0;
33 
LongPressEventSubscribeManager()34 LongPressEventSubscribeManager::LongPressEventSubscribeManager() {}
~LongPressEventSubscribeManager()35 LongPressEventSubscribeManager::~LongPressEventSubscribeManager() {}
36 
SubscribeLongPressEventInfo(const LongPressRequest & longPressRequest,std::function<void (LongPressEvent)> callback)37 LongPressEventSubscribeManager::SubscribeLongPressEventInfo::SubscribeLongPressEventInfo(
38     const LongPressRequest &longPressRequest,
39     std::function<void(LongPressEvent)> callback)
40     : longPressRequest_(longPressRequest), callback_(callback)
41 {
42 }
43 
SubscribeLongPressEvent(const LongPressRequest & longPressRequest,std::function<void (LongPressEvent)> callback)44 int32_t LongPressEventSubscribeManager::SubscribeLongPressEvent(
45     const LongPressRequest &longPressRequest,
46     std::function<void(LongPressEvent)> callback)
47 {
48     CALL_DEBUG_ENTER;
49     CHKPR(callback, ERROR_NULL_POINTER);
50     if (longPressRequest.fingerCount <= 0 || longPressRequest.fingerCount > MAX_FINGER_COUNT ||
51         longPressRequest.duration <= 0 || longPressRequest.duration > MAX_DURATION) {
52         MMI_HILOGE("FingerCount or duration is invalid");
53         return RET_ERR;
54     }
55     if (!MMIEventHdl.InitClient()) {
56         MMI_HILOGE("Client init failed");
57         return EVENT_REG_FAIL;
58     }
59     std::lock_guard<std::mutex> guard(mtx_);
60     if (LongPressEventSubscribeManager::subscribeManagerId_ >= INT_MAX) {
61         MMI_HILOGE("The subscribeId has reached the upper limit, cannot continue the subscription");
62         return INVALID_SUBSCRIBE_ID;
63     }
64     int32_t subscribeId = LongPressEventSubscribeManager::subscribeManagerId_;
65     ++LongPressEventSubscribeManager::subscribeManagerId_;
66     subscribeInfos_.emplace(std::make_pair(subscribeId, SubscribeLongPressEventInfo(longPressRequest, callback)));
67     int32_t ret = MMIEventHdl.SubscribeLongPressEvent(subscribeId, longPressRequest);
68     if (ret != RET_OK) {
69         subscribeInfos_.erase(subscribeId);
70         return INVALID_SUBSCRIBE_ID;
71     }
72     MMI_HILOGI("The subscribeId:%{public}d, fingerCount:%{public}d, duration:%{public}d", subscribeId,
73         longPressRequest.fingerCount, longPressRequest.duration);
74     return subscribeId;
75 }
76 
UnsubscribeLongPressEvent(int32_t subscribeId)77 int32_t LongPressEventSubscribeManager::UnsubscribeLongPressEvent(int32_t subscribeId)
78 {
79     CALL_INFO_TRACE;
80     if (subscribeId < 0) {
81         MMI_HILOGE("The subscribe id is less than 0");
82         return RET_ERR;
83     }
84 
85     std::lock_guard<std::mutex> guard(mtx_);
86     if (subscribeInfos_.empty()) {
87         MMI_HILOGE("The subscribeInfos is empty");
88         return RET_ERR;
89     }
90 
91     auto it = subscribeInfos_.find(subscribeId);
92     if (it != subscribeInfos_.end()) {
93         if (MMIEventHdl.UnsubscribeLongPressEvent(subscribeId) != RET_OK) {
94             MMI_HILOGE("Leave, unsubscribe long press event failed");
95             return RET_ERR;
96         }
97         subscribeInfos_.erase(it);
98         return RET_OK;
99     }
100     MMI_HILOGE("Failed to unsubscribe long press event, subscribeId:%{public}d", subscribeId);
101     return RET_ERR;
102 }
103 
OnSubscribeLongPressEventCallback(const LongPressEvent & longPressEvent,int32_t subscribeId)104 int32_t LongPressEventSubscribeManager::OnSubscribeLongPressEventCallback(const LongPressEvent &longPressEvent,
105     int32_t subscribeId)
106 {
107     CHK_PID_AND_TID();
108     if (subscribeId < 0) {
109         MMI_HILOGE("The subscribe id is less than 0");
110         return RET_ERR;
111     }
112     std::function<void(LongPressEvent)> callback = nullptr;
113     std::lock_guard<std::mutex> guard(mtx_);
114     auto it = subscribeInfos_.find(subscribeId);
115     if (it != subscribeInfos_.end()) {
116         SubscribeLongPressEventInfo &subscribeInfo = it->second;
117         callback = subscribeInfo.GetCallback();
118     }
119     CHKPR(callback, ERROR_NULL_POINTER);
120     callback(longPressEvent);
121     MMI_HILOGD("LongPressEvent fingerCount:%{public}d, duration:%{public}d, pid:%{public}d, displayId:%{public}d, "
122         "displayX:%{public}d, displayY:%{public}d, result:%{public}d, windowId:%{public}d, pointerId:%{public}d, "
123         "bundleName:%{public}s, subscribeId:%{public}d, downTime:%{public} " PRId64 "",
124         longPressEvent.fingerCount, longPressEvent.duration, longPressEvent.pid,
125         longPressEvent.displayId, longPressEvent.displayX, longPressEvent.displayY,
126         longPressEvent.result, longPressEvent.windowId, longPressEvent.pointerId, longPressEvent.bundleName.c_str(),
127         subscribeId, longPressEvent.downTime);
128     return RET_OK;
129 }
130 
OnConnected()131 void LongPressEventSubscribeManager::OnConnected()
132 {
133     CALL_DEBUG_ENTER;
134     std::lock_guard<std::mutex> guard(mtx_);
135     if (subscribeInfos_.empty()) {
136         MMI_HILOGD("Leave, subscribeInfos_ is empty");
137         return;
138     }
139     for (auto it = subscribeInfos_.begin(); it != subscribeInfos_.end(); ++it) {
140         SubscribeLongPressEventInfo &subscribeInfo = it->second;
141         LongPressRequest longPressRequest = subscribeInfo.GetLongPressRequest();
142         int32_t ret = MMIEventHdl.SubscribeLongPressEvent(it->first, longPressRequest);
143         if (ret != RET_OK) {
144             MMI_HILOGE("Subscribe long pres event failed, ret:%{public}d", ret);
145         }
146     }
147 }
148 } // namespace MMI
149 } // namespace OHOS
150