1 /*
2 * Copyright (c) 2023 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 "bundle_resource_helper.h"
17
18 #include "app_log_wrapper.h"
19 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
20 #include "account_helper.h"
21 #include "bundle_resource_callback.h"
22 #include "bundle_resource_manager.h"
23 #include "bundle_resource_param.h"
24 #include "bundle_resource_register.h"
25 #include "bundle_system_state.h"
26 #endif
27
28
29 namespace OHOS {
30 namespace AppExecFwk {
BundleSystemStateInit()31 void BundleResourceHelper::BundleSystemStateInit()
32 {
33 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
34 APP_LOGI("system state init start");
35 // init language and colorMode
36 BundleSystemState::GetInstance().SetSystemLanguage(BundleResourceParam::GetSystemLanguage());
37 BundleSystemState::GetInstance().SetSystemColorMode(BundleResourceParam::GetSystemColorMode());
38 #endif
39 }
40
RegisterConfigurationObserver()41 void BundleResourceHelper::RegisterConfigurationObserver()
42 {
43 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
44 BundleResourceRegister::RegisterConfigurationObserver();
45 #endif
46 }
47
RegisterCommonEventSubscriber()48 void BundleResourceHelper::RegisterCommonEventSubscriber()
49 {
50 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
51 BundleResourceRegister::RegisterCommonEventSubscriber();
52 #endif
53 }
54
AddResourceInfoByBundleName(const std::string & bundleName,const int32_t userId)55 void BundleResourceHelper::AddResourceInfoByBundleName(const std::string &bundleName,
56 const int32_t userId)
57 {
58 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
59 APP_LOGD("start");
60 auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
61 if (manager == nullptr) {
62 APP_LOGE("failed, manager is nullptr");
63 return;
64 }
65 // need delete current resource info
66 if (!manager->DeleteResourceInfo(bundleName)) {
67 APP_LOGW("failed, bundleName:%{public}s", bundleName.c_str());
68 }
69 int32_t currentUserId = userId;
70 // 0 and 100 exist
71 if ((userId != Constants::DEFAULT_USERID) && (userId != Constants::START_USERID)) {
72 currentUserId = AccountHelper::GetCurrentActiveUserId();
73 if (currentUserId <= 0) {
74 // invalid userId
75 currentUserId = userId;
76 }
77 }
78 // add new resource info
79 if (!manager->AddResourceInfoByBundleName(bundleName, currentUserId)) {
80 APP_LOGW("failed, bundleName:%{public}s", bundleName.c_str());
81 }
82 #endif
83 }
84
DeleteResourceInfo(const std::string & key,const int32_t userId)85 void BundleResourceHelper::DeleteResourceInfo(const std::string &key, const int32_t userId)
86 {
87 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
88 APP_LOGD("start");
89 if (userId != Constants::UNSPECIFIED_USERID) {
90 int32_t currentUserId = AccountHelper::GetCurrentActiveUserId();
91 if ((currentUserId > 0) && (userId != currentUserId)) {
92 APP_LOGW("currentUserId: %{public}d, userId: %{public}d is not same", currentUserId, userId);
93 return;
94 }
95 }
96 auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
97 if (manager == nullptr) {
98 APP_LOGE("failed, manager is nullptr");
99 return;
100 }
101 if (!manager->DeleteResourceInfo(key)) {
102 APP_LOGE("failed, key:%{public}s", key.c_str());
103 }
104 #endif
105 }
106
SetApplicationEnabled(const std::string & bundleName,bool enabled,const int32_t userId)107 void BundleResourceHelper::SetApplicationEnabled(const std::string &bundleName,
108 bool enabled, const int32_t userId)
109 {
110 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
111 APP_LOGD("bundleName: %{public}s, enable: %{public}d, userId: %{public}d", bundleName.c_str(), enabled, userId);
112 BundleResourceCallback callback;
113 callback.OnBundleStatusChanged(bundleName, enabled, userId);
114 #endif
115 }
116
SetAbilityEnabled(const std::string & bundleName,const std::string & moduleName,const std::string & abilityName,bool enabled,const int32_t userId)117 void BundleResourceHelper::SetAbilityEnabled(const std::string &bundleName, const std::string &moduleName,
118 const std::string &abilityName, bool enabled, const int32_t userId)
119 {
120 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
121 APP_LOGD("bundleName: %{public}s, abilityName: %{public}s, enable: %{public}d, userId: %{public}d",
122 bundleName.c_str(), abilityName.c_str(), enabled, userId);
123 BundleResourceCallback callback;
124 callback.OnAbilityStatusChanged(bundleName, moduleName, abilityName, enabled, userId);
125 #endif
126 }
127
GetAllBundleResourceName(std::vector<std::string> & resourceNames)128 void BundleResourceHelper::GetAllBundleResourceName(std::vector<std::string> &resourceNames)
129 {
130 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
131 APP_LOGI("start");
132 auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
133 if (manager == nullptr) {
134 APP_LOGE("failed, manager is nullptr");
135 return;
136 }
137 if (!manager->GetAllResourceName(resourceNames)) {
138 APP_LOGE("failed");
139 }
140 #endif
141 }
142 } // AppExecFwk
143 } // OHOS
144