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 "amsstabilitym1.h"
17
18 namespace OHOS {
19 namespace AppExecFwk {
Init(const std::shared_ptr<AbilityInfo> & abilityInfo,const std::shared_ptr<OHOSApplication> & application,std::shared_ptr<AbilityHandler> & handler,const sptr<IRemoteObject> & token)20 void AmsStAbilityM1::Init(const std::shared_ptr<AbilityInfo> &abilityInfo,
21 const std::shared_ptr<OHOSApplication> &application, std::shared_ptr<AbilityHandler> &handler,
22 const sptr<IRemoteObject> &token)
23 {
24 APP_LOGI("AmsStAbilityM1::Init");
25 Ability::Init(abilityInfo, application, handler, token);
26 pageAbilityEvent.SubscribeEvent(STEventName::g_eventList, shared_from_this());
27 SendPath("Init");
28 }
29
OnStart(const Want & want)30 void AmsStAbilityM1::OnStart(const Want &want)
31 {
32 GetWantInfo(want);
33 APP_LOGI("AmsStAbilityM1::OnStart");
34 Ability::OnStart(want);
35 std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnStart;
36 pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnStartCount(), eventData);
37 SendPath("OnStart");
38 }
39
OnNewWant(const Want & want)40 void AmsStAbilityM1::OnNewWant(const Want &want)
41 {
42 APP_LOGI("AmsStAbilityM1::OnNewWant");
43 Ability::OnNewWant(want);
44 std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnNewWant;
45 pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnNewWantCount(), eventData);
46 SendPath("OnNewWant");
47 }
48
OnForeground(const Want & want)49 void AmsStAbilityM1::OnForeground(const Want &want)
50 {
51 APP_LOGI("AmsStAbilityM1::OnForeground");
52 Ability::OnForeground(want);
53 std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnForeground;
54 pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnForegroundCount(), eventData);
55 SendPath("OnForeground");
56 }
57
OnStop()58 void AmsStAbilityM1::OnStop()
59 {
60 APP_LOGI("AmsStAbilityM1::OnStop");
61 Ability::OnStop();
62 pageAbilityEvent.UnsubscribeEvent();
63 std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnStop;
64 pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnStopCount(), eventData);
65 SendPath("OnStop");
66 }
67
OnActive()68 void AmsStAbilityM1::OnActive()
69 {
70 APP_LOGI("AmsStAbilityM1::OnActive");
71 Ability::OnActive();
72 std::string startBundleName = this->Split(targetBundle, ",");
73 std::string startAbilityName = this->Split(targetAbility, ",");
74 if (!startBundleName.empty() && !startAbilityName.empty()) {
75 Want want;
76 want.SetElementName(startBundleName, startAbilityName);
77 want.SetParam("shouldReturn", shouldReturn);
78 if (!targetBundle.empty() && !targetAbility.empty()) {
79 want.SetParam("targetBundle", targetBundle);
80 want.SetParam("targetAbility", targetAbility);
81 }
82 StartAbility(want);
83 }
84 if (std::string::npos != shouldReturn.find(GetAbilityName())) {
85 TerminateAbility();
86 }
87 Clear();
88 std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnActive;
89 pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnActiveCount(), eventData);
90 SendPath("OnActive");
91 }
92
OnInactive()93 void AmsStAbilityM1::OnInactive()
94 {
95 APP_LOGI("AmsStAbilityM1::OnInactive");
96 Ability::OnInactive();
97 std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnInactive;
98 pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnInactiveCount(), eventData);
99 SendPath("OnInactive");
100 }
101
OnBackground()102 void AmsStAbilityM1::OnBackground()
103 {
104 APP_LOGI("AmsStAbilityM1::OnBackground");
105 Ability::OnBackground();
106 std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnBackground;
107 pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnBackgroundCount(), eventData);
108 SendPath("OnBackground");
109 }
110
Clear()111 void AmsStAbilityM1::Clear()
112 {
113 shouldReturn = "";
114 targetBundle = "";
115 targetAbility = "";
116 }
117
GetWantInfo(const Want & want)118 void AmsStAbilityM1::GetWantInfo(const Want &want)
119 {
120 Want mWant(want);
121 shouldReturn = mWant.GetStringParam("shouldReturn");
122 targetBundle = mWant.GetStringParam("targetBundle");
123 targetAbility = mWant.GetStringParam("targetAbility");
124 }
125
Split(std::string & str,std::string delim)126 std::string AmsStAbilityM1::Split(std::string &str, std::string delim)
127 {
128 std::string result;
129 if (!str.empty()) {
130 size_t index = str.find(delim);
131 if (index != std::string::npos) {
132 result = str.substr(0, index);
133 str = str.substr(index + delim.size());
134 } else {
135 result = str;
136 str = "";
137 }
138 }
139 return result;
140 }
141
SendPath(const std::string & callBackPath)142 void AmsStAbilityM1::SendPath(const std::string &callBackPath)
143 {
144 std::string callBack = pageAbilityEvent.GetCallBackPath(callBackPath);
145 std::string abilityStatus = pageAbilityEvent.GetAbilityStatus(std::to_string(Ability::GetState()));
146 std::string callBackPathEventData = Ability::GetAbilityName() + callBack;
147 std::string abilityStatusEventData = Ability::GetAbilityName() + abilityStatus;
148 pageAbilityEvent.PublishEvent(STEventName::g_eventName, STEventName::g_defeventCode, callBackPathEventData);
149 pageAbilityEvent.PublishEvent(STEventName::g_eventName, STEventName::g_defeventCode, abilityStatusEventData);
150 }
151
152 REGISTER_AA(AmsStAbilityM1);
153 } // namespace AppExecFwk
154 } // namespace OHOS