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_observer.h"
17
18 #include <thread>
19
20 #include "app_log_wrapper.h"
21 #include "bundle_resource_callback.h"
22 #include "bundle_system_state.h"
23
24 #ifdef ABILITY_RUNTIME_ENABLE
25 #include "configuration.h"
26 #endif
27
28 namespace OHOS {
29 namespace AppExecFwk {
30 #ifdef ABILITY_RUNTIME_ENABLE
BundleResourceObserver()31 BundleResourceObserver::BundleResourceObserver()
32 {}
33
~BundleResourceObserver()34 BundleResourceObserver::~BundleResourceObserver()
35 {}
36
OnConfigurationUpdated(const AppExecFwk::Configuration & configuration)37 void BundleResourceObserver::OnConfigurationUpdated(const AppExecFwk::Configuration& configuration)
38 {
39 APP_LOGI("called");
40 std::string colorMode = configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE);
41 if (!colorMode.empty() && (colorMode != BundleSystemState::GetInstance().GetSystemColorMode())) {
42 APP_LOGI("OnSystemColorModeChanged colorMode:%{public}s", colorMode.c_str());
43 OnSystemColorModeChanged(colorMode, static_cast<uint32_t>(BundleResourceChangeType::SYSTEM_COLOR_MODE_CHANGE));
44 }
45 uint32_t type = 0;
46 std::string language = configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE);
47 if (!language.empty() && (language != BundleSystemState::GetInstance().GetSystemLanguage())) {
48 APP_LOGI("language change %{public}s", language.c_str());
49 type = (type == 0) ? static_cast<uint32_t>(BundleResourceChangeType::SYSTEM_LANGUE_CHANGE) :
50 (type | static_cast<uint32_t>(BundleResourceChangeType::SYSTEM_LANGUE_CHANGE));
51 }
52 std::string theme = configuration.GetItem(AAFwk::GlobalConfigurationKey::THEME);
53 int32_t id = 0;
54 if (!theme.empty()) {
55 std::string themeId = configuration.GetItem(AAFwk::GlobalConfigurationKey::THEME_ID);
56 APP_LOGI("theme change %{public}s, themeId %{public}s", theme.c_str(), themeId.c_str());
57 if (!OHOS::StrToInt(themeId, id)) {
58 id = 0;
59 }
60 type = (type == 0) ? static_cast<uint32_t>(BundleResourceChangeType::SYSTEM_THEME_CHANGE) :
61 (type | static_cast<uint32_t>(BundleResourceChangeType::SYSTEM_THEME_CHANGE));
62 }
63 switch (type) {
64 case 0 : {
65 break;
66 }
67 case static_cast<uint32_t>(BundleResourceChangeType::SYSTEM_LANGUE_CHANGE) : {
68 std::thread systemLanguageChangedThread(OnSystemLanguageChange, language, type);
69 systemLanguageChangedThread.detach();
70 break;
71 }
72 case static_cast<uint32_t>(BundleResourceChangeType::SYSTEM_THEME_CHANGE) : {
73 std::thread applicationThemeChangedThread(OnApplicationThemeChanged, theme, id, type);
74 applicationThemeChangedThread.detach();
75 break;
76 }
77 default: {
78 BundleSystemState::GetInstance().SetSystemLanguage(language);
79 std::thread applicationThemeChangedThread(OnApplicationThemeChanged, theme, id, type);
80 applicationThemeChangedThread.detach();
81 break;
82 }
83 }
84
85 APP_LOGI("end change type %{public}u", type);
86 }
87
OnSystemColorModeChanged(const std::string & colorMode,const uint32_t type)88 void BundleResourceObserver::OnSystemColorModeChanged(const std::string &colorMode, const uint32_t type)
89 {
90 BundleResourceCallback callback;
91 callback.OnSystemColorModeChanged(colorMode, type);
92 }
93
OnSystemLanguageChange(const std::string & language,const uint32_t type)94 void BundleResourceObserver::OnSystemLanguageChange(const std::string &language, const uint32_t type)
95 {
96 BundleResourceCallback callback;
97 callback.OnSystemLanguageChange(language, type);
98 }
99
OnApplicationThemeChanged(const std::string & theme,const int32_t themeId,const uint32_t type)100 void BundleResourceObserver::OnApplicationThemeChanged(const std::string &theme,
101 const int32_t themeId, const uint32_t type)
102 {
103 BundleResourceCallback callback;
104 callback.OnApplicationThemeChanged(theme, themeId, type);
105 }
106 #endif
107 } // AppExecFwk
108 } // OHOS
109