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