• 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 "amsabilityvisibletestservicea1.h"
17 #include "app_log_wrapper.h"
18 #include "common_event.h"
19 #include "common_event_manager.h"
20 using namespace OHOS::EventFwk;
21 
22 namespace OHOS {
23 namespace AppExecFwk {
24 
~AmsAbilityVisibleTestServiceA1()25 AmsAbilityVisibleTestServiceA1::~AmsAbilityVisibleTestServiceA1()
26 {}
27 
OnStart(const Want & want)28 void AmsAbilityVisibleTestServiceA1::OnStart(const Want &want)
29 {
30     APP_LOGI("AmsAbilityVisibleTestServiceA1::OnStart");
31     pageAbilityEvent.SubscribeEvent(STEventName::g_eventList, shared_from_this());
32     Ability::OnStart(want);
33     std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnStart;
34     pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnStartCount(), eventData);
35 }
36 
OnCommand(const AAFwk::Want & want,bool restart,int startId)37 void AmsAbilityVisibleTestServiceA1::OnCommand(const AAFwk::Want &want, bool restart, int startId)
38 {
39     APP_LOGI("AmsAbilityVisibleTestServiceA1::OnCommand");
40 
41     Ability::OnCommand(want, restart, startId);
42     std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnCommand;
43     pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnCommandCount(), eventData);
44 }
45 
OnNewWant(const Want & want)46 void AmsAbilityVisibleTestServiceA1::OnNewWant(const Want &want)
47 {
48     APP_LOGI("AmsAbilityVisibleTestServiceA1::OnNewWant");
49 
50     Ability::OnNewWant(want);
51     std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnNewWant;
52     pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnNewWantCount(), eventData);
53 }
54 
OnStop()55 void AmsAbilityVisibleTestServiceA1::OnStop()
56 {
57     APP_LOGI("AmsAbilityVisibleTestServiceA1::OnStop");
58 
59     Ability::OnStop();
60     pageAbilityEvent.UnsubscribeEvent();
61     std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnStop;
62     pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnStopCount(), eventData);
63 }
64 
OnActive()65 void AmsAbilityVisibleTestServiceA1::OnActive()
66 {
67     APP_LOGI("AmsAbilityVisibleTestServiceA1::OnActive");
68 
69     Ability::OnActive();
70     std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnActive;
71     pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnActiveCount(), eventData);
72 }
73 
OnInactive()74 void AmsAbilityVisibleTestServiceA1::OnInactive()
75 {
76     APP_LOGI("AmsAbilityVisibleTestServiceA1::OnInactive");
77 
78     Ability::OnInactive();
79     std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnInactive;
80     pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnInactiveCount(), eventData);
81 }
82 
OnBackground()83 void AmsAbilityVisibleTestServiceA1::OnBackground()
84 {
85     APP_LOGI("AmsAbilityVisibleTestServiceA1::OnBackground");
86 
87     Ability::OnBackground();
88     std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnBackground;
89     pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnBackgroundCount(), eventData);
90 }
91 
OnConnect(const Want & want)92 sptr<IRemoteObject> AmsAbilityVisibleTestServiceA1::OnConnect(const Want &want)
93 {
94     APP_LOGI("AmsAbilityVisibleTestServiceA1::OnConnect");
95 
96     sptr<IRemoteObject> ret = Ability::OnConnect(want);
97     GetWantInfo(want);
98     std::string startBundleName = this->Split(targetBundle_, ",");
99     std::string startAbilityName = this->Split(targetAbility_, ",");
100     std::string startAbilityType = this->Split(targetType_, ",");
101     if (!startBundleName.empty() && !startAbilityName.empty() && !startAbilityType.empty()) {
102         Want wantOther;
103         wantOther.SetElementName(startBundleName, startAbilityName);
104         if (!targetBundle_.empty() && !targetAbility_.empty() && !targetType_.empty()) {
105             wantOther.SetParam("targetBundle", targetBundle_);
106             wantOther.SetParam("targetAbility", targetAbility_);
107             wantOther.SetParam("type", targetType_);
108         }
109         if (startAbilityType == "Page") {
110             StartAbility(wantOther);
111         }
112         if (startAbilityType == "Service") {
113             sptr<AbilityConnectCallback> stub(new (std::nothrow) AbilityConnectCallback());
114             ConnectAbility(wantOther, stub);
115         }
116         if (startAbilityType == "Data") {
117             Uri dataAbilityUri("dataability:///" + startBundleName + "." + startAbilityName);
118             std::shared_ptr<DataAbilityHelper> helper = DataAbilityHelper::Creator(GetContext());
119             NativeRdb::ValuesBucket bucket;
120             helper->Insert(dataAbilityUri, bucket);
121         }
122     }
123     Clear();
124     std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnConnect;
125     pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnConnectCount(), eventData);
126     return ret;
127 }
128 
OnDisconnect(const Want & want)129 void AmsAbilityVisibleTestServiceA1::OnDisconnect(const Want &want)
130 {
131     APP_LOGI("AmsAbilityVisibleTestServiceA1::OnDisconnect");
132 
133     Ability::OnDisconnect(want);
134     std::string eventData = GetAbilityName() + STEventName::g_abilityStateOnDisconnect;
135     pageAbilityEvent.PublishEvent(STEventName::g_eventName, pageAbilityEvent.GetOnDisconnectCount(), eventData);
136 }
137 
Clear()138 void AmsAbilityVisibleTestServiceA1::Clear()
139 {
140     targetType_ = "";
141     targetBundle_ = "";
142     targetAbility_ = "";
143 }
144 
GetWantInfo(const Want & want)145 void AmsAbilityVisibleTestServiceA1::GetWantInfo(const Want &want)
146 {
147     Want mWant(want);
148     targetType_ = mWant.GetStringParam("type");
149     targetBundle_ = mWant.GetStringParam("targetBundle");
150     targetAbility_ = mWant.GetStringParam("targetAbility");
151 }
152 
Split(std::string & str,std::string delim)153 std::string AmsAbilityVisibleTestServiceA1::Split(std::string &str, std::string delim)
154 {
155     std::string result;
156     if (!str.empty()) {
157         size_t index = str.find(delim);
158         if (index != std::string::npos) {
159             result = str.substr(0, index);
160             str = str.substr(index + delim.size());
161         } else {
162             result = str;
163             str = "";
164         }
165     }
166     return result;
167 }
168 
169 REGISTER_AA(AmsAbilityVisibleTestServiceA1);
170 }  // namespace AppExecFwk
171 }  // namespace OHOS