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
16 #include <mutex>
17 #include <shared_mutex>
18 #include <unistd.h>
19
20 #include "base/utils/utils.h"
21 #include "base/utils/system_properties.h"
22
23 #include "parameter.h"
24 #include "parameters.h"
25
26 #include "base/log/log.h"
27 #include "core/common/ace_application_info.h"
28 #ifdef OHOS_STANDARD_SYSTEM
29 #include "systemcapability.h"
30 #endif
31
32 namespace OHOS::Ace {
33 namespace {
34
35 const char PROPERTY_DEVICE_TYPE[] = "const.product.devicetype";
36 const char PROPERTY_DEVICE_TYPE_DEFAULT[] = "default";
37 const char PROPERTY_DEVICE_TYPE_TV[] = "tv";
38 const char PROPERTY_DEVICE_TYPE_TABLET[] = "tablet";
39 const char PROPERTY_DEVICE_TYPE_WATCH[] = "watch";
40 const char PROPERTY_DEVICE_TYPE_CAR[] = "car";
41 #ifdef ENABLE_ROSEN_BACKEND
42 const char DISABLE_ROSEN_FILE_PATH[] = "/etc/disablerosen";
43 const char DISABLE_WINDOW_ANIMATION_PATH[] = "/etc/disable_window_size_animation";
44 #endif
45 const char ENABLE_DEBUG_BOUNDARY_KEY[] = "persist.ace.debug.boundary.enabled";
46 const char ANIMATION_SCALE_KEY[] = "persist.sys.arkui.animationscale";
47
48 constexpr int32_t ORIENTATION_PORTRAIT = 0;
49 constexpr int32_t ORIENTATION_LANDSCAPE = 1;
50 constexpr float DEFAULT_ANIMATION_SCALE = 1.0f;
51
52 float animationScale_ = DEFAULT_ANIMATION_SCALE;
53
54 std::shared_mutex mutex_;
55
Swap(int32_t & deviceWidth,int32_t & deviceHeight)56 void Swap(int32_t& deviceWidth, int32_t& deviceHeight)
57 {
58 int32_t temp = deviceWidth;
59 deviceWidth = deviceHeight;
60 deviceHeight = temp;
61 }
62
IsDebugBoundaryEnabled()63 bool IsDebugBoundaryEnabled()
64 {
65 return system::GetParameter(ENABLE_DEBUG_BOUNDARY_KEY, "false") == "true";
66 }
67
IsTraceEnabled()68 bool IsTraceEnabled()
69 {
70 return (system::GetParameter("persist.ace.trace.enabled", "0") == "1" ||
71 system::GetParameter("debug.ace.trace.enabled", "0") == "1");
72 }
73
IsSvgTraceEnabled()74 bool IsSvgTraceEnabled()
75 {
76 return (system::GetParameter("persist.ace.trace.svg.enabled", "0") == "1");
77 }
78
IsHookModeEnabled()79 bool IsHookModeEnabled()
80 {
81 #if defined(PREVIEW)
82 return false;
83 #endif
84 const int bufferLen = 128;
85 char paramOutBuf[bufferLen] = {0};
86 const char *hook_mode = "startup:";
87 int ret = GetParameter("persist.libc.hook_mode", "", paramOutBuf, bufferLen);
88 if (ret <= 0 || strncmp(paramOutBuf, hook_mode, strlen(hook_mode)) != 0) {
89 return false;
90 }
91 return true;
92 }
93
IsRosenBackendEnabled()94 bool IsRosenBackendEnabled()
95 {
96 #if defined(PREVIEW)
97 return false;
98 #endif
99 #ifdef ENABLE_ROSEN_BACKEND
100 if (system::GetParameter("persist.ace.rosen.backend.enabled", "0") == "1") {
101 return true;
102 }
103 if (system::GetParameter("persist.ace.rosen.backend.enabled", "0") == "2") {
104 return false;
105 }
106 if (access(DISABLE_ROSEN_FILE_PATH, F_OK) == 0) {
107 return false;
108 }
109 return true;
110 #else
111 return false;
112 #endif
113 }
114
IsWindowAnimationEnabled()115 bool IsWindowAnimationEnabled()
116 {
117 #if defined(PREVIEW)
118 return false;
119 #endif
120 #ifdef ENABLE_ROSEN_BACKEND
121 if (access(DISABLE_WINDOW_ANIMATION_PATH, F_OK) == 0) {
122 return false;
123 }
124 return true;
125 #else
126 return false;
127 #endif
128 }
129
IsAccessibilityEnabled()130 bool IsAccessibilityEnabled()
131 {
132 return (system::GetParameter("persist.ace.testmode.enabled", "0") == "1" ||
133 system::GetParameter("debug.ace.testmode.enabled", "0") == "1");
134 }
135
IsDebugEnabled()136 bool IsDebugEnabled()
137 {
138 return (system::GetParameter("persist.ace.debug.enabled", "0") == "1");
139 }
140
IsGpuUploadEnabled()141 bool IsGpuUploadEnabled()
142 {
143 return (system::GetParameter("persist.ace.gpuupload.enabled", "0") == "1" ||
144 system::GetParameter("debug.ace.gpuupload.enabled", "0") == "1");
145 }
146
OnAnimationScaleChanged(const char * key,const char * value,void * context)147 void OnAnimationScaleChanged(const char *key, const char *value, void *context)
148 {
149 CHECK_NULL_VOID(key);
150 if (strcmp(key, ANIMATION_SCALE_KEY) != 0) {
151 LOGE("AnimationScale key not matched. key: %{public}s", key);
152 return;
153 }
154 std::unique_lock<std::shared_mutex> lock(mutex_);
155 if (value == nullptr) {
156 LOGW("AnimationScale changes with null value, use default. key: %{public}s", key);
157 animationScale_ = DEFAULT_ANIMATION_SCALE;
158 return;
159 }
160 auto animationScale = std::atof(value);
161 if (animationScale < 0.0f) {
162 LOGE("AnimationScale changes with invalid value: %{public}s. ignore", value);
163 return;
164 }
165 LOGI("AnimationScale: %{public}f -> %{public}f", animationScale_, animationScale);
166 animationScale_ = animationScale;
167 }
168
169 } // namespace
170
IsSyscapExist(const char * cap)171 bool SystemProperties::IsSyscapExist(const char* cap)
172 {
173 #ifdef OHOS_STANDARD_SYSTEM
174 return HasSystemCapability(cap);
175 #else
176 return false;
177 #endif
178 }
179
InitDeviceType(DeviceType)180 void SystemProperties::InitDeviceType(DeviceType)
181 {
182 // Do nothing, no need to store type here, use system property at 'GetDeviceType' instead.
183 }
184
GetArkProperties()185 int SystemProperties::GetArkProperties()
186 {
187 return system::GetIntParameter<int>("persist.ark.properties", -1);
188 }
189
GetArkBundleName()190 std::string SystemProperties::GetArkBundleName()
191 {
192 return system::GetParameter("persist.ark.arkbundlename", "");
193 }
194
GetGcThreadNum()195 size_t SystemProperties::GetGcThreadNum()
196 {
197 size_t defaultGcThreadNums = 7;
198 return system::GetUintParameter<size_t>("persist.ark.gcthreads", defaultGcThreadNums);
199 }
200
GetLongPauseTime()201 size_t SystemProperties::GetLongPauseTime()
202 {
203 size_t defaultLongPauseTime = 40; // 40ms
204 return system::GetUintParameter<size_t>("persist.ark.longpausetime", defaultLongPauseTime);
205 }
206
GetAsmInterpreterEnabled()207 bool SystemProperties::GetAsmInterpreterEnabled()
208 {
209 return system::GetParameter("persist.ark.asminterpreter", "true") == "true";
210 }
211
GetAsmOpcodeDisableRange()212 std::string SystemProperties::GetAsmOpcodeDisableRange()
213 {
214 return system::GetParameter("persist.ark.asmopcodedisablerange", "");
215 }
216
IsScoringEnabled(const std::string & name)217 bool SystemProperties::IsScoringEnabled(const std::string& name)
218 {
219 if (name.empty()) {
220 return false;
221 }
222 std::string filePath = "/etc/" + name;
223 if (access(filePath.c_str(), F_OK) == 0) {
224 return true;
225 }
226 std::string prop = system::GetParameter("persist.ace.trace.scoringtool", "");
227 return prop == name;
228 }
229
GetAstcEnabled()230 bool GetAstcEnabled()
231 {
232 return system::GetParameter("persist.astc.enable", "true") == "true";
233 }
234
GetAstcMaxErrorProp()235 int32_t GetAstcMaxErrorProp()
236 {
237 return system::GetIntParameter<int>("persist.astc.max", 50000);
238 }
239
GetAstcPsnrProp()240 int32_t GetAstcPsnrProp()
241 {
242 return system::GetIntParameter<int>("persist.astc.psnr", 0);
243 }
244
IsExtSurfaceEnabled()245 bool IsExtSurfaceEnabled()
246 {
247 #ifdef EXT_SURFACE_ENABLE
248 return true;
249 #else
250 return false;
251 #endif
252 }
253
254 bool SystemProperties::traceEnabled_ = IsTraceEnabled();
255 bool SystemProperties::svgTraceEnable_ = IsSvgTraceEnabled();
256 bool SystemProperties::accessibilityEnabled_ = IsAccessibilityEnabled();
257 bool SystemProperties::isRound_ = false;
258 bool SystemProperties::isDeviceAccess_ = false;
259 int32_t SystemProperties::deviceWidth_ = 0;
260 int32_t SystemProperties::deviceHeight_ = 0;
261 double SystemProperties::resolution_ = 1.0;
262 DeviceType SystemProperties::deviceType_ { DeviceType::UNKNOWN };
263 DeviceOrientation SystemProperties::orientation_ { DeviceOrientation::PORTRAIT };
264 std::string SystemProperties::brand_ = INVALID_PARAM;
265 std::string SystemProperties::manufacturer_ = INVALID_PARAM;
266 std::string SystemProperties::model_ = INVALID_PARAM;
267 std::string SystemProperties::product_ = INVALID_PARAM;
268 std::string SystemProperties::apiVersion_ = INVALID_PARAM;
269 std::string SystemProperties::releaseType_ = INVALID_PARAM;
270 std::string SystemProperties::paramDeviceType_ = INVALID_PARAM;
271 int32_t SystemProperties::mcc_ = MCC_UNDEFINED;
272 int32_t SystemProperties::mnc_ = MNC_UNDEFINED;
273 ColorMode SystemProperties::colorMode_ { ColorMode::LIGHT };
274 ScreenShape SystemProperties::screenShape_ { ScreenShape::NOT_ROUND };
275 LongScreenType SystemProperties::LongScreen_ { LongScreenType::NOT_LONG };
276 bool SystemProperties::unZipHap_ = true;
277 bool SystemProperties::rosenBackendEnabled_ = IsRosenBackendEnabled();
278 bool SystemProperties::isHookModeEnabled_ = IsHookModeEnabled();
279 bool SystemProperties::debugBoundaryEnabled_ = IsDebugBoundaryEnabled();
280 bool SystemProperties::windowAnimationEnabled_ = IsWindowAnimationEnabled();
281 bool SystemProperties::debugEnabled_ = IsDebugEnabled();
282 bool SystemProperties::gpuUploadEnabled_ = IsGpuUploadEnabled();
283 bool SystemProperties::astcEnabled_ = GetAstcEnabled();
284 int32_t SystemProperties::astcMax_ = GetAstcMaxErrorProp();
285 int32_t SystemProperties::astcPsnr_ = GetAstcPsnrProp();
286 bool SystemProperties::extSurfaceEnabled_ = IsExtSurfaceEnabled();
287
GetDeviceType()288 DeviceType SystemProperties::GetDeviceType()
289 {
290 InitDeviceTypeBySystemProperty();
291 return deviceType_;
292 }
293
InitDeviceTypeBySystemProperty()294 void SystemProperties::InitDeviceTypeBySystemProperty()
295 {
296 if (deviceType_ != DeviceType::UNKNOWN) {
297 return;
298 }
299
300 auto deviceProp = system::GetParameter(PROPERTY_DEVICE_TYPE, PROPERTY_DEVICE_TYPE_DEFAULT);
301 // Properties: "default", "tv", "tablet", "watch", "car"
302 LOGD("GetDeviceType, deviceProp=%{private}s.", deviceProp.c_str());
303 if (deviceProp == PROPERTY_DEVICE_TYPE_TV) {
304 deviceType_ = DeviceType::TV;
305 } else if (deviceProp == PROPERTY_DEVICE_TYPE_CAR) {
306 deviceType_ = DeviceType::CAR;
307 } else if (deviceProp == PROPERTY_DEVICE_TYPE_WATCH) {
308 deviceType_ = DeviceType::WATCH;
309 } else if (deviceProp == PROPERTY_DEVICE_TYPE_TABLET) {
310 deviceType_ = DeviceType::TABLET;
311 } else {
312 deviceType_ = DeviceType::PHONE;
313 }
314 }
315
InitDeviceInfo(int32_t deviceWidth,int32_t deviceHeight,int32_t orientation,double resolution,bool isRound)316 void SystemProperties::InitDeviceInfo(
317 int32_t deviceWidth, int32_t deviceHeight, int32_t orientation, double resolution, bool isRound)
318 {
319 // SetDeviceOrientation should be earlier than deviceWidth/deviceHeight init.
320 SetDeviceOrientation(orientation);
321
322 isRound_ = isRound;
323 resolution_ = resolution;
324 deviceWidth_ = deviceWidth;
325 deviceHeight_ = deviceHeight;
326 brand_ = system::GetParameter("const.product.brand", INVALID_PARAM);
327 manufacturer_ = system::GetParameter("const.product.manufacturer", INVALID_PARAM);
328 model_ = system::GetParameter("const.product.model", INVALID_PARAM);
329 product_ = system::GetParameter("const.product.name", INVALID_PARAM);
330 apiVersion_ = system::GetParameter("const.ohos.apiversion", INVALID_PARAM);
331 releaseType_ = system::GetParameter("const.ohos.releasetype", INVALID_PARAM);
332 paramDeviceType_ = system::GetParameter("const.product.devicetype", INVALID_PARAM);
333
334 debugEnabled_ = IsDebugEnabled();
335 traceEnabled_ = IsTraceEnabled();
336 svgTraceEnable_ = IsSvgTraceEnabled();
337 accessibilityEnabled_ = IsAccessibilityEnabled();
338 rosenBackendEnabled_ = IsRosenBackendEnabled();
339 isHookModeEnabled_ = IsHookModeEnabled();
340 debugBoundaryEnabled_ = system::GetParameter(ENABLE_DEBUG_BOUNDARY_KEY, "false") == "true";
341 animationScale_ = std::atof(system::GetParameter(ANIMATION_SCALE_KEY, "1").c_str());
342 WatchParameter(ANIMATION_SCALE_KEY, OnAnimationScaleChanged, nullptr);
343
344 if (isRound_) {
345 screenShape_ = ScreenShape::ROUND;
346 } else {
347 screenShape_ = ScreenShape::NOT_ROUND;
348 }
349
350 InitDeviceTypeBySystemProperty();
351 }
352
SetDeviceOrientation(int32_t orientation)353 void SystemProperties::SetDeviceOrientation(int32_t orientation)
354 {
355 if (orientation == ORIENTATION_PORTRAIT && orientation_ != DeviceOrientation::PORTRAIT) {
356 Swap(deviceWidth_, deviceHeight_);
357 orientation_ = DeviceOrientation::PORTRAIT;
358 } else if (orientation == ORIENTATION_LANDSCAPE && orientation_ != DeviceOrientation::LANDSCAPE) {
359 Swap(deviceWidth_, deviceHeight_);
360 orientation_ = DeviceOrientation::LANDSCAPE;
361 }
362 }
363
GetFontWeightScale()364 float SystemProperties::GetFontWeightScale()
365 {
366 // Default value of font weight scale is 1.0.
367 std::string prop =
368 "persist.sys.font_wght_scale_for_user" + std::to_string(AceApplicationInfo::GetInstance().GetUserId());
369 return std::stof(system::GetParameter(prop, "1.0"));
370 }
371
InitMccMnc(int32_t mcc,int32_t mnc)372 void SystemProperties::InitMccMnc(int32_t mcc, int32_t mnc)
373 {
374 mcc_ = mcc;
375 mnc_ = mnc;
376 }
377
GetDebugEnabled()378 bool SystemProperties::GetDebugEnabled()
379 {
380 return debugEnabled_;
381 }
382
GetResourceUseHapPathEnable()383 bool SystemProperties::GetResourceUseHapPathEnable()
384 {
385 return system::GetBoolParameter("compress", false);
386 }
387
GetLanguage()388 std::string SystemProperties::GetLanguage()
389 {
390 return system::GetParameter("const.global.language", INVALID_PARAM);
391 }
392
GetRegion()393 std::string SystemProperties::GetRegion()
394 {
395 return system::GetParameter("const.global.region", INVALID_PARAM);
396 }
397
GetNewPipePkg()398 std::string SystemProperties::GetNewPipePkg()
399 {
400 return system::GetParameter("persist.ace.newpipe.pkgname", "");
401 }
402
GetAnimationScale()403 float SystemProperties::GetAnimationScale()
404 {
405 std::shared_lock<std::shared_mutex> lock(mutex_);
406 return animationScale_;
407 }
408
GetPartialUpdatePkg()409 std::string SystemProperties::GetPartialUpdatePkg()
410 {
411 return system::GetParameter("persist.ace.partial.pkgname", "");
412 }
413
GetSvgMode()414 int32_t SystemProperties::GetSvgMode()
415 {
416 return system::GetIntParameter<int>("persist.ace.svg.mode", 1);
417 }
418
419 } // namespace OHOS::Ace
420