1 /*
2 * Copyright (c) 2021-2022 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 #include "context_container.h"
16
17 #include <regex>
18
19 #include "ability_constants.h"
20 #include "ability_manager_client.h"
21 #include "ability_manager_errors.h"
22 #include "app_context.h"
23 #include "bundle_constants.h"
24 #include "hilog_wrapper.h"
25 #include "parameters.h"
26
27 namespace OHOS {
28 namespace AppExecFwk {
29 // for api7 demo special
30 constexpr int CURRENT_ACCOUNT_ID = 100;
31 /**
32 * Attaches a Context object to the current ability.
33 * Generally, this method is called after Ability is loaded to provide the application context for the current ability.
34 *
35 * @param base Indicates a Context object.
36 */
AttachBaseContext(const std::shared_ptr<Context> & base)37 void ContextContainer::AttachBaseContext(const std::shared_ptr<Context> &base)
38 {
39 if (base == nullptr) {
40 HILOG_ERROR("ContextDeal::AttachBaseContext failed, base is nullptr");
41 return;
42 }
43 baseContext_ = base;
44 }
45
DetachBaseContext()46 void ContextContainer::DetachBaseContext()
47 {
48 if (baseContext_ != nullptr) {
49 baseContext_.reset();
50 }
51 baseContext_ = nullptr;
52 }
53
54 /**
55 * Called when getting the ProcessInfo
56 *
57 * @return ProcessInfo
58 */
GetProcessInfo() const59 std::shared_ptr<ProcessInfo> ContextContainer::GetProcessInfo() const
60 {
61 if (baseContext_ != nullptr) {
62 return baseContext_->GetProcessInfo();
63 }
64 return nullptr;
65 }
66
67 /**
68 * @brief Obtains information about the current application. The returned application information includes basic
69 * information such as the application name and application permissions.
70 *
71 * @return Returns the ApplicationInfo for the current application.
72 */
GetApplicationInfo() const73 std::shared_ptr<ApplicationInfo> ContextContainer::GetApplicationInfo() const
74 {
75 if (baseContext_ != nullptr) {
76 return baseContext_->GetApplicationInfo();
77 } else {
78 HILOG_ERROR("ContextContainer::GetApplicationInfo baseContext_ is nullptr");
79 return nullptr;
80 }
81 }
82
83 /**
84 * @brief Obtains the Context object of the application.
85 *
86 * @return Returns the Context object of the application.
87 */
GetApplicationContext() const88 std::shared_ptr<Context> ContextContainer::GetApplicationContext() const
89 {
90 if (baseContext_ != nullptr) {
91 return baseContext_->GetApplicationContext();
92 } else {
93 HILOG_ERROR("ContextContainer::GetApplicationContext baseContext_ is nullptr");
94 return nullptr;
95 }
96 }
97
98 /**
99 * @brief Obtains the path of the package containing the current ability. The returned path contains the resources,
100 * source code, and configuration files of a module.
101 *
102 * @return Returns the path of the package file.
103 */
GetBundleCodePath()104 std::string ContextContainer::GetBundleCodePath()
105 {
106 if (baseContext_ != nullptr) {
107 return baseContext_->GetBundleCodePath();
108 } else {
109 HILOG_ERROR("ContextContainer::GetBundleCodePath baseContext_ is nullptr");
110 return "";
111 }
112 }
113
114 /**
115 * @brief Obtains information about the current ability.
116 * The returned information includes the class name, bundle name, and other information about the current ability.
117 *
118 * @return Returns the AbilityInfo object for the current ability.
119 */
GetAbilityInfo()120 const std::shared_ptr<AbilityInfo> ContextContainer::GetAbilityInfo()
121 {
122 if (baseContext_ != nullptr) {
123 return baseContext_->GetAbilityInfo();
124 } else {
125 HILOG_ERROR("ContextContainer::GetAbilityInfo baseContext_ is nullptr");
126 return nullptr;
127 }
128 }
129
130 /**
131 * @brief Obtains the Context object of the application.
132 *
133 * @return Returns the Context object of the application.
134 */
GetContext()135 std::shared_ptr<Context> ContextContainer::GetContext()
136 {
137 if (baseContext_ != nullptr) {
138 return baseContext_->GetContext();
139 } else {
140 HILOG_ERROR("ContextContainer::GetContext baseContext_ is nullptr");
141 return nullptr;
142 }
143 }
144
145 /**
146 * @brief Obtains an IBundleMgr instance.
147 * You can use this instance to obtain information about the application bundle.
148 *
149 * @return Returns an IBundleMgr instance.
150 */
GetBundleManager() const151 sptr<IBundleMgr> ContextContainer::GetBundleManager() const
152 {
153 if (baseContext_ != nullptr) {
154 return baseContext_->GetBundleManager();
155 } else {
156 HILOG_ERROR("ContextContainer::GetBundleManager baseContext_ is nullptr");
157 return nullptr;
158 }
159 }
160
161 /**
162 * @brief Obtains a resource manager.
163 *
164 * @return Returns a ResourceManager object.
165 */
GetResourceManager() const166 std::shared_ptr<Global::Resource::ResourceManager> ContextContainer::GetResourceManager() const
167 {
168 if (baseContext_ != nullptr) {
169 return baseContext_->GetResourceManager();
170 } else {
171 HILOG_ERROR("ContextContainer::GetResourceManager baseContext_ is nullptr");
172 return nullptr;
173 }
174 }
175
176 /**
177 * @brief Deletes the specified private file associated with the application.
178 *
179 * @param fileName Indicates the name of the file to delete. The file name cannot contain path separators.
180 *
181 * @return Returns true if the file is deleted successfully; returns false otherwise.
182 */
DeleteFile(const std::string & fileName)183 bool ContextContainer::DeleteFile(const std::string &fileName)
184 {
185 if (baseContext_ != nullptr) {
186 return baseContext_->DeleteFile(fileName);
187 } else {
188 HILOG_ERROR("ContextContainer::DeleteFile baseContext_ is nullptr");
189 return false;
190 }
191 }
192
193 /**
194 * @brief Obtains the application-specific cache directory on the device's internal storage. The system
195 * automatically deletes files from the cache directory if disk space is required elsewhere on the device.
196 * Older files are always deleted first.
197 *
198 * @return Returns the application-specific cache directory.
199 */
GetCacheDir()200 std::string ContextContainer::GetCacheDir()
201 {
202 if (baseContext_ != nullptr) {
203 return baseContext_->GetCacheDir();
204 } else {
205 HILOG_ERROR("ContextContainer::GetCacheDir baseContext_ is nullptr");
206 return "";
207 }
208 }
209
210 /**
211 * @brief Obtains the application-specific code-cache directory on the device's internal storage.
212 * The system will delete any files stored in this location both when your specific application is upgraded,
213 * and when the entire platform is upgraded.
214 *
215 * @return Returns the application-specific code-cache directory.
216 */
GetCodeCacheDir()217 std::string ContextContainer::GetCodeCacheDir()
218 {
219 if (baseContext_ != nullptr) {
220 return baseContext_->GetCodeCacheDir();
221 } else {
222 HILOG_ERROR("ContextContainer::GetCodeCacheDir baseContext_ is nullptr");
223 return "";
224 }
225 }
226
227 /**
228 * @brief Obtains the local database path.
229 * If the local database path does not exist, the system creates one and returns the created path.
230 *
231 * @return Returns the local database file.
232 */
GetDatabaseDir()233 std::string ContextContainer::GetDatabaseDir()
234 {
235 if (baseContext_ != nullptr) {
236 return baseContext_->GetDatabaseDir();
237 } else {
238 HILOG_ERROR("ContextContainer::GetDatabaseDir baseContext_ is nullptr");
239 return "";
240 }
241 }
242
243 /**
244 * @brief Obtains the absolute path where all private data files of this application are stored.
245 *
246 * @return Returns the absolute path storing all private data files of this application.
247 */
GetDataDir()248 std::string ContextContainer::GetDataDir()
249 {
250 if (baseContext_ != nullptr) {
251 return baseContext_->GetDataDir();
252 } else {
253 HILOG_ERROR("ContextContainer::GetDataDir baseContext_ is nullptr");
254 return "";
255 }
256 }
257
258 /**
259 * @brief Obtains the directory for storing custom data files of the application.
260 * You can use the returned File object to create and access files in this directory. The files
261 * can be accessible only by the current application.
262 *
263 * @param name Indicates the name of the directory to retrieve. This directory is created as part
264 * of your application data.
265 * @param mode Indicates the file operating mode. The value can be 0 or a combination of MODE_PRIVATE.
266 *
267 * @return Returns a File object for the requested directory.
268 */
GetDir(const std::string & name,int mode)269 std::string ContextContainer::GetDir(const std::string &name, int mode)
270 {
271 if (baseContext_ != nullptr) {
272 return baseContext_->GetDir(name, mode);
273 } else {
274 HILOG_ERROR("ContextContainer::GetDir baseContext_ is nullptr");
275 return "";
276 }
277 }
278
279 /**
280 * @brief Obtains the absolute path to the application-specific cache directory
281 * on the primary external or shared storage device.
282 *
283 * @return Returns the absolute path to the application-specific cache directory on the external or
284 * shared storage device; returns null if the external or shared storage device is temporarily unavailable.
285 */
GetExternalCacheDir()286 std::string ContextContainer::GetExternalCacheDir()
287 {
288 if (baseContext_ != nullptr) {
289 return baseContext_->GetExternalCacheDir();
290 } else {
291 HILOG_ERROR("ContextContainer::GetExternalCacheDir baseContext_ is nullptr");
292 return "";
293 }
294 }
295
296 /**
297 * @brief Obtains the absolute path to the directory for storing files for the application on the
298 * primary external or shared storage device.
299 *
300 * @param type Indicates the type of the file directory to return
301 *
302 * @return Returns the absolute path to the application file directory on the external or shared storage
303 * device; returns null if the external or shared storage device is temporarily unavailable.
304 */
GetExternalFilesDir(std::string & type)305 std::string ContextContainer::GetExternalFilesDir(std::string &type)
306 {
307 if (baseContext_ != nullptr) {
308 return baseContext_->GetExternalFilesDir(type);
309 } else {
310 HILOG_ERROR("ContextContainer::GetExternalFilesDir baseContext_ is nullptr");
311 return "";
312 }
313 }
314
315 /**
316 * @brief Obtains the directory for storing files for the application on the device's internal storage.
317 *
318 * @return Returns the application file directory.
319 */
GetFilesDir()320 std::string ContextContainer::GetFilesDir()
321 {
322 if (baseContext_ != nullptr) {
323 return baseContext_->GetFilesDir();
324 } else {
325 HILOG_ERROR("ContextContainer::GetFilesDir baseContext_ is nullptr");
326 return "";
327 }
328 }
329
330 /**
331 * @brief Obtains the absolute path which app created and will be excluded from automatic backup to remote storage.
332 * The returned path maybe changed if the application is moved to an adopted storage device.
333 *
334 * @return The path of the directory holding application files that will not be automatically backed up to remote
335 * storage.
336 */
GetNoBackupFilesDir()337 std::string ContextContainer::GetNoBackupFilesDir()
338 {
339 if (baseContext_ != nullptr) {
340 return baseContext_->GetNoBackupFilesDir();
341 } else {
342 HILOG_ERROR("ContextContainer::GetNoBackupFilesDir baseContext_ is nullptr");
343 return "";
344 }
345 }
346
347 /**
348 * @brief Checks whether the current process has the given permission.
349 * You need to call requestPermissionsFromUser(std::vector<std::string>,std::vector<int>, int) to request a permission
350 * only if the current process does not have the specific permission.
351 *
352 * @param permission Indicates the permission to check. This parameter cannot be null.
353 *
354 * @return Returns 0 (IBundleManager.PERMISSION_GRANTED) if the current process has the permission;
355 * returns -1 (IBundleManager.PERMISSION_DENIED) otherwise.
356 */
VerifySelfPermission(const std::string & permission)357 int ContextContainer::VerifySelfPermission(const std::string &permission)
358 {
359 if (baseContext_ != nullptr) {
360 return baseContext_->VerifySelfPermission(permission);
361 } else {
362 HILOG_ERROR("ContextContainer::VerifySelfPermission baseContext_ is nullptr");
363 return AppExecFwk::Constants::PERMISSION_NOT_GRANTED;
364 }
365 }
366
367 /**
368 * @brief Obtains the bundle name of the current ability.
369 *
370 * @return Returns the bundle name of the current ability.
371 */
GetBundleName() const372 std::string ContextContainer::GetBundleName() const
373 {
374 if (baseContext_ != nullptr) {
375 return baseContext_->GetBundleName();
376 } else {
377 HILOG_ERROR("ContextContainer::GetBundleName baseContext_ is nullptr");
378 return "";
379 }
380 }
381
382 /**
383 * @brief Obtains the path of the OHOS Ability Package (HAP} containing this ability.
384 *
385 * @return Returns the path of the HAP containing this ability.
386 */
GetBundleResourcePath()387 std::string ContextContainer::GetBundleResourcePath()
388 {
389 if (baseContext_ != nullptr) {
390 return baseContext_->GetBundleResourcePath();
391 } else {
392 HILOG_ERROR("ContextContainer::GetBundleResourcePath baseContext_ is nullptr");
393 return "";
394 }
395 }
396
397 /**
398 * @brief Remove permissions for all users who have access to specific permissions
399 *
400 * @param permission Indicates the permission to unauth. This parameter cannot be null.
401 * @param uri Indicates the URI to unauth. This parameter cannot be null.
402 * @param uid Indicates the UID of the unauth to check.
403 *
404 */
UnauthUriPermission(const std::string & permission,const Uri & uri,int uid)405 void ContextContainer::UnauthUriPermission(const std::string &permission, const Uri &uri, int uid)
406 {
407 if (baseContext_ != nullptr) {
408 baseContext_->UnauthUriPermission(permission, uri, uid);
409 } else {
410 HILOG_ERROR("ContextContainer::UnauthUriPermission baseContext_ is nullptr");
411 }
412 }
413
414 /**
415 * @brief Obtains an ability manager.
416 * The ability manager provides information about running processes and memory usage of an application.
417 *
418 * @return Returns an IAbilityManager instance.
419 */
GetAbilityManager()420 sptr<AAFwk::IAbilityManager> ContextContainer::GetAbilityManager()
421 {
422 if (baseContext_ != nullptr) {
423 return baseContext_->GetAbilityManager();
424 } else {
425 HILOG_ERROR("ContextContainer::GetAbilityManager baseContext_ is nullptr");
426 return nullptr;
427 }
428 }
429
430 /**
431 * @brief Obtains the type of this application.
432 *
433 * @return Returns system if this application is a system application;
434 * returns normal if it is released in OHOS AppGallery;
435 * returns other if it is released by a third-party vendor;
436 * returns an empty string if the query fails.
437 */
GetAppType()438 std::string ContextContainer::GetAppType()
439 {
440 if (baseContext_ != nullptr) {
441 return baseContext_->GetAppType();
442 } else {
443 HILOG_ERROR("ContextContainer::GetAppType baseContext_ is nullptr");
444 return "";
445 }
446 }
447
448 /**
449 * @brief Query whether the application of the specified PID and UID has been granted a certain permission
450 *
451 * @param permissions Indicates the list of permissions to be requested. This parameter cannot be null.
452 * @param pid Process id
453 * @param uid
454 * @return Returns 0 (IBundleManager.PERMISSION_GRANTED) if the current process has the permission;
455 * returns -1 (IBundleManager.PERMISSION_DENIED) otherwise.
456 */
VerifyPermission(const std::string & permission,int pid,int uid)457 int ContextContainer::VerifyPermission(const std::string &permission, int pid, int uid)
458 {
459 if (baseContext_ != nullptr) {
460 return baseContext_->VerifyPermission(permission, pid, uid);
461 } else {
462 HILOG_ERROR("ContextContainer::VerifyPermission baseContext_ is nullptr");
463 return AppExecFwk::Constants::PERMISSION_NOT_GRANTED;
464 }
465 }
466
467 /**
468 * @brief Obtains the distributed file path.
469 * If the distributed file path does not exist, the system creates one and returns the created path. This method is
470 * applicable only to the context of an ability rather than that of an application.
471 *
472 * @return Returns the distributed file.
473 */
GetDistributedDir()474 std::string ContextContainer::GetDistributedDir()
475 {
476 if (baseContext_ != nullptr) {
477 return baseContext_->GetDistributedDir();
478 } else {
479 HILOG_ERROR("ContextContainer::GetDistributedDir baseContext_ is nullptr");
480 return "";
481 }
482 }
483 /**
484 * @brief Sets the pattern of this Context based on the specified pattern ID.
485 *
486 * @param patternId Indicates the resource ID of the pattern to set.
487 */
SetPattern(int patternId)488 void ContextContainer::SetPattern(int patternId)
489 {
490 if (baseContext_ != nullptr) {
491 baseContext_->SetPattern(patternId);
492 } else {
493 HILOG_ERROR("ContextContainer::SetPattern baseContext_ is nullptr");
494 }
495 }
496
497 /**
498 * @brief Obtains the Context object of this ability.
499 *
500 * @return Returns the Context object of this ability.
501 */
GetAbilityPackageContext()502 std::shared_ptr<Context> ContextContainer::GetAbilityPackageContext()
503 {
504 if (baseContext_ != nullptr) {
505 return baseContext_->GetAbilityPackageContext();
506 } else {
507 HILOG_ERROR("ContextContainer::GetAbilityPackageContext baseContext_ is nullptr");
508 return nullptr;
509 }
510 }
511
512 /**
513 * @brief Obtains the HapModuleInfo object of the application.
514 *
515 * @return Returns the HapModuleInfo object of the application.
516 */
GetHapModuleInfo()517 std::shared_ptr<HapModuleInfo> ContextContainer::GetHapModuleInfo()
518 {
519 if (baseContext_ != nullptr) {
520 return baseContext_->GetHapModuleInfo();
521 } else {
522 HILOG_ERROR("ContextContainer::GetHapModuleInfo baseContext_ is nullptr");
523 return nullptr;
524 }
525 }
526
527 /**
528 * @brief Obtains the name of the current process.
529 *
530 * @return Returns the current process name.
531 */
GetProcessName()532 std::string ContextContainer::GetProcessName()
533 {
534 if (baseContext_ != nullptr) {
535 return baseContext_->GetProcessName();
536 } else {
537 HILOG_ERROR("ContextContainer::GetProcessName baseContext_ is nullptr");
538 return "";
539 }
540 }
541
542 /**
543 * @brief Requests certain permissions from the system.
544 * This method is called for permission request. This is an asynchronous method. When it is executed,
545 * the task will be called back.
546 *
547 * @param permissions Indicates the list of permissions to be requested. This parameter cannot be null.
548 * @param permissionsState Indicates the list of permissions' state to be requested. This parameter cannot be null.
549 * @param task The callback or promise fo js interface.
550 */
RequestPermissionsFromUser(std::vector<std::string> & permissions,std::vector<int> & permissionsState,PermissionRequestTask && task)551 void ContextContainer::RequestPermissionsFromUser(std::vector<std::string> &permissions,
552 std::vector<int> &permissionsState, PermissionRequestTask &&task)
553 {
554 if (baseContext_ != nullptr) {
555 baseContext_->RequestPermissionsFromUser(permissions, permissionsState, std::move(task));
556 } else {
557 HILOG_ERROR("ContextContainer::RequestPermissionsFromUser baseContext_ is nullptr");
558 }
559 }
560
CreateBundleContext(std::string bundleName,int flag,int accountId)561 std::shared_ptr<Context> ContextContainer::CreateBundleContext(std::string bundleName, int flag, int accountId)
562 {
563 if (bundleName.empty()) {
564 HILOG_ERROR("ContextContainer::CreateBundleContext bundleName is empty");
565 return nullptr;
566 }
567
568 if (strcmp(bundleName.c_str(), GetBundleName().c_str()) == 0) {
569 return GetApplicationContext();
570 }
571
572 sptr<IBundleMgr> bundleMgr = GetBundleManager();
573 if (bundleMgr == nullptr) {
574 HILOG_ERROR("ContextContainer::CreateBundleContext GetBundleManager is nullptr");
575 return nullptr;
576 }
577
578 BundleInfo bundleInfo;
579 HILOG_INFO("CreateBundleContext length: %{public}zu, bundleName: %{public}s, accountId is %{public}d",
580 bundleName.length(),
581 bundleName.c_str(),
582 accountId);
583 int realAccountId = CURRENT_ACCOUNT_ID;
584 if (accountId != DEFAULT_ACCOUNT_ID) {
585 realAccountId = accountId;
586 }
587 bundleMgr->GetBundleInfo(bundleName, BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo, realAccountId);
588
589 if (bundleInfo.name.empty() || bundleInfo.applicationInfo.name.empty()) {
590 HILOG_ERROR("ContextContainer::CreateBundleContext GetBundleInfo is error");
591 return nullptr;
592 }
593
594 std::shared_ptr<AppContext> appContext = std::make_shared<AppContext>();
595 std::shared_ptr<ContextDeal> deal = std::make_shared<ContextDeal>(true);
596
597 // init resourceManager.
598 InitResourceManager(bundleInfo, deal);
599
600 deal->SetApplicationInfo(std::make_shared<ApplicationInfo>(bundleInfo.applicationInfo));
601 appContext->AttachBaseContext(deal);
602 return appContext;
603 }
604
InitResourceManager(BundleInfo & bundleInfo,std::shared_ptr<ContextDeal> & deal)605 void ContextContainer::InitResourceManager(BundleInfo &bundleInfo, std::shared_ptr<ContextDeal> &deal)
606 {
607 std::shared_ptr<Global::Resource::ResourceManager> resourceManager(Global::Resource::CreateResourceManager());
608 if (deal == nullptr || resourceManager == nullptr) {
609 HILOG_ERROR("ContextContainer::InitResourceManager create resourceManager failed");
610 return;
611 }
612
613 HILOG_DEBUG(
614 "ContextContainer::InitResourceManager hapModuleInfos count: %{public}zu", bundleInfo.hapModuleInfos.size());
615 std::regex pattern(AbilityRuntime::Constants::ABS_CODE_PATH);
616 for (auto hapModuleInfo : bundleInfo.hapModuleInfos) {
617 std::string loadPath;
618 if (system::GetBoolParameter(AbilityRuntime::Constants::COMPRESS_PROPERTY, false) &&
619 !hapModuleInfo.hapPath.empty()) {
620 loadPath = hapModuleInfo.hapPath;
621 } else {
622 loadPath = hapModuleInfo.resourcePath;
623 }
624 if (loadPath.empty()) {
625 continue;
626 }
627 loadPath = std::regex_replace(loadPath, pattern, AbilityRuntime::Constants::LOCAL_BUNDLES);
628 HILOG_DEBUG("ContextContainer::InitResourceManager loadPath: %{public}s", loadPath.c_str());
629 if (!resourceManager->AddResource(loadPath.c_str())) {
630 HILOG_ERROR("ContextContainer::InitResourceManager AddResource failed");
631 }
632 }
633
634 std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
635 resConfig->SetLocaleInfo("zh", "Hans", "CN");
636 #ifdef SUPPORT_GRAPHICS
637 if (resConfig->GetLocaleInfo() != nullptr) {
638 HILOG_INFO(
639 "ContextContainer::InitResourceManager language: %{public}s, script: %{public}s, region: %{public}s,",
640 resConfig->GetLocaleInfo()->getLanguage(),
641 resConfig->GetLocaleInfo()->getScript(),
642 resConfig->GetLocaleInfo()->getCountry());
643 } else {
644 HILOG_INFO("ContextContainer::InitResourceManager language: GetLocaleInfo is null.");
645 }
646 #endif
647 resourceManager->UpdateResConfig(*resConfig);
648 deal->initResourceManager(resourceManager);
649 }
650 /**
651 * @brief Obtains information about the caller of this ability.
652 *
653 * @return Returns the caller information.
654 */
GetCaller()655 Uri ContextContainer::GetCaller()
656 {
657 if (baseContext_ != nullptr) {
658 return baseContext_->GetCaller();
659 } else {
660 HILOG_ERROR("ContextContainer::GetCaller baseContext_ is nullptr");
661 Uri uri("");
662 return uri;
663 }
664 }
665
666 /**
667 * @brief Get the string of this Context based on the specified resource ID.
668 *
669 * @param resId Indicates the resource ID of the string to get.
670 *
671 * @return Returns the string of this Context.
672 */
GetString(int resId)673 std::string ContextContainer::GetString(int resId)
674 {
675 if (baseContext_ != nullptr) {
676 std::string ret = baseContext_->GetString(resId);
677 return ret;
678 } else {
679 HILOG_ERROR("ContextContainer::GetString baseContext_ is nullptr");
680 return "";
681 }
682 }
683
684 /**
685 * @brief Get the string array of this Context based on the specified resource ID.
686 *
687 * @param resId Indicates the resource ID of the string array to get.
688 *
689 * @return Returns the string array of this Context.
690 */
GetStringArray(int resId)691 std::vector<std::string> ContextContainer::GetStringArray(int resId)
692 {
693 if (baseContext_ != nullptr) {
694 return baseContext_->GetStringArray(resId);
695 } else {
696 HILOG_ERROR("ContextContainer::GetStringArray baseContext_ is nullptr");
697 return std::vector<std::string>();
698 }
699 }
700
701 /**
702 * @brief Get the integer array of this Context based on the specified resource ID.
703 *
704 * @param resId Indicates the resource ID of the integer array to get.
705 *
706 * @return Returns the integer array of this Context.
707 */
GetIntArray(int resId)708 std::vector<int> ContextContainer::GetIntArray(int resId)
709 {
710 if (baseContext_ != nullptr) {
711 return baseContext_->GetIntArray(resId);
712 } else {
713 HILOG_ERROR("ContextContainer::GetIntArray baseContext_ is nullptr");
714 return std::vector<int>();
715 }
716 }
717
718 /**
719 * @brief Obtains the theme of this Context.
720 *
721 * @return theme Returns the theme of this Context.
722 */
GetTheme()723 std::map<std::string, std::string> ContextContainer::GetTheme()
724 {
725 if (baseContext_ != nullptr) {
726 return baseContext_->GetTheme();
727 } else {
728 HILOG_ERROR("ContextContainer::GetTheme baseContext_ is nullptr");
729 return std::map<std::string, std::string>();
730 }
731 }
732
733 /**
734 * @brief Sets the theme of this Context based on the specified theme ID.
735 *
736 * @param themeId Indicates the resource ID of the theme to set.
737 */
SetTheme(int themeId)738 void ContextContainer::SetTheme(int themeId)
739 {
740 if (baseContext_ != nullptr) {
741 baseContext_->SetTheme(themeId);
742 } else {
743 HILOG_ERROR("ContextContainer::SetTheme baseContext_ is nullptr");
744 }
745 }
746
747 /**
748 * @brief Obtains the pattern of this Context.
749 *
750 * @return getPattern in interface Context
751 */
GetPattern()752 std::map<std::string, std::string> ContextContainer::GetPattern()
753 {
754 if (baseContext_ != nullptr) {
755 return baseContext_->GetPattern();
756 } else {
757 HILOG_ERROR("ContextContainer::GetPattern baseContext_ is nullptr");
758 return std::map<std::string, std::string>();
759 }
760 }
761
762 /**
763 * @brief Get the color of this Context based on the specified resource ID.
764 *
765 * @param resId Indicates the resource ID of the color to get.
766 *
767 * @return Returns the color value of this Context.
768 */
GetColor(int resId)769 int ContextContainer::GetColor(int resId)
770 {
771 if (baseContext_ != nullptr) {
772 return baseContext_->GetColor(resId);
773 } else {
774 HILOG_ERROR("ContextContainer::GetColor baseContext_ is nullptr");
775 return INVALID_RESOURCE_VALUE;
776 }
777 }
778
779 /**
780 * @brief Obtains the theme id of this Context.
781 *
782 * @return int Returns the theme id of this Context.
783 */
GetThemeId()784 int ContextContainer::GetThemeId()
785 {
786 if (baseContext_ != nullptr) {
787 return baseContext_->GetThemeId();
788 } else {
789 HILOG_ERROR("ContextContainer::GetThemeId baseContext_ is nullptr");
790 return -1;
791 }
792 }
793
794 /**
795 * @brief Obtains the current display orientation of this ability.
796 *
797 * @return Returns the current display orientation.
798 */
GetDisplayOrientation()799 int ContextContainer::GetDisplayOrientation()
800 {
801 if (baseContext_ != nullptr) {
802 return baseContext_->GetDisplayOrientation();
803 } else {
804 HILOG_ERROR("ContextContainer::GetDisplayOrientation baseContext_ is nullptr");
805 return static_cast<int>(DisplayOrientation::UNSPECIFIED);
806 }
807 }
808
809 /**
810 * @brief Obtains the path storing the preference file of the application.
811 * If the preference file path does not exist, the system creates one and returns the created path.
812 *
813 * @return Returns the preference file path .
814 */
GetPreferencesDir()815 std::string ContextContainer::GetPreferencesDir()
816 {
817 if (baseContext_ != nullptr) {
818 return baseContext_->GetPreferencesDir();
819 } else {
820 HILOG_ERROR("ContextContainer::GetPreferencesDir baseContext_ is nullptr");
821 return "";
822 }
823 }
824
825 /**
826 * @brief Set color mode
827 *
828 * @param the value of color mode.
829 */
SetColorMode(int mode)830 void ContextContainer::SetColorMode(int mode)
831 {
832 if (baseContext_ == nullptr) {
833 HILOG_ERROR("ContextContainer::SetColorMode baseContext_ is nullptr");
834 return;
835 }
836
837 baseContext_->SetColorMode(mode);
838 }
839
840 /**
841 * @brief Obtains color mode.
842 *
843 * @return Returns the color mode value.
844 */
GetColorMode()845 int ContextContainer::GetColorMode()
846 {
847 if (baseContext_ == nullptr) {
848 HILOG_ERROR("ContextContainer::GetColorMode baseContext_ is nullptr");
849 return -1;
850 }
851
852 return baseContext_->GetColorMode();
853 }
854
855 /**
856 * @brief Obtains the unique ID of the mission containing this ability.
857 *
858 * @return Returns the unique mission ID.
859 */
GetMissionId()860 int ContextContainer::GetMissionId()
861 {
862 if (baseContext_ != nullptr) {
863 return baseContext_->GetMissionId();
864 } else {
865 HILOG_ERROR("ContextContainer::GetMissionId baseContext_ is nullptr");
866 return -1;
867 }
868 }
869
IsUpdatingConfigurations()870 bool ContextContainer::IsUpdatingConfigurations()
871 {
872 if (baseContext_ != nullptr) {
873 return baseContext_->IsUpdatingConfigurations();
874 }
875 return false;
876 }
877
PrintDrawnCompleted()878 bool ContextContainer::PrintDrawnCompleted()
879 {
880 if (baseContext_ != nullptr) {
881 return baseContext_->PrintDrawnCompleted();
882 }
883 return false;
884 }
885 } // namespace AppExecFwk
886 } // namespace OHOS
887