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 "amsstabilityp1.h"
17
18 namespace OHOS {
19 namespace AppExecFwk {
OnStart(const Want & want)20 void AmsStAbilityP1::OnStart(const Want &want)
21 {
22 GetWantInfo(want);
23 APP_LOGI("AmsStAbilityP1::onStart");
24 pageAbilityEvent.SubscribeEvent(STEventName::g_eventList, shared_from_this());
25 Ability::OnStart(want);
26 std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnStart;
27 std::shared_ptr<ProcessInfo> processInfo = AbilityContext::GetProcessInfo();
28 if (processInfo != nullptr) {
29 int amsStAbilityP1Code = 11;
30 std::string pidInfo = std::to_string(processInfo->GetPid());
31 pageAbilityEvent.PublishEvent(STEventName::g_pidEventName, amsStAbilityP1Code, pidInfo);
32 }
33 pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnStartCount(), eventData);
34 }
35
OnNewWant(const Want & want)36 void AmsStAbilityP1::OnNewWant(const Want &want)
37 {
38 APP_LOGI("AmsStAbilityP1::OnNewWant");
39 Ability::OnNewWant(want);
40 std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnNewWant;
41 pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnNewWantCount(), eventData);
42 }
43
OnForeground(const Want & want)44 void AmsStAbilityP1::OnForeground(const Want &want)
45 {
46 APP_LOGI("AmsStAbilityP1::OnForeground");
47 Ability::OnForeground(want);
48 std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnForeground;
49 pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnForegroundCount(), eventData);
50 }
51
OnStop()52 void AmsStAbilityP1::OnStop()
53 {
54 APP_LOGI("AmsStAbilityP1::onStop");
55 Ability::OnStop();
56 pageAbilityEvent.UnsubscribeEvent();
57 std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnStop;
58 pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnStopCount(), eventData);
59 }
60
OnActive()61 void AmsStAbilityP1::OnActive()
62 {
63 APP_LOGI("AmsStAbilityP1::OnActive");
64 Ability::OnActive();
65 std::string startBundleName = this->Split(targetBundle, ",");
66 std::string startAbilityName = this->Split(targetAbility, ",");
67 if (!startBundleName.empty() && !startAbilityName.empty()) {
68 Want want;
69 want.SetElementName(startBundleName, startAbilityName);
70 want.SetParam("shouldReturn", shouldReturn);
71 if (!targetBundle.empty() && !targetAbility.empty()) {
72 want.SetParam("targetBundle", targetBundle);
73 want.SetParam("targetAbility", targetAbility);
74 }
75 StartAbility(want);
76 }
77 if (std::string::npos != shouldReturn.find(GetAbilityName())) {
78 TerminateAbility();
79 }
80 Clear();
81 std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnActive;
82 pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnActiveCount(), eventData);
83 }
84
OnInactive()85 void AmsStAbilityP1::OnInactive()
86 {
87 APP_LOGI("AmsStAbilityP1::OnInactive");
88 Ability::OnInactive();
89 std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnInactive;
90 pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnInactiveCount(), eventData);
91 }
92
OnBackground()93 void AmsStAbilityP1::OnBackground()
94 {
95 APP_LOGI("AmsStAbilityP1::OnBackground");
96 Ability::OnBackground();
97 std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnBackground;
98 pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnBackgroundCount(), eventData);
99 }
100
Clear()101 void AmsStAbilityP1::Clear()
102 {
103 shouldReturn = "";
104 targetBundle = "";
105 targetAbility = "";
106 }
107
GetWantInfo(const Want & want)108 void AmsStAbilityP1::GetWantInfo(const Want &want)
109 {
110 Want mWant(want);
111 shouldReturn = mWant.GetStringParam("shouldReturn");
112 targetBundle = mWant.GetStringParam("targetBundle");
113 targetAbility = mWant.GetStringParam("targetAbility");
114 }
115
Split(std::string & str,std::string delim)116 std::string AmsStAbilityP1::Split(std::string &str, std::string delim)
117 {
118 std::string result;
119 if (!str.empty()) {
120 size_t index = str.find(delim);
121 if (index != std::string::npos) {
122 result = str.substr(0, index);
123 str = str.substr(index + delim.size());
124 } else {
125 result = str;
126 str = "";
127 }
128 }
129 return result;
130 }
131
132 REGISTER_AA(AmsStAbilityP1);
133 } // namespace AppExecFwk
134 } // namespace OHOS