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