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 "sixth_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 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 SixthAbility::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 APP_LOGI("SixthAbility::Init");
30 Ability::Init(abilityInfo, application, handler, token);
31 }
32
~SixthAbility()33 SixthAbility::~SixthAbility()
34 {
35 CommonEventManager::UnSubscribeCommonEvent(subscriber_);
36 }
37
OnStart(const Want & want)38 void SixthAbility::OnStart(const Want &want)
39 {
40 APP_LOGI("SixthAbility::OnStart");
41 SubscribeEvent();
42 Ability::OnStart(want);
43 callbackSeq += "OnStart";
44 TestUtils::PublishEvent(g_EVENT_RESP_SIXTH_LIFECYCLE, SIXTH_ABILITY_CODE, "OnStart");
45 }
46
OnStop()47 void SixthAbility::OnStop()
48 {
49 APP_LOGI("SixthAbility::OnStop");
50 Ability::OnStop();
51 CommonEventManager::UnSubscribeCommonEvent(subscriber_);
52 callbackSeq += "OnStop"; // OnInactiveOnBackgroundOnStop
53 TestUtils::PublishEvent(g_EVENT_RESP_SIXTH_LIFECYCLE, SIXTH_ABILITY_CODE, callbackSeq);
54 callbackSeq = "";
55 }
56
OnActive()57 void SixthAbility::OnActive()
58 {
59 APP_LOGI("SixthAbility::OnActive====<");
60 Ability::OnActive();
61 callbackSeq += "OnActive"; // OnStartOnActive
62 TestUtils::PublishEvent(g_EVENT_RESP_SIXTH_LIFECYCLE, SIXTH_ABILITY_CODE, callbackSeq);
63 callbackSeq = "";
64 }
65
OnConfigurationUpdated(const Configuration & configuration)66 void SixthAbility::OnConfigurationUpdated(const Configuration &configuration)
67 {
68 APP_LOGI("SixthAbility::OnConfigurationUpdated====<");
69 Ability::OnConfigurationUpdated(configuration);
70 TestUtils::PublishEvent(g_EVENT_RESP_SIXTH_LIFECYCLE, SIXTH_ABILITY_CODE, "OnConfigurationUpdated");
71 }
72
OnInactive()73 void SixthAbility::OnInactive()
74 {
75 APP_LOGI("SixthAbility::OnInactive");
76 Ability::OnInactive();
77 callbackSeq += "OnInactive";
78 TestUtils::PublishEvent(g_EVENT_RESP_SIXTH_LIFECYCLE, SIXTH_ABILITY_CODE, "OnInactive");
79 }
80
OnBackground()81 void SixthAbility::OnBackground()
82 {
83 APP_LOGI("SixthAbility::OnBackground");
84 Ability::OnBackground();
85 callbackSeq += "OnBackground";
86 TestUtils::PublishEvent(g_EVENT_RESP_SIXTH_LIFECYCLE, SIXTH_ABILITY_CODE, "OnBackground");
87 }
88
OnForeground(const Want & want)89 void SixthAbility::OnForeground(const Want &want)
90 {
91 APP_LOGI("SixthAbility::OnForeground");
92 Ability::OnForeground(want);
93 callbackSeq += "OnForeground";
94 TestUtils::PublishEvent(g_EVENT_RESP_SIXTH_LIFECYCLE, SIXTH_ABILITY_CODE, "OnForeground");
95 }
96
SubscribeEvent()97 void SixthAbility::SubscribeEvent()
98 {
99 std::vector<std::string> eventList = {
100 // g_EVENT_REQU_SIXTH,
101 };
102 MatchingSkills matchingSkills;
103 for (const auto &e : eventList) {
104 matchingSkills.AddEvent(e);
105 }
106 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
107 subscribeInfo.SetPriority(1);
108 subscriber_ = std::make_shared<SixthAbilityEventSubscriber>(subscribeInfo);
109 subscriber_->sixthAbility = this;
110 CommonEventManager::SubscribeCommonEvent(subscriber_);
111 }
112
OnReceiveEvent(const CommonEventData & data)113 void SixthAbilityEventSubscriber::OnReceiveEvent(const CommonEventData &data)
114 {
115 APP_LOGI("SixthAbilityEventSubscriber::OnReceiveEvent:event=%{public}s", data.GetWant().GetAction().c_str());
116 APP_LOGI("SixthAbilityEventSubscriber::OnReceiveEvent:data=%{public}s", data.GetData().c_str());
117 APP_LOGI("SixthAbilityEventSubscriber::OnReceiveEvent:code=%{public}d", data.GetCode());
118 auto eventName = data.GetWant().GetAction();
119 }
120
TestAbility(int apiIndex,int caseIndex,int code)121 void SixthAbility::TestAbility(int apiIndex, int caseIndex, int code)
122 {
123 APP_LOGI("SixthAbility::TestAbility");
124 if (mapCase_.find(apiIndex) != mapCase_.end()) {
125 if (caseIndex < (int)mapCase_[apiIndex].size()) {
126 mapCase_[apiIndex][caseIndex](code);
127 }
128 }
129 }
130
131 REGISTER_AA(SixthAbility)
132 } // namespace AppExecFwk
133 } // namespace OHOS
134