1 /* 2 * Copyright (c) 2021-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 #ifndef FOUNDATION_ACE_FRAMEWORKS_BASE_RESSCHED_RESSCHED_REPORT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_RESSCHED_RESSCHED_REPORT_H 18 19 #include <chrono> 20 #include <string> 21 #include <unordered_map> 22 23 #include "base/utils/macros.h" 24 #include "base/utils/noncopyable.h" 25 #include "core/event/touch_event.h" 26 #include "core/pipeline_ng/pipeline_context.h" 27 #include "base/geometry/offset.h" 28 #include "core/event/axis_event.h" 29 30 namespace OHOS::Ace { 31 namespace ResDefine { 32 constexpr int32_t LOAD_PAGE_START_EVENT = 0; 33 constexpr int32_t LOAD_PAGE_COMPLETE_EVENT = 1; 34 constexpr int32_t LOAD_PAGE_NO_REQUEST_FRAME_EVENT = 2; 35 constexpr double JUDGE_DISTANCE = 3.125; 36 } 37 38 struct ResEventInfo { 39 TimeStamp timeStamp; 40 Offset offset; 41 SourceTool sourceTool = SourceTool::UNKNOWN; 42 }; 43 44 using ReportDataFunc = void (*)(uint32_t resType, int64_t value, 45 const std::unordered_map<std::string, std::string>& payload); 46 47 using ReportSyncEventFunc = int32_t (*)(const uint32_t resType, const int64_t value, 48 const std::unordered_map<std::string, std::string>& payload, std::unordered_map<std::string, std::string>& reply); 49 50 ReportDataFunc ACE_EXPORT LoadReportDataFunc(); 51 ReportSyncEventFunc ACE_EXPORT LoadReportSyncEventFunc(); 52 53 class ACE_EXPORT ResSchedReport final { 54 public: 55 static ResSchedReport& GetInstance(); 56 void ResSchedDataReport(const char* name, const std::unordered_map<std::string, std::string>& param = {}); 57 void ResSchedDataReport(uint32_t resType, int32_t value = 0, 58 const std::unordered_map<std::string, std::string>& payload = {}); 59 void ResScheSyncEventReport(const uint32_t resType, const int64_t value, 60 const std::unordered_map<std::string, std::string>& payload, 61 std::unordered_map<std::string, std::string>& reply); 62 bool AppWhiteListCheck(const std::unordered_map<std::string, std::string>& payload, 63 std::unordered_map<std::string, std::string>& reply); 64 void OnTouchEvent(const TouchEvent& touchEvent); 65 void OnKeyEvent(const KeyEvent& event); 66 void LoadPageEvent(int32_t value); 67 void OnAxisEvent(const AxisEvent& axisEvent); 68 void AxisEventReportEnd(); 69 void HandlePageTransition(const std::string& fromPage, const std::string& toPage, const std::string& mode); 70 71 private: 72 ResSchedReport(); ~ResSchedReport()73 ~ResSchedReport() {} 74 void HandleTouchDown(const TouchEvent& touchEvent); 75 void HandleTouchUp(const TouchEvent& touchEvent); 76 bool IsRateLimit(int64_t maxCount, std::chrono::seconds durTime, 77 int64_t& keyEventCount, std::chrono::steady_clock::time_point& startTime); 78 bool IsPerSecRateLimit(); 79 bool IsPerMinRateLimit(); 80 void HandleKeyDown(const KeyEvent& event); 81 void HandleKeyUp(const KeyEvent& event); 82 void HandleTouchMove(const TouchEvent& touchEvent); 83 void HandleTouchCancel(const TouchEvent& touchEvent); 84 void HandleTouchPullDown(const TouchEvent& touchEvent); 85 void HandleTouchPullUp(const TouchEvent& touchEvent); 86 void HandleTouchPullMove(const TouchEvent& touchEvent); 87 double GetUpVelocity(const ResEventInfo& lastMoveInfo, 88 const ResEventInfo& upEventInfo); 89 void RecordTouchEvent(const TouchEvent& touchEvent, bool enforce = false); 90 91 void HandleAxisBegin(const AxisEvent& axisEvent); 92 void HandleAxisUpdate(const AxisEvent& axisEvent); 93 void HandleAxisEnd(const AxisEvent& axisEvent); 94 95 void RecordAxisEvent(const AxisEvent& axisEvent, bool enforce = false); 96 double GetAxisUpVelocity(const ResEventInfo& lastAxisEvent, const ResEventInfo& curAxisEvent); 97 98 ReportDataFunc reportDataFunc_ = nullptr; 99 ReportSyncEventFunc reportSyncEventFunc_ = nullptr; 100 bool loadPageOn_ = false; 101 bool loadPageRequestFrameOn_ = false; 102 ResEventInfo curTouchEvent_; 103 ResEventInfo lastTouchEvent_; 104 ResEventInfo curAxisEvent_; 105 ResEventInfo lastAxisEvent_; 106 Offset averageDistance_; 107 bool isInSlide_ = false; 108 bool isInTouch_ = false; 109 double dpi_ = PipelineBase::GetCurrentDensity(); 110 std::chrono::steady_clock::time_point startTimeMS = std::chrono::steady_clock::now(); 111 std::chrono::steady_clock::time_point startTimeS = std::chrono::steady_clock::now(); 112 int64_t keyEventCountMS = -1; 113 int64_t keyEventCountS = -1; 114 }; 115 116 class ACE_EXPORT ResSchedReportScope final { 117 public: 118 ACE_DISALLOW_COPY_AND_MOVE(ResSchedReportScope); 119 120 explicit ResSchedReportScope(const std::string& name, 121 const std::unordered_map<std::string, std::string>& param = {}); 122 ~ResSchedReportScope(); 123 124 private: 125 std::string name_; 126 std::unordered_map<std::string, std::string> payload_; 127 }; 128 } // namespace OHOS::Ace 129 130 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_RESSCHED_RESSCHED_REPORT_H 131