• 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 "ability_state_main.h"
17 #include <string>
18 #include "test_utils.h"
19 
20 namespace OHOS {
21 namespace AppExecFwk {
22 using namespace OHOS::EventFwk;
23 namespace {
24 std::string FwkAbilityState_Event_Resp_A = "resp_com_ohos_amsst_FwkAbilityStateA";
25 std::string FwkAbilityState_Event_Requ_A = "requ_com_ohos_amsst_FwkAbilityStateA";
26 const std::string FwkAbilityState_SaveData_Int = "int_data_1";
27 int OnStopCode = 3;
28 int OnActiveCode = 1;
29 int OnBackgroundCode = 2;
30 int OnRestoreCheckCode = 4;
31 constexpr int iBlockTime = 12;
32 const int iSaveData = 12345;
33 }  // namespace
34 
~FwkAbilityStateMain()35 FwkAbilityStateMain::~FwkAbilityStateMain()
36 {
37     CommonEventManager::UnSubscribeCommonEvent(subscriber_);
38 }
39 
OnStart(const Want & want)40 void FwkAbilityStateMain::OnStart(const Want &want)
41 {
42     HILOG_INFO("FwkAbilityStateMain::onStart");
43     SubscribeEvent();
44     bIsBlockRestore = want.HasParameter("StartType1");
45     bIsBlockSave = false;
46     Ability::OnStart(want);
47     callback_seq += "OnStart";
48 }
49 
OnNewWant(const Want & want)50 void FwkAbilityStateMain::OnNewWant(const Want &want)
51 {
52     HILOG_INFO("FwkAbilityStateMain::OnNewWant");
53     Ability::OnNewWant(want);
54     callback_seq += "OnNewWant";
55 }
56 
OnForeground(const Want & want)57 void FwkAbilityStateMain::OnForeground(const Want &want)
58 {
59     HILOG_INFO("FwkAbilityStateMain::OnForeground");
60     Ability::OnForeground(want);
61     callback_seq += "OnForeground";
62 }
63 
OnStop()64 void FwkAbilityStateMain::OnStop()
65 {
66     HILOG_INFO("FwkAbilityStateMain::onStop");
67     Ability::OnStop();
68     CommonEventManager::UnSubscribeCommonEvent(subscriber_);
69     callback_seq += "OnStop";
70     TestUtils::PublishEvent(FwkAbilityState_Event_Resp_A, OnStopCode, callback_seq);
71     callback_seq = "";
72 }
73 
OnActive()74 void FwkAbilityStateMain::OnActive()
75 {
76     HILOG_INFO("FwkAbilityStateMain::OnActive");
77     Ability::OnActive();
78     callback_seq += "OnActive";
79     TestUtils::PublishEvent(FwkAbilityState_Event_Resp_A, OnActiveCode, callback_seq);
80     callback_seq = "";
81 }
82 
OnInactive()83 void FwkAbilityStateMain::OnInactive()
84 {
85     HILOG_INFO("FwkAbilityStateMain::OnInactive");
86     Ability::OnInactive();
87     callback_seq += "OnInactive";
88 }
89 
OnBackground()90 void FwkAbilityStateMain::OnBackground()
91 {
92     HILOG_INFO("FwkAbilityStateMain::OnBackground");
93     Ability::OnBackground();
94     callback_seq += "OnBackground";
95     TestUtils::PublishEvent(FwkAbilityState_Event_Resp_A, OnBackgroundCode, callback_seq);
96     callback_seq = "";
97 }
98 
OnBlockProcess(bool & bIsBlockFlag)99 void FwkAbilityStateMain::OnBlockProcess(bool &bIsBlockFlag)
100 {
101     int i = iBlockTime;
102 
103     if (bIsBlockFlag) {
104         while (i-- > 0) {
105             HILOG_INFO("FwkAbilityStateMain::OnBlockProcess time left %{public}d", i);
106             sleep(1);
107         }
108         bIsBlockFlag = false;
109     }
110 }
111 
OnSaveAbilityState(PacMap & outState)112 void FwkAbilityStateMain::OnSaveAbilityState(PacMap &outState)
113 {
114     HILOG_INFO("FwkAbilityStateMain::OnSaveAbilityState");
115     OnBlockProcess(bIsBlockSave);
116     outState.PutIntValue(FwkAbilityState_SaveData_Int, iSaveData);
117     Ability::OnSaveAbilityState(outState);
118     callback_seq += "OnSaveAbilityState";
119 }
120 
OnRestoreAbilityState(const PacMap & inState)121 void FwkAbilityStateMain::OnRestoreAbilityState(const PacMap &inState)
122 {
123     int iGetSaveData;
124     PacMap tmpPacMap;
125 
126     HILOG_INFO("FwkAbilityStateMain::OnRestoreAbilityState");
127     OnBlockProcess(bIsBlockRestore);
128     Ability::OnRestoreAbilityState(inState);
129     tmpPacMap = (PacMap)inState;
130     iGetSaveData = tmpPacMap.GetIntValue(FwkAbilityState_SaveData_Int);
131     if (iSaveData != iGetSaveData) {
132         TestUtils::PublishEvent(FwkAbilityState_Event_Resp_A, OnRestoreCheckCode, "NotEqual");
133         HILOG_INFO("FwkAbilityStateMain::restore not equal %{public}d", iGetSaveData);
134     } else {
135         HILOG_INFO("FwkAbilityStateMain::restore equal %{public}d", iGetSaveData);
136     }
137     callback_seq += "OnRestoreAbilityState";
138 }
139 
SubscribeEvent()140 void FwkAbilityStateMain::SubscribeEvent()
141 {
142     std::vector<std::string> eventList = {
143         FwkAbilityState_Event_Requ_A,
144     };
145     MatchingSkills matchingSkills;
146     for (const auto &e : eventList) {
147         matchingSkills.AddEvent(e);
148     }
149     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
150     subscribeInfo.SetPriority(1);
151     subscriber_ = std::make_shared<FwkAbilityStateMainSubscriber>(subscribeInfo);
152     subscriber_->mainAbility = this;
153     CommonEventManager::SubscribeCommonEvent(subscriber_);
154 }
155 
OnReceiveEvent(const CommonEventData & data)156 void FwkAbilityStateMainSubscriber::OnReceiveEvent(const CommonEventData &data)
157 {
158     auto eventName = data.GetWant().GetAction();
159     if (strcmp(eventName.c_str(), FwkAbilityState_Event_Requ_A.c_str()) == 0) {
160         auto target = data.GetData();
161         if (mapAction_.find(target) != mapAction_.end()) {
162             mapAction_[target](target, data.GetCode());
163         } else {
164             HILOG_INFO("OnReceiveEvent: CommonEventData error(%{public}s)", target.c_str());
165         }
166     }
167 }
168 
Action(std::string action,int code)169 void FwkAbilityStateMain::Action(std::string action, int code)
170 {
171     HILOG_INFO("FwkAbilityStateMain::Action Called");
172     if (mapAction_.find(action) != mapAction_.end()) {
173         mapAction_[action](code);
174     }
175 }
176 
DoCrash(std::string action,int code)177 void FwkAbilityStateMain::DoCrash(std::string action, int code)
178 {
179     HILOG_INFO("FwkAbilityStateMain::DoCrash Called");
180     CrashMaker *pcCrashMaker = nullptr;
181     int a = pcCrashMaker->CrashTry();
182     HILOG_INFO("FwkAbilityStateMain::DoCrash Process %{public}d", a);
183 }
184 
StartNextAbility(int code)185 void FwkAbilityStateMain::StartNextAbility(int code)
186 {
187     std::string targetBundle = "com.ohos.amsst.fwkAbilityState";
188     std::string targetAbility = "FwkAbilityStateSecond";
189     Want want;
190     want.SetElementName(targetBundle, targetAbility);
191     StartAbility(want);
192 }
193 
BlockAndStart(std::string action,int code)194 void FwkAbilityStateMain::BlockAndStart(std::string action, int code)
195 {
196     bIsBlockSave = true;
197     StartNextAbility(code);
198 }
199 
200 REGISTER_AA(FwkAbilityStateMain);
201 }  // namespace AppExecFwk
202 }  // namespace OHOS