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 "second_ability.h"
17 #include "app_log_wrapper.h"
18 #include "test_utils.h"
19
20 namespace OHOS {
21 namespace AppExecFwk {
22 using namespace OHOS::EventFwk;
23 namespace {
24 constexpr int numZero = 0;
25 constexpr int numOne = 1;
26 constexpr int numTwo = 2;
27 constexpr int numThree = 3;
28 }
29
Init(const std::shared_ptr<AbilityInfo> & abilityInfo,const std::shared_ptr<OHOSApplication> & application,std::shared_ptr<AbilityHandler> & handler,const sptr<IRemoteObject> & token)30 void SecondAbility::Init(const std::shared_ptr<AbilityInfo> &abilityInfo,
31 const std::shared_ptr<OHOSApplication> &application, std::shared_ptr<AbilityHandler> &handler,
32 const sptr<IRemoteObject> &token)
33 {
34 APP_LOGI("SecondAbility::Init");
35 Ability::Init(abilityInfo, application, handler, token);
36 }
37
~SecondAbility()38 SecondAbility::~SecondAbility()
39 {
40 CommonEventManager::UnSubscribeCommonEvent(subscriber_);
41 }
42
OnStart(const Want & want)43 void SecondAbility::OnStart(const Want &want)
44 {
45 APP_LOGI("SecondAbility::onStart");
46 SubscribeEvent();
47 Ability::OnStart(want);
48 TestUtils::PublishEvent(g_EVENT_RESP_SECOND_B, Ability::GetState(), "onStart");
49 }
50
OnStop()51 void SecondAbility::OnStop()
52 {
53 APP_LOGI("SecondAbility::OnStop");
54 Ability::OnStop();
55 CommonEventManager::UnSubscribeCommonEvent(subscriber_);
56 TestUtils::PublishEvent(g_EVENT_RESP_SECOND_B, Ability::GetState(), "OnStop");
57 }
58
OnActive()59 void SecondAbility::OnActive()
60 {
61 APP_LOGI("SecondAbility::OnActive");
62 Ability::OnActive();
63 TestUtils::PublishEvent(g_EVENT_RESP_SECOND_B, Ability::GetState(), "OnActive");
64 }
65
OnInactive()66 void SecondAbility::OnInactive()
67 {
68 APP_LOGI("SecondAbility::OnInactive");
69 Ability::OnInactive();
70 TestUtils::PublishEvent(g_EVENT_RESP_SECOND_B, Ability::GetState(), "OnInactive");
71 }
72
OnBackground()73 void SecondAbility::OnBackground()
74 {
75 APP_LOGI("SecondAbility::OnBackground");
76 Ability::OnBackground();
77 TestUtils::PublishEvent(g_EVENT_RESP_SECOND_B, Ability::GetState(), "OnBackground");
78 }
79
OnForeground(const Want & want)80 void SecondAbility::OnForeground(const Want &want)
81 {
82 APP_LOGI("SecondAbility::OnForeground");
83 Ability::OnForeground(want);
84 TestUtils::PublishEvent(g_EVENT_RESP_SECOND_B, Ability::GetState(), "OnForeground");
85 }
86
SubscribeEvent()87 void SecondAbility::SubscribeEvent()
88 {
89 std::vector<std::string> eventList = {
90 g_EVENT_REQU_SECOND_B,
91 };
92 MatchingSkills matchingSkills;
93 for (const auto &e : eventList) {
94 matchingSkills.AddEvent(e);
95 }
96 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
97 subscribeInfo.SetPriority(1);
98 subscriber_ = std::make_shared<SecondEventSubscriber>(subscribeInfo);
99 subscriber_->mainAbility = this;
100 CommonEventManager::SubscribeCommonEvent(subscriber_);
101 }
102
OnReceiveEvent(const CommonEventData & data)103 void SecondEventSubscriber::OnReceiveEvent(const CommonEventData &data)
104 {
105 APP_LOGI("SecondEventSubscriber::OnReceiveEvent:event=%{public}s", data.GetWant().GetAction().c_str());
106 APP_LOGI("SecondEventSubscriber::OnReceiveEvent:data=%{public}s", data.GetData().c_str());
107 APP_LOGI("SecondEventSubscriber::OnReceiveEvent:code=%{public}d", data.GetCode());
108 auto eventName = data.GetWant().GetAction();
109 if (std::strcmp(eventName.c_str(), g_EVENT_REQU_SECOND_B.c_str()) == 0) {
110 auto target = data.GetData();
111 auto caseInfo = TestUtils::split(target, "_");
112 if (caseInfo.size() < numThree) {
113 return;
114 }
115 if (mapTestFunc_.find(caseInfo[numZero]) != mapTestFunc_.end()) {
116 mapTestFunc_[caseInfo[numZero]](std::stoi(caseInfo[numOne]), std::stoi(caseInfo[numTwo]), data.GetCode());
117 } else {
118 APP_LOGI("OnReceiveEvent: CommonEventData error(%{public}s)", target.c_str());
119 }
120 }
121 }
122
TestDispatcher(int apiIndex,int caseIndex,int code)123 void SecondAbility::TestDispatcher(int apiIndex, int caseIndex, int code)
124 {
125 APP_LOGI("SecondAbility::TestAbility");
126 if (mapCase_.find(apiIndex) != mapCase_.end()) {
127 if (caseIndex < (int)mapCase_[apiIndex].size()) {
128 mapCase_[apiIndex][caseIndex](code);
129 }
130 }
131 }
132
ExtraCase1(int code)133 void SecondAbility::ExtraCase1(int code)
134 {}
135
136 REGISTER_AA(SecondAbility)
137 } // namespace AppExecFwk
138 } // namespace OHOS
139