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 "res_config_impl.h"
16 #ifdef SUPPORT_GRAPHICS
17 #include <unicode/localebuilder.h>
18 #include <unicode/locid.h>
19 #include <unicode/utypes.h>
20 #endif
21 #include "locale_matcher.h"
22 #include "res_locale.h"
23 #include "utils/utils.h"
24 #ifdef SUPPORT_GRAPHICS
25 using icu::Locale;
26 using icu::LocaleBuilder;
27 #endif
28 namespace OHOS {
29 namespace Global {
30 namespace Resource {
31
32 static const std::vector<std::pair<float, ScreenDensity>> resolutions = {
33 { 0.0, ScreenDensity::SCREEN_DENSITY_NOT_SET },
34 { 120.0, ScreenDensity::SCREEN_DENSITY_SDPI },
35 { 160.0, ScreenDensity::SCREEN_DENSITY_MDPI },
36 { 240.0, ScreenDensity::SCREEN_DENSITY_LDPI },
37 { 320.0, ScreenDensity::SCREEN_DENSITY_XLDPI },
38 { 480.0, ScreenDensity::SCREEN_DENSITY_XXLDPI },
39 { 640.0, ScreenDensity::SCREEN_DENSITY_XXXLDPI },
40 };
41
ResConfigImpl()42 ResConfigImpl::ResConfigImpl()
43 : resLocale_(nullptr),
44 direction_(DIRECTION_NOT_SET),
45 density_(SCREEN_DENSITY_NOT_SET),
46 screenDensityDpi_(SCREEN_DENSITY_NOT_SET),
47 colorMode_(LIGHT),
48 mcc_(MCC_UNDEFINED),
49 mnc_(MNC_UNDEFINED),
50 deviceType_(DEVICE_NOT_SET),
51 inputDevice_(INPUTDEVICE_NOT_SET),
52 #ifdef SUPPORT_GRAPHICS
53 resPreferredLocale_(nullptr),
54 preferredLocaleInfo_(nullptr),
55 localeInfo_(nullptr),
56 #endif
57 isCompletedScript_(false),
58 isAppColorMode_(false),
59 isAppDarkRes_(false),
60 themeId_(0)
61 {}
62
63 #ifdef SUPPORT_GRAPHICS
SetPreferredLocaleInfo(Locale & preferredLocaleInfo)64 RState ResConfigImpl::SetPreferredLocaleInfo(Locale &preferredLocaleInfo)
65 {
66 const char *language = preferredLocaleInfo.getLanguage();
67 const char *script = preferredLocaleInfo.getScript();
68 const char *region = preferredLocaleInfo.getCountry();
69 if (Utils::IsStrEmpty(language)) {
70 delete this->resPreferredLocale_;
71 delete this->preferredLocaleInfo_;
72 this->resPreferredLocale_ = nullptr;
73 this->preferredLocaleInfo_ = nullptr;
74 return SUCCESS;
75 }
76 RState state = BuildResLocale(language, script, region, &this->resPreferredLocale_);
77 if (state != SUCCESS) {
78 return state;
79 }
80 return BuildLocaleInfo(this->resPreferredLocale_, &this->preferredLocaleInfo_);
81 }
82
SetLocaleInfo(Locale & localeInfo)83 RState ResConfigImpl::SetLocaleInfo(Locale &localeInfo)
84 {
85 return this->SetLocaleInfo(localeInfo.getLanguage(), localeInfo.getScript(), localeInfo.getCountry());
86 }
87
BuildResLocale(const char * language,const char * script,const char * region,ResLocale ** resLocale)88 RState ResConfigImpl::BuildResLocale(const char *language, const char *script,
89 const char *region, ResLocale **resLocale)
90 {
91 RState state = SUCCESS;
92 ResLocale *temp = ResLocale::BuildFromParts(language, script, region, state);
93 if (state != SUCCESS) {
94 return state;
95 }
96 if (script == nullptr || script[0] == '\0') {
97 if (!LocaleMatcher::Normalize(temp)) {
98 delete temp;
99 temp = nullptr;
100 return NOT_ENOUGH_MEM;
101 }
102 }
103 delete *resLocale;
104 *resLocale = temp;
105 return state;
106 }
107
BuildLocaleInfo(const ResLocale * resLocale,Locale ** localeInfo)108 RState ResConfigImpl::BuildLocaleInfo(const ResLocale *resLocale, Locale **localeInfo)
109 {
110 UErrorCode errCode = U_ZERO_ERROR;
111 Locale temp = icu::LocaleBuilder().setLanguage(resLocale->GetLanguage())
112 .setRegion(resLocale->GetRegion()).setScript(resLocale->GetScript()).build(errCode);
113 if (!U_SUCCESS(errCode)) {
114 return NOT_ENOUGH_MEM;
115 }
116 delete *localeInfo;
117 *localeInfo = new Locale(temp);
118 return SUCCESS;
119 }
120 #endif
121
SetLocaleInfo(const char * localeStr)122 RState ResConfigImpl::SetLocaleInfo(const char* localeStr)
123 {
124 #ifdef SUPPORT_GRAPHICS
125 icu::Locale localeInfo(localeStr);
126 return this->SetLocaleInfo(localeInfo);
127 #endif
128 return SUCCESS;
129 }
130
SetLocaleInfo(const char * language,const char * script,const char * region)131 RState ResConfigImpl::SetLocaleInfo(const char *language,
132 const char *script,
133 const char *region)
134 {
135 #ifdef SUPPORT_GRAPHICS
136 RState state = SUCCESS;
137 if (Utils::IsStrEmpty(language)) {
138 delete this->resLocale_;
139 delete this->localeInfo_;
140 this->resLocale_ = nullptr;
141 this->localeInfo_ = nullptr;
142 return state;
143 }
144 this->isCompletedScript_ = false;
145 state = BuildResLocale(language, script, region, &this->resLocale_);
146 if (state != SUCCESS) {
147 return state;
148 }
149 state = BuildLocaleInfo(this->resLocale_, &this->localeInfo_);
150 if (state != SUCCESS) {
151 return state;
152 }
153 this->isCompletedScript_ = true;
154 return state;
155 #else
156 return NOT_SUPPORT_SEP;
157 #endif
158 }
159
SetDeviceType(DeviceType deviceType)160 void ResConfigImpl::SetDeviceType(DeviceType deviceType)
161 {
162 this->deviceType_ = deviceType;
163 }
164
SetDirection(Direction direction)165 void ResConfigImpl::SetDirection(Direction direction)
166 {
167 this->direction_ = direction;
168 }
169
SetColorMode(ColorMode colorMode)170 void ResConfigImpl::SetColorMode(ColorMode colorMode)
171 {
172 this->colorMode_ = colorMode;
173 }
174
SetInputDevice(InputDevice inputDevice)175 void ResConfigImpl::SetInputDevice(InputDevice inputDevice)
176 {
177 this->inputDevice_ = inputDevice;
178 }
179
SetMcc(uint32_t mcc)180 void ResConfigImpl::SetMcc(uint32_t mcc)
181 {
182 this->mcc_ = mcc;
183 }
184
SetMnc(uint32_t mnc)185 void ResConfigImpl::SetMnc(uint32_t mnc)
186 {
187 this->mnc_ = mnc;
188 }
189
SetThemeId(uint32_t themeId)190 void ResConfigImpl::SetThemeId(uint32_t themeId)
191 {
192 this->themeId_ = themeId;
193 }
194
ConvertDensity(float density)195 ScreenDensity ResConfigImpl::ConvertDensity(float density)
196 {
197 float deviceDpi = density * Utils::DPI_BASE;
198 auto resolution = SCREEN_DENSITY_NOT_SET;
199 for (const auto& [dpi, value] : resolutions) {
200 resolution = value;
201 if (deviceDpi <= dpi) {
202 break;
203 }
204 }
205 return resolution;
206 }
207
SetScreenDensity(float screenDensity)208 void ResConfigImpl::SetScreenDensity(float screenDensity)
209 {
210 this->density_ = screenDensity;
211 this->screenDensityDpi_ = ConvertDensity(screenDensity);
212 }
213
SetScreenDensityDpi(ScreenDensity screenDensityDpi)214 void ResConfigImpl::SetScreenDensityDpi(ScreenDensity screenDensityDpi)
215 {
216 this->density_ = screenDensityDpi / Utils::DPI_BASE;
217 this->screenDensityDpi_ = screenDensityDpi;
218 }
219
220 #ifdef SUPPORT_GRAPHICS
GetResPreferredLocale() const221 const ResLocale *ResConfigImpl::GetResPreferredLocale() const
222 {
223 return this->resPreferredLocale_;
224 }
225
GetPreferredLocaleInfo() const226 const Locale *ResConfigImpl::GetPreferredLocaleInfo() const
227 {
228 return this->preferredLocaleInfo_;
229 }
230
GetLocaleInfo() const231 const Locale *ResConfigImpl::GetLocaleInfo() const
232 {
233 return localeInfo_;
234 }
235 #endif
236
GetResLocale() const237 const ResLocale *ResConfigImpl::GetResLocale() const
238 {
239 return this->resLocale_;
240 }
241
GetDirection() const242 Direction ResConfigImpl::GetDirection() const
243 {
244 return this->direction_;
245 }
246
GetScreenDensity() const247 float ResConfigImpl::GetScreenDensity() const
248 {
249 return this->density_;
250 }
251
GetScreenDensityDpi() const252 ScreenDensity ResConfigImpl::GetScreenDensityDpi() const
253 {
254 return this->screenDensityDpi_;
255 }
256
GetColorMode() const257 ColorMode ResConfigImpl::GetColorMode() const
258 {
259 return this->colorMode_;
260 }
261
GetInputDevice() const262 InputDevice ResConfigImpl::GetInputDevice() const
263 {
264 return this->inputDevice_;
265 }
266
GetMcc() const267 uint32_t ResConfigImpl::GetMcc() const
268 {
269 return this->mcc_;
270 }
271
GetMnc() const272 uint32_t ResConfigImpl::GetMnc() const
273 {
274 return this->mnc_;
275 }
276
GetThemeId() const277 uint32_t ResConfigImpl::GetThemeId() const
278 {
279 return this->themeId_;
280 }
281
GetDeviceType() const282 DeviceType ResConfigImpl::GetDeviceType() const
283 {
284 return this->deviceType_;
285 }
286
287 #ifdef SUPPORT_GRAPHICS
CopyLocale(Locale ** currentLocaleInfo,ResLocale ** currentResLocale,const Locale * otherLocaleInfo)288 bool ResConfigImpl::CopyLocale(Locale **currentLocaleInfo, ResLocale **currentResLocale,
289 const Locale *otherLocaleInfo)
290 {
291 bool needCopy = false;
292 if (*currentLocaleInfo == nullptr && otherLocaleInfo != nullptr) {
293 needCopy = true;
294 }
295 if (*currentLocaleInfo != nullptr && otherLocaleInfo == nullptr) {
296 delete *currentResLocale;
297 delete *currentLocaleInfo;
298 *currentResLocale = nullptr;
299 *currentLocaleInfo = nullptr;
300 return true;
301 }
302 if (*currentResLocale != nullptr && otherLocaleInfo != nullptr) {
303 uint64_t encodedLocale = Utils::EncodeLocale(
304 (*currentResLocale)->GetLanguage(),
305 (*currentResLocale)->GetScript(), (*currentResLocale)->GetRegion());
306 uint64_t otherEncodedLocale = Utils::EncodeLocale(
307 otherLocaleInfo->getLanguage(),
308 otherLocaleInfo->getScript(), otherLocaleInfo->getCountry());
309 if (encodedLocale != otherEncodedLocale) {
310 needCopy = true;
311 }
312 }
313 if (needCopy) {
314 ResLocale *temp = new(std::nothrow) ResLocale;
315 if (temp == nullptr) {
316 return false;
317 }
318 RState rs = temp->CopyFromLocaleInfo(otherLocaleInfo);
319 if (rs != SUCCESS) {
320 delete temp;
321 return false;
322 }
323 UErrorCode errCode = U_ZERO_ERROR;
324 Locale tempLocale = icu::LocaleBuilder().setLocale(*otherLocaleInfo).build(errCode);
325
326 if (!U_SUCCESS(errCode)) {
327 delete temp;
328 return false;
329 }
330 delete *currentResLocale;
331 delete *currentLocaleInfo;
332 *currentResLocale = temp;
333 *currentLocaleInfo = new Locale(tempLocale);
334 }
335 return true;
336 }
337 #endif
338
339 #ifdef SUPPORT_GRAPHICS
CopyPreferredLocale(ResConfig & other)340 bool ResConfigImpl::CopyPreferredLocale(ResConfig &other)
341 {
342 return CopyLocale(&this->preferredLocaleInfo_, &this->resPreferredLocale_, other.GetPreferredLocaleInfo());
343 }
344 #endif
345
CopyLocale(ResConfig & other)346 bool ResConfigImpl::CopyLocale(ResConfig &other)
347 {
348 #ifdef SUPPORT_GRAPHICS
349 return CopyLocale(&this->localeInfo_, &this->resLocale_, other.GetLocaleInfo());
350 #else
351 return false;
352 #endif
353 }
354
isLocaleInfoSet()355 bool ResConfigImpl::isLocaleInfoSet()
356 {
357 #ifdef SUPPORT_GRAPHICS
358 return localeInfo_ != nullptr;
359 #endif
360 return false;
361 }
362
CopyLocaleAndPreferredLocale(ResConfig & other)363 bool ResConfigImpl::CopyLocaleAndPreferredLocale(ResConfig &other)
364 {
365 if (!this->CopyLocale(other)) {
366 return false;
367 }
368 #ifdef SUPPORT_GRAPHICS
369 if (!this->CopyPreferredLocale(other)) {
370 return false;
371 }
372 #endif
373 return true;
374 }
375
Copy(ResConfig & other,bool isRead)376 bool ResConfigImpl::Copy(ResConfig &other, bool isRead)
377 {
378 if (!this->CopyLocaleAndPreferredLocale(other)) {
379 return false;
380 }
381 if (this->GetDeviceType() != other.GetDeviceType()) {
382 this->SetDeviceType(other.GetDeviceType());
383 }
384 if (this->GetDirection() != other.GetDirection()) {
385 this->SetDirection(other.GetDirection());
386 }
387 if (this->GetColorMode() != other.GetColorMode()) {
388 this->SetColorMode(other.GetColorMode());
389 }
390 if (this->GetInputDevice() != other.GetInputDevice()) {
391 this->SetInputDevice(other.GetInputDevice());
392 }
393 if (this->GetMcc() != other.GetMcc()) {
394 this->SetMcc(other.GetMcc());
395 }
396 if (this->GetMnc() != other.GetMnc()) {
397 this->SetMnc(other.GetMnc());
398 }
399 if (this->GetScreenDensity() != other.GetScreenDensity()) {
400 this->SetScreenDensity(other.GetScreenDensity());
401 }
402 if (this->GetAppColorMode() != other.GetAppColorMode()) {
403 this->SetAppColorMode(other.GetAppColorMode());
404 }
405 if (isRead) {
406 this->SetAppDarkRes(other.GetAppDarkRes());
407 }
408 if (this->GetThemeId() != other.GetThemeId()) {
409 this->SetThemeId(other.GetThemeId());
410 }
411 return true;
412 }
413
Match(const std::shared_ptr<ResConfigImpl> other,bool isCheckDarkAdaptation) const414 bool ResConfigImpl::Match(const std::shared_ptr<ResConfigImpl> other, bool isCheckDarkAdaptation) const
415 {
416 if (other == nullptr) {
417 return false;
418 }
419 if (!IsMccMncMatch(other->mcc_, other->mnc_)) {
420 return false;
421 }
422
423 bool isPreferredLocaleMatch = false;
424 #ifdef SUPPORT_GRAPHICS
425 if (this->resPreferredLocale_ != nullptr) {
426 isPreferredLocaleMatch = true;
427 if (!LocaleMatcher::Match(this->resPreferredLocale_, other->GetResLocale())) {
428 return false;
429 }
430 }
431 #endif
432
433 if (!isPreferredLocaleMatch && !(LocaleMatcher::Match(this->resLocale_, other->GetResLocale()))) {
434 return false;
435 }
436 if (!IsDirectionMatch(other->direction_)) {
437 return false;
438 }
439 if (!IsDeviceTypeMatch(other->deviceType_)) {
440 return false;
441 }
442 if (!IsColorModeMatch(other->colorMode_, isCheckDarkAdaptation)) {
443 return false;
444 }
445 if (!IsInputDeviceMatch(other->inputDevice_)) {
446 return false;
447 }
448 return true;
449 }
450
IsMccMncMatch(uint32_t mcc,uint32_t mnc) const451 bool ResConfigImpl::IsMccMncMatch(uint32_t mcc, uint32_t mnc) const
452 {
453 if (mcc == MCC_UNDEFINED && mnc == MNC_UNDEFINED) {
454 return true;
455 }
456 if (this->mcc_ == mcc) {
457 if (mnc == MNC_UNDEFINED || this->mnc_ == mnc) {
458 return true;
459 }
460 }
461 return false;
462 }
463
IsDirectionMatch(Direction direction) const464 bool ResConfigImpl::IsDirectionMatch(Direction direction) const
465 {
466 if (this->direction_ != DIRECTION_NOT_SET && direction != DIRECTION_NOT_SET) {
467 if (this->direction_ != direction) {
468 return false;
469 }
470 }
471 return true;
472 }
473
IsDeviceTypeMatch(DeviceType deviceType) const474 bool ResConfigImpl::IsDeviceTypeMatch(DeviceType deviceType) const
475 {
476 if (this->deviceType_ != DEVICE_NOT_SET && deviceType != DEVICE_NOT_SET) {
477 if (this->deviceType_ != deviceType) {
478 return false;
479 }
480 }
481 return true;
482 }
483
IsColorModeMatch(ColorMode colorMode,bool isCheckDarkAdaptation) const484 bool ResConfigImpl::IsColorModeMatch(ColorMode colorMode, bool isCheckDarkAdaptation) const
485 {
486 if (isCheckDarkAdaptation && this->colorMode_ == DARK && !this->GetAppColorMode() && !this->GetAppDarkRes()) {
487 return colorMode == COLOR_MODE_NOT_SET;
488 }
489 if (this->colorMode_ != COLOR_MODE_NOT_SET && colorMode != COLOR_MODE_NOT_SET) {
490 if (this->colorMode_ != colorMode) {
491 return false;
492 }
493 }
494 return true;
495 }
496
IsInputDeviceMatch(InputDevice inputDevice) const497 bool ResConfigImpl::IsInputDeviceMatch(InputDevice inputDevice) const
498 {
499 if (this->inputDevice_ == INPUTDEVICE_NOT_SET && inputDevice != INPUTDEVICE_NOT_SET) {
500 return false;
501 }
502 // reserve for future InputDevice expansion
503 if (this->inputDevice_ != INPUTDEVICE_NOT_SET && inputDevice != INPUTDEVICE_NOT_SET) {
504 if (this->inputDevice_ != inputDevice) {
505 return false;
506 }
507 }
508 return true;
509 }
510
511 /**
512 * compare this and target
513 * if this more match request,then return true
514 * else
515 * return false
516 *
517 */
IsMoreSuitable(const std::shared_ptr<ResConfigImpl> other,const std::shared_ptr<ResConfigImpl> request,uint32_t density) const518 bool ResConfigImpl::IsMoreSuitable(const std::shared_ptr<ResConfigImpl> other,
519 const std::shared_ptr<ResConfigImpl> request, uint32_t density) const
520 {
521 if (request != nullptr && other != nullptr) {
522 int ret = IsMccMncMoreSuitable(other->mcc_, other->mnc_, request->mcc_, request->mnc_);
523 if (ret != 0) {
524 return ret > 0;
525 }
526 #ifdef SUPPORT_GRAPHICS
527 if (request->GetResPreferredLocale() != nullptr) {
528 int8_t preferredResult = LocaleMatcher::IsMoreSuitable(this->GetResLocale(), other->GetResLocale(),
529 request->GetResPreferredLocale());
530 if (preferredResult != 0) {
531 return preferredResult > 0;
532 }
533 }
534 #endif
535 int8_t result = LocaleMatcher::IsMoreSuitable(this->GetResLocale(), other->GetResLocale(),
536 request->GetResLocale());
537 if (result != 0) {
538 return result > 0;
539 }
540 /**
541 * direction must full match.
542 * when request is set direction and this is not equal other.
543 * this or other oriention is not set.
544 */
545 if (this->direction_ != other->direction_ &&
546 request->direction_ != Direction::DIRECTION_NOT_SET) {
547 return this->direction_ != Direction::DIRECTION_NOT_SET;
548 }
549 if (this->deviceType_ != other->deviceType_ &&
550 request->deviceType_ != DeviceType::DEVICE_NOT_SET) {
551 return this->deviceType_ != DeviceType::DEVICE_NOT_SET;
552 }
553 if (this->colorMode_ != other->colorMode_ &&
554 request->colorMode_ != ColorMode::COLOR_MODE_NOT_SET) {
555 return this->colorMode_ != ColorMode::COLOR_MODE_NOT_SET;
556 }
557 if (this->inputDevice_ != other->inputDevice_ &&
558 request->inputDevice_ != InputDevice::INPUTDEVICE_NOT_SET) {
559 return this->inputDevice_ != InputDevice::INPUTDEVICE_NOT_SET;
560 }
561 ret = IsDensityMoreSuitable(other->screenDensityDpi_, request->screenDensityDpi_, density);
562 if (ret != 0) {
563 return ret > 0;
564 }
565 }
566 return this->IsMoreSpecificThan(other, density);
567 }
568
569 /**
570 * compare this and target mcc/mnc
571 * if this more match other,then return 1, else if other more match this, return -1,
572 * else
573 * return 0
574 *
575 */
IsMccMncMoreSuitable(uint32_t otherMcc,uint32_t otherMnc,uint32_t requestMcc,uint32_t requestMnc) const576 int ResConfigImpl::IsMccMncMoreSuitable(uint32_t otherMcc, uint32_t otherMnc, uint32_t requestMcc,
577 uint32_t requestMnc) const
578 {
579 int ret = 0;
580 bool defined = requestMcc != MCC_UNDEFINED && requestMnc != MNC_UNDEFINED;
581 bool mccDefined = requestMcc != MCC_UNDEFINED && requestMnc == MNC_UNDEFINED;
582 bool isMccOrMncDiff = this->mcc_ != otherMcc || this->mnc_ != otherMnc;
583 bool isMccDiff = this->mcc_ != otherMcc;
584 int weightsThis = static_cast<int>(this->mcc_ != MCC_UNDEFINED) + static_cast<int>(this->mnc_ != MNC_UNDEFINED);
585 int weightsOther = static_cast<int>(otherMcc != MCC_UNDEFINED) + static_cast<int>(otherMnc != MNC_UNDEFINED);
586 if ((defined && isMccOrMncDiff) || (mccDefined && isMccDiff)) {
587 // 1 means the mcc/mnc of this resConfig is suitable than other resConfig
588 // -1 means the mcc/mnc of other resConfig mcc/mnc is suitable than this resConfig
589 ret = weightsThis > weightsOther ? 1 : -1;
590 }
591 return ret;
592 }
593
594 /**
595 * compare this and target density
596 * if this more match other,then return 1, else if other more match this, return -1,
597 * else
598 * return 0
599 *
600 */
IsDensityMoreSuitable(ScreenDensity otherDensity,ScreenDensity requestDensity,uint32_t density) const601 __attribute__((no_sanitize("integer"))) int ResConfigImpl::IsDensityMoreSuitable(ScreenDensity otherDensity,
602 ScreenDensity requestDensity, uint32_t density) const
603 {
604 int ret = 0;
605 int thisDistance;
606 int otherDistance;
607 if (density == ScreenDensity::SCREEN_DENSITY_NOT_SET) {
608 if (requestDensity != ScreenDensity::SCREEN_DENSITY_NOT_SET &&
609 this->screenDensityDpi_ != otherDensity) {
610 thisDistance = this->screenDensityDpi_ - requestDensity;
611 otherDistance = otherDensity - requestDensity;
612 if (IsDensityMoreSuitable(thisDistance, otherDistance)) {
613 // the density of this resConfig is suitable than other resConfig
614 ret = 1;
615 } else {
616 // the density of other resConfig is suitable than this resConfig
617 ret = -1;
618 }
619 }
620 } else {
621 if (this->screenDensityDpi_ != otherDensity) {
622 thisDistance = static_cast<int>(this->screenDensityDpi_ - density);
623 otherDistance = static_cast<int>(otherDensity - density);
624 if (IsDensityMoreSuitable(thisDistance, otherDistance)) {
625 // the density of this resConfig is suitable than other resConfig
626 ret = 1;
627 } else {
628 // the density of other resConfig is suitable than this resConfig
629 ret = -1;
630 }
631 }
632 }
633 return ret;
634 }
635
IsDensityMoreSuitable(int thisDistance,int otherDistance) const636 bool ResConfigImpl::IsDensityMoreSuitable(int thisDistance, int otherDistance) const
637 {
638 if (thisDistance >= 0 && otherDistance >= 0) {
639 return (thisDistance <= otherDistance);
640 }
641 if (thisDistance > 0) {
642 return true;
643 }
644 if (otherDistance > 0) {
645 return false;
646 }
647 return (thisDistance >= otherDistance);
648 }
649
~ResConfigImpl()650 ResConfigImpl::~ResConfigImpl()
651 {
652 if (resLocale_ != nullptr) {
653 delete resLocale_;
654 resLocale_ = nullptr;
655 }
656 #ifdef SUPPORT_GRAPHICS
657 if (resPreferredLocale_ != nullptr) {
658 delete resPreferredLocale_;
659 resPreferredLocale_ = nullptr;
660 }
661 if (localeInfo_ != nullptr) {
662 delete localeInfo_;
663 localeInfo_ = nullptr;
664 }
665 if (preferredLocaleInfo_ != nullptr) {
666 delete preferredLocaleInfo_;
667 preferredLocaleInfo_ = nullptr;
668 }
669 #endif
670 }
671
CompleteScript()672 void ResConfigImpl::CompleteScript()
673 {
674 if (isCompletedScript_) {
675 return;
676 }
677 if (LocaleMatcher::Normalize(this->resLocale_)) {
678 isCompletedScript_ = true;
679 }
680 }
681
IsCompletedScript() const682 bool ResConfigImpl::IsCompletedScript() const
683 {
684 return isCompletedScript_;
685 }
686
IsMoreSpecificThan(const std::shared_ptr<ResConfigImpl> other,uint32_t density) const687 bool ResConfigImpl::IsMoreSpecificThan(const std::shared_ptr<ResConfigImpl> other, uint32_t density) const
688 {
689 if (other == nullptr) {
690 return true;
691 }
692 if (this->mcc_ != MCC_UNDEFINED && this->mnc_ != MNC_UNDEFINED) {
693 if (this->mcc_ != other->mcc_ || this->mnc_ != other->mnc_) {
694 return false;
695 }
696 } else if (this->mcc_ != MCC_UNDEFINED && this->mnc_ == MNC_UNDEFINED) {
697 if (this->mcc_ != other->mcc_) {
698 return true;
699 }
700 }
701 int8_t result = LocaleMatcher::IsMoreSpecificThan(
702 this->GetResLocale(),
703 (other == nullptr) ? nullptr : other->GetResLocale());
704 if (result > 0) {
705 return true;
706 }
707 if (result < 0) {
708 return false;
709 }
710 if (this->direction_ != other->direction_) {
711 return (this->direction_ != Direction::DIRECTION_NOT_SET);
712 }
713 if (this->deviceType_ != other->deviceType_) {
714 return (this->deviceType_ != DeviceType::DEVICE_NOT_SET);
715 }
716 if (this->colorMode_ != other->colorMode_) {
717 return (this->colorMode_ != ColorMode::COLOR_MODE_NOT_SET);
718 }
719 if (this->inputDevice_ != other->inputDevice_) {
720 return (this->inputDevice_ == InputDevice::INPUTDEVICE_NOT_SET);
721 }
722 int ret = IsDensityMoreSpecificThan(other->screenDensityDpi_, density);
723 if (ret != 0) {
724 return ret > 0;
725 }
726
727 return true;
728 }
729
IsDensityMoreSpecificThan(ScreenDensity otherDensity,uint32_t density) const730 int ResConfigImpl::IsDensityMoreSpecificThan(ScreenDensity otherDensity, uint32_t density) const
731 {
732 int ret = 0;
733 if (density == SCREEN_DENSITY_NOT_SET) {
734 if (this->screenDensityDpi_ != otherDensity) {
735 if (this->screenDensityDpi_ != ScreenDensity::SCREEN_DENSITY_NOT_SET) {
736 // the density of this resConfig is suitable than other resConfig
737 ret = 1;
738 } else {
739 // the density of other resConfig is suitable than this resConfig
740 ret = -1;
741 }
742 }
743 } else {
744 if ((this->screenDensityDpi_ != ScreenDensity::SCREEN_DENSITY_NOT_SET) &&
745 (otherDensity == ScreenDensity::SCREEN_DENSITY_NOT_SET)) {
746 // the density of this resConfig is suitable than other resConfig
747 ret = 1;
748 }
749 if ((this->screenDensityDpi_ == ScreenDensity::SCREEN_DENSITY_NOT_SET) &&
750 (otherDensity != ScreenDensity::SCREEN_DENSITY_NOT_SET)) {
751 // the density of other resConfig is suitable than this resConfig
752 ret = -1;
753 }
754 if (this->screenDensityDpi_ != otherDensity) {
755 int thisDistance = static_cast<int>(this->screenDensityDpi_ - density);
756 int otherDistance = static_cast<int>(otherDensity - density);
757 if (IsDensityMoreSuitable(thisDistance, otherDistance)) {
758 // the density of this resConfig is suitable than other resConfig
759 ret = 1;
760 } else {
761 // the density of other resConfig is suitable than this resConfig
762 ret = -1;
763 }
764 }
765 }
766 return ret;
767 }
768
CreateResConfig()769 ResConfig *CreateResConfig()
770 {
771 ResConfigImpl *temp = new(std::nothrow) ResConfigImpl;
772 return temp;
773 }
774
CreateDefaultResConfig()775 ResConfig *CreateDefaultResConfig()
776 {
777 ResConfigImpl *temp = new(std::nothrow) ResConfigImpl;
778 if (temp != nullptr) {
779 temp->SetColorMode(COLOR_MODE_NOT_SET);
780 }
781 return temp;
782 }
783
SetAppColorMode(bool isAppColorMode)784 void ResConfigImpl::SetAppColorMode(bool isAppColorMode)
785 {
786 this->isAppColorMode_ = isAppColorMode;
787 }
788
GetAppColorMode() const789 bool ResConfigImpl::GetAppColorMode() const
790 {
791 return this->isAppColorMode_;
792 }
793
SetAppDarkRes(bool isAppDarkRes)794 void ResConfigImpl::SetAppDarkRes(bool isAppDarkRes)
795 {
796 this->isAppDarkRes_ = isAppDarkRes;
797 }
798
GetAppDarkRes() const799 bool ResConfigImpl::GetAppDarkRes() const
800 {
801 return this->isAppDarkRes_;
802 }
803 } // namespace Resource
804 } // namespace Global
805 } // namespace OHOS