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 "ability_context.h"
17 #include "ability_distributed_connection.h"
18 #include "ability_manager_client.h"
19 #include "distributed_client.h"
20 #include "app_log_wrapper.h"
21 #include "resource_manager.h"
22 #include "bundle_constants.h"
23 #include "iservice_registry.h"
24 #include "system_ability_definition.h"
25 #include "sys_mgr_client.h"
26
27 namespace OHOS {
28 namespace AppExecFwk {
29
30 int AbilityContext::ABILITY_CONTEXT_DEFAULT_REQUEST_CODE(0);
31
32 /**
33 * @brief Starts a new ability.
34 * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method
35 * to start a specific ability. The system locates the target ability from installed abilities based on the value
36 * of the want parameter and then starts it. You can specify the ability to start using the want parameter.
37 *
38 * @param want Indicates the Want containing information about the target ability to start.
39 *
40 * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE
41 * template is started. You can define the request code to identify the results returned by abilities. The value
42 * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE
43 * template.
44 *
45 */
StartAbility(const AAFwk::Want & want,int requestCode)46 void AbilityContext::StartAbility(const AAFwk::Want &want, int requestCode)
47 {
48 APP_LOGI("%{public}s begin.", __func__);
49 APP_LOGI("AbilityContext::StartAbility called, requestCode = %{public}d", requestCode);
50
51 AppExecFwk::AbilityType type = GetAbilityInfoType();
52 if (type != AppExecFwk::AbilityType::PAGE && type != AppExecFwk::AbilityType::SERVICE) {
53 APP_LOGE("AbilityContext::StartAbility AbilityType = %{public}d", type);
54 return;
55 }
56
57 if (CheckIfOperateRemote(want)) {
58 APP_LOGI("%{public}s. Start calling GetDistributedSchedServiceProxy.", __func__);
59 std::shared_ptr<OHOS::DistributedSchedule::DistributedSchedProxy> dms = GetDistributedSchedServiceProxy();
60 APP_LOGI("%{public}s. End calling GetDistributedSchedServiceProxy.", __func__);
61 if (dms != nullptr) {
62 AppExecFwk::AbilityInfo abilityInfo;
63 APP_LOGI("AbilityContext::StartAbility. try to StartRemoteAbility");
64 want.DumpInfo(0);
65 int result = dms->StartRemoteAbility(want, abilityInfo, requestCode);
66 if (result != ERR_NONE) {
67 APP_LOGE("AbilityContext::StartAbility start remote ability failed, the result is %{public}d", result);
68 }
69 } else {
70 APP_LOGE("AbilityContext::StartAbility failed. It wants to start a remote ability, but failed to get dms.");
71 return;
72 }
73 } else {
74 APP_LOGI("%{public}s. Start calling ams->StartAbility.", __func__);
75 ErrCode err = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want, token_, requestCode);
76 APP_LOGI("%{public}s. End calling ams->StartAbility. ret=%{public}d", __func__, err);
77 if (err != ERR_OK) {
78 APP_LOGE("AbilityContext::StartAbility is failed %{public}d", err);
79 }
80 }
81 APP_LOGI("%{public}s end.", __func__);
82 }
83
84 /**
85 * @brief Starts a new ability with special ability start setting.
86 *
87 * @param want Indicates the Want containing information about the target ability to start.
88 * @param requestCode Indicates the request code returned after the ability is started. You can define the request code
89 * to identify the results returned by abilities. The value ranges from 0 to 65535.
90 * @param abilityStartSetting Indicates the special start setting used in starting ability.
91 *
92 */
StartAbility(const Want & want,int requestCode,const AbilityStartSetting & abilityStartSetting)93 void AbilityContext::StartAbility(const Want &want, int requestCode, const AbilityStartSetting &abilityStartSetting)
94 {
95 APP_LOGI("%{public}s begin.", __func__);
96 AppExecFwk::AbilityType type = GetAbilityInfoType();
97 if (AppExecFwk::AbilityType::PAGE != type && AppExecFwk::AbilityType::SERVICE != type) {
98 APP_LOGE("AbilityContext::StartAbility AbilityType = %{public}d", type);
99 return;
100 }
101
102 APP_LOGI("%{public}s. Start calling ams->StartAbility.", __func__);
103 ErrCode err =
104 AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want, abilityStartSetting, token_, requestCode);
105 APP_LOGI("%{public}s. End calling ams->StartAbility. ret=%{public}d", __func__, err);
106 if (err != ERR_OK) {
107 APP_LOGE("AbilityContext::StartAbility is failed %{public}d", err);
108 }
109
110 APP_LOGI("%{public}s end.", __func__);
111 }
112
113 /**
114 * @brief Destroys another ability you had previously started by calling Ability.startAbilityForResult
115 * (ohos.aafwk.content.Want, int, ohos.aafwk.ability.startsetting.AbilityStartSetting) with the same requestCode passed.
116 *
117 * @param requestCode Indicates the request code passed for starting the ability.
118 *
119 */
TerminateAbility(int requestCode)120 void AbilityContext::TerminateAbility(int requestCode)
121 {
122 APP_LOGI("%{public}s begin.", __func__);
123 ErrCode err = AAFwk::AbilityManagerClient::GetInstance()->TerminateAbility(token_, requestCode);
124 if (err != ERR_OK) {
125 APP_LOGE("AbilityContext::TerminateAbility is failed %{public}d", err);
126 }
127 APP_LOGI("%{public}s end.", __func__);
128 }
129
130 /**
131 * @brief Destroys the current ability.
132 *
133 */
TerminateAbility()134 void AbilityContext::TerminateAbility()
135 {
136 APP_LOGI("%{public}s begin.", __func__);
137 std::shared_ptr<AbilityInfo> info = GetAbilityInfo();
138 if (info == nullptr) {
139 APP_LOGE("AbilityContext::TerminateAbility info == nullptr");
140 return;
141 }
142
143 ErrCode err = ERR_OK;
144
145 switch (info->type) {
146 case AppExecFwk::AbilityType::PAGE:
147 APP_LOGI("%{public}s begin ams->TerminateAbility for PAGE.", __func__);
148 err = AAFwk::AbilityManagerClient::GetInstance()->TerminateAbility(token_, resultCode_, &resultWant_);
149 APP_LOGI("%{public}s end ams->TerminateAbility for PAGE, ret=%{public}d", __func__, err);
150 break;
151 case AppExecFwk::AbilityType::SERVICE:
152 APP_LOGI("%{public}s begin ams->TerminateAbility for SERVICE.", __func__);
153 err = AAFwk::AbilityManagerClient::GetInstance()->TerminateAbility(token_, -1, nullptr);
154 APP_LOGI("%{public}s end ams->TerminateAbility for SERVICE, ret=%{public}d", __func__, err);
155 break;
156 default:
157 APP_LOGE("AbilityContext::TerminateAbility info type error is %{public}d", info->type);
158 break;
159 }
160
161 if (err != ERR_OK) {
162 APP_LOGE("AbilityContext::TerminateAbility is failed %{public}d", err);
163 }
164 APP_LOGI("%{public}s end.", __func__);
165 }
166
167 /**
168 * @brief Obtains the bundle name of the ability that called the current ability.
169 * You can use the obtained bundle name to check whether the calling ability is allowed to receive the data you will
170 * send. If you did not use Ability.startAbilityForResult(ohos.aafwk.content.Want, int,
171 * ohos.aafwk.ability.startsetting.AbilityStartSetting) to start the calling ability, null is returned.
172 *
173 * @return Returns the bundle name of the calling ability; returns null if no calling ability is available.
174 */
GetCallingBundle()175 std::string AbilityContext::GetCallingBundle()
176 {
177 return callingBundleName_;
178 }
179
180 /**
181 * @brief Obtains the ohos.bundle.ElementName object of the current ability.
182 *
183 * @return Returns the ohos.bundle.ElementName object of the current ability.
184 */
GetElementName()185 std::shared_ptr<ElementName> AbilityContext::GetElementName()
186 {
187 APP_LOGI("%{public}s begin.", __func__);
188 std::shared_ptr<AbilityInfo> info = GetAbilityInfo();
189 if (info == nullptr) {
190 APP_LOGE("AbilityContext::GetElementName info == nullptr");
191 return nullptr;
192 }
193
194 std::shared_ptr<ElementName> elementName = std::make_shared<ElementName>();
195 if (elementName == nullptr) {
196 APP_LOGE("AbilityContext::GetElementName elementName == nullptr");
197 return nullptr;
198 }
199 elementName->SetAbilityName(info->name);
200 elementName->SetBundleName(info->bundleName);
201 elementName->SetDeviceID(info->deviceId);
202 APP_LOGI("%{public}s end.", __func__);
203 return elementName;
204 }
205
206 /**
207 * @brief Obtains the ElementName of the ability that called the current ability.
208 *
209 * @return Returns the ElementName of the calling ability; returns null if no calling ability is available.
210 */
GetCallingAbility()211 std::shared_ptr<ElementName> AbilityContext::GetCallingAbility()
212 {
213 APP_LOGI("%{public}s begin.", __func__);
214 std::shared_ptr<ElementName> elementName = std::make_shared<ElementName>();
215
216 if (elementName == nullptr) {
217 APP_LOGE("AbilityContext::GetElementName elementName == nullptr");
218 return nullptr;
219 }
220 elementName->SetAbilityName(callingAbilityName_);
221 elementName->SetBundleName(callingBundleName_);
222 elementName->SetDeviceID(callingDeviceId_);
223 APP_LOGI("%{public}s end.", __func__);
224 return elementName;
225 }
226
227 /**
228 * @brief Connects the current ability to an ability
229 *
230 * @param want Indicates the want containing information about the ability to connect
231 *
232 * @param conn Indicates the callback object when the target ability is connected.
233 *
234 * @return True means success and false means failure
235 */
ConnectAbility(const Want & want,const sptr<AAFwk::IAbilityConnection> & conn)236 bool AbilityContext::ConnectAbility(const Want &want, const sptr<AAFwk::IAbilityConnection> &conn)
237 {
238 APP_LOGI("%{public}s begin.", __func__);
239
240 AppExecFwk::AbilityType type = GetAbilityInfoType();
241
242 std::shared_ptr<AbilityInfo> abilityInfo = GetAbilityInfo();
243 if (abilityInfo == nullptr) {
244 APP_LOGE("AbilityContext::ConnectAbility info == nullptr");
245 return false;
246 }
247
248 if (AppExecFwk::AbilityType::PAGE != type && AppExecFwk::AbilityType::SERVICE != type) {
249 APP_LOGE("AbilityContext::ConnectAbility AbilityType = %{public}d", type);
250 return false;
251 }
252
253 ErrCode ret = ERR_OK;
254 if (want.GetOperation().GetDeviceId() == "") {
255 APP_LOGI("%{public}s begin ams->ConnectAbilityLocal", __func__);
256 ret = AAFwk::AbilityManagerClient::GetInstance()->ConnectAbility(want, conn, token_);
257 } else {
258 APP_LOGI("%{public}s begin ams->ConnectAbilityRemote", __func__);
259 auto pos = abilityConnectionMap_.find(conn);
260 if (pos != abilityConnectionMap_.end()) {
261 APP_LOGI("%{public}s begin ams->ConnectAbilityHasDistributedConnection", __func__);
262 return false;
263 } else {
264 APP_LOGI("%{public}s begin ams->ConnectAbilitySetDistributedConnection", __func__);
265 sptr<AbilityDistributedConnection> distributedConnection = new AbilityDistributedConnection(conn);
266 abilityConnectionMap_.emplace(conn, distributedConnection);
267 ret = DistributedClient::GetInstance()->ConnectRemoteAbility(want, *abilityInfo, distributedConnection);
268 }
269 }
270
271 APP_LOGI("%{public}s end ConnectAbility, ret=%{public}d", __func__, ret);
272 bool value = ((ret == ERR_OK) ? true : false);
273 if (!value) {
274 APP_LOGE("AbilityContext::ConnectAbility ErrorCode = %{public}d", ret);
275 }
276 APP_LOGI("%{public}s end.", __func__);
277 return value;
278 }
279
280 /**
281 *
282 * @param conn Indicates the IAbilityDisConnection callback object passed by disconnectAbility after the disconnection
283 * is set up. The IAbilityDisConnection object uniquely identifies a disconnection between two abilities.
284 */
DisconnectAbility(const sptr<AAFwk::IAbilityConnection> & conn)285 void AbilityContext::DisconnectAbility(const sptr<AAFwk::IAbilityConnection> &conn)
286 {
287 APP_LOGI("%{public}s begin.", __func__);
288
289 AppExecFwk::AbilityType type = GetAbilityInfoType();
290 if (AppExecFwk::AbilityType::PAGE != type && AppExecFwk::AbilityType::SERVICE != type) {
291 APP_LOGE("AbilityContext::DisconnectAbility AbilityType = %{public}d", type);
292 return;
293 }
294
295 ErrCode ret = ERR_OK;
296 auto pos = abilityConnectionMap_.find(conn);
297 if (pos != abilityConnectionMap_.end()) {
298 APP_LOGI("%{public}s begin ams->DisconnectAbilityRemote", __func__);
299 ret = DistributedClient::GetInstance()->DisconnectRemoteAbility(pos->second);
300 abilityConnectionMap_.erase(conn);
301 } else {
302 APP_LOGI("%{public}s begin ams->DisconnectAbilityLocal", __func__);
303 ret = AAFwk::AbilityManagerClient::GetInstance()->DisconnectAbility(conn);
304 }
305
306 APP_LOGI("%{public}s end ams->DisconnectAbility, ret=%{public}d", __func__, ret);
307 if (ret != ERR_OK) {
308 APP_LOGE("AbilityContext::DisconnectAbility error");
309 }
310 APP_LOGD("AbilityContext::DisconnectAbility end");
311 }
312
313 /**
314 * @brief Destroys another ability that uses the AbilityInfo.AbilityType.SERVICE template.
315 * The current ability using either the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE
316 * template can call this method to destroy another ability that uses the AbilityInfo.AbilityType.SERVICE
317 * template. The current ability itself can be destroyed by calling the terminateAbility() method.
318 *
319 * @param want Indicates the Want containing information about the ability to destroy.
320 *
321 * @return Returns true if the ability is destroyed successfully; returns false otherwise.
322 */
StopAbility(const AAFwk::Want & want)323 bool AbilityContext::StopAbility(const AAFwk::Want &want)
324 {
325 APP_LOGI("%{public}s begin.", __func__);
326 AppExecFwk::AbilityType type = GetAbilityInfoType();
327 if (AppExecFwk::AbilityType::PAGE != type && AppExecFwk::AbilityType::SERVICE != type) {
328 APP_LOGE("AbilityContext::StopAbility AbilityType = %{public}d", type);
329 return false;
330 }
331
332 APP_LOGI("%{public}s begin ams->StopServiceAbility", __func__);
333 ErrCode err = AAFwk::AbilityManagerClient::GetInstance()->StopServiceAbility(want);
334 APP_LOGI("%{public}s end ams->StopServiceAbility, ret=%{public}d", __func__, err);
335 if (err != ERR_OK) {
336 APP_LOGE("AbilityContext::StopAbility is failed %{public}d", err);
337 return false;
338 }
339
340 APP_LOGI("%{public}s end.", __func__);
341 return true;
342 }
343
GetToken()344 sptr<IRemoteObject> AbilityContext::GetToken()
345 {
346 return token_;
347 }
348
349 /**
350 * @brief Obtains information about the current application. The returned application information includes basic
351 * information such as the application name and application permissions.
352 *
353 * @return Returns the ApplicationInfo for the current application.
354 */
GetApplicationInfo() const355 std::shared_ptr<ApplicationInfo> AbilityContext::GetApplicationInfo() const
356 {
357 return ContextContainer::GetApplicationInfo();
358 }
359
360 /**
361 * @brief Obtains the application-specific cache directory on the device's internal storage. The system
362 * automatically deletes files from the cache directory if disk space is required elsewhere on the device.
363 * Older files are always deleted first.
364 *
365 * @return Returns the application-specific cache directory.
366 */
GetCacheDir()367 std::string AbilityContext::GetCacheDir()
368 {
369 return ContextContainer::GetCacheDir();
370 }
371
372 /**
373 * @brief Obtains the application-specific code-cache directory on the device's internal storage.
374 * The system will delete any files stored in this location both when your specific application is upgraded,
375 * and when the entire platform is upgraded.
376 *
377 * @return Returns the application-specific code-cache directory.
378 */
GetCodeCacheDir()379 std::string AbilityContext::GetCodeCacheDir()
380 {
381 return ContextContainer::GetCodeCacheDir();
382 }
383
384 /**
385 * @brief Obtains the local database path.
386 * If the local database path does not exist, the system creates one and returns the created path.
387 *
388 * @return Returns the local database file.
389 */
GetDatabaseDir()390 std::string AbilityContext::GetDatabaseDir()
391 {
392 return ContextContainer::GetDatabaseDir();
393 }
394
395 /**
396 * @brief Obtains the absolute path where all private data files of this application are stored.
397 *
398 * @return Returns the absolute path storing all private data files of this application.
399 */
GetDataDir()400 std::string AbilityContext::GetDataDir()
401 {
402 return ContextContainer::GetDataDir();
403 }
404
405 /**
406 * @brief Obtains the directory for storing custom data files of the application.
407 * You can use the returned File object to create and access files in this directory. The files
408 * can be accessible only by the current application.
409 *
410 * @param name Indicates the name of the directory to retrieve. This directory is created as part
411 * of your application data.
412 * @param mode Indicates the file operating mode. The value can be 0 or a combination of MODE_PRIVATE.
413 *
414 * @return Returns a File object for the requested directory.
415 */
GetDir(const std::string & name,int mode)416 std::string AbilityContext::GetDir(const std::string &name, int mode)
417 {
418 return ContextContainer::GetDir(name, mode);
419 }
420
421 /**
422 * @brief Obtains an IBundleMgr instance.
423 * You can use this instance to obtain information about the application bundle.
424 *
425 * @return Returns an IBundleMgr instance.
426 */
GetBundleManager() const427 sptr<IBundleMgr> AbilityContext::GetBundleManager() const
428 {
429 return ContextContainer::GetBundleManager();
430 }
431
432 /**
433 * @brief Obtains the path of the package containing the current ability. The returned path contains the resources,
434 * source code, and configuration files of a module.
435 *
436 * @return Returns the path of the package file.
437 */
GetBundleCodePath()438 std::string AbilityContext::GetBundleCodePath()
439 {
440 return ContextContainer::GetBundleCodePath();
441 }
442
443 /**
444 * @brief Obtains the bundle name of the current ability.
445 *
446 * @return Returns the bundle name of the current ability.
447 */
GetBundleName()448 std::string AbilityContext::GetBundleName()
449 {
450 return ContextContainer::GetBundleName();
451 }
452
453 /**
454 * @brief Obtains the path of the OHOS Ability Package (HAP} containing this ability.
455 *
456 * @return Returns the path of the HAP containing this ability.
457 */
GetBundleResourcePath()458 std::string AbilityContext::GetBundleResourcePath()
459 {
460 return ContextContainer::GetBundleResourcePath();
461 }
462
463 /**
464 * @brief Obtains the Context object of the application.
465 *
466 * @return Returns the Context object of the application.
467 */
GetApplicationContext() const468 std::shared_ptr<Context> AbilityContext::GetApplicationContext() const
469 {
470 return ContextContainer::GetApplicationContext();
471 }
472
473 /**
474 * @brief Obtains the Context object of the application.
475 *
476 * @return Returns the Context object of the application.
477 */
GetContext()478 std::shared_ptr<Context> AbilityContext::GetContext()
479 {
480 return ContextContainer::GetContext();
481 }
482
483 /**
484 * @brief Obtains an ability manager.
485 * The ability manager provides information about running processes and memory usage of an application.
486 *
487 * @return Returns an IAbilityManager instance.
488 */
GetAbilityManager()489 sptr<AAFwk::IAbilityManager> AbilityContext::GetAbilityManager()
490 {
491 return ContextContainer::GetAbilityManager();
492 }
493
494 /**
495 * Called when getting the ProcessInfo
496 *
497 * @return ProcessInfo
498 */
GetProcessInfo() const499 std::shared_ptr<ProcessInfo> AbilityContext::GetProcessInfo() const
500 {
501 return ContextContainer::GetProcessInfo();
502 }
503
504 /**
505 * @brief Obtains the type of this application.
506 *
507 * @return Returns system if this application is a system application;
508 * returns normal if it is released in official AppGallery;
509 * returns other if it is released by a third-party vendor;
510 * returns an empty string if the query fails.
511 */
GetAppType()512 std::string AbilityContext::GetAppType()
513 {
514 return ContextContainer::GetAppType();
515 }
516
517 /**
518 * @brief Obtains information about the current ability.
519 * The returned information includes the class name, bundle name, and other information about the current ability.
520 *
521 * @return Returns the AbilityInfo object for the current ability.
522 */
GetAbilityInfo()523 const std::shared_ptr<AbilityInfo> AbilityContext::GetAbilityInfo()
524 {
525 return ContextContainer::GetAbilityInfo();
526 }
527
528 /**
529 * @brief Obtains the HapModuleInfo object of the application.
530 *
531 * @return Returns the HapModuleInfo object of the application.
532 */
GetHapModuleInfo()533 std::shared_ptr<HapModuleInfo> AbilityContext::GetHapModuleInfo()
534 {
535 return ContextContainer::GetHapModuleInfo();
536 }
537
538 /**
539 * @brief Get Current Ability Type
540 *
541 * @return Current Ability Type
542 */
GetAbilityInfoType()543 AppExecFwk::AbilityType AbilityContext::GetAbilityInfoType()
544 {
545 std::shared_ptr<AbilityInfo> info = GetAbilityInfo();
546 if (info == nullptr) {
547 APP_LOGE("AbilityContext::GetAbilityInfoType info == nullptr");
548 return AppExecFwk::AbilityType::UNKNOWN;
549 }
550
551 return info->type;
552 }
553
554 /**
555 * @brief Creates a Context object for an application with the given bundle name.
556 *
557 * @param bundleName Indicates the bundle name of the application.
558 *
559 * @param flag Indicates the flag for creating a Context object. It can be 0, any of
560 * the following values, or any combination of the following values: CONTEXT_IGNORE_SECURITY,
561 * CONTEXT_INCLUDE_CODE, and CONTEXT_RESTRICTED. The value 0 indicates that there is no restriction
562 * on creating contexts for applications.
563 *
564 * @return Returns a Context object created for the specified application.
565 */
CreateBundleContext(std::string bundleName,int flag)566 std::shared_ptr<Context> AbilityContext::CreateBundleContext(std::string bundleName, int flag)
567 {
568 return ContextContainer::CreateBundleContext(bundleName, flag);
569 }
570
571 /**
572 * @brief Obtains a resource manager.
573 *
574 * @return Returns a ResourceManager object.
575 */
GetResourceManager() const576 std::shared_ptr<Global::Resource::ResourceManager> AbilityContext::GetResourceManager() const
577 {
578 APP_LOGI("%{public}s begin.", __func__);
579 std::shared_ptr<Context> appcontext = GetApplicationContext();
580 if (appcontext == nullptr) {
581 APP_LOGE("AbilityContext::GetResourceManager appcontext is nullptr");
582 return nullptr;
583 }
584
585 APP_LOGI("%{public}s begin appcontext->GetResourceManager.", __func__);
586 std::shared_ptr<Global::Resource::ResourceManager> resourceManager = appcontext->GetResourceManager();
587 APP_LOGI("%{public}s end appcontext->GetResourceManager.", __func__);
588 if (resourceManager == nullptr) {
589 APP_LOGE("AbilityContext::GetResourceManager resourceManager is nullptr");
590 return nullptr;
591 }
592 APP_LOGI("%{public}s end.", __func__);
593 return resourceManager;
594 }
595
596 /**
597 * @brief Checks whether the current process has the given permission.
598 * You need to call requestPermissionsFromUser(java.lang.std::string[],int) to request a permission only
599 * if the current process does not have the specific permission.
600 *
601 * @param permission Indicates the permission to check. This parameter cannot be null.
602 *
603 * @return Returns 0 (IBundleManager.PERMISSION_GRANTED) if the current process has the permission;
604 * returns -1 (IBundleManager.PERMISSION_DENIED) otherwise.
605 */
VerifySelfPermission(const std::string & permission)606 int AbilityContext::VerifySelfPermission(const std::string &permission)
607 {
608 APP_LOGI("%{public}s begin. permission=%{public}s", __func__, permission.c_str());
609 if (permission.empty()) {
610 APP_LOGE("VerifySelfPermission permission invalid");
611 return AppExecFwk::Constants::PERMISSION_NOT_GRANTED;
612 }
613
614 std::string bundle_name = GetBundleName();
615 if (bundle_name.empty()) {
616 APP_LOGE("VerifySelfPermission failed to get bundle name error");
617 return AppExecFwk::Constants::PERMISSION_NOT_GRANTED;
618 }
619
620 sptr<IBundleMgr> ptr = GetBundleManager();
621 if (ptr == nullptr) {
622 APP_LOGE("VerifySelfPermission failed to get bundle manager service");
623 return AppExecFwk::Constants::PERMISSION_NOT_GRANTED;
624 }
625
626 APP_LOGI("%{public}s start bms->CheckPermission. bundle_name=%{public}s", __func__, bundle_name.c_str());
627 int ret = ptr->CheckPermission(bundle_name, permission);
628 APP_LOGI("%{public}s end bms->CheckPermission, ret=%{public}d", __func__, ret);
629 APP_LOGI("%{public}s end.", __func__);
630 return ret;
631 }
632
633 /**
634 * @brief Checks whether the calling process for inter-process communication has the given permission.
635 * The calling process is not the current process.
636 *
637 * @param permission Indicates the permission to check. This parameter cannot be null.
638 *
639 * @return Returns 0 (IBundleManager.PERMISSION_GRANTED) if the calling process has the permission;
640 * returns -1 (IBundleManager.PERMISSION_DENIED) otherwise.
641 */
VerifyCallingPermission(const std::string & permission)642 int AbilityContext::VerifyCallingPermission(const std::string &permission)
643 {
644 APP_LOGI("%{public}s begin. permission=%{public}s", __func__, permission.c_str());
645 if (permission.empty()) {
646 APP_LOGE("VerifyCallingPermission permission invalid");
647 return AppExecFwk::Constants::PERMISSION_NOT_GRANTED;
648 }
649
650 std::string bundle_name = GetCallingBundle();
651 if (bundle_name.empty()) {
652 APP_LOGE("VerifyCallingPermission failed to get bundle name by uid");
653 return AppExecFwk::Constants::PERMISSION_NOT_GRANTED;
654 }
655
656 sptr<IBundleMgr> ptr = GetBundleManager();
657 if (ptr == nullptr) {
658 APP_LOGE("VerifyCallingPermission failed to get bundle manager service");
659 return AppExecFwk::Constants::PERMISSION_NOT_GRANTED;
660 }
661
662 APP_LOGI("%{public}s start bms->CheckPermission. bundle_name=%{public}s", __func__, bundle_name.c_str());
663 int ret = ptr->CheckPermission(bundle_name, permission);
664 APP_LOGI("%{public}s end bms->CheckPermission, ret=%{public}d", __func__, ret);
665 APP_LOGI("%{public}s end.", __func__);
666 return ret;
667 }
668
669 /**
670 * @brief Confirms with the permission management module to check whether a request prompt is required for granting a
671 * certain permission. You need to call the current method to check whether a prompt is required before calling
672 * requestPermissionsFromUser(java.lang.String[],int) to request a permission. If a prompt is not required, permission
673 * request will not be initiated.
674 *
675 * @param requestCode Indicates the permission to be queried. This parameter cannot be null.
676 *
677 * @return Returns true if the current application does not have the permission and the user does not turn off further
678 * requests; returns false if the current application already has the permission, the permission is rejected by the
679 * system, or the permission is denied by the user and the user has turned off further requests.
680 */
CanRequestPermission(const std::string & permission)681 bool AbilityContext::CanRequestPermission(const std::string &permission)
682 {
683 APP_LOGI("%{public}s begin. permission=%{public}s", __func__, permission.c_str());
684 if (permission.empty()) {
685 APP_LOGE("CanRequestPermission permission invalid");
686 return true;
687 }
688
689 std::string bundle_name = GetBundleName();
690 if (bundle_name.empty()) {
691 APP_LOGE("CanRequestPermission failed to get bundle name error");
692 return true;
693 }
694
695 sptr<IBundleMgr> ptr = GetBundleManager();
696 if (ptr == nullptr) {
697 APP_LOGE("CanRequestPermission failed to get bundle manager service");
698 return true;
699 }
700
701 APP_LOGI("%{public}s start bms->CanRequestPermission. bundle_name=%{public}s", __func__, bundle_name.c_str());
702 bool ret = ptr->CanRequestPermission(bundle_name, permission, 0);
703 APP_LOGI("%{public}s end bms->CanRequestPermission, ret=%{public}s", __func__, ret ? "true" : "false");
704 APP_LOGI("%{public}s end.", __func__);
705 return ret;
706 }
707
708 /**
709 * @brief When there is a remote call to check whether the remote has permission, otherwise check whether it has
710 * permission
711 *
712 * @param permissions Indicates the list of permissions to be requested. This parameter cannot be null.
713 * @return Returns 0 (IBundleManager.PERMISSION_GRANTED) if the current process has the permission;
714 * returns -1 (IBundleManager.PERMISSION_DENIED) otherwise.
715 */
VerifyCallingOrSelfPermission(const std::string & permission)716 int AbilityContext::VerifyCallingOrSelfPermission(const std::string &permission)
717 {
718 return VerifySelfPermission(permission);
719 }
720
721 /**
722 * @brief Query whether the application of the specified PID and UID has been granted a certain permission
723 *
724 * @param permissions Indicates the list of permissions to be requested. This parameter cannot be null.
725 * @param pid Process id
726 * @param uid
727 * @return Returns 0 (IBundleManager.PERMISSION_GRANTED) if the current process has the permission;
728 * returns -1 (IBundleManager.PERMISSION_DENIED) otherwise.
729 */
VerifyPermission(const std::string & permission,int pid,int uid)730 int AbilityContext::VerifyPermission(const std::string &permission, int pid, int uid)
731 {
732 APP_LOGI("%{public}s begin. permission=%{public}s, pid=%{public}d, uid=%{public}d",
733 __func__,
734 permission.c_str(),
735 pid,
736 uid);
737 if (permission.empty()) {
738 APP_LOGE("VerifyPermission permission invalid");
739 return AppExecFwk::Constants::PERMISSION_NOT_GRANTED;
740 }
741
742 sptr<IBundleMgr> ptr = GetBundleManager();
743 if (ptr == nullptr) {
744 APP_LOGE("VerifyPermission failed to get bundle manager service");
745 return AppExecFwk::Constants::PERMISSION_NOT_GRANTED;
746 }
747
748 std::string bundle_name;
749 if (!ptr->GetBundleNameForUid(uid, bundle_name)) {
750 APP_LOGE("VerifyPermission failed to get bundle name by uid");
751 return AppExecFwk::Constants::PERMISSION_NOT_GRANTED;
752 }
753
754 APP_LOGI("%{public}s start bms->CheckPermission. bundle_name=%{public}s", __func__, bundle_name.c_str());
755 int ret = ptr->CheckPermission(bundle_name, permission);
756 APP_LOGI("%{public}s end bms->CheckPermission, ret=%{public}d", __func__, ret);
757 APP_LOGI("%{public}s end.", __func__);
758 return ret;
759 }
760
GetPermissionDes(const std::string & permissionName,std::string & des)761 void AbilityContext::GetPermissionDes(const std::string &permissionName, std::string &des)
762 {
763 sptr<IBundleMgr> ptr = GetBundleManager();
764 if (ptr == nullptr) {
765 APP_LOGE("GetPermissionDes failed to get bundle manager service");
766 return;
767 }
768
769 PermissionDef permissionDef;
770 APP_LOGI("%{public}s start bms->GetPermissionDef. permissionName=%{public}s", __func__, permissionName.c_str());
771 if (ptr->GetPermissionDef(permissionName, permissionDef)) {
772 des = permissionDef.description;
773 }
774 APP_LOGI("%{public}s end bms->GetPermissionDef.", __func__);
775 }
776
777 /**
778 * @brief Requests certain permissions from the system.
779 * This method is called for permission request. This is an asynchronous method. When it is executed,
780 * the Ability.onRequestPermissionsFromUserResult(int, String[], int[]) method will be called back.
781 *
782 * @param permissions Indicates the list of permissions to be requested. This parameter cannot be null.
783 * @param requestCode Indicates the request code to be passed to the Ability.onRequestPermissionsFromUserResult(int,
784 * String[], int[]) callback method. This code cannot be a negative number.
785 */
RequestPermissionsFromUser(std::vector<std::string> & permissions,int requestCode)786 void AbilityContext::RequestPermissionsFromUser(std::vector<std::string> &permissions, int requestCode)
787 {
788 APP_LOGI("%{public}s begin.", __func__);
789 if (permissions.size() == 0) {
790 APP_LOGE("AbilityContext::RequestPermissionsFromUser permissions is empty");
791 return;
792 }
793
794 if (requestCode < 0) {
795 APP_LOGE("AbilityContext::RequestPermissionsFromUser requestCode should be >= 0");
796 return;
797 }
798
799 std::vector<std::string> permissionDes;
800 std::string des;
801 for (size_t i = 0; i < permissions.size(); i++) {
802 des = "";
803 GetPermissionDes(permissions[i], des);
804 permissionDes.push_back(des);
805 }
806
807 AAFwk::Want want;
808 want.SetElementName(OHOS_REQUEST_PERMISSION_BUNDLENAME, OHOS_REQUEST_PERMISSION_ABILITY_NAME);
809
810 want.SetParam(OHOS_REQUEST_PERMISSION_KEY, OHOS_REQUEST_PERMISSION_VALUE);
811 want.SetParam(OHOS_REQUEST_PERMISSIONS_LIST, permissions);
812 want.SetParam(OHOS_REQUEST_PERMISSIONS_DES_LIST, permissionDes);
813 want.SetParam(OHOS_REQUEST_CALLER_BUNDLERNAME, GetBundleName());
814
815 StartAbility(want, requestCode);
816 APP_LOGI("%{public}s end.", __func__);
817 }
818
819 /* @brief Deletes the specified private file associated with the application.
820 *
821 * @param fileName Indicates the name of the file to delete. The file name cannot contain path separators.
822 *
823 * @return Returns true if the file is deleted successfully; returns false otherwise.
824 */
DeleteFile(const std::string & fileName)825 bool AbilityContext::DeleteFile(const std::string &fileName)
826 {
827 return ContextContainer::DeleteFile(fileName);
828 }
829
830 /**
831 * @brief Set deviceId/bundleName/abilityName of the calling ability
832 *
833 * @param deviceId deviceId of the calling ability
834 *
835 * @param deviceId bundleName of the calling ability
836 *
837 * @param deviceId abilityName of the calling ability
838 */
SetCallingContext(const std::string & deviceId,const std::string & bundleName,const std::string & abilityName)839 void AbilityContext::SetCallingContext(
840 const std::string &deviceId, const std::string &bundleName, const std::string &abilityName)
841 {
842 callingDeviceId_ = deviceId;
843 callingBundleName_ = bundleName;
844 callingAbilityName_ = abilityName;
845 }
846
847 /**
848 * @brief Obtains information about the caller of this ability.
849 *
850 * @return Returns the caller information.
851 */
GetCaller()852 Uri AbilityContext::GetCaller()
853 {
854 return ContextContainer::GetCaller();
855 }
856
857 /**
858 * Attaches a Context object to the current ability.
859 * Generally, this method is called after Ability is loaded to provide the application context for the current
860 * ability.
861 *
862 * @param base Indicates a Context object.
863 */
AttachBaseContext(const std::shared_ptr<Context> & base)864 void AbilityContext::AttachBaseContext(const std::shared_ptr<Context> &base)
865 {
866 APP_LOGI("AbilityContext::AttachBaseContext. Start.");
867 ContextContainer::AttachBaseContext(base);
868 APP_LOGI("AbilityContext::AttachBaseContext. End.");
869 }
870
871 /**
872 * @brief Obtains the absolute path to the application-specific cache directory
873 * on the primary external or shared storage device.
874 *
875 * @return Returns the absolute path to the application-specific cache directory on the external or
876 * shared storage device; returns null if the external or shared storage device is temporarily unavailable.
877 */
GetExternalCacheDir()878 std::string AbilityContext::GetExternalCacheDir()
879 {
880 return ContextContainer::GetExternalCacheDir();
881 }
882
883 /**
884 * @brief Obtains the absolute path to the directory for storing files for the application on the
885 * primary external or shared storage device.
886 *
887 * @param type Indicates the type of the file directory to return
888 *
889 * @return Returns the absolute path to the application file directory on the external or shared storage
890 * device; returns null if the external or shared storage device is temporarily unavailable.
891 */
GetExternalFilesDir(std::string & type)892 std::string AbilityContext::GetExternalFilesDir(std::string &type)
893 {
894 return ContextContainer::GetExternalFilesDir(type);
895 }
896
897 /**
898 * @brief Obtains the directory for storing files for the application on the device's internal storage.
899 *
900 * @return Returns the application file directory.
901 */
GetFilesDir()902 std::string AbilityContext::GetFilesDir()
903 {
904 return ContextContainer::GetFilesDir();
905 }
906
907 /**
908 * @brief Obtains the absolute path which app created and will be excluded from automatic backup to remote storage.
909 * The returned path maybe changed if the application is moved to an adopted storage device.
910 *
911 * @return The path of the directory holding application files that will not be automatically backed up to remote
912 * storage.
913 */
GetNoBackupFilesDir()914 std::string AbilityContext::GetNoBackupFilesDir()
915 {
916 return ContextContainer::GetNoBackupFilesDir();
917 }
918
919 /**
920 * @brief Remove permissions for all users who have access to specific permissions
921 *
922 * @param permission Indicates the permission to unauth. This parameter cannot be null.
923 * @param uri Indicates the URI to unauth. This parameter cannot be null.
924 * @param uid Indicates the UID of the unauth to check.
925 *
926 */
UnauthUriPermission(const std::string & permission,const Uri & uri,int uid)927 void AbilityContext::UnauthUriPermission(const std::string &permission, const Uri &uri, int uid)
928 {
929 ContextContainer::UnauthUriPermission(permission, uri, uid);
930 }
931
932 /**
933 * @brief Obtains the distributed file path.
934 * If the distributed file path does not exist, the system creates one and returns the created path. This method is
935 * applicable only to the context of an ability rather than that of an application.
936 *
937 * @return Returns the distributed file.
938 */
GetDistributedDir()939 std::string AbilityContext::GetDistributedDir()
940 {
941 return ContextContainer::GetDistributedDir();
942 }
943
944 /**
945 * @brief Sets the pattern of this Context based on the specified pattern ID.
946 *
947 * @param patternId Indicates the resource ID of the pattern to set.
948 */
SetPattern(int patternId)949 void AbilityContext::SetPattern(int patternId)
950 {
951 ContextContainer::SetPattern(patternId);
952 }
953
954 /**
955 * @brief Obtains the Context object of this ability.
956 *
957 * @return Returns the Context object of this ability.
958 */
GetAbilityPackageContext()959 std::shared_ptr<Context> AbilityContext::GetAbilityPackageContext()
960 {
961 return ContextContainer::GetAbilityPackageContext();
962 }
963
964 /**
965 * @brief Obtains the name of the current process.
966 *
967 * @return Returns the current process name.
968 */
GetProcessName()969 std::string AbilityContext::GetProcessName()
970 {
971 return ContextContainer::GetProcessName();
972 }
973
974 /**
975 * @brief InitResourceManager
976 *
977 * @param bundleInfo BundleInfo
978 */
InitResourceManager(BundleInfo & bundleInfo,std::shared_ptr<ContextDeal> & deal)979 void AbilityContext::InitResourceManager(BundleInfo &bundleInfo, std::shared_ptr<ContextDeal> &deal)
980 {
981 ContextContainer::InitResourceManager(bundleInfo, deal);
982 }
983
984 /**
985 * @brief Get the string of this Context based on the specified resource ID.
986 *
987 * @param resId Indicates the resource ID of the string to get.
988 *
989 * @return Returns the string of this Context.
990 */
GetString(int resId)991 std::string AbilityContext::GetString(int resId)
992 {
993 return ContextContainer::GetString(resId);
994 }
995
996 /**
997 * @brief Get the string array of this Context based on the specified resource ID.
998 *
999 * @param resId Indicates the resource ID of the string array to get.
1000 *
1001 * @return Returns the string array of this Context.
1002 */
GetStringArray(int resId)1003 std::vector<std::string> AbilityContext::GetStringArray(int resId)
1004 {
1005 return ContextContainer::GetStringArray(resId);
1006 }
1007
1008 /**
1009 * @brief Get the integer array of this Context based on the specified resource ID.
1010 *
1011 * @param resId Indicates the resource ID of the integer array to get.
1012 *
1013 * @return Returns the integer array of this Context.
1014 */
GetIntArray(int resId)1015 std::vector<int> AbilityContext::GetIntArray(int resId)
1016 {
1017 return ContextContainer::GetIntArray(resId);
1018 }
1019
1020 /**
1021 * @brief Obtains the theme of this Context.
1022 *
1023 * @return theme Returns the theme of this Context.
1024 */
GetTheme()1025 std::map<std::string, std::string> AbilityContext::GetTheme()
1026 {
1027 return ContextContainer::GetTheme();
1028 }
1029
1030 /**
1031 * @brief Sets the theme of this Context based on the specified theme ID.
1032 *
1033 * @param themeId Indicates the resource ID of the theme to set.
1034 */
SetTheme(int themeId)1035 void AbilityContext::SetTheme(int themeId)
1036 {
1037 ContextContainer::SetTheme(themeId);
1038 }
1039
1040 /**
1041 * @brief Obtains the pattern of this Context.
1042 *
1043 * @return getPattern in interface Context
1044 */
GetPattern()1045 std::map<std::string, std::string> AbilityContext::GetPattern()
1046 {
1047 return ContextContainer::GetPattern();
1048 }
1049
1050 /**
1051 * @brief Get the color of this Context based on the specified resource ID.
1052 *
1053 * @param resId Indicates the resource ID of the color to get.
1054 *
1055 * @return Returns the color value of this Context.
1056 */
GetColor(int resId)1057 int AbilityContext::GetColor(int resId)
1058 {
1059 return ContextContainer::GetColor(resId);
1060 }
1061
1062 /**
1063 * @brief Obtains the theme id of this Context.
1064 *
1065 * @return int Returns the theme id of this Context.
1066 */
GetThemeId()1067 int AbilityContext::GetThemeId()
1068 {
1069 return ContextContainer::GetThemeId();
1070 }
1071
1072 /**
1073 * @brief
1074 * Destroys this Service ability if the number of times it has been started equals the number represented by the
1075 * given {@code startId}. This method is the same as calling {@link #terminateAbility} to destroy this Service
1076 * ability, except that this method helps you avoid destroying it if a client has requested a Service
1077 * ability startup in {@link ohos.aafwk.ability.Ability#onCommand} but you are unaware of it.
1078 *
1079 * @param startId Indicates the number of startup times of this Service ability passed to
1080 * {@link ohos.aafwk.ability.Ability#onCommand}. The {@code startId} is
1081 * incremented by 1 every time this ability is started. For example,
1082 * if this ability has been started for six times, the value of {@code startId} is {@code 6}.
1083 *
1084 * @return Returns {@code true} if the {@code startId} matches the number of startup times
1085 * and this Service ability will be destroyed; returns {@code false} otherwise.
1086 */
TerminateAbilityResult(int startId)1087 bool AbilityContext::TerminateAbilityResult(int startId)
1088 {
1089 APP_LOGI("%{public}s begin.", __func__);
1090 auto abilityClient = AAFwk::AbilityManagerClient::GetInstance();
1091 if (abilityClient == nullptr) {
1092 APP_LOGE("AbilityContext::TerminateAbilityResult abilityClient is nullptr");
1093 return false;
1094 }
1095
1096 APP_LOGI("%{public}s begin ams->TerminateAbilityResult, startId=%{public}d.", __func__, startId);
1097 ErrCode errval = abilityClient->TerminateAbilityResult(token_, startId);
1098 APP_LOGI("%{public}s end ams->TerminateAbilityResult, ret=%{public}d.", __func__, errval);
1099 if (errval != ERR_OK) {
1100 APP_LOGE("AbilityContext::TerminateAbilityResult TerminateAbilityResult retval is %d", errval);
1101 }
1102
1103 APP_LOGI("%{public}s end.", __func__);
1104 return (errval == ERR_OK) ? true : false;
1105 }
1106
1107 /**
1108 * @brief Obtains the current display orientation of this ability.
1109 *
1110 * @return Returns the current display orientation.
1111 */
GetDisplayOrientation()1112 int AbilityContext::GetDisplayOrientation()
1113 {
1114 return ContextContainer::GetDisplayOrientation();
1115 }
1116
1117 /**
1118 * @brief Obtains the path storing the preference file of the application.
1119 * If the preference file path does not exist, the system creates one and returns the created path.
1120 *
1121 * @return Returns the preference file path .
1122 */
GetPreferencesDir()1123 std::string AbilityContext::GetPreferencesDir()
1124 {
1125 return ContextContainer::GetPreferencesDir();
1126 }
1127
1128 /**
1129 * @brief Set color mode
1130 *
1131 * @param the value of color mode.
1132 */
SetColorMode(int mode)1133 void AbilityContext::SetColorMode(int mode)
1134 {
1135 ContextContainer::SetColorMode(mode);
1136 }
1137
1138 /**
1139 * @brief Obtains color mode.
1140 *
1141 * @return Returns the color mode value.
1142 */
GetColorMode()1143 int AbilityContext::GetColorMode()
1144 {
1145 return ContextContainer::GetColorMode();
1146 }
1147
1148 /**
1149 * @brief Obtains the unique ID of the mission containing this ability.
1150 *
1151 * @return Returns the unique mission ID.
1152 */
GetMissionId()1153 int AbilityContext::GetMissionId()
1154 {
1155 return ContextContainer::GetMissionId();
1156 }
1157
1158 /**
1159 * @brief Call this when your ability should be closed and the mission should be completely removed as a part of
1160 * finishing the root ability of the mission.
1161 */
TerminateAndRemoveMission()1162 void AbilityContext::TerminateAndRemoveMission()
1163 {
1164 ContextContainer::TerminateAndRemoveMission();
1165 }
1166
1167 /**
1168 * @brief Starts multiple abilities.
1169 *
1170 * @param wants Indicates the Want containing information array about the target ability to start.
1171 */
StartAbilities(const std::vector<AAFwk::Want> & wants)1172 void AbilityContext::StartAbilities(const std::vector<AAFwk::Want> &wants)
1173 {
1174 APP_LOGI("%{public}s begin.", __func__);
1175 for (auto want : wants) {
1176 StartAbility(want, ABILITY_CONTEXT_DEFAULT_REQUEST_CODE);
1177 }
1178 APP_LOGI("%{public}s end.", __func__);
1179 }
1180
1181 /**
1182 * @brief Checks whether this ability is the first ability in a mission.
1183 *
1184 * @return Returns true is first in Mission.
1185 */
IsFirstInMission()1186 bool AbilityContext::IsFirstInMission()
1187 {
1188 APP_LOGI("%{public}s begin.", __func__);
1189 auto abilityClient = AAFwk::AbilityManagerClient::GetInstance();
1190 if (abilityClient == nullptr) {
1191 APP_LOGE("AbilityContext::IsFirstInMission abilityClient is nullptr");
1192 return false;
1193 }
1194 APP_LOGI("%{public}s begin ams->IsFirstInMission.", __func__);
1195 ErrCode errval = abilityClient->IsFirstInMission(token_);
1196 APP_LOGI("%{public}s end ams->IsFirstInMission, ret=%{public}d", __func__, errval);
1197 if (errval != ERR_OK) {
1198 APP_LOGE("AbilityContext::IsFirstInMission IsFirstInMission retval is %d", errval);
1199 }
1200 APP_LOGI("%{public}s end.", __func__);
1201
1202 return (errval == ERR_OK) ? true : false;
1203 }
1204
1205 /**
1206 * @brief Check whether it wants to operate a remote ability
1207 *
1208 * @param want Indicates the Want containing information about the ability to start.
1209 *
1210 * @return return true if it wamts to operate a remote ability, ohterwise return false.
1211 */
CheckIfOperateRemote(const Want & want)1212 bool AbilityContext::CheckIfOperateRemote(const Want &want)
1213 {
1214 if (want.GetElement().GetDeviceID() != "") {
1215 return true;
1216 }
1217 return false;
1218 }
1219
1220 /**
1221 * @brief Obtains a distributedSchedService.
1222 *
1223 * @return Returns an IDistributedSched proxy.
1224 */
GetDistributedSchedServiceProxy()1225 std::shared_ptr<OHOS::DistributedSchedule::DistributedSchedProxy> AbilityContext::GetDistributedSchedServiceProxy()
1226 {
1227 APP_LOGI("%{public}s begin.", __func__);
1228 auto remoteObject = OHOS::DelayedSingleton<SysMrgClient>::GetInstance()->GetSystemAbility(DISTRIBUTED_SCHED_SA_ID);
1229 if (remoteObject == nullptr) {
1230 APP_LOGE("failed to get dms service");
1231 return nullptr;
1232 }
1233
1234 APP_LOGI("get dms proxy success.");
1235 std::shared_ptr<OHOS::DistributedSchedule::DistributedSchedProxy> proxy = nullptr;
1236 proxy = std::make_shared<OHOS::DistributedSchedule::DistributedSchedProxy>(remoteObject);
1237 APP_LOGI("%{public}s end.", __func__);
1238 return proxy;
1239 }
1240
1241 /**
1242 * @brief Obtains a task dispatcher that is bound to the UI thread.
1243 *
1244 * @return Returns the task dispatcher that is bound to the UI thread.
1245 */
GetUITaskDispatcher()1246 std::shared_ptr<TaskDispatcher> AbilityContext::GetUITaskDispatcher()
1247 {
1248 return ContextContainer::GetUITaskDispatcher();
1249 }
1250
1251 /**
1252 * @brief Obtains a task dispatcher that is bound to the application main thread.
1253 *
1254 * @return Returns the task dispatcher that is bound to the application main thread.
1255 */
GetMainTaskDispatcher()1256 std::shared_ptr<TaskDispatcher> AbilityContext::GetMainTaskDispatcher()
1257 {
1258 return ContextContainer::GetMainTaskDispatcher();
1259 }
1260 /**
1261 * @brief Creates a parallel task dispatcher with a specified priority.
1262 *
1263 * @param name Indicates the task dispatcher name. This parameter is used to locate problems.
1264 * @param priority Indicates the priority of all tasks dispatched by the parallel task dispatcher.
1265 *
1266 * @return Returns a parallel task dispatcher.
1267 */
CreateParallelTaskDispatcher(const std::string & name,const TaskPriority & priority)1268 std::shared_ptr<TaskDispatcher> AbilityContext::CreateParallelTaskDispatcher(
1269 const std::string &name, const TaskPriority &priority)
1270 {
1271 return ContextContainer::CreateParallelTaskDispatcher(name, priority);
1272 }
1273
1274 /**
1275 * @brief Creates a serial task dispatcher with a specified priority.
1276 *
1277 * @param name Indicates the task dispatcher name. This parameter is used to locate problems.
1278 * @param priority Indicates the priority of all tasks dispatched by the created task dispatcher.
1279 *
1280 * @return Returns a serial task dispatcher.
1281 */
CreateSerialTaskDispatcher(const std::string & name,const TaskPriority & priority)1282 std::shared_ptr<TaskDispatcher> AbilityContext::CreateSerialTaskDispatcher(
1283 const std::string &name, const TaskPriority &priority)
1284 {
1285 return ContextContainer::CreateSerialTaskDispatcher(name, priority);
1286 }
1287
1288 /**
1289 * @brief Obtains a global task dispatcher with a specified priority.
1290 *
1291 * @param priority Indicates the priority of all tasks dispatched by the global task dispatcher.
1292 *
1293 * @return Returns a global task dispatcher.
1294 */
GetGlobalTaskDispatcher(const TaskPriority & priority)1295 std::shared_ptr<TaskDispatcher> AbilityContext::GetGlobalTaskDispatcher(const TaskPriority &priority)
1296 {
1297 return ContextContainer::GetGlobalTaskDispatcher(priority);
1298 }
1299
1300 /**
1301 * @brief Requires that tasks associated with a given capability token be moved to the background
1302 *
1303 * @param nonFirst If nonfirst is false and not the lowest ability of the mission, you cannot move mission to end
1304 *
1305 * @return Returns true on success, others on failure.
1306 */
MoveMissionToEnd(bool nonFirst)1307 bool AbilityContext::MoveMissionToEnd(bool nonFirst)
1308 {
1309 return ContextContainer::MoveMissionToEnd(nonFirst);
1310 }
1311
1312 /**
1313 * @brief Sets the application to start its ability in lock mission mode.
1314 */
LockMission()1315 void AbilityContext::LockMission()
1316 {
1317 ContextContainer::LockMission();
1318 }
1319
1320 /**
1321 * @brief Unlocks this ability by exiting the lock mission mode.
1322 */
UnlockMission()1323 void AbilityContext::UnlockMission()
1324 {
1325 ContextContainer::UnlockMission();
1326 }
1327
1328 /**
1329 * @brief Sets description information about the mission containing this ability.
1330 *
1331 * @param MissionInformation Indicates the object containing information about the
1332 * mission. This parameter cannot be null.
1333 * @return Returns true on success, others on failure.
1334 */
SetMissionInformation(const MissionInformation & missionInformation)1335 bool AbilityContext::SetMissionInformation(const MissionInformation &missionInformation)
1336 {
1337 return ContextContainer::SetMissionInformation(missionInformation);
1338 }
1339
1340 } // namespace AppExecFwk
1341 } // namespace OHOS
1342