• 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 "amsstabilityr4.h"
17 
18 namespace OHOS {
19 namespace AppExecFwk {
OnStart(const Want & want)20 void AmsStAbilityR4::OnStart(const Want &want)
21 {
22     GetWantInfo(want);
23 
24     APP_LOGI("AmsStAbilityR4::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 AmsStAbilityR4::OnNewWant(const Want &want)
32 {
33     APP_LOGI("AmsStAbilityR4::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 AmsStAbilityR4::OnForeground(const Want &want)
40 {
41     APP_LOGI("AmsStAbilityR4::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 AmsStAbilityR4::OnStop()
48 {
49     APP_LOGI("AmsStAbilityR4::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 AmsStAbilityR4::OnActive()
57 {
58     APP_LOGI("AmsStAbilityR4::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 AmsStAbilityR4::OnInactive()
81 {
82     APP_LOGI("AmsStAbilityR4::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 AmsStAbilityR4::OnBackground()
89 {
90     APP_LOGI("AmsStAbilityR4::OnBackground");
91     Ability::OnBackground();
92     std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnBackground;
93     pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnBackgroundCount(), eventData);
94 }
95 
Clear()96 void AmsStAbilityR4::Clear()
97 {
98     shouldReturn = "";
99     targetBundle = "";
100     targetAbility = "";
101 }
102 
GetWantInfo(const Want & want)103 void AmsStAbilityR4::GetWantInfo(const Want &want)
104 {
105     Want mWant(want);
106     shouldReturn = mWant.GetStringParam("shouldReturn");
107     targetBundle = mWant.GetStringParam("targetBundle");
108     targetAbility = mWant.GetStringParam("targetAbility");
109 }
110 
Split(std::string & str,std::string delim)111 std::string AmsStAbilityR4::Split(std::string &str, std::string delim)
112 {
113     std::string result;
114     if (!str.empty()) {
115         size_t index = str.find(delim);
116         if (index != std::string::npos) {
117             result = str.substr(0, index);
118             str = str.substr(index + delim.size());
119         } else {
120             result = str;
121             str = "";
122         }
123     }
124     return result;
125 }
126 
127 REGISTER_AA(AmsStAbilityR4);
128 }  // namespace AppExecFwk
129 }  // namespace OHOS