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