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 "core/components_ng/manager/drag_drop/drag_drop_behavior_reporter/drag_drop_behavior_reporter.h"
17
18 #include "interfaces/inner_api/ace_kit/include/ui/base/geometry/point.h"
19
20 #include "base/log/event_report.h"
21 #include "core/common/reporter/reporter.h"
22 #include "core/components_ng/manager/event/json_child_report.h"
23 #include "core/pipeline_ng/pipeline_context.h"
24
25 namespace OHOS::Ace::NG {
26 namespace {
27 const std::string PNAME_ID {"ARKUI"};
28 const std::string PVERSION_ID {"1.0.0"};
29 } // namespace
30
GetInstance()31 DragDropBehaviorReporter& DragDropBehaviorReporter::GetInstance()
32 {
33 static DragDropBehaviorReporter behaviorReporter;
34 return behaviorReporter;
35 }
36
UpdateDragStartResult(DragStartResult result)37 void DragDropBehaviorReporter::UpdateDragStartResult(DragStartResult result)
38 {
39 if (startResult_ != DragStartResult::UNKNOW && result == DragStartResult::DRAG_START_SUCCESS) {
40 return;
41 }
42 startResult_ = result;
43 }
44
UpdateDragStopResult(DragStopResult result)45 void DragDropBehaviorReporter::UpdateDragStopResult(DragStopResult result)
46 {
47 if (stopResult_ != DragStopResult::UNKNOW && result == DragStopResult::DRAG_SOTP_SUCCESS) {
48 return;
49 }
50 stopResult_ = result;
51 }
52
UpdateRecordSize(int32_t recordSize)53 void DragDropBehaviorReporter::UpdateRecordSize(int32_t recordSize)
54 {
55 recordSize_ = recordSize;
56 }
57
UpdateSummaryType(const std::string & summaryType)58 void DragDropBehaviorReporter::UpdateSummaryType(const std::string& summaryType)
59 {
60 summaryType_ = summaryType;
61 }
62
UpdateAllowDropType(const std::set<std::string> & allowDropType)63 void DragDropBehaviorReporter::UpdateAllowDropType(const std::set<std::string>& allowDropType)
64 {
65 allowDropType_ = allowDropType;
66 }
67
UpdateIsCrossing(CrossingEnd isCrossing)68 void DragDropBehaviorReporter::UpdateIsCrossing(CrossingEnd isCrossing)
69 {
70 isCrossing_ = isCrossing;
71 }
72
UpdateStartPoint(Point startPoint)73 void DragDropBehaviorReporter::UpdateStartPoint(Point startPoint)
74 {
75 startPoint_ = startPoint;
76 }
77
UpdateEndPoint(Point endPoint)78 void DragDropBehaviorReporter::UpdateEndPoint(Point endPoint)
79 {
80 endPoint_ = endPoint;
81 }
82
UpdateFrameNodeStartId(int32_t startId)83 void DragDropBehaviorReporter::UpdateFrameNodeStartId(int32_t startId)
84 {
85 startId_ = startId;
86 }
87
UpdateFrameNodeDropId(int32_t dropId)88 void DragDropBehaviorReporter::UpdateFrameNodeDropId(int32_t dropId)
89 {
90 dropId_ = dropId;
91 }
92
UpdateLongPressDurationStart(int64_t longPressDurationStart)93 void DragDropBehaviorReporter::UpdateLongPressDurationStart(int64_t longPressDurationStart)
94 {
95 if (longPressDurationStart == 0 || longPressDurationStart_ == 0) {
96 longPressDurationStart_ = longPressDurationStart;
97 }
98 }
99
UpdateLongPressDurationEnd(int64_t longPressDurationEnd)100 void DragDropBehaviorReporter::UpdateLongPressDurationEnd(int64_t longPressDurationEnd)
101 {
102 longPressDurationEnd_ = longPressDurationEnd;
103 }
104
UpdateDropResult(DropResult dropResult)105 void DragDropBehaviorReporter::UpdateDropResult(DropResult dropResult)
106 {
107 dropResult_ = dropResult;
108 }
109
UpdateContainerId(int32_t containerId)110 void DragDropBehaviorReporter::UpdateContainerId(int32_t containerId)
111 {
112 containerId_ = containerId;
113 }
114
Reset()115 void DragDropBehaviorReporter::Reset()
116 {
117 isCrossing_ = CrossingEnd::NOT_CROSSING;
118 startResult_ = DragStartResult::UNKNOW;
119 stopResult_ = DragStopResult::UNKNOW;
120 recordSize_ = 0;
121 summaryType_ = "";
122 allowDropType_ = {};
123 }
124
Submit(DragReporterPharse pharse,int32_t containerId)125 void DragDropBehaviorReporter::Submit(DragReporterPharse pharse, int32_t containerId)
126 {
127 HandleBehaviorEventReport(pharse, containerId);
128 HandleUISessionReport(pharse, containerId);
129 Reset();
130 }
131
HandleBehaviorEventReport(DragReporterPharse pharse,int32_t containerId)132 void DragDropBehaviorReporter::HandleBehaviorEventReport(DragReporterPharse pharse, int32_t containerId)
133 {
134 bool isStart = pharse == DragReporterPharse::DRAG_START;
135 std::string dragBehavior = isStart ? "DRAG_START" : "DRAG_STOP";
136 int32_t result = isStart ? static_cast<int32_t>(startResult_) : static_cast<int32_t>(stopResult_);
137 std::string allowDropTypes;
138 for (const auto& type: allowDropType_) {
139 std::string str = type + ";";
140 allowDropTypes += str;
141 }
142 containerId_ = containerId_ == INSTANCE_ID_UNDEFINED ? containerId : containerId_;
143 auto container = Container::GetContainer(containerId_);
144 CHECK_NULL_VOID(container);
145 std::string hostName = container->GetBundleName();
146
147 DragInfo dragInfo { static_cast<int32_t>(isCrossing_), result, recordSize_, dragBehavior, PNAME_ID,
148 PVERSION_ID, hostName, summaryType_, allowDropTypes };
149
150 auto pipeline = container->GetPipelineContext();
151 CHECK_NULL_VOID(pipeline);
152 auto taskScheduler = pipeline->GetTaskExecutor();
153 CHECK_NULL_VOID(taskScheduler);
154 taskScheduler->PostTask(
155 [dragInfo]() {
156 EventReport::ReportDragInfo(dragInfo);
157 },
158 TaskExecutor::TaskType::BACKGROUND, "ArkUIDragDropBehaviorReporter");
159 }
160
HandleUISessionReport(DragReporterPharse pharse,int32_t containerId)161 void DragDropBehaviorReporter::HandleUISessionReport(DragReporterPharse pharse, int32_t containerId)
162 {
163 containerId_ = containerId_ == INSTANCE_ID_UNDEFINED ? containerId : containerId_;
164 auto container = Container::GetContainer(containerId_);
165 CHECK_NULL_VOID(container);
166 std::string hostName = container->GetBundleName();
167 int32_t convertToMs = 1000000;
168 int64_t longPressDuration = (longPressDurationEnd_ - longPressDurationStart_) / convertToMs;
169 DragJsonReport dragJsonReport;
170 dragJsonReport.SetStartId(startId_);
171 dragJsonReport.SetDropId(dropId_);
172 dragJsonReport.SetHostName(hostName);
173 dragJsonReport.SetActualDuration(longPressDuration);
174 dragJsonReport.SetStartPoint(startPoint_);
175 dragJsonReport.SetEndPoint(endPoint_);
176 dragJsonReport.SetDropResult(dropResult_);
177 dragJsonReport.SetDragReporterPharse(pharse);
178 Reporter::GetInstance().HandleUISessionReporting(dragJsonReport);
179 }
180 } // namespace OHOS::Ace::NG
181