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 "application_context.h"
17 #include "app_log_wrapper.h"
18 #include "task_dispatcher_context.h"
19
20 namespace OHOS {
21 namespace AppExecFwk {
22
ApplicationContext()23 ApplicationContext::ApplicationContext()
24 {}
~ApplicationContext()25 ApplicationContext::~ApplicationContext()
26 {}
27
28 /**
29 * @brief Obtains information about the current ability.
30 * The returned information includes the class name, bundle name, and other information about the current ability.
31 *
32 * @return Returns the AbilityInfo object for the current ability.
33 */
GetAbilityInfo()34 const std::shared_ptr<AbilityInfo> ApplicationContext::GetAbilityInfo()
35 {
36 return nullptr;
37 }
38
39 /**
40 * @brief Starts a new ability.
41 * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method
42 * to start a specific ability. The system locates the target ability from installed abilities based on the value
43 * of the want parameter and then starts it. You can specify the ability to start using the want parameter.
44 *
45 * @param want Indicates the Want containing information about the target ability to start.
46 *
47 * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE
48 * template is started. You can define the request code to identify the results returned by abilities. The value
49 * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE
50 * template.
51 *
52 */
StartAbility(const AAFwk::Want & want,int requestCode)53 void ApplicationContext::StartAbility(const AAFwk::Want &want, int requestCode)
54 {}
55
56 /**
57 * @brief Starts a new ability with special ability start setting.
58 *
59 * @param want Indicates the Want containing information about the target ability to start.
60 * @param requestCode Indicates the request code returned after the ability is started. You can define the request code
61 * to identify the results returned by abilities. The value ranges from 0 to 65535.
62 * @param abilityStartSetting Indicates the special start setting used in starting ability.
63 *
64 */
StartAbility(const Want & want,int requestCode,const AbilityStartSetting & abilityStartSetting)65 void ApplicationContext::StartAbility(const Want &want, int requestCode, const AbilityStartSetting &abilityStartSetting)
66 {}
67
68 /**
69 * @brief Destroys another ability you had previously started by calling Ability.startAbilityForResult
70 * (ohos.aafwk.content.Want, int, ohos.aafwk.ability.startsetting.AbilityStartSetting) with the same requestCode passed.
71 *
72 * @param requestCode Indicates the request code passed for starting the ability.
73 *
74 */
TerminateAbility(int requestCode)75 void ApplicationContext::TerminateAbility(int requestCode)
76 {}
77
78 /**
79 * @brief Destroys the current ability.
80 *
81 */
TerminateAbility()82 void ApplicationContext::TerminateAbility()
83 {}
84
85 /**
86 * @brief
87 * Destroys this Service ability if the number of times it has been started equals the number represented by the
88 * given {@code startId}. This method is the same as calling {@link #terminateAbility} to destroy this Service
89 * ability, except that this method helps you avoid destroying it if a client has requested a Service
90 * ability startup in {@link ohos.aafwk.ability.Ability#onCommand} but you are unaware of it.
91 *
92 * @param startId Indicates the number of startup times of this Service ability passed to
93 * {@link ohos.aafwk.ability.Ability#onCommand}. The {@code startId} is
94 * incremented by 1 every time this ability is started. For example,
95 * if this ability has been started for six times, the value of {@code startId} is {@code 6}.
96 *
97 * @return Returns {@code true} if the {@code startId} matches the number of startup times
98 * and this Service ability will be destroyed; returns {@code false} otherwise.
99 */
TerminateAbilityResult(int startId)100 bool ApplicationContext::TerminateAbilityResult(int startId)
101 {
102 return false;
103 }
104
105 /**
106 * @brief Obtains the bundle name of the ability that called the current ability.
107 * You can use the obtained bundle name to check whether the calling ability is allowed to receive the data you will
108 * send. If you did not use Ability.startAbilityForResult(ohos.aafwk.content.Want, int,
109 * ohos.aafwk.ability.startsetting.AbilityStartSetting) to start the calling ability, null is returned.
110 *
111 * @return Returns the bundle name of the calling ability; returns null if no calling ability is available.
112 */
GetCallingBundle()113 std::string ApplicationContext::GetCallingBundle()
114 {
115 return "";
116 }
117
118 /**
119 * @brief Connects the current ability to an ability
120 *
121 * @param want Indicates the want containing information about the ability to connect
122 *
123 * @param conn Indicates the callback object when the target ability is connected.
124 *
125 * @return True means success and false means failure
126 */
ConnectAbility(const Want & want,const sptr<AAFwk::IAbilityConnection> & conn)127 bool ApplicationContext::ConnectAbility(const Want &want, const sptr<AAFwk::IAbilityConnection> &conn)
128 {
129 return false;
130 }
131
132 /**
133 * @brief Disconnects the current ability from an ability
134 *
135 * @param conn Indicates the IAbilityConnection callback object passed by connectAbility after the connection
136 * is set up. The IAbilityConnection object uniquely identifies a connection between two abilities.
137 */
DisconnectAbility(const sptr<AAFwk::IAbilityConnection> & conn)138 void ApplicationContext::DisconnectAbility(const sptr<AAFwk::IAbilityConnection> &conn)
139 {}
140
141 /**
142 * @brief Destroys another ability that uses the AbilityInfo.AbilityType.SERVICE template.
143 * The current ability using either the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE
144 * template can call this method to destroy another ability that uses the AbilityInfo.AbilityType.SERVICE
145 * template. The current ability itself can be destroyed by calling the terminateAbility() method.
146 *
147 * @param want Indicates the Want containing information about the ability to destroy.
148 *
149 * @return Returns true if the ability is destroyed successfully; returns false otherwise.
150 */
StopAbility(const AAFwk::Want & want)151 bool ApplicationContext::StopAbility(const AAFwk::Want &want)
152 {
153 return false;
154 }
155
GetToken()156 sptr<IRemoteObject> ApplicationContext::GetToken()
157 {
158 return nullptr;
159 }
160
161 /**
162 * @brief Starts multiple abilities.
163 *
164 * @param wants Indicates the Want containing information array about the target ability to start.
165 */
StartAbilities(const std::vector<AAFwk::Want> & wants)166 void ApplicationContext::StartAbilities(const std::vector<AAFwk::Want> &wants)
167 {}
168
169 /**
170 * @brief Checks whether this ability is the first ability in a mission.
171 *
172 * @return Returns true is first in Mission.
173 */
IsFirstInMission()174 bool ApplicationContext::IsFirstInMission()
175 {
176 return false;
177 }
178
179 /**
180 * @brief Obtains the unique ID of the mission containing this ability.
181 *
182 * @return Returns the unique mission ID.
183 */
GetMissionId()184 int ApplicationContext::GetMissionId()
185 {
186 return -1;
187 }
188
189 /**
190 * @brief Creates a parallel task dispatcher with a specified priority.
191 *
192 * @param name Indicates the task dispatcher name. This parameter is used to locate problems.
193 * @param priority Indicates the priority of all tasks dispatched by the parallel task dispatcher.
194 *
195 * @return Returns a parallel task dispatcher.
196 */
CreateParallelTaskDispatcher(const std::string & name,const TaskPriority & priority)197 std::shared_ptr<TaskDispatcher> ApplicationContext::CreateParallelTaskDispatcher(
198 const std::string &name, const TaskPriority &priority)
199 {
200 APP_LOGI("ApplicationContext::CreateParallelTaskDispatcher begin");
201 if (taskDispatcherContext_ == nullptr) {
202 std::lock_guard<std::mutex> lock_l(mutex_);
203 if (taskDispatcherContext_ == nullptr) {
204 taskDispatcherContext_ = std::make_shared<TaskDispatcherContext>();
205 APP_LOGI("ApplicationContext::CreateParallelTaskDispatcher threadpool create");
206 }
207
208 if (taskDispatcherContext_ == nullptr) {
209 APP_LOGE("ApplicationContext::CreateParallelTaskDispatcher taskDispatcherContext_ is nullptr");
210 return nullptr;
211 }
212 }
213
214 std::shared_ptr<TaskDispatcher> task = taskDispatcherContext_->CreateParallelDispatcher(name, priority);
215 APP_LOGI("ApplicationContext::CreateParallelTaskDispatcher end");
216 return task;
217 }
218
219 /**
220 * @brief Creates a serial task dispatcher with a specified priority.
221 *
222 * @param name Indicates the task dispatcher name. This parameter is used to locate problems.
223 * @param priority Indicates the priority of all tasks dispatched by the created task dispatcher.
224 *
225 * @return Returns a serial task dispatcher.
226 */
CreateSerialTaskDispatcher(const std::string & name,const TaskPriority & priority)227 std::shared_ptr<TaskDispatcher> ApplicationContext::CreateSerialTaskDispatcher(
228 const std::string &name, const TaskPriority &priority)
229 {
230 APP_LOGI("ApplicationContext::CreateSerialTaskDispatcher begin");
231 if (taskDispatcherContext_ == nullptr) {
232 std::lock_guard<std::mutex> lock_l(mutex_);
233 if (taskDispatcherContext_ == nullptr) {
234 taskDispatcherContext_ = std::make_shared<TaskDispatcherContext>();
235 APP_LOGI("ApplicationContext::CreateSerialTaskDispatcher threadpool create");
236 }
237
238 if (taskDispatcherContext_ == nullptr) {
239 APP_LOGE("ApplicationContext::CreateSerialTaskDispatcher taskDispatcherContext_ is nullptr");
240 return nullptr;
241 }
242 }
243
244 std::shared_ptr<TaskDispatcher> task = taskDispatcherContext_->CreateSerialDispatcher(name, priority);
245 APP_LOGI("ApplicationContext::CreateSerialTaskDispatcher end");
246 return task;
247 }
248
249 /**
250 * @brief Obtains a global task dispatcher with a specified priority.
251 *
252 * @param priority Indicates the priority of all tasks dispatched by the global task dispatcher.
253 *
254 * @return Returns a global task dispatcher.
255 */
GetGlobalTaskDispatcher(const TaskPriority & priority)256 std::shared_ptr<TaskDispatcher> ApplicationContext::GetGlobalTaskDispatcher(const TaskPriority &priority)
257 {
258 APP_LOGI("ApplicationContext::GetGlobalTaskDispatcher begin");
259 if (taskDispatcherContext_ == nullptr) {
260 std::lock_guard<std::mutex> lock_l(mutex_);
261 if (taskDispatcherContext_ == nullptr) {
262 taskDispatcherContext_ = std::make_shared<TaskDispatcherContext>();
263 APP_LOGI("ApplicationContext::GetGlobalTaskDispatcher threadpool create");
264 }
265
266 if (taskDispatcherContext_ == nullptr) {
267 APP_LOGE("ApplicationContext::GetGlobalTaskDispatcher taskDispatcherContext_ is nullptr");
268 return nullptr;
269 }
270 }
271
272 std::shared_ptr<TaskDispatcher> task = taskDispatcherContext_->GetGlobalTaskDispatcher(priority);
273 APP_LOGI("ApplicationContext::GetGlobalTaskDispatcher end");
274 return task;
275 }
276
277 } // namespace AppExecFwk
278 } // namespace OHOS
279