• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_adapter_impl.h"
17 #include "adapter/ohos/osal/resource_convertor.h"
18 #include "adapter/preview/entrance/ace_application_info.h"
19 
20 namespace OHOS::Ace {
21 
22 namespace {
23 #ifdef WINDOWS_PLATFORM
24 constexpr char DELIMITER[] = "\\";
25 #else
26 constexpr char DELIMITER[] = "/";
27 #endif
28 } // namespace
29 
Create()30 RefPtr<ResourceAdapter> ResourceAdapter::Create()
31 {
32     return AceType::MakeRefPtr<ResourceAdapterImpl>();
33 }
34 
ParseDimensionUnit(const std::string & unit)35 DimensionUnit ParseDimensionUnit(const std::string& unit)
36 {
37     if (unit == "px") {
38         return DimensionUnit::PX;
39     } else if (unit == "fp") {
40         return DimensionUnit::FP;
41     } else if (unit == "lpx") {
42         return DimensionUnit::LPX;
43     } else if (unit == "%") {
44         return DimensionUnit::PERCENT;
45     } else {
46         return DimensionUnit::VP;
47     }
48 }
49 
Init(const ResourceInfo & resourceInfo)50 void ResourceAdapterImpl::Init(const ResourceInfo& resourceInfo)
51 {
52     std::string appResPath = resourceInfo.GetPackagePath();
53     std::string sysResPath = resourceInfo.GetSystemPackagePath();
54     auto resConfig = ConvertConfigToGlobal(resourceInfo.GetResourceConfiguration());
55     std::shared_ptr<Global::Resource::ResourceManager> newResMgr(Global::Resource::CreateResourceManager());
56 
57     std::string appResIndexPath = appResPath + DELIMITER + "resources.index";
58     auto appResRet = newResMgr->AddResource(appResIndexPath.c_str());
59     std::string sysResIndexPath = sysResPath + DELIMITER + "resources.index";
60     auto sysResRet = newResMgr->AddResource(sysResIndexPath.c_str());
61 
62     auto configRet = newResMgr->UpdateResConfig(*resConfig);
63     LOGI("AddAppRes result=%{public}d, AddSysRes result=%{public}d,  UpdateResConfig result=%{public}d, "
64          "ori=%{public}d, dpi=%{public}d, device=%{public}d",
65         appResRet, sysResRet, configRet, resConfig->GetDirection(), resConfig->GetScreenDensity(),
66         resConfig->GetDeviceType());
67     Platform::AceApplicationInfoImpl::GetInstance().SetResourceManager(newResMgr);
68     resourceManager_ = newResMgr;
69     packagePathStr_ = appResPath;
70 }
71 
UpdateConfig(const ResourceConfiguration & config)72 void ResourceAdapterImpl::UpdateConfig(const ResourceConfiguration& config)
73 {
74     auto resConfig = ConvertConfigToGlobal(config);
75     LOGI("UpdateConfig ori=%{public}d, dpi=%{public}d, device=%{public}d", resConfig->GetDirection(),
76         resConfig->GetScreenDensity(), resConfig->GetDeviceType());
77     resourceManager_->UpdateResConfig(*resConfig);
78 }
79 
GetColor(uint32_t resId)80 Color ResourceAdapterImpl::GetColor(uint32_t resId)
81 {
82     uint32_t result = 0;
83     if (resourceManager_) {
84         auto state = resourceManager_->GetColorById(resId, result);
85         if (state != Global::Resource::SUCCESS) {
86             LOGE("GetColor error, id=%{public}u", resId);
87         }
88     }
89     return Color(result);
90 }
91 
GetDimension(uint32_t resId)92 Dimension ResourceAdapterImpl::GetDimension(uint32_t resId)
93 {
94     float dimensionFloat = 0.0f;
95     std::string unit = "";
96     if (resourceManager_) {
97         auto state = resourceManager_->GetFloatById(resId, dimensionFloat, unit);
98         if (state != Global::Resource::SUCCESS) {
99             LOGE("GetDimension error, id=%{public}u", resId);
100         }
101     }
102     return Dimension(static_cast<double>(dimensionFloat), ParseDimensionUnit(unit));
103 }
104 
GetString(uint32_t resId)105 std::string ResourceAdapterImpl::GetString(uint32_t resId)
106 {
107     std::string strResult = "";
108     if (resourceManager_) {
109         auto state = resourceManager_->GetStringById(resId, strResult);
110         if (state != Global::Resource::SUCCESS) {
111             LOGE("GetString error, id=%{public}u", resId);
112         }
113     }
114     return strResult;
115 }
116 
GetPluralString(uint32_t resId,int quantity)117 std::string ResourceAdapterImpl::GetPluralString(uint32_t resId, int quantity)
118 {
119     std::string strResult = "";
120     if (resourceManager_) {
121         auto state = resourceManager_->GetPluralStringById(resId, quantity, strResult);
122         if (state != Global::Resource::SUCCESS) {
123             LOGE("GetPluralString error, id=%{public}u", resId);
124         }
125     }
126     return strResult;
127 }
128 
GetStringArray(uint32_t resId) const129 std::vector<std::string> ResourceAdapterImpl::GetStringArray(uint32_t resId) const
130 {
131     std::vector<std::string> strResults;
132     if (resourceManager_) {
133         auto state = resourceManager_->GetStringArrayById(resId, strResults);
134         if (state != Global::Resource::SUCCESS) {
135             LOGE("GetStringArray error, id=%{public}u", resId);
136         }
137     }
138     return strResults;
139 }
140 
GetDouble(uint32_t resId)141 double ResourceAdapterImpl::GetDouble(uint32_t resId)
142 {
143     float result = 0.0f;
144     if (resourceManager_) {
145         auto state = resourceManager_->GetFloatById(resId, result);
146         if (state != Global::Resource::SUCCESS) {
147             LOGE("GetDouble error, id=%{public}u", resId);
148         }
149     }
150     return static_cast<double>(result);
151 }
152 
GetInt(uint32_t resId)153 int32_t ResourceAdapterImpl::GetInt(uint32_t resId)
154 {
155     int32_t result = 0;
156     if (resourceManager_) {
157         auto state = resourceManager_->GetIntegerById(resId, result);
158         if (state != Global::Resource::SUCCESS) {
159             LOGE("GetInt error, id=%{public}u", resId);
160         }
161     }
162     return result;
163 }
164 
GetIntArray(uint32_t resId) const165 std::vector<uint32_t> ResourceAdapterImpl::GetIntArray(uint32_t resId) const
166 {
167     std::vector<int> intVectorResult;
168     if (resourceManager_) {
169         auto state = resourceManager_->GetIntArrayById(resId, intVectorResult);
170         if (state != Global::Resource::SUCCESS) {
171             LOGE("GetIntArray error, id=%{public}u", resId);
172         }
173     }
174     std::vector<uint32_t> result;
175     std::transform(
176         intVectorResult.begin(), intVectorResult.end(), result.begin(), [](int x) { return static_cast<uint32_t>(x); });
177     return result;
178 }
179 
GetBoolean(uint32_t resId) const180 bool ResourceAdapterImpl::GetBoolean(uint32_t resId) const
181 {
182     bool result = false;
183     if (resourceManager_) {
184         auto state = resourceManager_->GetBooleanById(resId, result);
185         if (state != Global::Resource::SUCCESS) {
186             LOGE("GetBoolean error, id=%{public}u", resId);
187         }
188     }
189     return result;
190 }
191 
GetMediaPath(uint32_t resId)192 std::string ResourceAdapterImpl::GetMediaPath(uint32_t resId)
193 {
194     std::string mediaPath = "";
195     if (resourceManager_) {
196         auto state = resourceManager_->GetMediaById(resId, mediaPath);
197         if (state != Global::Resource::SUCCESS) {
198             LOGE("GetMediaPath error, id=%{public}u", resId);
199             return "";
200         }
201         // The Media file directory starts with file// on the PC Preview
202         return "file://" + mediaPath;
203     }
204     return "";
205 }
206 
GetRawfile(const std::string & fileName)207 std::string ResourceAdapterImpl::GetRawfile(const std::string& fileName)
208 {
209     // The rawfile file directory starts with file// on the PC Preview
210     return "file://" + packagePathStr_ + "/resources/rawfile/" + fileName;
211 }
212 
213 } // namespace OHOS::Ace
214