• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #include "base/resource/ace_res_config.h"
16 
17 #include <algorithm>
18 #include <cstddef>
19 #include <cstring>
20 #include <memory>
21 #include <unordered_map>
22 #include <utility>
23 
24 #include "base/i18n/localization.h"
25 #include "base/log/ace_trace.h"
26 #include "base/log/log.h"
27 #include "base/resource/ace_res_data_struct.h"
28 #include "base/resource/ace_res_key_parser.h"
29 #include "base/utils/linear_map.h"
30 #include "base/utils/utils.h"
31 #include "core/pipeline/pipeline_base.h"
32 
33 namespace OHOS::Ace {
34 namespace {
35 
36 const std::unordered_map<std::string, std::string> DEFAULT_LANG {
37     { "es-419", "es-US" },
38     { "en-Qaag", "en-GB" },
39     { "en-001", "en-GB" },
40 };
41 
42 const std::unordered_map<std::string, std::string> LANGUAGE_CODE_MAP {
43     { "iw", "he" },
44     { "tl", "fil" },
45     { "in", "id" },
46 };
47 
ComputeScript(const AceResConfig & resConfig)48 std::string ComputeScript(const AceResConfig& resConfig)
49 {
50     return Localization::ComputeScript(resConfig.language_, resConfig.region_);
51 }
52 
ConvertLocaleTagToConfig(const std::string & localeTag)53 AceResConfig ConvertLocaleTagToConfig(const std::string& localeTag)
54 {
55     std::string language, script, region;
56     Localization::ParseLocaleTag(localeTag, language, script, region, false);
57     return AceResConfig(language, script, region);
58 }
59 
ConvertLocaleConfigToTag(const AceResConfig & localeConfig)60 std::string ConvertLocaleConfigToTag(const AceResConfig& localeConfig)
61 {
62     std::string localeTag = localeConfig.language_;
63     if (!localeConfig.script_.empty()) {
64         localeTag += ('-' + localeConfig.script_);
65     }
66     if (!localeConfig.region_.empty()) {
67         localeTag += ('-' + localeConfig.region_);
68     }
69     return localeTag;
70 }
71 
IsPseudoLocale(const AceResConfig & resConfig)72 bool IsPseudoLocale(const AceResConfig& resConfig)
73 {
74     return (resConfig.language_ == "en" && resConfig.region_ == "XA") ||
75            (resConfig.language_ == "ar" && resConfig.region_ == "XB");
76 }
77 
MatchLocaleConfig(const std::vector<AceResConfig> & candidatesLocaleConfig,const AceResConfig & deviceLocaleConfig,std::vector<AceResConfig> & matchedLocaleConfigs,bool isDeclarative=false)78 void MatchLocaleConfig(const std::vector<AceResConfig>& candidatesLocaleConfig, const AceResConfig& deviceLocaleConfig,
79     std::vector<AceResConfig>& matchedLocaleConfigs, bool isDeclarative = false)
80 {
81     std::string deviceLanguage = deviceLocaleConfig.language_;
82     std::string deviceScript = deviceLocaleConfig.script_;
83     auto iter = LANGUAGE_CODE_MAP.find(deviceLanguage);
84     if (iter != LANGUAGE_CODE_MAP.end()) {
85         deviceLanguage = iter->second;
86     }
87     for (auto& candidate : candidatesLocaleConfig) {
88         if (isDeclarative && candidate.language_ == "" && candidate.script_ == "") {
89             matchedLocaleConfigs.emplace_back(candidate);
90             continue;
91         }
92         std::string candidateLanguage = candidate.language_;
93         // Languages codes need to be specially handled.
94         auto candidateIter = LANGUAGE_CODE_MAP.find(candidateLanguage);
95         if (candidateIter != LANGUAGE_CODE_MAP.end()) {
96             candidateLanguage = candidateIter->second;
97         }
98         if (candidateLanguage != deviceLanguage) {
99             continue;
100         }
101         if (IsPseudoLocale(candidate)) {
102             if (IsPseudoLocale(deviceLocaleConfig)) {
103                 matchedLocaleConfigs.emplace_back(candidate);
104             }
105             continue;
106         }
107         if (deviceLanguage == "en" && deviceScript == "Qaag") {
108             deviceScript = "Latn";
109         }
110         // Compute Script when it is not provided.
111         std::string candidateScript = candidate.script_.empty() ? ComputeScript(candidate) : candidate.script_;
112         if (deviceScript != candidateScript) {
113             continue;
114         }
115         matchedLocaleConfigs.emplace_back(candidate);
116     }
117 }
118 
FindBacktrackPath(const AceResConfig & localeConfig,std::vector<std::string> & backtrackPath,const std::string & localeTag)119 void FindBacktrackPath(
120     const AceResConfig& localeConfig, std::vector<std::string>& backtrackPath, const std::string& localeTag)
121 {
122     static const std::unordered_map<std::string, std::string> LOCALE_PARENTS {
123         { "ar-DZ", "ar-015" },
124         { "ar-EH", "ar-015" },
125         { "ar-LY", "ar-015" },
126         { "ar-MA", "ar-015" },
127         { "ar-TN", "ar-015" },
128         { "zh-MO", "zh-HK" },
129         { "en-150", "en-001" },
130         { "en-AG", "en-001" },
131         { "en-AI", "en-001" },
132         { "en-AU", "en-001" },
133         { "en-BB", "en-001" },
134         { "en-BE", "en-001" },
135         { "en-BM", "en-001" },
136         { "en-BS", "en-001" },
137         { "en-BZ", "en-001" },
138         { "en-CC", "en-001" },
139         { "en-CK", "en-001" },
140         { "en-CX", "en-001" },
141         { "en-DG", "en-001" },
142         { "en-ER", "en-001" },
143         { "en-FK", "en-001" },
144         { "en-FM", "en-001" },
145         { "en-GB", "en-001" },
146         { "en-GD", "en-001" },
147         { "en-GG", "en-001" },
148         { "en-GI", "en-001" },
149         { "en-GY", "en-001" },
150         { "en-HK", "en-001" },
151         { "en-IE", "en-001" },
152         { "en-IM", "en-001" },
153         { "en-IN", "en-001" },
154         { "en-IO", "en-001" },
155         { "en-JE", "en-001" },
156         { "en-KI", "en-001" },
157         { "en-KN", "en-001" },
158         { "en-KY", "en-001" },
159         { "en-LC", "en-001" },
160         { "en-LR", "en-001" },
161         { "en-LS", "en-001" },
162         { "en-MM", "en-001" },
163         { "en-MO", "en-001" },
164         { "en-MS", "en-001" },
165         { "en-MT", "en-001" },
166         { "en-MY", "en-001" },
167         { "en-NF", "en-001" },
168         { "en-NR", "en-001" },
169         { "en-NU", "en-001" },
170         { "en-NZ", "en-001" },
171         { "en-PG", "en-001" },
172         { "en-PK", "en-001" },
173         { "en-PN", "en-001" },
174         { "en-PW", "en-001" },
175         { "en-SB", "en-001" },
176         { "en-SC", "en-001" },
177         { "en-SD", "en-001" },
178         { "en-SG", "en-001" },
179         { "en-SH", "en-001" },
180         { "en-SL", "en-001" },
181         { "en-SS", "en-001" },
182         { "en-SX", "en-001" },
183         { "en-SZ", "en-001" },
184         { "en-TC", "en-001" },
185         { "en-TK", "en-001" },
186         { "en-TT", "en-001" },
187         { "en-TV", "en-001" },
188         { "en-VC", "en-001" },
189         { "en-VG", "en-001" },
190         { "en-WS", "en-001" },
191         { "en-ZG", "en-001" },
192         { "es-AR", "es-419" },
193         { "es-BO", "es-419" },
194         { "es-BR", "es-419" },
195         { "es-BZ", "es-419" },
196         { "es-CL", "es-419" },
197         { "es-CO", "es-419" },
198         { "es-CR", "es-419" },
199         { "es-CU", "es-419" },
200         { "es-DO", "es-419" },
201         { "es-EC", "es-419" },
202         { "es-GT", "es-419" },
203         { "es-HN", "es-419" },
204         { "es-MX", "es-419" },
205         { "es-NI", "es-419" },
206         { "es-PA", "es-419" },
207         { "es-PE", "es-419" },
208         { "es-PR", "es-419" },
209         { "es-PY", "es-419" },
210         { "es-SV", "es-419" },
211         { "es-US", "es-419" },
212         { "es-UY", "es-419" },
213         { "es-VE", "es-419" },
214         { "pt-AO", "pt-PT" },
215         { "pt-CH", "pt-PT" },
216         { "pt-CV", "pt-PT" },
217         { "pt-GQ", "pt-PT" },
218         { "pt-GW", "pt-PT" },
219         { "pt-LU", "pt-PT" },
220         { "pt-MO", "pt-PT" },
221         { "pt-MZ", "pt-PT" },
222         { "pt-ST", "pt-PT" },
223         { "pt-TL", "pt-PT" },
224     };
225     if (LOCALE_PARENTS.find(localeTag) != LOCALE_PARENTS.end()) {
226         std::string curTag = LOCALE_PARENTS.find(localeTag)->second;
227         backtrackPath.emplace_back(curTag);
228         FindBacktrackPath(localeConfig, backtrackPath, curTag);
229     } else {
230         backtrackPath.emplace_back(localeConfig.language_);
231     }
232 }
233 
IsOnBacktrackingPath(const std::string & bestConfigTag,const std::string & candidateConfigTag,const AceResConfig & bestLocaleConfig,const AceResConfig & candidateLocaleConfig,std::vector<std::string> backtrackPath)234 bool IsOnBacktrackingPath(const std::string& bestConfigTag, const std::string& candidateConfigTag,
235     const AceResConfig& bestLocaleConfig, const AceResConfig& candidateLocaleConfig,
236     std::vector<std::string> backtrackPath)
237 {
238     std::vector<std::string> bestConfigBacktrackPaths;
239     FindBacktrackPath(bestLocaleConfig, bestConfigBacktrackPaths, bestConfigTag);
240     std::vector<std::string> candidateConfigBacktrackPaths;
241     FindBacktrackPath(candidateLocaleConfig, candidateConfigBacktrackPaths, candidateConfigTag);
242     int32_t bestConfigIndex = -1;
243     int32_t candidateConfigIndex = -1;
244     for (std::size_t i = 0; i < backtrackPath.size(); i++) {
245         for (const auto& bestConfigPath : bestConfigBacktrackPaths) {
246             if (bestConfigPath == backtrackPath[i] && bestConfigIndex == -1) {
247                 bestConfigIndex = static_cast<int32_t>(i);
248             }
249         }
250         for (const auto& candidateConfigPath : candidateConfigBacktrackPaths) {
251             if (candidateConfigPath == backtrackPath[i] && candidateConfigIndex == -1) {
252                 candidateConfigIndex = static_cast<int32_t>(i);
253             }
254         }
255     }
256     if (bestConfigIndex < candidateConfigIndex) {
257         return (bestConfigIndex == -1);
258     } else if (candidateConfigIndex < bestConfigIndex) {
259         return (candidateConfigIndex != -1);
260     }
261     // Check if candidate and best are on the default area of device's backtracking path.
262     for (auto& path : backtrackPath) {
263         if (DEFAULT_LANG.find(path) != DEFAULT_LANG.end()) {
264             std::string curTag = DEFAULT_LANG.find(path)->second;
265             if (bestConfigTag == curTag) {
266                 return false;
267             } else if (candidateConfigTag == curTag) {
268                 return true;
269             }
270         }
271     }
272 
273     // Compute the default region code through the device language code.
274     std::string language, script, region;
275     Localization::ParseLocaleTag(backtrackPath[backtrackPath.size() - 1], language, script, region, true);
276 
277     if (bestLocaleConfig.region_ == region) {
278         return false;
279     }
280     if (candidateLocaleConfig.region_ == region) {
281         return true;
282     }
283 
284     // If configs did not meet all the conditions above, sort the config region with dictionary order.
285     return strcmp(bestLocaleConfig.region_.c_str(), candidateLocaleConfig.region_.c_str()) >= 0;
286 }
287 
IsLocaleConfigMoreSpecified(const AceResConfig & bestLocaleConfig,const AceResConfig & deviceLocaleConfig,const AceResConfig & candidateLocaleConfig)288 bool IsLocaleConfigMoreSpecified(const AceResConfig& bestLocaleConfig, const AceResConfig& deviceLocaleConfig,
289     const AceResConfig& candidateLocaleConfig)
290 {
291     // Candidates' language and script are equal with the device's after being filtered MatchLocaleConfig().
292     std::string deviceRegion = deviceLocaleConfig.region_;
293     if (bestLocaleConfig.region_ == deviceRegion) {
294         return false;
295     }
296     if (candidateLocaleConfig.region_ == deviceRegion) {
297         return true;
298     }
299     std::string deviceTag = deviceLocaleConfig.language_ + '-' + deviceLocaleConfig.region_;
300     std::vector<std::string> deviceBacktrackPath;
301     FindBacktrackPath(deviceLocaleConfig, deviceBacktrackPath, deviceTag);
302     std::string bestConfigTag = bestLocaleConfig.language_;
303     if (!bestLocaleConfig.region_.empty()) {
304         bestConfigTag += '-' + bestLocaleConfig.region_;
305     }
306     std::string candidateConfigTag = candidateLocaleConfig.language_;
307     if (!candidateLocaleConfig.region_.empty()) {
308         candidateConfigTag += '-' + candidateLocaleConfig.region_;
309     }
310     int32_t bestConfigIndex = -1;
311     int32_t candidateConfigIndex = -1;
312     for (std::size_t i = 0; i < deviceBacktrackPath.size(); i++) {
313         if (deviceBacktrackPath[i] == bestConfigTag && bestConfigIndex == -1) {
314             bestConfigIndex = static_cast<int32_t>(i);
315         }
316         if (deviceBacktrackPath[i] == candidateConfigTag && candidateConfigIndex == -1) {
317             candidateConfigIndex = static_cast<int32_t>(i);
318         }
319     }
320     if (bestConfigIndex < candidateConfigIndex) {
321         return (bestConfigIndex == -1);
322     } else if (candidateConfigIndex < bestConfigIndex) {
323         return (candidateConfigIndex != -1);
324     }
325     // Neither best nor candidate on the device's backtracking path, check if they are the default locale of the device.
326     if (candidateConfigIndex == bestConfigIndex && candidateConfigIndex == -1) {
327         std::string deviceTagWithScript = deviceLocaleConfig.language_ + "-" + deviceLocaleConfig.script_;
328         if (DEFAULT_LANG.find(deviceTagWithScript) != DEFAULT_LANG.end()) {
329             std::string defaultTag = DEFAULT_LANG.find(deviceTagWithScript)->second;
330             std::string defaultRegion = defaultTag.substr(defaultTag.find('-') + 1);
331             if (candidateLocaleConfig.region_ == defaultRegion) {
332                 return true;
333             }
334             if (bestLocaleConfig.region_ == defaultRegion) {
335                 return false;
336             }
337         }
338         // If they are not on the default area, continue to check the candidates' backtacking path.
339         return IsOnBacktrackingPath(
340             bestConfigTag, candidateConfigTag, bestLocaleConfig, candidateLocaleConfig, deviceBacktrackPath);
341     }
342     // Both best and candidate on the device's backtracking path with same index.
343     return false;
344 }
345 
SortLocaleConfigs(std::vector<AceResConfig> & candidatesLocaleConfig,const AceResConfig & deviceLocaleConfig,int32_t left,int32_t right)346 void SortLocaleConfigs(std::vector<AceResConfig>& candidatesLocaleConfig, const AceResConfig& deviceLocaleConfig,
347     int32_t left, int32_t right)
348 {
349     if (left < right) {
350         AceResConfig bestLocaleConfig = candidatesLocaleConfig[left];
351         int32_t i = left;
352         int32_t j = right;
353         while (i < j) {
354             while (i < j &&
355                    !IsLocaleConfigMoreSpecified(bestLocaleConfig, deviceLocaleConfig, candidatesLocaleConfig[j])) {
356                 j--;
357             }
358             candidatesLocaleConfig[i] = candidatesLocaleConfig[j];
359             while (
360                 i < j && IsLocaleConfigMoreSpecified(bestLocaleConfig, deviceLocaleConfig, candidatesLocaleConfig[i])) {
361                 i++;
362             }
363             candidatesLocaleConfig[j] = candidatesLocaleConfig[i];
364         }
365         candidatesLocaleConfig[i] = bestLocaleConfig;
366         SortLocaleConfigs(candidatesLocaleConfig, deviceLocaleConfig, left, i - 1);
367         SortLocaleConfigs(candidatesLocaleConfig, deviceLocaleConfig, i + 1, right);
368     }
369 }
370 
IsLowerResolutionPreferred(int32_t high,int32_t low,int32_t request)371 bool IsLowerResolutionPreferred(int32_t high, int32_t low, int32_t request)
372 {
373     if (request <= low) {
374         return true;
375     }
376 
377     if (request >= high) {
378         return false;
379     }
380 
381     return ((2 * low) - request) * high > request * request; // scaling down is 2x better than up
382 }
383 
IsCandidateResolutionPreferred(const AceResConfig & deviceConfig,const AceResConfig & bestConfig,const AceResConfig & candidateConfig)384 bool IsCandidateResolutionPreferred(
385     const AceResConfig& deviceConfig, const AceResConfig& bestConfig, const AceResConfig& candidateConfig)
386 {
387     if (bestConfig.resolution_ == candidateConfig.resolution_) {
388         return false;
389     }
390 
391     const ResolutionType bestRes = bestConfig.resolution_ != ResolutionType::RESOLUTION_NONE
392                                        ? bestConfig.resolution_
393                                        : ResolutionType::RESOLUTION_MDPI;
394     const ResolutionType candidateRes = candidateConfig.resolution_ != ResolutionType::RESOLUTION_NONE
395                                             ? candidateConfig.resolution_
396                                             : ResolutionType::RESOLUTION_MDPI;
397 
398     if (bestRes == ResolutionType::RESOLUTION_ANY) {
399         return false;
400     } else if (candidateRes == ResolutionType::RESOLUTION_ANY) {
401         return true;
402     }
403 
404     ResolutionType deviceRes = deviceConfig.resolution_;
405     if (deviceConfig.resolution_ == ResolutionType::RESOLUTION_NONE ||
406         deviceConfig.resolution_ == ResolutionType::RESOLUTION_ANY) {
407         deviceRes = ResolutionType::RESOLUTION_MDPI;
408     }
409 
410     if (bestRes > candidateRes) {
411         return IsLowerResolutionPreferred(
412             static_cast<int32_t>(bestRes), static_cast<int32_t>(candidateRes), static_cast<int32_t>(deviceRes));
413     }
414 
415     if (bestRes < candidateRes) {
416         return !IsLowerResolutionPreferred(
417             static_cast<int32_t>(candidateRes), static_cast<int32_t>(bestRes), static_cast<int32_t>(deviceRes));
418     }
419 
420     return false;
421 }
422 
IsCandidateResConfigPreferred(const AceResConfig & deviceConfig,const AceResConfig & bestConfig,const AceResConfig & candidateConfig)423 bool IsCandidateResConfigPreferred(
424     const AceResConfig& deviceConfig, const AceResConfig& bestConfig, const AceResConfig& candidateConfig)
425 {
426     if ((candidateConfig.mcc_ != bestConfig.mcc_) && deviceConfig.mcc_) {
427         return candidateConfig.mcc_;
428     }
429     if ((candidateConfig.mnc_ != bestConfig.mnc_) && deviceConfig.mnc_) {
430         return candidateConfig.mnc_;
431     }
432 
433     if (bestConfig.orientation_ != candidateConfig.orientation_ &&
434         deviceConfig.orientation_ != DeviceOrientation::ORIENTATION_UNDEFINED) {
435         return candidateConfig.orientation_ != DeviceOrientation::ORIENTATION_UNDEFINED;
436     }
437 
438     if (bestConfig.colorMode_ != candidateConfig.colorMode_) {
439         return candidateConfig.colorMode_ == deviceConfig.colorMode_;
440     }
441 
442     if (bestConfig.deviceType_ != DeviceType::UNKNOWN || candidateConfig.deviceType_ != DeviceType::UNKNOWN) {
443         if (bestConfig.deviceType_ != candidateConfig.deviceType_) {
444             return candidateConfig.deviceType_ != DeviceType::UNKNOWN;
445         }
446     }
447 
448     return IsCandidateResolutionPreferred(deviceConfig, bestConfig, candidateConfig);
449 }
450 
IsCandidateDeclarativeResConfigPreferred(const AceResConfig & deviceConfig,const AceResConfig & bestConfig,const AceResConfig & candidateConfig)451 bool IsCandidateDeclarativeResConfigPreferred(
452     const AceResConfig& deviceConfig, const AceResConfig& bestConfig, const AceResConfig& candidateConfig)
453 {
454     if (bestConfig.screenLong_ != candidateConfig.screenLong_ &&
455         deviceConfig.screenLong_ != LongScreenType::LONG_SCREEN_UNDEFINED) {
456         if ((bestConfig.screenLong_ == deviceConfig.screenLong_) ||
457             (candidateConfig.screenLong_ == deviceConfig.screenLong_)) {
458             return candidateConfig.screenLong_ == deviceConfig.screenLong_;
459         }
460         return candidateConfig.screenLong_ == LongScreenType::LONG_SCREEN_UNDEFINED;
461     }
462 
463     if (bestConfig.screenShape_ != candidateConfig.screenShape_ &&
464         deviceConfig.screenShape_ != ScreenShape::SCREEN_SHAPE_UNDEFINED) {
465         if ((bestConfig.screenShape_ == deviceConfig.screenShape_) ||
466             (candidateConfig.screenShape_ == deviceConfig.screenShape_)) {
467             return candidateConfig.screenShape_ == deviceConfig.screenShape_;
468         }
469         return candidateConfig.screenShape_ == ScreenShape::SCREEN_SHAPE_UNDEFINED;
470     }
471 
472     if (bestConfig.orientation_ != candidateConfig.orientation_ &&
473         deviceConfig.orientation_ != DeviceOrientation::ORIENTATION_UNDEFINED) {
474         return candidateConfig.orientation_ != DeviceOrientation::ORIENTATION_UNDEFINED;
475     }
476 
477     if (bestConfig.colorMode_ != candidateConfig.colorMode_ &&
478         deviceConfig.colorMode_ != ColorMode::COLOR_MODE_UNDEFINED) {
479         if ((bestConfig.colorMode_ == deviceConfig.colorMode_) ||
480             (candidateConfig.colorMode_ == deviceConfig.colorMode_)) {
481             return candidateConfig.colorMode_ == deviceConfig.colorMode_;
482         }
483         return candidateConfig.colorMode_ == ColorMode::COLOR_MODE_UNDEFINED;
484     }
485 
486     if (bestConfig.deviceType_ != DeviceType::UNKNOWN || candidateConfig.deviceType_ != DeviceType::UNKNOWN) {
487         if (bestConfig.deviceType_ != candidateConfig.deviceType_) {
488             return candidateConfig.deviceType_ != DeviceType::UNKNOWN;
489         }
490     }
491 
492     return IsCandidateResolutionPreferred(deviceConfig, bestConfig, candidateConfig);
493 }
494 
IsDialectsOfEnUs(const AceResConfig & config)495 bool IsDialectsOfEnUs(const AceResConfig& config)
496 {
497     int tracebackPathLength = 0;
498     if (config.language_ == "en") {
499         std::string configTag = config.language_ + "-" + config.region_;
500         std::vector<std::string> backtrackPath;
501         backtrackPath.push_back(configTag);
502         FindBacktrackPath(config, backtrackPath, configTag);
503         tracebackPathLength = static_cast<int>(backtrackPath.size());
504     }
505     // backtracking size 2, means that the locale is an American English dialect.
506     return tracebackPathLength == 2;
507 }
508 
IsBaseLanguagePreferred(const AceResConfig & deviceConfig,const AceResConfig & bestConfig,const AceResConfig & candidateConfig)509 CompareResult IsBaseLanguagePreferred(
510     const AceResConfig& deviceConfig, const AceResConfig& bestConfig, const AceResConfig& candidateConfig)
511 {
512     if (deviceConfig.language_ == "en" && deviceConfig.region_ == "US") {
513         if (candidateConfig.region_.length() == 0 && candidateConfig.language_.length() == 0) {
514             return (bestConfig.region_ != "US" && bestConfig.region_.length() != 0) ? CompareResult::PREFERRED
515                                                                                     : CompareResult::NOT_PREFERRED;
516         }
517         if (bestConfig.region_.length() == 0 && bestConfig.language_.length() == 0) {
518             return (candidateConfig.region_ == "US" || candidateConfig.region_.length() == 0)
519                        ? CompareResult::PREFERRED
520                        : CompareResult::NOT_PREFERRED;
521         }
522     }
523     if (!IsDialectsOfEnUs(deviceConfig)) {
524         if (bestConfig.language_.length() == 0 && candidateConfig.language_.length() > 0) {
525             return CompareResult::PREFERRED;
526         } else if (bestConfig.language_.length() > 0 && candidateConfig.language_.length() == 0) {
527             return CompareResult::NOT_PREFERRED;
528         }
529     }
530     return CompareResult::EQUAL_RES;
531 }
532 
IsCandidateDeclarativeConfigPreferred(const AceResConfig & bestConfig,const AceResConfig & deviceConfig,const AceResConfig & candidateConfig)533 bool IsCandidateDeclarativeConfigPreferred(
534     const AceResConfig& bestConfig, const AceResConfig& deviceConfig, const AceResConfig& candidateConfig)
535 {
536     if (bestConfig.language_ != candidateConfig.language_) {
537         auto isBasePreferred = IsBaseLanguagePreferred(deviceConfig, bestConfig, candidateConfig);
538         if (isBasePreferred == CompareResult::PREFERRED) {
539             return true;
540         }
541         if (isBasePreferred == CompareResult::NOT_PREFERRED) {
542             return false;
543         }
544     }
545 
546     if (bestConfig.region_ != candidateConfig.region_) {
547         return IsLocaleConfigMoreSpecified(bestConfig, deviceConfig, candidateConfig);
548     }
549 
550     return IsCandidateDeclarativeResConfigPreferred(deviceConfig, bestConfig, candidateConfig);
551 }
552 
SortResConfigs(const AceResConfig & desiredConfig,std::vector<AceResConfig> & candidateConfigs,int32_t left,int32_t right)553 void SortResConfigs(
554     const AceResConfig& desiredConfig, std::vector<AceResConfig>& candidateConfigs, int32_t left, int32_t right)
555 {
556     if (left < right) {
557         AceResConfig bestConfig = candidateConfigs[left];
558         int32_t i = left;
559         int32_t j = right;
560         while (i < j) {
561             while (i < j && !IsCandidateResConfigPreferred(desiredConfig, bestConfig, candidateConfigs[j])) {
562                 j--;
563             }
564             candidateConfigs[i] = candidateConfigs[j];
565             while (i < j && IsCandidateResConfigPreferred(desiredConfig, bestConfig, candidateConfigs[i])) {
566                 i++;
567             }
568             candidateConfigs[j] = candidateConfigs[i];
569         }
570         candidateConfigs[i] = bestConfig;
571         SortResConfigs(desiredConfig, candidateConfigs, left, i - 1);
572         SortResConfigs(desiredConfig, candidateConfigs, i + 1, right);
573     }
574 }
575 
SortDeclarativeResConfigs(const AceResConfig & desiredConfig,std::vector<AceResConfig> & candidateConfigs,int32_t left,int32_t right)576 void SortDeclarativeResConfigs(
577     const AceResConfig& desiredConfig, std::vector<AceResConfig>& candidateConfigs, int32_t left, int32_t right)
578 {
579     if (left < right) {
580         AceResConfig bestConfig = candidateConfigs[left];
581         int32_t i = left;
582         int32_t j = right;
583         while (i < j) {
584             while (i < j && !IsCandidateDeclarativeConfigPreferred(bestConfig, desiredConfig, candidateConfigs[j])) {
585                 j--;
586             }
587             candidateConfigs[i] = candidateConfigs[j];
588             while (i < j && IsCandidateDeclarativeConfigPreferred(bestConfig, desiredConfig, candidateConfigs[i])) {
589                 i++;
590             }
591             candidateConfigs[j] = candidateConfigs[i];
592         }
593         candidateConfigs[i] = bestConfig;
594         SortDeclarativeResConfigs(desiredConfig, candidateConfigs, left, i - 1);
595         SortDeclarativeResConfigs(desiredConfig, candidateConfigs, i + 1, right);
596     }
597 }
598 
IsOrientationMatch(const AceResConfig & desired,const AceResConfig & supported)599 bool IsOrientationMatch(const AceResConfig& desired, const AceResConfig& supported)
600 {
601     return supported.orientation_ == DeviceOrientation::ORIENTATION_UNDEFINED ||
602            supported.orientation_ == desired.orientation_;
603 }
604 
IsDeviceTypeMatch(const AceResConfig & desired,const AceResConfig & supported)605 bool IsDeviceTypeMatch(const AceResConfig& desired, const AceResConfig& supported)
606 {
607     return supported.deviceType_ == DeviceType::UNKNOWN || supported.deviceType_ == desired.deviceType_;
608 }
609 
IsColorModeMatch(const AceResConfig & desired,const AceResConfig & supported)610 bool IsColorModeMatch(const AceResConfig& desired, const AceResConfig& supported)
611 {
612     return supported.colorMode_ == ColorMode::COLOR_MODE_UNDEFINED || supported.colorMode_ == desired.colorMode_;
613 }
614 
IsMccMncMatch(const AceResConfig & desired,const AceResConfig & supported)615 bool IsMccMncMatch(const AceResConfig& desired, const AceResConfig& supported)
616 {
617     if (supported.mcc_ != 0 && supported.mcc_ != desired.mcc_) {
618         return false;
619     }
620     return !(supported.mnc_ != 0 && supported.mnc_ != desired.mnc_);
621 }
622 
IsResolutionMatch(const AceResConfig & desired,const AceResConfig & supported)623 bool IsResolutionMatch(const AceResConfig& desired, const AceResConfig& supported)
624 {
625     return supported.resolution_ == ResolutionType::RESOLUTION_NONE || supported.resolution_ == desired.resolution_;
626 }
627 
MatchResConfig(const AceResConfig & desired,const AceResConfig & supported)628 bool MatchResConfig(const AceResConfig& desired, const AceResConfig& supported)
629 {
630     return !(!IsOrientationMatch(desired, supported) || !IsDeviceTypeMatch(desired, supported) ||
631              !IsColorModeMatch(desired, supported) || !IsMccMncMatch(desired, supported) ||
632              !IsResolutionMatch(desired, supported));
633 }
634 
DeclarativeMatchResConfig(const AceResConfig & desired,const AceResConfig & supported)635 bool DeclarativeMatchResConfig(const AceResConfig& desired, const AceResConfig& supported)
636 {
637     return !(!IsOrientationMatch(desired, supported) || !IsDeviceTypeMatch(desired, supported));
638 }
639 
GetConfigString(KeyType type,int32_t value,std::string & buf)640 void GetConfigString(KeyType type, int32_t value, std::string& buf)
641 {
642     std::string appending;
643     if (!buf.empty()) {
644         appending = "-";
645     }
646 
647     switch (type) {
648         case KeyType::ORIENTATION:
649             appending.append(
650                 AceResKeyParser::GetInstance().GetOrientationByType(static_cast<DeviceOrientation>(value)));
651             break;
652         case KeyType::DEVICETYPE:
653             appending.append(AceResKeyParser::GetInstance().GetDeviceByType(static_cast<DeviceType>(value)));
654             break;
655         case KeyType::RESOLUTION:
656             appending.append(AceResKeyParser::GetInstance().GetResolutionByType(static_cast<ResolutionType>(value)));
657             break;
658         case KeyType::COLOR_MODE:
659             appending.append(AceResKeyParser::GetInstance().GetColorModeByType(static_cast<ColorMode>(value)));
660             break;
661         case KeyType::DECLARATIVE_COLOR_MODE:
662             appending.append(
663                 AceResKeyParser::GetInstance().GetDeclarativeColorModeByType(static_cast<ColorMode>(value)));
664             break;
665         case KeyType::MCC:
666             appending.append(AceResKeyParser::GetInstance().GetMccByValue(value));
667             break;
668         case KeyType::MNC_SHORT_LEN:
669             appending.append(AceResKeyParser::GetInstance().GetMncShortLenByValue(value));
670             break;
671         case KeyType::MNC:
672             appending.append(AceResKeyParser::GetInstance().GetMncByValue(value));
673             break;
674         case KeyType::SCREENSHAPE:
675             appending.append(AceResKeyParser::GetInstance().GetScreenShapeByType(static_cast<ScreenShape>(value)));
676             break;
677         case KeyType::LONGSCREEN:
678             appending.append(AceResKeyParser::GetInstance().GetScreenLongByType(static_cast<LongScreenType>(value)));
679             break;
680         default:
681             break;
682     }
683 
684     if (appending != "" && appending != "-") {
685         buf.append(appending);
686     }
687 }
688 
689 } // namespace
690 
operator ==(const AceResConfig & other) const691 bool AceResConfig::operator==(const AceResConfig& other) const
692 {
693     return language_ == other.language_ && script_ == other.script_ && region_ == other.region_ && mcc_ == other.mcc_ &&
694            mnc_ == other.mnc_ && screenLong_ == other.screenLong_ && screenShape_ == other.screenShape_ &&
695            orientation_ == other.orientation_ && colorMode_ == other.colorMode_ && deviceType_ == other.deviceType_ &&
696            resolution_ == other.resolution_;
697 }
698 
GetLocaleFallback(const std::string & localeTag,const std::vector<std::string> & localeList)699 std::vector<std::string> AceResConfig::GetLocaleFallback(
700     const std::string& localeTag, const std::vector<std::string>& localeList)
701 {
702     ACE_SCOPED_TRACE("GetLocaleFallback");
703     std::vector<std::string> fileList;
704     AceResConfig::MatchAndSortI18nConfigs(localeList, localeTag, fileList);
705     return fileList;
706 }
707 
GetResourceFallback(const std::vector<std::string> & resourceList)708 std::vector<std::string> AceResConfig::GetResourceFallback(const std::vector<std::string>& resourceList)
709 {
710     ACE_SCOPED_TRACE("GetResourceFallback");
711     std::string deviceConfigTag = GetCurrentDeviceResTag();
712     std::vector<std::string> fileList;
713     AceResConfig::MatchAndSortResConfigs(resourceList, deviceConfigTag, fileList);
714     return fileList;
715 }
716 
GetStyleResourceFallback(const std::vector<std::string> & resourceList)717 std::vector<std::string> AceResConfig::GetStyleResourceFallback(const std::vector<std::string>& resourceList)
718 {
719     std::vector<std::string> fileList;
720     std::string deviceConfigTag = GetCurrentDeviceResTag();
721     AceResConfig::MatchAndSortStyleResConfigs(resourceList, deviceConfigTag, fileList);
722     return fileList;
723 }
724 
GetDeclarativeResourceFallback(const std::set<std::string> & resourceFolderList)725 std::vector<std::string> AceResConfig::GetDeclarativeResourceFallback(const std::set<std::string>& resourceFolderList)
726 {
727     std::string deviceConfigTag = GetCurrentDeviceDeclarativeResTag();
728     std::vector<std::string> folderList;
729     AceResConfig::MatchAndSortDeclarativeResConfigs(resourceFolderList, deviceConfigTag, folderList);
730     return folderList;
731 }
732 
GetCurrentDeviceResTag()733 std::string AceResConfig::GetCurrentDeviceResTag()
734 {
735     ResolutionType resolutionType = AceResConfig::GetResolutionType(PipelineBase::GetCurrentDensity());
736     AceResConfig deviceResConfig = AceResConfig(SystemProperties::GetMcc(), SystemProperties::GetMnc(),
737         SystemProperties::GetDeviceOrientation(), SystemProperties::GetColorMode(),
738         (SystemProperties::GetParamDeviceType() == "tablet" || SystemProperties::GetParamDeviceType() == "2in1")
739             ? DeviceType::TABLET
740             : SystemProperties::GetDeviceType(),
741         resolutionType);
742     return AceResConfig::ConvertResConfigToTag(deviceResConfig, false);
743 }
744 
GetCurrentDeviceDeclarativeResTag()745 std::string AceResConfig::GetCurrentDeviceDeclarativeResTag()
746 {
747     auto localeTag = Localization::GetInstance()->GetLanguageTag();
748     std::string language, script, region;
749     Localization::ParseLocaleTag(localeTag, language, script, region, false);
750 
751     ResolutionType resolutionType = AceResConfig::GetResolutionType(PipelineBase::GetCurrentDensity());
752     LongScreenType longScreenType = AceResConfig::GetLongScreenType(PipelineBase::GetCurrentDensity());
753     AceResConfig deviceResConfig;
754 
755     deviceResConfig = AceResConfig(language, script, region, longScreenType, SystemProperties::GetScreenShape(),
756         SystemProperties::GetDeviceOrientation(), SystemProperties::GetColorMode(),
757         (SystemProperties::GetParamDeviceType() == "tablet" || SystemProperties::GetParamDeviceType() == "2in1")
758             ? DeviceType::TABLET
759             : SystemProperties::GetDeviceType(),
760         resolutionType);
761 
762     return AceResConfig::ConvertDeclarativeResConfigToTag(deviceResConfig);
763 }
764 
GetTargetMediaScaleRatio(const std::string & targetResTag)765 double AceResConfig::GetTargetMediaScaleRatio(const std::string& targetResTag)
766 {
767     std::string deviceConfigTag = GetCurrentDeviceDeclarativeResTag();
768     auto deviceConfig = AceResConfig::ConvertDeclarativeResTagToConfig(deviceConfigTag);
769     ResolutionType deviceResolution = (deviceConfig.resolution_ != ResolutionType::RESOLUTION_NONE)
770                                           ? deviceConfig.resolution_
771                                           : ResolutionType::RESOLUTION_MDPI;
772 
773     ResolutionType targetResolution;
774     if (targetResTag == "default") {
775         targetResolution = ResolutionType::RESOLUTION_MDPI;
776     } else {
777         AceResConfig targetConfig = AceResConfig::ConvertDeclarativeResTagToConfig(targetResTag);
778         targetResolution = (targetConfig.resolution_ != ResolutionType::RESOLUTION_NONE)
779                                ? targetConfig.resolution_
780                                : ResolutionType::RESOLUTION_MDPI;
781     }
782 
783     return static_cast<double>(deviceResolution) / static_cast<double>(targetResolution);
784 }
785 
MatchAndSortI18nConfigs(const std::vector<std::string> & candidatesFiles,const std::string & devicesLocaleTag,std::vector<std::string> & fileList)786 void AceResConfig::MatchAndSortI18nConfigs(const std::vector<std::string>& candidatesFiles,
787     const std::string& devicesLocaleTag, std::vector<std::string>& fileList)
788 {
789     std::vector<AceResConfig> candidateLocaleConfigs;
790     for (auto& file : candidatesFiles) {
791         AceResConfig LocaleConfig = ConvertLocaleTagToConfig(file);
792         if (file == ConvertLocaleConfigToTag(LocaleConfig)) {
793             candidateLocaleConfigs.emplace_back(LocaleConfig);
794         }
795     }
796     AceResConfig deviceLocaleConfig = ConvertLocaleTagToConfig(devicesLocaleTag);
797     // Compute Script when it is not provided.
798     if (deviceLocaleConfig.script_.empty()) {
799         deviceLocaleConfig.script_ = ComputeScript(deviceLocaleConfig);
800     }
801     std::vector<AceResConfig> matchedLocaleConfigs;
802     MatchLocaleConfig(candidateLocaleConfigs, deviceLocaleConfig, matchedLocaleConfigs);
803     int32_t left = 0;
804     int32_t right = static_cast<int32_t>(matchedLocaleConfigs.size()) - 1;
805     SortLocaleConfigs(matchedLocaleConfigs, deviceLocaleConfig, left, right);
806     bool existDefault = false;
807     for (const auto& matchedLocaleConfig : matchedLocaleConfigs) {
808         std::string localeConfigTag = ConvertLocaleConfigToTag(matchedLocaleConfig);
809         if (localeConfigTag == "en-US") {
810             existDefault = true;
811         }
812         fileList.emplace_back(localeConfigTag);
813     }
814 
815     if (!existDefault) {
816         fileList.emplace_back("en-US");
817     }
818 }
819 
MatchAndSortResConfigs(const std::vector<std::string> & candidateFiles,const std::string & deviceResTag,std::vector<std::string> & matchedFileList,bool styleRes)820 void AceResConfig::MatchAndSortResConfigs(const std::vector<std::string>& candidateFiles,
821     const std::string& deviceResTag, std::vector<std::string>& matchedFileList, bool styleRes)
822 {
823     std::vector<AceResConfig> candidateResConfigs;
824     for (auto& file : candidateFiles) {
825         AceResConfig ResConfig = ConvertResTagToConfig(file, styleRes);
826         if (file == ConvertResConfigToTag(ResConfig, styleRes)) {
827             candidateResConfigs.emplace_back(ResConfig);
828         }
829     }
830 
831     AceResConfig deviceResConfig = ConvertResTagToConfig(deviceResTag, false);
832     int32_t candidateConfigSize = static_cast<int32_t>(candidateResConfigs.size());
833     std::vector<AceResConfig> matchedResConfigs;
834     for (auto i = 0; i < candidateConfigSize; i++) {
835         if (!MatchResConfig(deviceResConfig, candidateResConfigs[i])) {
836             continue;
837         } else {
838             matchedResConfigs.emplace_back(candidateResConfigs[i]);
839         }
840     }
841     int32_t left = 0;
842     int32_t right = static_cast<int32_t>(matchedResConfigs.size()) - 1;
843     SortResConfigs(deviceResConfig, matchedResConfigs, left, right);
844     for (const auto& matchedConfig : matchedResConfigs) {
845         matchedFileList.emplace_back(ConvertResConfigToTag(matchedConfig, styleRes));
846     }
847 
848     if (styleRes) {
849         matchedFileList.emplace_back("default");
850     } else {
851         matchedFileList.emplace_back("res-defaults");
852     }
853 }
854 
MatchAndSortStyleResConfigs(const std::vector<std::string> & candidateFiles,const std::string & deviceResTag,std::vector<std::string> & matchedFileList)855 void AceResConfig::MatchAndSortStyleResConfigs(const std::vector<std::string>& candidateFiles,
856     const std::string& deviceResTag, std::vector<std::string>& matchedFileList)
857 {
858     MatchAndSortResConfigs(candidateFiles, deviceResTag, matchedFileList, true);
859 }
860 
MatchAndSortDeclarativeResConfigs(const std::set<std::string> & candidateFolders,const std::string & deviceConfigTag,std::vector<std::string> & matchedFoldersList)861 void AceResConfig::MatchAndSortDeclarativeResConfigs(const std::set<std::string>& candidateFolders,
862     const std::string& deviceConfigTag, std::vector<std::string>& matchedFoldersList)
863 {
864     std::vector<AceResConfig> candidateResConfigs;
865     for (auto& folder : candidateFolders) {
866         if (folder == "default") {
867             continue;
868         }
869         AceResConfig resConfig = ConvertDeclarativeResTagToConfig(folder);
870         if (folder == ConvertDeclarativeResConfigToTag(resConfig)) {
871             candidateResConfigs.emplace_back(resConfig);
872         }
873     }
874 
875     AceResConfig deviceResConfig = ConvertDeclarativeResTagToConfig(deviceConfigTag);
876     // Compute Script when it is not provided.
877     if (deviceResConfig.script_.empty()) {
878         deviceResConfig.script_ = ComputeScript(deviceResConfig);
879     }
880 
881     std::vector<AceResConfig> matchedLocaleConfigs;
882     MatchLocaleConfig(candidateResConfigs, deviceResConfig, matchedLocaleConfigs, true);
883     int32_t candidateConfigSize = static_cast<int32_t>(matchedLocaleConfigs.size());
884     std::vector<AceResConfig> matchedResConfigs;
885     for (auto i = 0; i < candidateConfigSize; i++) {
886         if (!DeclarativeMatchResConfig(deviceResConfig, matchedLocaleConfigs[i])) {
887             continue;
888         } else {
889             matchedResConfigs.emplace_back(matchedLocaleConfigs[i]);
890         }
891     }
892 
893     int32_t left = 0;
894     int32_t right = static_cast<int32_t>(matchedResConfigs.size()) - 1;
895     SortDeclarativeResConfigs(deviceResConfig, matchedResConfigs, left, right);
896     for (const auto& matchedConfig : matchedResConfigs) {
897         matchedFoldersList.emplace_back(ConvertDeclarativeResConfigToTag(matchedConfig));
898     }
899 
900     matchedFoldersList.emplace_back("default");
901 }
902 
ParseConfig(const std::vector<KeyParam> & keyParams)903 bool AceResConfig::ParseConfig(const std::vector<KeyParam>& keyParams)
904 {
905     for (auto keyParam : keyParams) {
906         switch (keyParam.keyType) {
907             case KeyType::RESOLUTION:
908                 resolution_ = static_cast<ResolutionType>(keyParam.value);
909                 break;
910             case KeyType::ORIENTATION:
911                 orientation_ = static_cast<DeviceOrientation>(keyParam.value);
912                 break;
913             case KeyType::DEVICETYPE:
914                 deviceType_ = static_cast<DeviceType>(keyParam.value);
915                 break;
916             case KeyType::DECLARATIVE_COLOR_MODE:
917             case KeyType::COLOR_MODE:
918                 colorMode_ = static_cast<ColorMode>(keyParam.value);
919                 break;
920             case KeyType::MCC:
921                 mcc_ = keyParam.value;
922                 break;
923             case KeyType::MNC:
924                 mnc_ = keyParam.value;
925                 break;
926             case KeyType::MNC_SHORT_LEN:
927                 mnc_ = keyParam.value;
928                 mncShortLen_ = true;
929                 break;
930             case KeyType::SCREENSHAPE:
931                 screenShape_ = static_cast<ScreenShape>(keyParam.value);
932                 break;
933             case KeyType::LONGSCREEN:
934                 screenLong_ = static_cast<LongScreenType>(keyParam.value);
935                 break;
936             default:
937                 break;
938         }
939     }
940     return true;
941 }
942 
GetResolutionType(double resolution)943 ResolutionType AceResConfig::GetResolutionType(double resolution)
944 {
945     static const LinearEnumMapNode<ResolutionType, bool (*)(double)> resolutionMap[] = {
946         { ResolutionType::RESOLUTION_LDPI,
947             [](double resolution) { return GreatNotEqual(resolution, 0.0) && LessNotEqual(resolution, 0.875); } },
948         { ResolutionType::RESOLUTION_MDPI,
949             [](double resolution) { return GreatOrEqual(resolution, 0.875) && LessNotEqual(resolution, 1.25); } },
950         { ResolutionType::RESOLUTION_HDPI,
951             [](double resolution) { return GreatOrEqual(resolution, 1.25) && LessNotEqual(resolution, 1.75); } },
952         { ResolutionType::RESOLUTION_XHDPI,
953             [](double resolution) { return GreatOrEqual(resolution, 1.75) && LessNotEqual(resolution, 2.5); } },
954         { ResolutionType::RESOLUTION_XXHDPI,
955             [](double resolution) { return GreatOrEqual(resolution, 2.5) && LessNotEqual(resolution, 3.5); } },
956         { ResolutionType::RESOLUTION_XXXHDPI, [](double resolution) { return GreatOrEqual(resolution, 3.5); } },
957     };
958 
959     for (const auto& idx : resolutionMap) {
960         if (idx.value(resolution)) {
961             return idx.key;
962         }
963     }
964     return ResolutionType::RESOLUTION_MDPI;
965 }
966 
GetLongScreenType(double resolution)967 LongScreenType AceResConfig::GetLongScreenType(double resolution)
968 {
969     if (GreatNotEqual(resolution, 1.5)) {
970         return LongScreenType::LONG;
971     } else if (GreatNotEqual(resolution, 0.0) && LessOrEqual(resolution, 1.5)) {
972         return LongScreenType::NOT_LONG;
973     }
974 
975     return LongScreenType::NOT_LONG;
976 }
977 
ConvertResTagToConfig(const std::string & deviceResConfigTag,bool styleRes)978 AceResConfig AceResConfig::ConvertResTagToConfig(const std::string& deviceResConfigTag, bool styleRes)
979 {
980     AceResConfig resConfig;
981     std::vector<KeyParam> keyParams;
982     bool parseSucceed = AceResKeyParser::GetInstance().Parse(deviceResConfigTag, keyParams, styleRes);
983     if (parseSucceed) {
984         resConfig.ParseConfig(keyParams);
985     }
986     return resConfig;
987 }
988 
ConvertDeclarativeResTagToConfig(const std::string & deviceResConfigTag)989 AceResConfig AceResConfig::ConvertDeclarativeResTagToConfig(const std::string& deviceResConfigTag)
990 {
991     AceResConfig resConfig;
992     std::vector<KeyParam> keyParams;
993     if (deviceResConfigTag.find_first_of("-") == std::string::npos) {
994         if (deviceResConfigTag.find_first_of("_") == std::string::npos) {
995             bool parseSucceed = AceResKeyParser::GetInstance().DeclarativeParse(deviceResConfigTag, keyParams);
996             if (parseSucceed) {
997                 resConfig = AceResConfig("", "", "");
998                 resConfig.ParseConfig(keyParams);
999                 return resConfig;
1000             }
1001         }
1002 
1003         std::string tempResTag(deviceResConfigTag);
1004         std::replace(tempResTag.begin(), tempResTag.end(), '_', '-');
1005         std::string language, script, region;
1006         Localization::ParseLocaleTag(tempResTag, language, script, region, false);
1007         resConfig = AceResConfig(language, script, region);
1008 
1009         return resConfig;
1010     }
1011 
1012     resConfig = AceResConfig("", "", "");
1013     bool parseSucceed = AceResKeyParser::GetInstance().DeclarativeParse(deviceResConfigTag, keyParams);
1014     if (parseSucceed) {
1015         resConfig.ParseConfig(keyParams);
1016         return resConfig;
1017     }
1018 
1019     std::string localeTag = deviceResConfigTag.substr(0, deviceResConfigTag.find_first_of("-"));
1020     std::string resConfigTag = deviceResConfigTag.substr(deviceResConfigTag.find_first_of("-"));
1021     std::replace(localeTag.begin(), localeTag.end(), '_', '-');
1022 
1023     std::string language, script, region;
1024     Localization::ParseLocaleTag(localeTag, language, script, region, false);
1025     resConfig = AceResConfig(language, script, region);
1026 
1027     parseSucceed = AceResKeyParser::GetInstance().DeclarativeParse(resConfigTag, keyParams);
1028     if (parseSucceed) {
1029         resConfig.ParseConfig(keyParams);
1030     }
1031 
1032     return resConfig;
1033 }
1034 
ConvertResConfigToTag(const AceResConfig & resConfig,bool styleRes)1035 std::string AceResConfig::ConvertResConfigToTag(const AceResConfig& resConfig, bool styleRes)
1036 {
1037     std::string resTag;
1038     if (!styleRes) {
1039         resTag = "res";
1040     }
1041     if (resConfig.mcc_ != MCC_UNDEFINED && resConfig.mnc_ != MNC_UNDEFINED) {
1042         GetConfigString(KeyType::MCC, resConfig.mcc_, resTag);
1043         if (resConfig.mncShortLen_) {
1044             GetConfigString(KeyType::MNC_SHORT_LEN, resConfig.mnc_, resTag);
1045         } else {
1046             GetConfigString(KeyType::MNC, resConfig.mnc_, resTag);
1047         }
1048     }
1049 
1050     if (resConfig.orientation_ != DeviceOrientation::ORIENTATION_UNDEFINED) {
1051         GetConfigString(KeyType::ORIENTATION, static_cast<int32_t>(resConfig.orientation_), resTag);
1052     }
1053 
1054     if (resConfig.colorMode_ != ColorMode::COLOR_MODE_UNDEFINED) {
1055         GetConfigString(KeyType::COLOR_MODE, static_cast<int32_t>(resConfig.colorMode_), resTag);
1056     }
1057 
1058     if (resConfig.deviceType_ != DeviceType::UNKNOWN) {
1059         GetConfigString(KeyType::DEVICETYPE, static_cast<int32_t>(resConfig.deviceType_), resTag);
1060     }
1061 
1062     if (resConfig.resolution_ != ResolutionType::RESOLUTION_NONE) {
1063         GetConfigString(KeyType::RESOLUTION, static_cast<int32_t>(resConfig.resolution_), resTag);
1064     }
1065     return resTag;
1066 }
1067 
ConvertDeclarativeResConfigToTag(const AceResConfig & resConfig)1068 std::string AceResConfig::ConvertDeclarativeResConfigToTag(const AceResConfig& resConfig)
1069 {
1070     std::string resTag = resConfig.language_;
1071     if (!resConfig.script_.empty()) {
1072         resTag += ('_' + resConfig.script_);
1073     }
1074     if (!resConfig.region_.empty()) {
1075         resTag += ('_' + resConfig.region_);
1076     }
1077 
1078     if (resConfig.screenLong_ != LongScreenType::LONG_SCREEN_UNDEFINED) {
1079         GetConfigString(KeyType::LONGSCREEN, static_cast<int32_t>(resConfig.screenLong_), resTag);
1080     }
1081 
1082     if (resConfig.screenShape_ != ScreenShape::SCREEN_SHAPE_UNDEFINED) {
1083         GetConfigString(KeyType::SCREENSHAPE, static_cast<int32_t>(resConfig.screenShape_), resTag);
1084     }
1085 
1086     if (resConfig.orientation_ != DeviceOrientation::ORIENTATION_UNDEFINED) {
1087         GetConfigString(KeyType::ORIENTATION, static_cast<int32_t>(resConfig.orientation_), resTag);
1088     }
1089 
1090     if (resConfig.colorMode_ != ColorMode::COLOR_MODE_UNDEFINED) {
1091         GetConfigString(KeyType::DECLARATIVE_COLOR_MODE, static_cast<int32_t>(resConfig.colorMode_), resTag);
1092     }
1093 
1094     if (resConfig.deviceType_ != DeviceType::UNKNOWN) {
1095         GetConfigString(KeyType::DEVICETYPE, static_cast<int32_t>(resConfig.deviceType_), resTag);
1096     }
1097 
1098     if (resConfig.resolution_ != ResolutionType::RESOLUTION_NONE) {
1099         GetConfigString(KeyType::RESOLUTION, static_cast<int32_t>(resConfig.resolution_), resTag);
1100     }
1101     return resTag;
1102 }
1103 
1104 } // namespace OHOS::Ace
1105