• 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 "amsstabilityr2.h"
17 
18 namespace OHOS {
19 namespace AppExecFwk {
20 namespace {
21 constexpr uint32_t multiple = 100;
22 }
Init(const std::shared_ptr<AbilityInfo> & abilityInfo,const std::shared_ptr<OHOSApplication> & application,std::shared_ptr<AbilityHandler> & handler,const sptr<IRemoteObject> & token)23 void AmsStAbilityR2::Init(const std::shared_ptr<AbilityInfo> &abilityInfo,
24     const std::shared_ptr<OHOSApplication> &application, std::shared_ptr<AbilityHandler> &handler,
25     const sptr<IRemoteObject> &token)
26 {
27     APP_LOGI("AmsStAbilityR2::Init");
28     Ability::Init(abilityInfo, application, handler, token);
29     std::string eventData = GetAbilityName() + STEventName::g_abilityStateInit;
30     pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnStartCount(), eventData);
31     pageAbilityEvent.SubscribeEvent(STEventName::g_eventList, shared_from_this());
32 }
33 
OnStart(const Want & want)34 void AmsStAbilityR2::OnStart(const Want &want)
35 {
36     GetWantInfo(want);
37     APP_LOGI("AmsStAbilityR2::onStart");
38     Ability::OnStart(want);
39     std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnStart;
40     pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnStartCount(), eventData);
41 }
42 
OnNewWant(const Want & want)43 void AmsStAbilityR2::OnNewWant(const Want &want)
44 {
45     APP_LOGI("AmsStAbilityR2::OnNewWant");
46     Ability::OnNewWant(want);
47     std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnNewWant;
48     pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnNewWantCount(), eventData);
49 }
50 
OnForeground(const Want & want)51 void AmsStAbilityR2::OnForeground(const Want &want)
52 {
53     APP_LOGI("AmsStAbilityR2::OnForeground");
54     Ability::OnForeground(want);
55     std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnForeground;
56     pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnForegroundCount(), eventData);
57 }
58 
OnStop()59 void AmsStAbilityR2::OnStop()
60 {
61     APP_LOGI("AmsStAbilityR2::onStop");
62     Ability::OnStop();
63     pageAbilityEvent.UnsubscribeEvent();
64     std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnStop;
65     pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnStopCount(), eventData);
66 }
67 
OnActive()68 void AmsStAbilityR2::OnActive()
69 {
70     APP_LOGI("AmsStAbilityR2::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 }
91 
OnInactive()92 void AmsStAbilityR2::OnInactive()
93 {
94     APP_LOGI("AmsStAbilityR2::OnInactive");
95     Ability::OnInactive();
96     std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnInactive;
97     pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnInactiveCount(), eventData);
98 }
99 
OnBackground()100 void AmsStAbilityR2::OnBackground()
101 {
102     APP_LOGI("AmsStAbilityR2::OnBackground");
103     Ability::OnBackground();
104     std::this_thread::sleep_for(std::chrono::milliseconds(STEventName::BACKGROUND_TIMEOUT * multiple));
105     std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnBackground;
106     pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnBackgroundCount(), eventData);
107 }
108 
Clear()109 void AmsStAbilityR2::Clear()
110 {
111     shouldReturn = "";
112     targetBundle = "";
113     targetAbility = "";
114 }
115 
GetWantInfo(const Want & want)116 void AmsStAbilityR2::GetWantInfo(const Want &want)
117 {
118     Want mWant(want);
119     shouldReturn = mWant.GetStringParam("shouldReturn");
120     targetBundle = mWant.GetStringParam("targetBundle");
121     targetAbility = mWant.GetStringParam("targetAbility");
122 }
123 
Split(std::string & str,std::string delim)124 std::string AmsStAbilityR2::Split(std::string &str, std::string delim)
125 {
126     std::string result;
127     if (!str.empty()) {
128         size_t index = str.find(delim);
129         if (index != std::string::npos) {
130             result = str.substr(0, index);
131             str = str.substr(index + delim.size());
132         } else {
133             result = str;
134             str = "";
135         }
136     }
137     return result;
138 }
139 
140 REGISTER_AA(AmsStAbilityR2);
141 }  // namespace AppExecFwk
142 }  // namespace OHOS