• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "adapter/ohos/osal/resource_theme_style.h"
17 
18 #include <regex>
19 #include <unordered_set>
20 
21 namespace OHOS::Ace {
22 namespace {
23 constexpr char COLOR_VALUE_PREFIX[] = "$color:";
24 constexpr char MEDIA_VALUE_PREFIX[] = "/";
25 constexpr char REF_ATTR_VALUE_KEY_WORD[] = "?theme:";
26 
27 constexpr char RES_TAG[] = "resource:///";
28 // resource manager hap for system resource
29 constexpr char RES_HAP_PREFIX[] = "ohos.global.systemres";
30 #ifdef PREVIEW
31 constexpr char RES_PATH_TAG[] = "file://";
32 // resource manager hap absolute path, as resource manager api don't return
33 constexpr char RES_HAP_PATH[] = "../resources/";
34 #else
35 constexpr char RES_PATH_TAG[] = "file:///";
36 // resource manager hap absolute path, as resource manager api don't return
37 constexpr char RES_HAP_PATH[] = "/data/storage/el1/bundle/ohos.global.systemres/ohos.global.systemres/assets/";
38 #endif
39 
40 const std::regex DIMENSION_REGEX(R"(^([+-]?\d+(\.\d+)?)(px|fp|lpx|vp|%)?)");
41 constexpr int32_t WAIT_FOR_TIME = 50;
42 static const std::unordered_set<std::string> stringAttrs = {
43     "attribute_text_font_family_regular",
44     "attribute_text_font_family_medium",
45     "description_current_location",
46     "description_add_location",
47     "description_select_location",
48     "description_share_location",
49     "description_send_location",
50     "description_locating",
51     "description_location",
52     "description_send_current_location",
53     "description_relocation",
54     "description_punch_in",
55     "description_current_position",
56     "description_paste",
57     "description_download",
58     "description_download_file",
59     "description_save",
60     "description_save_image",
61     "description_save_file",
62     "description_download_and_share",
63     "description_receive",
64     "description_continue_to_receive",
65     "description_save_to_gallery",
66     "description_export_to_gallery",
67     "description_quick_save_to_gallery",
68     "description_quick_resave_to_gallery",
69     "description_save_all",
70     "draggable",
71     "divider_shadow_enable",
72     "camera_input",
73     "menu_bg_blur_effect_enable",
74     "menu_double_border_enable",
75     "section_unfocus_effect_enable",
76     "section_unfocus_color",
77     "sheet_type",
78     "multiple_dialog_display",
79     "menu_expand_display",
80     "popup_double_border_enable",
81     "tips_double_border_enable",
82     "dialog_expand_display",
83     "show_password_directly",
84     "textfield_show_handle",
85     "dialog_radius_level10",
86     "dialog_icon_primary",
87     "dialog_font_primary",
88     "menu_has_filter",
89     "navigation_general_more",
90     "calendar_picker_dialog_button_transparent",
91     "calendar_picker_dialog_divider_transparent",
92     "textfield_accessibility_property_clear",
93     "textfield_accessibility_show_password",
94     "textfield_accessibility_hide_password",
95     "rich_editor_show_handle",
96     "text_show_handle",
97     "list_fadeout_enable",
98     "text_fadeout_enable",
99     "textfield_show_password_button",
100     "textfield_hide_password_button",
101     "textfield_has_showed_password",
102     "textfield_has_hidden_password",
103     "calendar_picker_mon",
104     "calendar_picker_tue",
105     "calendar_picker_wed",
106     "calendar_picker_thu",
107     "calendar_picker_fri",
108     "calendar_picker_sat",
109     "calendar_picker_sun",
110     "filter_accessibility_expand",
111     "filter_accessibility_collapse",
112     "filter_accessibility_collapsed",
113     "filter_accessibility_expanded",
114     "slider_accessibility_selected",
115     "slider_accessibility_unselected",
116     "slider_accessibility_unselectedDesc",
117     "slider_accessibility_disabledDesc",
118     "textfield_writting_bundle_name",
119     "textfield_writting_ability_name",
120     "rich_editor_writting_bundle_name",
121     "rich_editor_writting_ability_name",
122     "textfield_writting_is_support",
123     "rich_editor_writting_is_support",
124     "ai_write_menu_name",
125     "menu_translate_is_support",
126     "text_menu_search_is_support",
127     "textfield_menu_search_is_support",
128     "richeditor_menu_search_is_support",
129     "textfield_accessibility_clear",
130     "pass_point",
131     "side_length",
132     "general_next_year",
133     "general_next_month",
134     "general_pre_year",
135     "general_pre_month",
136     "prev_arrow_accessibility_text",
137     "next_arrow_accessibility_text",
138     "menu_haptic_feedback",
139 };
140 
ParseNumberUnit(const std::string & value,std::string & number,std::string & unit)141 void ParseNumberUnit(const std::string& value, std::string& number, std::string& unit)
142 {
143     std::smatch results;
144     if (std::regex_search(value, results, DIMENSION_REGEX)) {
145         number = results[1];
146         // The unit is in the 3rd sub-match. If the value doesn't have unit,
147         // the 3rd match result is empty.
148         unit = results[3];
149     }
150 }
151 
ParseDimensionUnit(const std::string & unit)152 DimensionUnit ParseDimensionUnit(const std::string& unit)
153 {
154     if (unit == "px") {
155         return DimensionUnit::PX;
156     } else if (unit == "fp") {
157         return DimensionUnit::FP;
158     } else if (unit == "lpx") {
159         return DimensionUnit::LPX;
160     } else if (unit == "%") {
161         return DimensionUnit::PERCENT;
162     } else {
163         return DimensionUnit::VP;
164     }
165 }
166 }
167 
ParseContent()168 void ResourceThemeStyle::ParseContent()
169 {
170     for (const auto& [attrName, attrValue] : rawAttrs_) {
171         if (attrName.empty() || attrValue.empty()) {
172             continue;
173         }
174         if (stringAttrs.find(attrName) != stringAttrs.end()) {
175             // string
176             attributes_[attrName] = { .type = ThemeConstantsType::STRING, .value = attrValue };
177             continue;
178         }
179         if (attrValue.front() == '#' || attrValue.find(COLOR_VALUE_PREFIX) != std::string::npos) {
180             // color
181             attributes_[attrName] = { .type = ThemeConstantsType::COLOR, .value = Color::FromString(attrValue) };
182         } else if (attrValue.find(MEDIA_VALUE_PREFIX) != std::string::npos) {
183             OnParseResourceMedia(attrName, attrValue);
184         } else if (attrValue.find(REF_ATTR_VALUE_KEY_WORD) != std::string::npos) {
185             attributes_[attrName] = { .type = ThemeConstantsType::REFERENCE_ATTR, .value = attrValue };
186         } else {
187             // int & double & dimension
188             std::string number;
189             std::string unit;
190             ParseNumberUnit(attrValue, number, unit);
191             if (number.empty()) {
192                 continue;
193             } else if (!unit.empty()) {
194                 attributes_[attrName] = { .type = ThemeConstantsType::DIMENSION,
195                     .value = Dimension(std::atof(number.c_str()), ParseDimensionUnit(unit)) };
196             } else if (number.find(".") == std::string::npos) {
197                 attributes_[attrName] = { .type = ThemeConstantsType::INT, .value = std::atoi(number.c_str()) };
198             } else {
199                 attributes_[attrName] = { .type = ThemeConstantsType::DOUBLE, .value = std::atof(number.c_str()) };
200             }
201         }
202     }
203     OnParseStyle();
204 }
205 
OnParseStyle()206 void ResourceThemeStyle::OnParseStyle()
207 {
208     for (const auto& [patternName, patternMap]: patternAttrs_) {
209         auto patternStyle = AceType::MakeRefPtr<ResourceThemeStyle>(resAdapter_);
210         patternStyle->SetName(patternName);
211         patternStyle->parentStyle_ = AceType::WeakClaim(this);
212         patternStyle->rawAttrs_ = patternMap;
213         patternStyle->ParseContent();
214         attributes_[patternName] = { .type = ThemeConstantsType::PATTERN,
215             .value = RefPtr<ThemeStyle>(std::move(patternStyle)) };
216     }
217 }
218 
OnParseResourceMedia(const std::string & attrName,const std::string & attrValue)219 void ResourceThemeStyle::OnParseResourceMedia(const std::string& attrName, const std::string& attrValue)
220 {
221     std::string mediaPath;
222     if (SystemProperties::GetUnZipHap()) {
223         mediaPath = RES_PATH_TAG;
224         if (attrValue.find(RES_HAP_PREFIX) == std::string::npos) {
225             mediaPath.append(RES_HAP_PATH);
226         }
227 #ifdef PREVIEW
228         auto pos = attrValue.find(MEDIA_VALUE_PREFIX);
229         if (pos == std::string::npos) {
230             return;
231         }
232         mediaPath += attrValue.substr(pos + 1);
233 #else
234         mediaPath += attrValue;
235 #endif
236     } else {
237         // hap is not unzip, should use resource name to read file
238         auto pos = attrValue.find_last_of(MEDIA_VALUE_PREFIX);
239         if (pos == std::string::npos) {
240             LOGW("resource media invalid:[%{public}s, %{public}s]", attrName.c_str(), attrValue.c_str());
241             return;
242         }
243         mediaPath = std::string(RES_TAG) + attrValue.substr(pos + 1);
244     }
245     attributes_[attrName] = { .type = ThemeConstantsType::STRING, .value = mediaPath };
246 }
247 
CheckThemeStyleLoaded(const std::string & patternName)248 void ResourceThemeStyle::CheckThemeStyleLoaded(const std::string& patternName)
249 {
250     if (!CheckThemeStyle(patternName)) {
251         return;
252     }
253     if (future_.valid()) {
254         future_.wait_until(std::chrono::system_clock::now() + std::chrono::milliseconds(WAIT_FOR_TIME));
255     }
256 }
257 } // namespace OHOS::Ace
258