• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 #include "analytics_util.h"
16 #include "distributed_device_service.h"
17 
18 namespace OHOS {
19 namespace Notification {
20 
GetInstance()21 AnalyticsUtil& AnalyticsUtil::GetInstance()
22 {
23     static AnalyticsUtil analyticsUtil;
24     return analyticsUtil;
25 }
26 
InitHACallBack(std::function<void (int32_t,int32_t,uint32_t,std::string)> callback)27 void AnalyticsUtil::InitHACallBack(std::function<void(int32_t, int32_t, uint32_t, std::string)> callback)
28 {
29     haCallback_ = callback;
30 }
31 
InitSendReportCallBack(std::function<void (int32_t,int32_t,std::string)> callback)32 void AnalyticsUtil::InitSendReportCallBack(std::function<void(int32_t, int32_t, std::string)> callback)
33 {
34     sendReportCallback_ = callback;
35 }
36 
InitOperationCallback(std::function<void (const std::string &,int32_t,int32_t,std::string)> callback)37 void AnalyticsUtil::InitOperationCallback(
38     std::function<void(const std::string&, int32_t, int32_t, std::string)>callback)
39 {
40     operationCallback_ = callback;
41 }
42 
SendHaReport(int32_t eventCode,int32_t errorCode,uint32_t branchId,const std::string & errorReason,int32_t code)43 void AnalyticsUtil::SendHaReport(int32_t eventCode, int32_t errorCode, uint32_t branchId,
44     const std::string& errorReason, int32_t code)
45 {
46     if (haCallback_ == nullptr || !DistributedDeviceService::GetInstance().IsReportDataByHa()) {
47         return;
48     }
49     if (code == -1) {
50         haCallback_(eventCode, errorCode, branchId, errorReason);
51     } else {
52         haCallback_(code, errorCode, branchId, errorReason);
53     }
54 }
55 
SendEventReport(int32_t messageType,int32_t errCode,const std::string & errorReason)56 void AnalyticsUtil::SendEventReport(int32_t messageType, int32_t errCode, const std::string& errorReason)
57 {
58     if (sendReportCallback_ == nullptr || !DistributedDeviceService::GetInstance().IsReportDataByHa()) {
59         return;
60     }
61     sendReportCallback_(messageType, errCode, errorReason);
62 }
63 
OperationalReporting(int32_t deviceType,int32_t sceneType,int32_t slotType,const std::string & reason)64 void AnalyticsUtil::OperationalReporting(int32_t deviceType, int32_t sceneType, int32_t slotType,
65     const std::string& reason)
66 {
67     if (operationCallback_ == nullptr || !DistributedDeviceService::GetInstance().IsReportDataByHa()) {
68         return;
69     }
70     std::string deviceString = DistributedDeviceService::DeviceTypeToTypeString(deviceType);
71     operationCallback_(deviceString, sceneType, slotType, reason);
72 }
73 
AbnormalReporting(int32_t eventCode,int result,uint32_t branchId,const std::string & errorReason)74 void AnalyticsUtil::AbnormalReporting(int32_t eventCode, int result, uint32_t branchId,
75     const std::string &errorReason)
76 {
77     if (!DistributedDeviceService::GetInstance().IsReportDataByHa()) {
78         return;
79     }
80 
81     if (result != 0) {
82         SendEventReport(0, result, errorReason);
83     }
84     if (haCallback_ == nullptr) {
85         return;
86     }
87     haCallback_(eventCode, result, branchId, errorReason);
88 }
89 }
90 }
91