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 "hilog_wrapper.h"
18 #include "test_utils.h"
19
20 namespace OHOS {
21 namespace AppExecFwk {
22 using namespace OHOS::EventFwk;
23 using namespace OHOS::AAFwk;
24
Init(const std::shared_ptr<AbilityInfo> & abilityInfo,const std::shared_ptr<OHOSApplication> & application,std::shared_ptr<AbilityHandler> & handler,const sptr<IRemoteObject> & token)25 void SecondAbility::Init(const std::shared_ptr<AbilityInfo> &abilityInfo,
26 const std::shared_ptr<OHOSApplication> &application, std::shared_ptr<AbilityHandler> &handler,
27 const sptr<IRemoteObject> &token)
28 {
29 HILOG_INFO("SecondAbility::Init");
30 Ability::Init(abilityInfo, application, handler, token);
31 }
32
~SecondAbility()33 SecondAbility::~SecondAbility()
34 {
35 CommonEventManager::UnSubscribeCommonEvent(subscriber_);
36 }
37
OnStart(const Want & want)38 void SecondAbility::OnStart(const Want &want)
39 {
40 HILOG_INFO("SecondAbility::OnStart");
41 SubscribeEvent();
42 Ability::OnStart(want);
43 callbackSeq += "OnStart";
44 TestUtils::PublishEvent(g_EVENT_RESP_MAIN_LIFECYCLE, SECOND_ABILITY_CODE, "OnStart");
45 }
46
OnStop()47 void SecondAbility::OnStop()
48 {
49 HILOG_INFO("SecondAbility::OnStop");
50 Ability::OnStop();
51 CommonEventManager::UnSubscribeCommonEvent(subscriber_);
52 callbackSeq += "OnStop"; // OnInactiveOnBackgroundOnStop
53 TestUtils::PublishEvent(g_EVENT_RESP_MAIN_LIFECYCLE, SECOND_ABILITY_CODE, callbackSeq);
54 callbackSeq = "";
55 }
56
OnActive()57 void SecondAbility::OnActive()
58 {
59 HILOG_INFO("SecondAbility::OnActive====<");
60 Ability::OnActive();
61 callbackSeq += "OnActive"; // OnStartOnActive
62 TestUtils::PublishEvent(g_EVENT_RESP_MAIN_LIFECYCLE, SECOND_ABILITY_CODE, callbackSeq);
63 callbackSeq = "";
64 }
65
OnConfigurationUpdated(const Configuration & configuration)66 void SecondAbility::OnConfigurationUpdated(const Configuration &configuration)
67 {
68 std::string languageValue;
69 std::string orientationValue;
70
71 HILOG_INFO("SecondAbility::OnConfigurationUpdated====<");
72 Ability::OnConfigurationUpdated(configuration);
73
74 languageValue = configuration.GetItem(GlobalConfigurationKey::SYSTEM_LANGUAGE);
75 orientationValue = configuration.GetItem(GlobalConfigurationKey::SYSTEM_ORIENTATION);
76 TestUtils::PublishEvent(g_EVENT_RESP_MAIN_LIFECYCLE, SECOND_ABILITY_CODE, languageValue);
77 TestUtils::PublishEvent(g_EVENT_RESP_MAIN_LIFECYCLE, SECOND_ABILITY_CODE, orientationValue);
78 callbackUpdated += "Updated";
79 TestUtils::PublishEvent(g_EVENT_RESP_MAIN_LIFECYCLE, SECOND_ABILITY_CODE, callbackUpdated);
80 callbackUpdated = "";
81 }
82
OnInactive()83 void SecondAbility::OnInactive()
84 {
85 HILOG_INFO("SecondAbility::OnInactive");
86 Ability::OnInactive();
87 callbackSeq += "OnInactive";
88 TestUtils::PublishEvent(g_EVENT_RESP_MAIN_LIFECYCLE, SECOND_ABILITY_CODE, "OnInactive");
89 }
90
OnBackground()91 void SecondAbility::OnBackground()
92 {
93 HILOG_INFO("SecondAbility::OnBackground");
94 Ability::OnBackground();
95 callbackSeq += "OnBackground";
96 TestUtils::PublishEvent(g_EVENT_RESP_MAIN_LIFECYCLE, SECOND_ABILITY_CODE, "OnBackground");
97 }
98
OnForeground(const Want & want)99 void SecondAbility::OnForeground(const Want &want)
100 {
101 HILOG_INFO("SecondAbility::OnForeground");
102 Ability::OnForeground(want);
103 callbackSeq += "OnForeground";
104 TestUtils::PublishEvent(g_EVENT_RESP_MAIN_LIFECYCLE, SECOND_ABILITY_CODE, "OnForeground");
105 }
106
SubscribeEvent()107 void SecondAbility::SubscribeEvent()
108 {
109 std::vector<std::string> eventList = {
110 g_EVENT_REQU_MAIN,
111 };
112 MatchingSkills matchingSkills;
113 for (const auto &e : eventList) {
114 matchingSkills.AddEvent(e);
115 }
116 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
117 subscribeInfo.SetPriority(1);
118 subscriber_ = std::make_shared<SecondAbilityEventSubscriber>(subscribeInfo);
119 subscriber_->secondAbility = this;
120 CommonEventManager::SubscribeCommonEvent(subscriber_);
121 }
122
OnReceiveEvent(const CommonEventData & data)123 void SecondAbilityEventSubscriber::OnReceiveEvent(const CommonEventData &data)
124 {
125 HILOG_INFO("SecondAbilityEventSubscriber::OnReceiveEvent:event=%{public}s", data.GetWant().GetAction().c_str());
126 HILOG_INFO("SecondAbilityEventSubscriber::OnReceiveEvent:data=%{public}s", data.GetData().c_str());
127 HILOG_INFO("SecondAbilityEventSubscriber::OnReceiveEvent:code=%{public}d", data.GetCode());
128 auto eventName = data.GetWant().GetAction();
129 }
130
TestAbility(int apiIndex,int caseIndex,int code)131 void SecondAbility::TestAbility(int apiIndex, int caseIndex, int code)
132 {
133 HILOG_INFO("SecondAbility::TestAbility");
134 if (mapCase_.find(apiIndex) != mapCase_.end()) {
135 if (caseIndex < (int)mapCase_[apiIndex].size()) {
136 mapCase_[apiIndex][caseIndex](code);
137 }
138 }
139 }
140
141 REGISTER_AA(SecondAbility)
142 } // namespace AppExecFwk
143 } // namespace OHOS
144