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 "drag_hisysevent.h"
17 
18 #include "fi_log.h"
19 
20 #undef LOG_TAG
21 #define LOG_TAG "DragHiSysEvent"
22 
23 namespace OHOS {
24 namespace Msdp {
25 namespace DeviceStatus {
26 
27 std::map<DragState, std::string> DragDFX::dragState_ = {
28     { DragState::ERROR, "ERROR" },
29     { DragState::START, "START" },
30     { DragState::STOP, "STOP" },
31     { DragState::CANCEL, "CANCEL" },
32     { DragState::MOTION_DRAGGING, "MOTION_DRAGGING" }
33 };
34 
35 std::map<DragCursorStyle, std::string> DragDFX::dragStyle_ = {
36     { DragCursorStyle::DEFAULT, "DEFAULT" },
37     { DragCursorStyle::FORBIDDEN, "FORBIDDEN" },
38     { DragCursorStyle::COPY, "COPY" },
39     { DragCursorStyle::MOVE, "MOVE" }
40 };
41 
42 std::map<DragResult, std::string> DragDFX::dragResult_ = {
43     { DragResult::DRAG_SUCCESS, "DRAG_SUCCESS" },
44     { DragResult::DRAG_FAIL, "DRAG_FAIL" },
45     { DragResult::DRAG_CANCEL, "DRAG_CANCEL" },
46     { DragResult::DRAG_EXCEPTION, "DRAG_EXPECTION" }
47 };
48 
49 std::map<DragType, std::pair<std::string, std::string>> DragDFX::serialStr_ = {
50     { DragType::STA_DRAG_SUCC, { "START_DRAG_SUCCESS", "Start drag successfully" } },
51     { DragType::STA_DRAG_FAIL, { "START_DRAG_FAILED", "Start drag failed" } },
52     { DragType::SET_DRAG_WINDOW_SUCC, { "SET_DRAG_WINDOW_VISIBLE_SUCCESS", "Set drag window visible successfully" } },
53     { DragType::SET_DRAG_WINDOW_FAIL, { "SET_DRAG_WINDOW_VISIBLE_FAILED", "Set drag window visible failed" } },
54     { DragType::UPDATE_DRAG_STYLE_SUCC, { "UPDATE_DRAG_STYLE_SUCCESS", "Update drag style successfully" } },
55     { DragType::UPDATE_DRAG_STYLE_FAIL, { "UPDATE_DRAG_STYLE_FAILED", "Update drag style failed"} },
56     { DragType::SEND_TOKENID, { "SEND_TOKENID", "Send token id failed" } },
57     { DragType::STOP_DRAG_SUCC, { "STOP_DRAG_SUCCESS", "Stop drag successfully" } },
58     { DragType::STOP_DRAG_FAIL, { "STOP_DRAG_FAILED", "Stop drag failed"} },
59     { DragType::NOTIFY_DRAG_RESULT_SUCC, { "NOTIFY_DRAG_RESULT_SUCCESS", "Notify drag result successfully" } },
60     { DragType::NOTIFY_DRAG_RESULT_FAIL, { "NOTIFY_DRAG_RESULT_FAILED", "Notify drag result failed"} }
61 };
62 
63 template<typename... Types>
WriteModel(const DragType & dragType,Types...paras)64 int32_t DragDFX::WriteModel(const DragType &dragType, Types... paras)
65 {
66     if (serialStr_.find(dragType) == serialStr_.end()) {
67         FI_HILOGE("serialStr_ can't find the drag hisysevent type");
68         return RET_ERR;
69     }
70     auto &[label, dec] = serialStr_[dragType];
71     OHOS::HiviewDFX::HiSysEvent::EventType eventType = (static_cast<uint32_t>(dragType) & 1) != 0 ?
72         OHOS::HiviewDFX::HiSysEvent::EventType::FAULT : OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR;
73     int32_t ret = HiSysEventWrite(
74         OHOS::HiviewDFX::HiSysEvent::Domain::MSDP,
75         label,
76         eventType,
77         "MSG",
78         dec,
79         paras...);
80     if (ret == RET_ERR) {
81         FI_HILOGE("HiviewDFX write failed, ret:%{public}d", ret);
82     }
83     return ret;
84 }
85 
WriteStartDrag(const DragState & dragState,OHOS::HiviewDFX::HiSysEvent::EventType type)86 int32_t DragDFX::WriteStartDrag(const DragState &dragState, OHOS::HiviewDFX::HiSysEvent::EventType type)
87 {
88     if (dragState_.find(dragState) == dragState_.end()) {
89         FI_HILOGE("dragState_ can't find the drag state");
90         return RET_ERR;
91     }
92     std::string curDragState = dragState_[dragState];
93     if (type == OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR) {
94         return WriteModel(DragType::STA_DRAG_SUCC, "dragState", curDragState);
95     }
96     return WriteModel(DragType::STA_DRAG_FAIL, "dragState", curDragState);
97 }
98 
WriteDragWindowVisible(const DragState & dragState,bool visible,OHOS::HiviewDFX::HiSysEvent::EventType type)99 int32_t DragDFX::WriteDragWindowVisible(const DragState &dragState, bool visible,
100     OHOS::HiviewDFX::HiSysEvent::EventType type)
101 {
102     if (dragState_.find(dragState) == dragState_.end()) {
103         FI_HILOGE("dragState_ can't find the drag state");
104         return RET_ERR;
105     }
106     std::string curDragState = dragState_[dragState];
107     if (type == OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR) {
108         return WriteModel(DragType::SET_DRAG_WINDOW_SUCC, "IsVisible", visible, "dragState", curDragState);
109     }
110     return WriteModel(DragType::SET_DRAG_WINDOW_FAIL, "IsVisible", visible, "dragState", curDragState);
111 }
112 
WriteUpdateDragStyle(const DragCursorStyle & style,OHOS::HiviewDFX::HiSysEvent::EventType type)113 int32_t DragDFX::WriteUpdateDragStyle(const DragCursorStyle &style, OHOS::HiviewDFX::HiSysEvent::EventType type)
114 {
115     if (dragStyle_.find(style) == dragStyle_.end()) {
116         FI_HILOGE("dragStyle_ can't find the drag style");
117         return RET_ERR;
118     }
119     std::string dragStyle = dragStyle_[style];
120     if (type == OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR) {
121         return WriteModel(DragType::UPDATE_DRAG_STYLE_SUCC, "dragStyle", dragStyle);
122     }
123     return WriteModel(DragType::UPDATE_DRAG_STYLE_FAIL, "dragStyle", dragStyle);
124 }
125 
WriteSendTokenid(int32_t targetTid,const std::string & udKey)126 int32_t DragDFX::WriteSendTokenid(int32_t targetTid, const std::string &udKey)
127 {
128     return WriteModel(DragType::SEND_TOKENID, "targetTid", targetTid, "udKey", udKey);
129 }
130 
WriteStopDrag(const DragState & dragState,const DragDropResult & dropResult,OHOS::HiviewDFX::HiSysEvent::EventType type)131 int32_t DragDFX::WriteStopDrag(const DragState &dragState, const DragDropResult &dropResult,
132     OHOS::HiviewDFX::HiSysEvent::EventType type)
133 {
134     if (dragState_.find(dragState) == dragState_.end()) {
135         FI_HILOGE("dragState_ can't find the drag state");
136         return RET_ERR;
137     }
138     std::string curDragState = dragState_[dragState];
139     if (type == OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR) {
140         return WriteModel(DragType::STOP_DRAG_SUCC, "dragState", curDragState, "animate",
141             dropResult.hasCustomAnimation);
142     }
143     return WriteModel(DragType::STOP_DRAG_FAIL, "dragstate", curDragState, "animate", dropResult.hasCustomAnimation);
144 }
145 
WriteNotifyDragResult(const DragResult & result,OHOS::HiviewDFX::HiSysEvent::EventType type)146 int32_t DragDFX::WriteNotifyDragResult(const DragResult &result, OHOS::HiviewDFX::HiSysEvent::EventType type)
147 {
148     if (dragResult_.find(result) == dragResult_.end()) {
149         FI_HILOGE("dragResult_ can't find the drag result");
150         return RET_ERR;
151     }
152     std::string dragResult = dragResult_[result];
153     if (type == OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR) {
154         return WriteModel(DragType::NOTIFY_DRAG_RESULT_SUCC, "DragResult", dragResult);
155     }
156     return WriteModel(DragType::NOTIFY_DRAG_RESULT_FAIL, "DragResult", dragResult);
157 }
158 } // namespace DeviceStatus
159 } // namespace Msdp
160 } // namespace OHOS