1600cc4afSopenharmony_ci/*
2600cc4afSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3600cc4afSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4600cc4afSopenharmony_ci * you may not use this file except in compliance with the License.
5600cc4afSopenharmony_ci * You may obtain a copy of the License at
6600cc4afSopenharmony_ci *
7600cc4afSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8600cc4afSopenharmony_ci *
9600cc4afSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10600cc4afSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11600cc4afSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12600cc4afSopenharmony_ci * See the License for the specific language governing permissions and
13600cc4afSopenharmony_ci * limitations under the License.
14600cc4afSopenharmony_ci */
15600cc4afSopenharmony_ci
16600cc4afSopenharmony_ci#include "stpageabilityevent.h"
17600cc4afSopenharmony_ci
18600cc4afSopenharmony_cinamespace OHOS {
19600cc4afSopenharmony_cinamespace AppExecFwk {
20600cc4afSopenharmony_ciusing namespace OHOS::EventFwk;
21600cc4afSopenharmony_ci
22600cc4afSopenharmony_ciSTPageAbilityEvent::STPageAbilityEvent(const std::string &className)
23600cc4afSopenharmony_ci{
24600cc4afSopenharmony_ci    this->className_ = className;
25600cc4afSopenharmony_ci}
26600cc4afSopenharmony_ci
27600cc4afSopenharmony_cibool STPageAbilityEvent::PublishEvent(const std::string &eventName, const int &code, const std::string &data)
28600cc4afSopenharmony_ci{
29600cc4afSopenharmony_ci    Want want;
30600cc4afSopenharmony_ci    want.SetAction(eventName);
31600cc4afSopenharmony_ci    CommonEventData commonData;
32600cc4afSopenharmony_ci    commonData.SetWant(want);
33600cc4afSopenharmony_ci    commonData.SetCode(code);
34600cc4afSopenharmony_ci    commonData.SetData(data);
35600cc4afSopenharmony_ci    return CommonEventManager::PublishCommonEvent(commonData);
36600cc4afSopenharmony_ci}
37600cc4afSopenharmony_ci
38600cc4afSopenharmony_civoid STPageAbilityEvent::SubscribeEvent(
39600cc4afSopenharmony_ci    std::vector<std::string> eventList, std::shared_ptr<Ability> ability, sptr<AAFwk::IAbilityConnection> stub)
40600cc4afSopenharmony_ci{
41600cc4afSopenharmony_ci    MatchingSkills matchingSkills;
42600cc4afSopenharmony_ci    for (const auto &e : eventList) {
43600cc4afSopenharmony_ci        matchingSkills.AddEvent(e);
44600cc4afSopenharmony_ci    }
45600cc4afSopenharmony_ci    CommonEventSubscribeInfo subscribeInfo(matchingSkills);
46600cc4afSopenharmony_ci    subscribeInfo.SetPriority(1);
47600cc4afSopenharmony_ci    subscriber_ = std::make_shared<STPageAbilityEventSubscriber>(subscribeInfo, ability, stub);
48600cc4afSopenharmony_ci    CommonEventManager::SubscribeCommonEvent(subscriber_);
49600cc4afSopenharmony_ci}
50600cc4afSopenharmony_ci
51600cc4afSopenharmony_civoid STPageAbilityEvent::UnsubscribeEvent()
52600cc4afSopenharmony_ci{
53600cc4afSopenharmony_ci    CommonEventManager::UnSubscribeCommonEvent(subscriber_);
54600cc4afSopenharmony_ci}
55600cc4afSopenharmony_ci
56600cc4afSopenharmony_cistd::string STPageAbilityEvent::GetEventDate(const std::string &stateCallbackCount)
57600cc4afSopenharmony_ci{
58600cc4afSopenharmony_ci    return this->className_ + stateCallbackCount;
59600cc4afSopenharmony_ci}
60600cc4afSopenharmony_ci
61600cc4afSopenharmony_cistd::string STPageAbilityEvent::GetCallBackPath(const std::string &callBackPath)
62600cc4afSopenharmony_ci{
63600cc4afSopenharmony_ci    this->callBackPath_ += callBackPath;
64600cc4afSopenharmony_ci    return this->callBackPath_;
65600cc4afSopenharmony_ci}
66600cc4afSopenharmony_ci
67600cc4afSopenharmony_cistd::string STPageAbilityEvent::GetAbilityStatus(const std::string &abilityStatus)
68600cc4afSopenharmony_ci{
69600cc4afSopenharmony_ci    this->abilityStatus_ += abilityStatus;
70600cc4afSopenharmony_ci    return this->abilityStatus_;
71600cc4afSopenharmony_ci}
72600cc4afSopenharmony_ci
73600cc4afSopenharmony_ciint STPageAbilityEvent::GetOnStartCount()
74600cc4afSopenharmony_ci{
75600cc4afSopenharmony_ci    onStartCount_++;
76600cc4afSopenharmony_ci    return onStartCount_;
77600cc4afSopenharmony_ci}
78600cc4afSopenharmony_ci
79600cc4afSopenharmony_ciint STPageAbilityEvent::GetOnStopCount()
80600cc4afSopenharmony_ci{
81600cc4afSopenharmony_ci    onStopCount_++;
82600cc4afSopenharmony_ci    return onStopCount_;
83600cc4afSopenharmony_ci}
84600cc4afSopenharmony_ci
85600cc4afSopenharmony_ciint STPageAbilityEvent::GetOnActiveCount()
86600cc4afSopenharmony_ci{
87600cc4afSopenharmony_ci    onActiveCount_++;
88600cc4afSopenharmony_ci    return onActiveCount_;
89600cc4afSopenharmony_ci}
90600cc4afSopenharmony_ci
91600cc4afSopenharmony_ciint STPageAbilityEvent::GetOnInactiveCount()
92600cc4afSopenharmony_ci{
93600cc4afSopenharmony_ci    onInactiveCount_++;
94600cc4afSopenharmony_ci    return onInactiveCount_;
95600cc4afSopenharmony_ci}
96600cc4afSopenharmony_ci
97600cc4afSopenharmony_ciint STPageAbilityEvent::GetOnBackgroundCount()
98600cc4afSopenharmony_ci{
99600cc4afSopenharmony_ci    onBackgroundCount_++;
100600cc4afSopenharmony_ci    return onBackgroundCount_;
101600cc4afSopenharmony_ci}
102600cc4afSopenharmony_ci
103600cc4afSopenharmony_ciint STPageAbilityEvent::GetOnForegroundCount()
104600cc4afSopenharmony_ci{
105600cc4afSopenharmony_ci    onForegroundCount_++;
106600cc4afSopenharmony_ci    return onForegroundCount_;
107600cc4afSopenharmony_ci}
108600cc4afSopenharmony_ci
109600cc4afSopenharmony_ciint STPageAbilityEvent::GetOnNewWantCount()
110600cc4afSopenharmony_ci{
111600cc4afSopenharmony_ci    onNewWantCount_++;
112600cc4afSopenharmony_ci    return onNewWantCount_;
113600cc4afSopenharmony_ci}
114600cc4afSopenharmony_ci
115600cc4afSopenharmony_ciint STPageAbilityEvent::GetOnCommandCount()
116600cc4afSopenharmony_ci{
117600cc4afSopenharmony_ci    onCommandCount_++;
118600cc4afSopenharmony_ci    return onCommandCount_;
119600cc4afSopenharmony_ci}
120600cc4afSopenharmony_ci
121600cc4afSopenharmony_ciint STPageAbilityEvent::GetOnConnectCount()
122600cc4afSopenharmony_ci{
123600cc4afSopenharmony_ci    onConnectCount_++;
124600cc4afSopenharmony_ci    return onConnectCount_;
125600cc4afSopenharmony_ci}
126600cc4afSopenharmony_ci
127600cc4afSopenharmony_ciint STPageAbilityEvent::GetOnDisconnectCount()
128600cc4afSopenharmony_ci{
129600cc4afSopenharmony_ci    onDisconnectCount_++;
130600cc4afSopenharmony_ci    return onDisconnectCount_;
131600cc4afSopenharmony_ci}
132600cc4afSopenharmony_ci
133600cc4afSopenharmony_civoid STPageAbilityEventSubscriber::OnReceiveEvent(const CommonEventData &data)
134600cc4afSopenharmony_ci{
135600cc4afSopenharmony_ci    APP_LOGI("DataTestPageAEventSubscriber::OnReceiveEvent:event=%{public}s", data.GetWant().GetAction().c_str());
136600cc4afSopenharmony_ci    APP_LOGI("DataTestPageAEventSubscriber::OnReceiveEvent:data=%{public}s", data.GetData().c_str());
137600cc4afSopenharmony_ci    APP_LOGI("DataTestPageAEventSubscriber::OnReceiveEvent:code=%{public}d", data.GetCode());
138600cc4afSopenharmony_ci    auto eventName = data.GetWant().GetAction();
139600cc4afSopenharmony_ci    if (!this->ability_.lock()) {
140600cc4afSopenharmony_ci        APP_LOGI("STPageAbilityEventSubscriber:ability_ is nullptr");
141600cc4afSopenharmony_ci    }
142600cc4afSopenharmony_ci    if (eventName.compare("requ_page_ability_terminate") == 0) {
143600cc4afSopenharmony_ci        std::string target = data.GetData();
144600cc4afSopenharmony_ci        if (target.compare((this->ability_.lock())->GetAbilityName()) == 0) {
145600cc4afSopenharmony_ci            (this->ability_.lock())->TerminateAbility();
146600cc4afSopenharmony_ci        }
147600cc4afSopenharmony_ci    }
148600cc4afSopenharmony_ci    if (eventName.compare("requ_get_process_memory_info") == 0) {
149600cc4afSopenharmony_ci    }
150600cc4afSopenharmony_ci    if (eventName.compare("requ_disconnect_service") == 0) {
151600cc4afSopenharmony_ci        std::string target = data.GetData();
152600cc4afSopenharmony_ci        if (target.compare((this->ability_.lock())->GetAbilityName()) == 0) {
153600cc4afSopenharmony_ci            if (stub_.promote()) {
154600cc4afSopenharmony_ci                (this->ability_.lock())->DisconnectAbility(stub_.promote());
155600cc4afSopenharmony_ci            }
156600cc4afSopenharmony_ci            APP_LOGI("GetMyProcessMemoryInfo:stub_ is nullptr");
157600cc4afSopenharmony_ci        }
158600cc4afSopenharmony_ci    }
159600cc4afSopenharmony_ci    if (eventName.compare("requ_page_ability_terminate_caller") == 0) {
160600cc4afSopenharmony_ci        std::string target = data.GetData();
161600cc4afSopenharmony_ci        if (target.compare((this->ability_.lock())->GetAbilityName()) == 0) {
162600cc4afSopenharmony_ci            int requestCode = data.GetCode();
163600cc4afSopenharmony_ci            (this->ability_.lock())->TerminateAbility(requestCode);
164600cc4afSopenharmony_ci        }
165600cc4afSopenharmony_ci    }
166600cc4afSopenharmony_ci}
167600cc4afSopenharmony_ci
168600cc4afSopenharmony_cistd::string STPageAbilityEventSubscriber::RunningProcessInfoToString(std::vector<RunningProcessInfo> &infos)
169600cc4afSopenharmony_ci{
170600cc4afSopenharmony_ci    std::string data = "";
171600cc4afSopenharmony_ci    return data;
172600cc4afSopenharmony_ci}
173600cc4afSopenharmony_ci
174600cc4afSopenharmony_cibool STPageAbilityEventSubscriber::PublishEvent(const std::string &eventName, const int &code, const std::string &data)
175600cc4afSopenharmony_ci{
176600cc4afSopenharmony_ci    Want want;
177600cc4afSopenharmony_ci    want.SetAction(eventName);
178600cc4afSopenharmony_ci    CommonEventData commonData;
179600cc4afSopenharmony_ci    commonData.SetWant(want);
180600cc4afSopenharmony_ci    commonData.SetCode(code);
181600cc4afSopenharmony_ci    commonData.SetData(data);
182600cc4afSopenharmony_ci    return CommonEventManager::PublishCommonEvent(commonData);
183600cc4afSopenharmony_ci}
184600cc4afSopenharmony_ci}  // namespace AppExecFwk
185600cc4afSopenharmony_ci}  // namespace OHOS