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 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
19 #include "account_helper.h"
20 #include "bundle_resource_callback.h"
21 #include "bundle_resource_manager.h"
22 #include "bundle_resource_param.h"
23 #include "bundle_resource_parser.h"
24 #include "bundle_resource_register.h"
25 #include "bundle_system_state.h"
26 #include "resource_manager.h"
27 #endif
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 APP_LOGI("current system state: %{public}s", BundleSystemState::GetInstance().ToString().c_str());
39 #endif
40 }
41
RegisterConfigurationObserver()42 void BundleResourceHelper::RegisterConfigurationObserver()
43 {
44 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
45 BundleResourceRegister::RegisterConfigurationObserver();
46 #endif
47 }
48
RegisterCommonEventSubscriber()49 void BundleResourceHelper::RegisterCommonEventSubscriber()
50 {
51 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
52 BundleResourceRegister::RegisterCommonEventSubscriber();
53 #endif
54 }
55
AddResourceInfoByBundleName(const std::string & bundleName,const int32_t userId)56 void BundleResourceHelper::AddResourceInfoByBundleName(const std::string &bundleName,
57 const int32_t userId)
58 {
59 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
60 APP_LOGI_NOFUNC("-n %{public}s -u %{public}d add resource start", bundleName.c_str(), userId);
61 auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
62 if (manager == nullptr) {
63 APP_LOGE("failed, manager is nullptr");
64 return;
65 }
66 // add new resource info
67 if (!manager->AddResourceInfoByBundleName(bundleName, userId)) {
68 APP_LOGW("failed, bundleName:%{public}s", bundleName.c_str());
69 }
70 APP_LOGI_NOFUNC("-n %{public}s -u %{public}d add resource end", bundleName.c_str(), userId);
71 #endif
72 }
73
DeleteResourceInfo(const std::string & key,const int32_t userId)74 bool BundleResourceHelper::DeleteResourceInfo(const std::string &key, const int32_t userId)
75 {
76 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
77 APP_LOGI("start delete key %{public}s userId:%{public}d", key.c_str(), userId);
78 auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
79 if (manager == nullptr) {
80 APP_LOGE("failed, manager is nullptr");
81 return false;
82 }
83
84 if (!manager->DeleteResourceInfo(key)) {
85 APP_LOGE("failed, key:%{public}s", key.c_str());
86 return false;
87 }
88
89 return true;
90 #else
91 return false;
92 #endif
93 }
94
GetAllBundleResourceName(std::vector<std::string> & resourceNames)95 void BundleResourceHelper::GetAllBundleResourceName(std::vector<std::string> &resourceNames)
96 {
97 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
98 APP_LOGI("start");
99 auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
100 if (manager == nullptr) {
101 APP_LOGE("failed, manager is nullptr");
102 return;
103 }
104 if (!manager->GetAllResourceName(resourceNames)) {
105 APP_LOGE("failed");
106 }
107 #endif
108 }
109
ParseBundleName(const std::string & keyName)110 std::string BundleResourceHelper::ParseBundleName(const std::string &keyName)
111 {
112 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
113 ResourceInfo info;
114 info.ParseKey(keyName);
115 return info.bundleName_;
116 #else
117 return keyName;
118 #endif
119 }
120
SetOverlayEnabled(const std::string & bundleName,const std::string & moduleName,bool isEnabled,int32_t userId)121 void BundleResourceHelper::SetOverlayEnabled(const std::string &bundleName, const std::string &moduleName,
122 bool isEnabled, int32_t userId)
123 {
124 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
125 APP_LOGD("bundleName: %{public}s, moduleName: %{public}s,isEnabled: %{public}d, userId: %{public}d",
126 bundleName.c_str(), moduleName.c_str(), isEnabled, userId);
127 BundleResourceCallback callback;
128 callback.OnOverlayStatusChanged(bundleName, isEnabled, userId);
129 #endif
130 }
131
DeleteAllResourceInfo()132 bool BundleResourceHelper::DeleteAllResourceInfo()
133 {
134 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
135 auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
136 if (manager == nullptr) {
137 APP_LOGE("failed, manager is nullptr");
138 return false;
139 }
140 if (!manager->DeleteAllResourceInfo()) {
141 APP_LOGE("delete all bundle resource failed");
142 return false;
143 }
144 return true;
145 #else
146 return false;
147 #endif
148 }
149
AddCloneBundleResourceInfo(const std::string & bundleName,const int32_t appIndex,const int32_t userId)150 bool BundleResourceHelper::AddCloneBundleResourceInfo(const std::string &bundleName,
151 const int32_t appIndex, const int32_t userId)
152 {
153 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
154 APP_LOGI("start add clone bundle:%{public}s appIndex:%{public}d userId:%{public}d",
155 bundleName.c_str(), appIndex, userId);
156 auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
157 if (manager == nullptr) {
158 APP_LOGE("failed, manager is nullptr");
159 return false;
160 }
161 if (!manager->AddCloneBundleResourceInfo(bundleName, appIndex)) {
162 APP_LOGE("add clone bundle resource failed, bundleName:%{public}s, appIndex:%{public}d",
163 bundleName.c_str(), appIndex);
164 return false;
165 }
166 APP_LOGI("end add clone bundle:%{public}s appIndex:%{public}d userId:%{public}d",
167 bundleName.c_str(), appIndex, userId);
168 return true;
169 #else
170 APP_LOGI("bundle resource is not support");
171 return true;
172 #endif
173 }
174
DeleteCloneBundleResourceInfo(const std::string & bundleName,const int32_t appIndex,const int32_t userId)175 bool BundleResourceHelper::DeleteCloneBundleResourceInfo(const std::string &bundleName,
176 const int32_t appIndex, const int32_t userId)
177 {
178 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
179 APP_LOGI("start delete clone bundle:%{public}s appIndex:%{public}d userId:%{public}d",
180 bundleName.c_str(), appIndex, userId);
181 auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
182 if (manager == nullptr) {
183 APP_LOGE("failed, manager is nullptr");
184 return false;
185 }
186
187 if (!manager->DeleteCloneBundleResourceInfo(bundleName, appIndex)) {
188 APP_LOGE("failed, key:%{public}s appIndex:%{public}d", bundleName.c_str(), appIndex);
189 return false;
190 }
191
192 return true;
193 #else
194 return false;
195 #endif
196 }
197
DeleteNotExistResourceInfo()198 void BundleResourceHelper::DeleteNotExistResourceInfo()
199 {
200 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
201 APP_LOGI_NOFUNC("start delete not exist resource");
202 auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
203 if (manager == nullptr) {
204 APP_LOGE("failed, manager is nullptr");
205 return;
206 }
207
208 if (!manager->DeleteNotExistResourceInfo()) {
209 APP_LOGE("delete not exist resource failed");
210 return;
211 }
212 #endif
213 }
214 } // AppExecFwk
215 } // OHOS
216