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