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