• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.. All rights reserved.
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 "text_bundle_config_parser.h"
17 #include "modules/skparagraph/include/TextGlobalConfig.h"
18 
19 #ifdef ENABLE_OHOS_ENHANCE
20 #include "application_info.h"
21 #include "hap_module_info.h"
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24 #include "utils/text_log.h"
25 #endif
26 
27 namespace OHOS {
28 namespace Rosen {
29 namespace SPText {
30 #ifdef ENABLE_OHOS_ENHANCE
31 const std::string ADAPTER_TEXT_HEIGHT_META_DATA = "ohos.graphics2d.text.adapter_text_height";
32 const size_t VERSION_DIVISOR = 100;
33 
IsMetaDataExistInModule(const std::string & metaData,const AppExecFwk::BundleInfo & bundleInfo)34 bool TextBundleConfigParser::IsMetaDataExistInModule(const std::string& metaData,
35     const AppExecFwk::BundleInfo& bundleInfo)
36 {
37     for (const auto& info : bundleInfo.hapModuleInfos) {
38         for (const auto& data : info.metadata) {
39             if (data.name == metaData) {
40                 return true;
41             }
42         }
43     }
44     return false;
45 }
46 
GetBundleInfo(AppExecFwk::BundleInfo & bundleInfo)47 bool TextBundleConfigParser::GetBundleInfo(AppExecFwk::BundleInfo& bundleInfo)
48 {
49     sptr<ISystemAbilityManager> systemAbilityManager =
50         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
51     if (systemAbilityManager == nullptr) {
52         TEXT_LOGE("Failed to get system ability manager");
53         return false;
54     }
55 
56     sptr<IRemoteObject> remoteObject =
57         systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
58     if (remoteObject == nullptr) {
59         TEXT_LOGE("Failed to get remote object");
60         return false;
61     }
62 
63     sptr<AppExecFwk::IBundleMgr> bundleMgr =
64         iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
65     if (bundleMgr == nullptr) {
66         TEXT_LOGE("Failed to get bundle manager");
67         return false;
68     }
69     ErrCode errCode = bundleMgr->GetBundleInfoForSelf(
70         static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE) |
71         static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA),
72         bundleInfo);
73     if (errCode != ERR_OK) {
74         TEXT_LOGE("Failed to get bundle info, errcode: %{public}x", errCode);
75         return false;
76     }
77     return true;
78 }
79 #endif
80 
IsAdapterTextHeightEnabled() const81 bool TextBundleConfigParser::IsAdapterTextHeightEnabled() const
82 {
83     return initStatus_ && adapterTextHeightEnable_;
84 }
85 
IsTargetApiVersion(size_t targetVersion) const86 bool TextBundleConfigParser::IsTargetApiVersion(size_t targetVersion) const
87 {
88     return initStatus_ && bundleApiVersion_ >= targetVersion;
89 }
90 
91 #ifdef ENABLE_OHOS_ENHANCE
InitTextBundleConfig()92 void TextBundleConfigParser::InitTextBundleConfig()
93 {
94     AppExecFwk::BundleInfo bundleInfo;
95     if (!GetBundleInfo(bundleInfo)) {
96         InitTextBundleFailed();
97         return;
98     }
99 
100     initStatus_ = true;
101     bundleApiVersion_ = bundleInfo.targetVersion % VERSION_DIVISOR;
102     adapterTextHeightEnable_ = IsMetaDataExistInModule(ADAPTER_TEXT_HEIGHT_META_DATA, bundleInfo);
103     TEXT_LOGI("Adapter text height enabled %{public}d", adapterTextHeightEnable_);
104 }
105 #endif
106 
InitTextBundleFailed()107 void TextBundleConfigParser::InitTextBundleFailed()
108 {
109     initStatus_ = false;
110     adapterTextHeightEnable_ = false;
111     bundleApiVersion_ = std::numeric_limits<uint32_t>::max();
112 }
113 
InitBundleInfo()114 void TextBundleConfigParser::InitBundleInfo()
115 {
116 #ifdef ENABLE_OHOS_ENHANCE
117     InitTextBundleConfig();
118 #else
119     InitTextBundleFailed();
120 #endif
121     skia::textlayout::TextGlobalConfig::SetTargetVersion(bundleApiVersion_);
122 }
123 } // namespace SPText
124 } // namespace Rosen
125 } // namespace OHOS