1/*
2 * Copyright (c) 2021 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#include "stpageabilityevent.h"
17
18namespace OHOS {
19namespace AppExecFwk {
20using namespace OHOS::EventFwk;
21
22STPageAbilityEvent::STPageAbilityEvent(const std::string &className)
23{
24    this->className_ = className;
25}
26
27bool STPageAbilityEvent::PublishEvent(const std::string &eventName, const int &code, const std::string &data)
28{
29    Want want;
30    want.SetAction(eventName);
31    CommonEventData commonData;
32    commonData.SetWant(want);
33    commonData.SetCode(code);
34    commonData.SetData(data);
35    return CommonEventManager::PublishCommonEvent(commonData);
36}
37
38void STPageAbilityEvent::SubscribeEvent(
39    std::vector<std::string> eventList, std::shared_ptr<Ability> ability, sptr<AAFwk::IAbilityConnection> stub)
40{
41    MatchingSkills matchingSkills;
42    for (const auto &e : eventList) {
43        matchingSkills.AddEvent(e);
44    }
45    CommonEventSubscribeInfo subscribeInfo(matchingSkills);
46    subscribeInfo.SetPriority(1);
47    subscriber_ = std::make_shared<STPageAbilityEventSubscriber>(subscribeInfo, ability, stub);
48    CommonEventManager::SubscribeCommonEvent(subscriber_);
49}
50
51void STPageAbilityEvent::UnsubscribeEvent()
52{
53    CommonEventManager::UnSubscribeCommonEvent(subscriber_);
54}
55
56std::string STPageAbilityEvent::GetEventDate(const std::string &stateCallbackCount)
57{
58    return this->className_ + stateCallbackCount;
59}
60
61std::string STPageAbilityEvent::GetCallBackPath(const std::string &callBackPath)
62{
63    this->callBackPath_ += callBackPath;
64    return this->callBackPath_;
65}
66
67std::string STPageAbilityEvent::GetAbilityStatus(const std::string &abilityStatus)
68{
69    this->abilityStatus_ += abilityStatus;
70    return this->abilityStatus_;
71}
72
73int STPageAbilityEvent::GetOnStartCount()
74{
75    onStartCount_++;
76    return onStartCount_;
77}
78
79int STPageAbilityEvent::GetOnStopCount()
80{
81    onStopCount_++;
82    return onStopCount_;
83}
84
85int STPageAbilityEvent::GetOnActiveCount()
86{
87    onActiveCount_++;
88    return onActiveCount_;
89}
90
91int STPageAbilityEvent::GetOnInactiveCount()
92{
93    onInactiveCount_++;
94    return onInactiveCount_;
95}
96
97int STPageAbilityEvent::GetOnBackgroundCount()
98{
99    onBackgroundCount_++;
100    return onBackgroundCount_;
101}
102
103int STPageAbilityEvent::GetOnForegroundCount()
104{
105    onForegroundCount_++;
106    return onForegroundCount_;
107}
108
109int STPageAbilityEvent::GetOnNewWantCount()
110{
111    onNewWantCount_++;
112    return onNewWantCount_;
113}
114
115int STPageAbilityEvent::GetOnCommandCount()
116{
117    onCommandCount_++;
118    return onCommandCount_;
119}
120
121int STPageAbilityEvent::GetOnConnectCount()
122{
123    onConnectCount_++;
124    return onConnectCount_;
125}
126
127int STPageAbilityEvent::GetOnDisconnectCount()
128{
129    onDisconnectCount_++;
130    return onDisconnectCount_;
131}
132
133void STPageAbilityEventSubscriber::OnReceiveEvent(const CommonEventData &data)
134{
135    APP_LOGI("DataTestPageAEventSubscriber::OnReceiveEvent:event=%{public}s", data.GetWant().GetAction().c_str());
136    APP_LOGI("DataTestPageAEventSubscriber::OnReceiveEvent:data=%{public}s", data.GetData().c_str());
137    APP_LOGI("DataTestPageAEventSubscriber::OnReceiveEvent:code=%{public}d", data.GetCode());
138    auto eventName = data.GetWant().GetAction();
139    if (!this->ability_.lock()) {
140        APP_LOGI("STPageAbilityEventSubscriber:ability_ is nullptr");
141    }
142    if (eventName.compare("requ_page_ability_terminate") == 0) {
143        std::string target = data.GetData();
144        if (target.compare((this->ability_.lock())->GetAbilityName()) == 0) {
145            (this->ability_.lock())->TerminateAbility();
146        }
147    }
148    if (eventName.compare("requ_get_process_memory_info") == 0) {
149    }
150    if (eventName.compare("requ_disconnect_service") == 0) {
151        std::string target = data.GetData();
152        if (target.compare((this->ability_.lock())->GetAbilityName()) == 0) {
153            if (stub_.promote()) {
154                (this->ability_.lock())->DisconnectAbility(stub_.promote());
155            }
156            APP_LOGI("GetMyProcessMemoryInfo:stub_ is nullptr");
157        }
158    }
159    if (eventName.compare("requ_page_ability_terminate_caller") == 0) {
160        std::string target = data.GetData();
161        if (target.compare((this->ability_.lock())->GetAbilityName()) == 0) {
162            int requestCode = data.GetCode();
163            (this->ability_.lock())->TerminateAbility(requestCode);
164        }
165    }
166}
167
168std::string STPageAbilityEventSubscriber::RunningProcessInfoToString(std::vector<RunningProcessInfo> &infos)
169{
170    std::string data = "";
171    return data;
172}
173
174bool STPageAbilityEventSubscriber::PublishEvent(const std::string &eventName, const int &code, const std::string &data)
175{
176    Want want;
177    want.SetAction(eventName);
178    CommonEventData commonData;
179    commonData.SetWant(want);
180    commonData.SetCode(code);
181    commonData.SetData(data);
182    return CommonEventManager::PublishCommonEvent(commonData);
183}
184}  // namespace AppExecFwk
185}  // namespace OHOS