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