1e0dac50fSopenharmony_ci/*
2e0dac50fSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3e0dac50fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e0dac50fSopenharmony_ci * you may not use this file except in compliance with the License.
5e0dac50fSopenharmony_ci * You may obtain a copy of the License at
6e0dac50fSopenharmony_ci *
7e0dac50fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e0dac50fSopenharmony_ci *
9e0dac50fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e0dac50fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e0dac50fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e0dac50fSopenharmony_ci * See the License for the specific language governing permissions and
13e0dac50fSopenharmony_ci * limitations under the License.
14e0dac50fSopenharmony_ci */
15e0dac50fSopenharmony_ci
16e0dac50fSopenharmony_ci#include <map>
17e0dac50fSopenharmony_ci#include <hisysevent.h>
18e0dac50fSopenharmony_ci#include "pip_report.h"
19e0dac50fSopenharmony_ci#include "window_manager_hilog.h"
20e0dac50fSopenharmony_ci
21e0dac50fSopenharmony_cinamespace OHOS {
22e0dac50fSopenharmony_cinamespace Rosen {
23e0dac50fSopenharmony_cinamespace {
24e0dac50fSopenharmony_ciconst std::string PNAMEID = "windowManager";
25e0dac50fSopenharmony_ciconst std::string PVERSION = "1.0";
26e0dac50fSopenharmony_ci}
27e0dac50fSopenharmony_ciWM_IMPLEMENT_SINGLE_INSTANCE(PiPReporter)
28e0dac50fSopenharmony_ci
29e0dac50fSopenharmony_ci#define LOG_WHEN_ERROR(errCode)                                                               \
30e0dac50fSopenharmony_ci    do {                                                                                      \
31e0dac50fSopenharmony_ci        if ((errCode) != 0) {                                                                 \
32e0dac50fSopenharmony_ci            TLOGE(WmsLogTag::WMS_PIP, "Write HiSysEvent error, errCode:%{public}d", errCode); \
33e0dac50fSopenharmony_ci        }                                                                                     \
34e0dac50fSopenharmony_ci    } while (false)
35e0dac50fSopenharmony_ci
36e0dac50fSopenharmony_cistatic std::map<std::string, int32_t> CONTROL_ACTION_MAP = {
37e0dac50fSopenharmony_ci    {"playbackStateChanged", 0},
38e0dac50fSopenharmony_ci    {"nextVideo", 1},
39e0dac50fSopenharmony_ci    {"previousVideo", 2},
40e0dac50fSopenharmony_ci    {"hangUp", 3},
41e0dac50fSopenharmony_ci    {"micStateChanged", 4},
42e0dac50fSopenharmony_ci    {"videoStateChanged", 5},
43e0dac50fSopenharmony_ci    {"voiceStateChanged", 6},
44e0dac50fSopenharmony_ci    {"fastForward", 7},
45e0dac50fSopenharmony_ci    {"fastBackward", 8}
46e0dac50fSopenharmony_ci};
47e0dac50fSopenharmony_ciconstexpr char EVENT_KEY_SOURCE[] = "SOURCE";
48e0dac50fSopenharmony_ciconstexpr char EVENT_KEY_TEMPLATE_TYPE[] = "TEMPLATE_TYPE";
49e0dac50fSopenharmony_ciconstexpr char EVENT_KEY_PNAMEID[] = "PNAMEID";
50e0dac50fSopenharmony_ciconstexpr char EVENT_KEY_PVERSION[] = "PVERSION";
51e0dac50fSopenharmony_ciconstexpr char EVENT_KEY_START_PACKAGE_NAME[] = "START_PACKAGE_NAME";
52e0dac50fSopenharmony_ciconstexpr char EVENT_KEY_STOP_PACKAGE_NAME[] = "STOP_PACKAGE_NAME";
53e0dac50fSopenharmony_ciconstexpr char EVENT_KEY_OPERATION_PACKAGE_NAME[] = "OPERATION_PACKAGE_NAME";
54e0dac50fSopenharmony_ciconstexpr char EVENT_KEY_OPERATION_CODE[] = "OPERATION_CODE";
55e0dac50fSopenharmony_ciconstexpr char EVENT_KEY_OPERATION_ERROR_REASON[] = "OPERATION_ERROR_REASON";
56e0dac50fSopenharmony_ciconstexpr char EVENT_KEY_ACTION_EVENT[] = "ACTION_EVENT";
57e0dac50fSopenharmony_ciconstexpr char EVENT_KEY_WINDOW_WIDTH[] = "WINDOW_WIDTH";
58e0dac50fSopenharmony_ciconstexpr char EVENT_KEY_WINDOW_HEIGHT[] = "WINDOW_HEIGHT";
59e0dac50fSopenharmony_ci
60e0dac50fSopenharmony_civoid PiPReporter::SetCurrentPackageName(const std::string& packageName)
61e0dac50fSopenharmony_ci{
62e0dac50fSopenharmony_ci    std::lock_guard<std::mutex> lock(packageNameMutex_);
63e0dac50fSopenharmony_ci    packageName_ = packageName;
64e0dac50fSopenharmony_ci}
65e0dac50fSopenharmony_ci
66e0dac50fSopenharmony_cistd::string PiPReporter::GetPackageName() const
67e0dac50fSopenharmony_ci{
68e0dac50fSopenharmony_ci    std::lock_guard<std::mutex> lock(packageNameMutex_);
69e0dac50fSopenharmony_ci    return packageName_;
70e0dac50fSopenharmony_ci}
71e0dac50fSopenharmony_ci
72e0dac50fSopenharmony_civoid PiPReporter::ReportPiPStartWindow(int32_t source, int32_t templateType,
73e0dac50fSopenharmony_ci                                       int32_t isSuccess, const std::string& errorReason)
74e0dac50fSopenharmony_ci{
75e0dac50fSopenharmony_ci    TLOGI(WmsLogTag::WMS_PIP, "Report start pip widow");
76e0dac50fSopenharmony_ci    if (source == 0) {
77e0dac50fSopenharmony_ci        TLOGI(WmsLogTag::WMS_PIP, "need not report start pip widow");
78e0dac50fSopenharmony_ci        return;
79e0dac50fSopenharmony_ci    }
80e0dac50fSopenharmony_ci    std::string eventName = "START_PIP";
81e0dac50fSopenharmony_ci    int32_t ret = HiSysEventWrite(
82e0dac50fSopenharmony_ci        OHOS::HiviewDFX::HiSysEvent::Domain::MULTIWINDOW_UE, eventName,
83e0dac50fSopenharmony_ci        OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
84e0dac50fSopenharmony_ci        EVENT_KEY_PNAMEID, PNAMEID,
85e0dac50fSopenharmony_ci        EVENT_KEY_PVERSION, PVERSION,
86e0dac50fSopenharmony_ci        EVENT_KEY_SOURCE, source,
87e0dac50fSopenharmony_ci        EVENT_KEY_TEMPLATE_TYPE, templateType,
88e0dac50fSopenharmony_ci        EVENT_KEY_START_PACKAGE_NAME, GetPackageName(),
89e0dac50fSopenharmony_ci        EVENT_KEY_OPERATION_CODE, isSuccess,
90e0dac50fSopenharmony_ci        EVENT_KEY_OPERATION_ERROR_REASON, errorReason);
91e0dac50fSopenharmony_ci    LOG_WHEN_ERROR(ret);
92e0dac50fSopenharmony_ci}
93e0dac50fSopenharmony_ci
94e0dac50fSopenharmony_civoid PiPReporter::ReportPiPStopWindow(int32_t source, int32_t templateType,
95e0dac50fSopenharmony_ci                                      int32_t isSuccess, const std::string& errorReason)
96e0dac50fSopenharmony_ci{
97e0dac50fSopenharmony_ci    TLOGI(WmsLogTag::WMS_PIP, "Report stop pip widow");
98e0dac50fSopenharmony_ci    if (source == 0) {
99e0dac50fSopenharmony_ci        TLOGI(WmsLogTag::WMS_PIP, "need not report stop pip widow");
100e0dac50fSopenharmony_ci        return;
101e0dac50fSopenharmony_ci    }
102e0dac50fSopenharmony_ci    std::string eventName = "STOP_PIP";
103e0dac50fSopenharmony_ci    int32_t ret = HiSysEventWrite(
104e0dac50fSopenharmony_ci        OHOS::HiviewDFX::HiSysEvent::Domain::MULTIWINDOW_UE, eventName,
105e0dac50fSopenharmony_ci        OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
106e0dac50fSopenharmony_ci        EVENT_KEY_PNAMEID, PNAMEID,
107e0dac50fSopenharmony_ci        EVENT_KEY_PVERSION, PVERSION,
108e0dac50fSopenharmony_ci        EVENT_KEY_SOURCE, source,
109e0dac50fSopenharmony_ci        EVENT_KEY_TEMPLATE_TYPE, templateType,
110e0dac50fSopenharmony_ci        EVENT_KEY_STOP_PACKAGE_NAME, GetPackageName(),
111e0dac50fSopenharmony_ci        EVENT_KEY_OPERATION_CODE, isSuccess,
112e0dac50fSopenharmony_ci        EVENT_KEY_OPERATION_ERROR_REASON, errorReason);
113e0dac50fSopenharmony_ci    LOG_WHEN_ERROR(ret);
114e0dac50fSopenharmony_ci}
115e0dac50fSopenharmony_ci
116e0dac50fSopenharmony_civoid PiPReporter::ReportPiPActionEvent(int32_t templateType, const std::string& actionEvent)
117e0dac50fSopenharmony_ci{
118e0dac50fSopenharmony_ci    TLOGI(WmsLogTag::WMS_PIP, "Report pip widow action event");
119e0dac50fSopenharmony_ci    std::string eventName = "CONTROL_PANNEL_ACTION_EVENT";
120e0dac50fSopenharmony_ci    if (CONTROL_ACTION_MAP.find(actionEvent) == CONTROL_ACTION_MAP.end()) {
121e0dac50fSopenharmony_ci        TLOGE(WmsLogTag::WMS_PIP, "actionEvent %{public}s not found", actionEvent.c_str());
122e0dac50fSopenharmony_ci        return;
123e0dac50fSopenharmony_ci    }
124e0dac50fSopenharmony_ci    int32_t currentAction = CONTROL_ACTION_MAP[actionEvent];
125e0dac50fSopenharmony_ci    int32_t ret = HiSysEventWrite(
126e0dac50fSopenharmony_ci        OHOS::HiviewDFX::HiSysEvent::Domain::MULTIWINDOW_UE, eventName,
127e0dac50fSopenharmony_ci        OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
128e0dac50fSopenharmony_ci        EVENT_KEY_PNAMEID, PNAMEID,
129e0dac50fSopenharmony_ci        EVENT_KEY_PVERSION, PVERSION,
130e0dac50fSopenharmony_ci        EVENT_KEY_TEMPLATE_TYPE, templateType,
131e0dac50fSopenharmony_ci        EVENT_KEY_ACTION_EVENT, currentAction,
132e0dac50fSopenharmony_ci        EVENT_KEY_OPERATION_PACKAGE_NAME, GetPackageName());
133e0dac50fSopenharmony_ci    LOG_WHEN_ERROR(ret);
134e0dac50fSopenharmony_ci}
135e0dac50fSopenharmony_ci
136e0dac50fSopenharmony_civoid PiPReporter::ReportPiPControlEvent(int32_t templateType, PiPControlType controlType)
137e0dac50fSopenharmony_ci{
138e0dac50fSopenharmony_ci    TLOGI(WmsLogTag::WMS_PIP, "templateType:%{public}d, controlType:%{public}d", templateType, controlType);
139e0dac50fSopenharmony_ci    int32_t ret = HiSysEventWrite(
140e0dac50fSopenharmony_ci        OHOS::HiviewDFX::HiSysEvent::Domain::MULTIWINDOW_UE, "CONTROL_CONTROL_EVENT",
141e0dac50fSopenharmony_ci        OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
142e0dac50fSopenharmony_ci        EVENT_KEY_PNAMEID, PNAMEID,
143e0dac50fSopenharmony_ci        EVENT_KEY_PVERSION, PVERSION,
144e0dac50fSopenharmony_ci        EVENT_KEY_TEMPLATE_TYPE, templateType,
145e0dac50fSopenharmony_ci        EVENT_KEY_ACTION_EVENT, static_cast<uint32_t>(controlType),
146e0dac50fSopenharmony_ci        EVENT_KEY_OPERATION_PACKAGE_NAME, GetPackageName());
147e0dac50fSopenharmony_ci    LOG_WHEN_ERROR(ret);
148e0dac50fSopenharmony_ci}
149e0dac50fSopenharmony_ci
150e0dac50fSopenharmony_civoid PiPReporter::ReportPiPRatio(int32_t windowWidth, int32_t windowHeight)
151e0dac50fSopenharmony_ci{
152e0dac50fSopenharmony_ci    TLOGI(WmsLogTag::WMS_PIP, "Report pip widow ratio");
153e0dac50fSopenharmony_ci    std::string eventName = "UPDATE_PIP_RATIO";
154e0dac50fSopenharmony_ci    int32_t ret = HiSysEventWrite(
155e0dac50fSopenharmony_ci        OHOS::HiviewDFX::HiSysEvent::Domain::MULTIWINDOW_UE, eventName,
156e0dac50fSopenharmony_ci        OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
157e0dac50fSopenharmony_ci        EVENT_KEY_PNAMEID, PNAMEID,
158e0dac50fSopenharmony_ci        EVENT_KEY_PVERSION, PVERSION,
159e0dac50fSopenharmony_ci        EVENT_KEY_WINDOW_WIDTH, windowWidth,
160e0dac50fSopenharmony_ci        EVENT_KEY_WINDOW_HEIGHT, windowHeight,
161e0dac50fSopenharmony_ci        EVENT_KEY_OPERATION_PACKAGE_NAME, GetPackageName());
162e0dac50fSopenharmony_ci    LOG_WHEN_ERROR(ret);
163e0dac50fSopenharmony_ci}
164e0dac50fSopenharmony_ci
165e0dac50fSopenharmony_civoid PiPReporter::ReportPiPRestore()
166e0dac50fSopenharmony_ci{
167e0dac50fSopenharmony_ci    TLOGI(WmsLogTag::WMS_PIP, "Report pip widow restore");
168e0dac50fSopenharmony_ci    std::string eventName = "RESOTRE_PIP";
169e0dac50fSopenharmony_ci    int32_t ret = HiSysEventWrite(
170e0dac50fSopenharmony_ci        OHOS::HiviewDFX::HiSysEvent::Domain::MULTIWINDOW_UE, eventName,
171e0dac50fSopenharmony_ci        OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
172e0dac50fSopenharmony_ci        EVENT_KEY_PNAMEID, PNAMEID,
173e0dac50fSopenharmony_ci        EVENT_KEY_PVERSION, PVERSION,
174e0dac50fSopenharmony_ci        EVENT_KEY_OPERATION_PACKAGE_NAME, GetPackageName());
175e0dac50fSopenharmony_ci    LOG_WHEN_ERROR(ret);
176e0dac50fSopenharmony_ci}
177e0dac50fSopenharmony_ci} // namespace Rosen
178e0dac50fSopenharmony_ci} // namespace OHOS
179