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     ColorMode colorMode = resConfig.GetColorMode();
37     Direction direction = resConfig.GetDirection();
38     ColorMode themeColorMode_ = themeConfig->GetThemeColorMode();
39     Direction themeDirection_ = themeConfig->GetThemeDirection();
40     if (direction != DIRECTION_NOT_SET && themeDirection_ != DIRECTION_NOT_SET) {
41         if (direction != themeDirection_) {
42             return false;
43         }
44     }
45     if (colorMode == DARK && !resConfig.GetAppColorMode() && !resConfig.GetAppDarkRes()) {
46         return themeColorMode_ == COLOR_MODE_NOT_SET;
47     }
48     if (colorMode != COLOR_MODE_NOT_SET && themeColorMode_ != COLOR_MODE_NOT_SET) {
49         if (colorMode != themeColorMode_) {
50             return false;
51         }
52     }
53     return true;
54 }
55 
BestMatch(const std::shared_ptr<ThemeConfig> & themeConfig,const ResConfigImpl & resConfig) const56 bool ThemeConfig::BestMatch(const std::shared_ptr<ThemeConfig> &themeConfig, const ResConfigImpl &resConfig) const
57 {
58     ColorMode colorMode = resConfig.GetColorMode();
59     Direction direction = resConfig.GetDirection();
60     ColorMode themeColorMode_ = themeConfig->GetThemeColorMode();
61     Direction themeDirection_ = themeConfig->GetThemeDirection();
62     if (this->themeDirection_ != themeDirection_ && direction != DIRECTION_NOT_SET) {
63         return this->themeDirection_ != DIRECTION_NOT_SET;
64     }
65 
66     if (this->themeColorMode_ != themeColorMode_ && colorMode != COLOR_MODE_NOT_SET) {
67         return this->themeColorMode_ != COLOR_MODE_NOT_SET;
68     }
69     return this->IsMoreMatchThan(themeConfig);
70 }
71 
IsMoreMatchThan(const std::shared_ptr<ThemeConfig> & themeConfig) const72 bool ThemeConfig::IsMoreMatchThan(const std::shared_ptr<ThemeConfig> &themeConfig) const
73 {
74     if (this->themeDirection_ != themeConfig->themeDirection_) {
75         return this->themeDirection_ != DIRECTION_NOT_SET;
76     }
77 
78     if (this->themeColorMode_ != themeConfig->themeColorMode_) {
79         return this->themeColorMode_ != COLOR_MODE_NOT_SET;
80     }
81 
82     return true;
83 }
84 } // namespace Resource
85 } // namespace Global
86 } // namespace OHOS
87