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