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 #include "theme_pack_config.h"
16
17 #include "hap_manager.h"
18 #include "hilog_wrapper.h"
19 namespace OHOS {
20 namespace Global {
21 namespace Resource {
ThemeConfig()22 ThemeConfig::ThemeConfig() : themeDirection_(DIRECTION_NOT_SET), themeColorMode_(COLOR_MODE_NOT_SET) {}
23
SetThemeDirection(Direction direction)24 void ThemeConfig::SetThemeDirection(Direction direction)
25 {
26 this->themeDirection_ = direction;
27 }
28
SetThemeColorMode(ColorMode colorMode)29 void ThemeConfig::SetThemeColorMode(ColorMode colorMode)
30 {
31 this->themeColorMode_ = colorMode;
32 }
33
Match(const std::shared_ptr<ThemeConfig> & themeConfig,const ResConfigImpl & resConfig)34 bool ThemeConfig::Match(const std::shared_ptr<ThemeConfig> &themeConfig, const ResConfigImpl &resConfig)
35 {
36 if (themeConfig == nullptr) {
37 return false;
38 }
39 ColorMode colorMode = resConfig.GetColorMode();
40 Direction direction = resConfig.GetDirection();
41 ColorMode themeColorMode_ = themeConfig->GetThemeColorMode();
42 Direction themeDirection_ = themeConfig->GetThemeDirection();
43 if (direction != DIRECTION_NOT_SET && themeDirection_ != DIRECTION_NOT_SET) {
44 if (direction != themeDirection_) {
45 return false;
46 }
47 }
48 if (colorMode == DARK && !resConfig.GetAppColorMode() && !resConfig.GetAppDarkRes()) {
49 return themeColorMode_ == COLOR_MODE_NOT_SET;
50 }
51 if (colorMode != COLOR_MODE_NOT_SET && themeColorMode_ != COLOR_MODE_NOT_SET) {
52 if (colorMode != themeColorMode_) {
53 return false;
54 }
55 }
56 return true;
57 }
58
BestMatch(const std::shared_ptr<ThemeConfig> & themeConfig,const ResConfigImpl & resConfig) const59 bool ThemeConfig::BestMatch(const std::shared_ptr<ThemeConfig> &themeConfig, const ResConfigImpl &resConfig) const
60 {
61 ColorMode colorMode = resConfig.GetColorMode();
62 Direction direction = resConfig.GetDirection();
63 ColorMode themeColorMode_ = themeConfig->GetThemeColorMode();
64 Direction themeDirection_ = themeConfig->GetThemeDirection();
65 if (this->themeDirection_ != themeDirection_ && direction != DIRECTION_NOT_SET) {
66 return this->themeDirection_ != DIRECTION_NOT_SET;
67 }
68
69 if (this->themeColorMode_ != themeColorMode_ && colorMode != COLOR_MODE_NOT_SET) {
70 return this->themeColorMode_ != COLOR_MODE_NOT_SET;
71 }
72 return this->IsMoreMatchThan(themeConfig);
73 }
74
IsMoreMatchThan(const std::shared_ptr<ThemeConfig> & themeConfig) const75 bool ThemeConfig::IsMoreMatchThan(const std::shared_ptr<ThemeConfig> &themeConfig) const
76 {
77 if (this->themeDirection_ != themeConfig->themeDirection_) {
78 return this->themeDirection_ != DIRECTION_NOT_SET;
79 }
80
81 if (this->themeColorMode_ != themeConfig->themeColorMode_) {
82 return this->themeColorMode_ != COLOR_MODE_NOT_SET;
83 }
84
85 return true;
86 }
87 } // namespace Resource
88 } // namespace Global
89 } // namespace OHOS
90