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_callback.h"
17
18 #include "account_helper.h"
19 #include "bundle_constants.h"
20 #include "bundle_resource_manager.h"
21
22 #ifdef GLOBAL_RESMGR_ENABLE
23 #include "resource_manager.h"
24 #endif
25 namespace OHOS {
26 namespace AppExecFwk {
OnUserIdSwitched(const int32_t oldUserId,const int32_t userId,const uint32_t type)27 bool BundleResourceCallback::OnUserIdSwitched(const int32_t oldUserId, const int32_t userId, const uint32_t type)
28 {
29 APP_LOGI("start, oldUserId:%{public}d to newUserId:%{public}d", oldUserId, userId);
30 if (userId != Constants::START_USERID) {
31 int32_t currentUserId = AccountHelper::GetCurrentActiveUserId();
32 if (currentUserId != userId) {
33 APP_LOGE("userId:%{public}d current:%{public}d not same", userId, currentUserId);
34 return false;
35 }
36 }
37 auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
38 if (manager == nullptr) {
39 APP_LOGE("switch userId %{public}d failed", userId);
40 return false;
41 }
42 if (!manager->AddAllResourceInfo(userId, type, oldUserId)) {
43 APP_LOGE("AddAllResourceInfo userId : %{public}d failed", userId);
44 return false;
45 }
46 APP_LOGI("end, oldUserId:%{public}d to newUserId:%{public}d", oldUserId, userId);
47 return true;
48 }
49
OnSystemColorModeChanged(const std::string & colorMode,const uint32_t type)50 bool BundleResourceCallback::OnSystemColorModeChanged(const std::string &colorMode, const uint32_t type)
51 {
52 APP_LOGI("start, colorMode: %{public}s", colorMode.c_str());
53 if (colorMode == BundleSystemState::GetInstance().GetSystemColorMode()) {
54 APP_LOGD("colorMode: %{public}s no change", colorMode.c_str());
55 return true;
56 }
57 APP_LOGI("end, colorMode: %{public}s", colorMode.c_str());
58 return true;
59 }
60
OnSystemLanguageChange(const std::string & language,const uint32_t type)61 bool BundleResourceCallback::OnSystemLanguageChange(const std::string &language, const uint32_t type)
62 {
63 APP_LOGI("start, language %{public}s", language.c_str());
64 if (language == BundleSystemState::GetInstance().GetSystemLanguage()) {
65 APP_LOGD("current language is %{public}s no change", language.c_str());
66 return true;
67 }
68 BundleSystemState::GetInstance().SetSystemLanguage(language);
69 // need delete all and reload all
70 auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
71 if (manager == nullptr) {
72 APP_LOGE("manager is nullptr");
73 return false;
74 }
75
76 int32_t currentUserId = AccountHelper::GetCurrentActiveUserId();
77 if (currentUserId <= 0) {
78 currentUserId = Constants::START_USERID;
79 }
80
81 if (!manager->AddAllResourceInfo(currentUserId, type)) {
82 APP_LOGE("AddAllResourceInfo currentUserId %{public}d failed", currentUserId);
83 return false;
84 }
85 APP_LOGI("end, language %{public}s", language.c_str());
86 return true;
87 }
88
OnApplicationThemeChanged(const std::string & theme,const int32_t themeId,const uint32_t type)89 bool BundleResourceCallback::OnApplicationThemeChanged(const std::string &theme,
90 const int32_t themeId, const uint32_t type)
91 {
92 APP_LOGI("start, theme:%{public}s, themeId:%{public}d", theme.c_str(), themeId);
93 if (theme.empty()) {
94 APP_LOGW("theme is empty, no need to change");
95 return false;
96 }
97
98 nlohmann::json jsonObject = nlohmann::json::parse(theme, nullptr, false);
99 if (jsonObject.is_discarded()) {
100 APP_LOGE("failed parse theme %{public}s", theme.c_str());
101 return false;
102 }
103 const auto &jsonObjectEnd = jsonObject.end();
104 int32_t parseResult = ERR_OK;
105 int32_t updateIcons = 0;
106 GetValueIfFindKey<int32_t>(jsonObject, jsonObjectEnd, "icons", updateIcons,
107 JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
108 if (updateIcons == 0) {
109 APP_LOGI("icons no need to change, return");
110 return false;
111 }
112 #ifdef GLOBAL_RESMGR_ENABLE
113 if (!SetThemeIdForThemeChanged(themeId)) {
114 APP_LOGE("set theme id failed, themeId %{public}d", themeId);
115 }
116 #endif
117
118 auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
119 if (manager == nullptr) {
120 APP_LOGE("manager is nullptr");
121 return false;
122 }
123 int32_t currentUserId = AccountHelper::GetCurrentActiveUserId();
124 if (currentUserId <= 0) {
125 currentUserId = Constants::START_USERID;
126 }
127
128 if (!manager->AddAllResourceInfo(currentUserId, type)) {
129 APP_LOGE("AddAllResourceInfo currentUserId %{public}d failed", currentUserId);
130 return false;
131 }
132 APP_LOGI("end, theme:%{public}s", theme.c_str());
133 return true;
134 }
135
OnOverlayStatusChanged(const std::string & bundleName,bool isEnabled,int32_t userId)136 bool BundleResourceCallback::OnOverlayStatusChanged(
137 const std::string &bundleName,
138 bool isEnabled,
139 int32_t userId)
140 {
141 APP_LOGI("start, bundleName:%{public}s, isEnabled:%{public}d, userId:%{public}d",
142 bundleName.c_str(), isEnabled, userId);
143 int32_t currentUserId = AccountHelper::GetCurrentActiveUserId();
144 if ((currentUserId > 0) && (userId != currentUserId)) {
145 APP_LOGW("userId:%{public}d current:%{public}d not same", currentUserId, userId);
146 return false;
147 }
148 auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
149 if (manager == nullptr) {
150 APP_LOGE("manager is nullptr");
151 return false;
152 }
153 std::string targetBundleName = bundleName;
154 manager->GetTargetBundleName(bundleName, targetBundleName);
155 APP_LOGI("bundleName:%{public}s, targetBundleName:%{public}s overlay changed", bundleName.c_str(),
156 targetBundleName.c_str());
157 if (!manager->DeleteResourceInfo(targetBundleName)) {
158 APP_LOGW("delete resource failed %{public}s", targetBundleName.c_str());
159 }
160
161 if (!manager->AddResourceInfoByBundleName(targetBundleName, userId)) {
162 APP_LOGE("add resource failed %{public}s", targetBundleName.c_str());
163 return false;
164 }
165 APP_LOGI("end, targetBundleName:%{public}s, isEnabled:%{public}d, userId:%{public}d",
166 targetBundleName.c_str(), isEnabled, userId);
167 return true;
168 }
169
SetThemeIdForThemeChanged(const int32_t themeId)170 bool BundleResourceCallback::SetThemeIdForThemeChanged(const int32_t themeId)
171 {
172 if (themeId <= 0) {
173 return false;
174 }
175 #ifdef GLOBAL_RESMGR_ENABLE
176 auto resourcePtr = std::shared_ptr<Global::Resource::ResourceManager>(Global::Resource::CreateResourceManager());
177 if (resourcePtr == nullptr) {
178 APP_LOGE("resource is nullptr, themeId %{public}d", themeId);
179 return false;
180 }
181 int32_t currentUserId = AccountHelper::GetCurrentActiveUserId();
182 if (currentUserId <= 0) {
183 APP_LOGW("currentUserId get failed");
184 currentUserId = Constants::START_USERID;
185 }
186 resourcePtr->userId = currentUserId;
187 APP_LOGI("start set themeId %{public}d userId %{public}d", themeId, currentUserId);
188 std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
189 if (resConfig == nullptr) {
190 APP_LOGE("resConfig is nullptr, themeId %{public}d", themeId);
191 return false;
192 }
193 resConfig->SetThemeId(themeId);
194 Global::Resource::RState ret = resourcePtr->UpdateResConfig(*resConfig); // no need to process ret
195 if (ret != Global::Resource::RState::SUCCESS) {
196 APP_LOGW("UpdateResConfig ret %{public}d, themeId %{public}d", static_cast<int32_t>(ret), themeId);
197 }
198 APP_LOGI("end set themeId %{public}d", themeId);
199 #endif
200 return true;
201 }
202 } // AppExecFwk
203 } // OHOS
204