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