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 BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_NOTIFICATION_ANALYTICS_UTIL_H 17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_NOTIFICATION_ANALYTICS_UTIL_H 18 19 #include <string> 20 #include <map> 21 #include "notification_request.h" 22 23 namespace OHOS { 24 namespace Notification { 25 26 enum EventSceneId { 27 SCENE_0 = 0, 28 SCENE_1 = 1, 29 SCENE_2 = 2, 30 SCENE_3 = 3, 31 SCENE_4 = 4, 32 SCENE_5 = 5, 33 SCENE_6 = 6, 34 SCENE_7 = 7, 35 SCENE_8 = 8, 36 SCENE_9 = 9, 37 SCENE_10 = 10, 38 SCENE_11 = 11, 39 }; 40 41 enum EventBranchId { 42 BRANCH_0 = 0, 43 BRANCH_1 = 1, 44 BRANCH_2 = 2, 45 BRANCH_3 = 3, 46 BRANCH_4 = 4, 47 BRANCH_5 = 5, 48 BRANCH_6 = 6, 49 BRANCH_8 = 8, 50 }; 51 class HaMetaMessage { 52 public: 53 HaMetaMessage() = default; 54 ~HaMetaMessage() = default; 55 56 explicit HaMetaMessage(uint32_t sceneId, uint32_t branchId); 57 58 HaMetaMessage& SceneId(uint32_t sceneId); 59 HaMetaMessage& BranchId(uint32_t branchId); 60 HaMetaMessage& ErrorCode(uint32_t errorCode); 61 HaMetaMessage& Message(const std::string& message, bool print = false); 62 HaMetaMessage& Append(const std::string& message); 63 HaMetaMessage& BundleName(const std::string& bundleName_); 64 HaMetaMessage& AgentBundleName(const std::string& agentBundleName); 65 HaMetaMessage& TypeCode(int32_t typeCode); 66 HaMetaMessage& NotificationId(int32_t notificationId); 67 HaMetaMessage& SlotType(int32_t slotType); 68 std::string GetMessage() const; 69 HaMetaMessage& Checkfailed(bool checkfailed); 70 bool NeedReport() const; 71 72 std::string Build() const; 73 74 std::string bundleName_; 75 int32_t notificationId_ = -1; 76 std::string agentBundleName_ = ""; 77 int32_t typeCode_ = -1; 78 uint32_t slotType_ = -1; 79 uint32_t sceneId_; 80 uint32_t branchId_; 81 uint32_t errorCode_ = ERR_OK; 82 std::string message_; 83 bool checkfailed_ = true; 84 }; 85 86 87 struct FlowControllerOption { 88 int32_t count; 89 int32_t time; 90 }; 91 92 struct ReportCache { 93 EventFwk::Want want; 94 int32_t eventCode; 95 }; 96 97 class NotificationAnalyticsUtil { 98 public: 99 static void ReportPublishFailedEvent(const sptr<NotificationRequest>& request, const HaMetaMessage& message); 100 101 static void ReportDeleteFailedEvent(const sptr<NotificationRequest>& request, HaMetaMessage& message); 102 103 static void ReportPublishSuccessEvent(const sptr<NotificationRequest>& request, const HaMetaMessage& message); 104 105 static void ReportModifyEvent(const HaMetaMessage& message); 106 107 static void ReportDeleteFailedEvent(const HaMetaMessage& message); 108 109 static void RemoveExpired(std::list<std::chrono::system_clock::time_point> &list, 110 const std::chrono::system_clock::time_point &now, int32_t time = 1); 111 112 static int64_t GetCurrentTime(); 113 private: 114 static void ReportNotificationEvent(const sptr<NotificationRequest>& request, 115 EventFwk::Want want, int32_t eventCode, const std::string& reason); 116 static void CommonNotificationEvent(const sptr<NotificationRequest>& request, 117 int32_t eventCode, const HaMetaMessage& message); 118 119 static void CommonNotificationEvent(int32_t eventCode, const HaMetaMessage& message); 120 121 static void ReportNotificationEvent(EventFwk::Want want, int32_t eventCode, const std::string& reason); 122 123 static bool ReportFlowControl(const int32_t reportType); 124 125 static bool IsAllowedBundle(const sptr<NotificationRequest>& request); 126 127 static std::string BuildAnsData(const sptr<NotificationRequest>& request, const HaMetaMessage& message); 128 129 static ReportCache Aggregate(); 130 131 static uint32_t SetControlFlags(const std::shared_ptr<NotificationFlags> &flags, uint32_t controlFlags); 132 133 static std::string GetDeviceStatus(const sptr<NotificationRequest>& request); 134 135 static FlowControllerOption GetFlowOptionByType(const int32_t reportType); 136 137 static std::string BuildExtraInfo(const HaMetaMessage& message); 138 139 static std::string BuildExtraInfoWithReq(const HaMetaMessage& message, 140 const sptr<NotificationRequest>& request); 141 142 static void SetCommonWant(EventFwk::Want& want, const HaMetaMessage& message, std::string& extraInfo); 143 144 static void AddListCache(EventFwk::Want& want, int32_t eventCode); 145 146 static void AddSuccessListCache(EventFwk::Want& want, int32_t eventCode); 147 148 static void ExecuteCacheList(); 149 150 static void ExecuteSuccessCacheList(); 151 152 static void ReportCommonEvent(const ReportCache& reportCache); 153 }; 154 } // namespace Notification 155 } // namespace OHOS 156 157 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_NOTIFICATION_ANALYTICS_UTIL_H 158