1 /*
2 * Copyright (c) 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 "run_args_parser.h"
17
18 #include <string>
19 #include <map>
20
21 #include "base/utils/device_type.h"
22 #include "base/utils/device_config.h"
23 #include "frameworks/base/log/log.h"
24 #include "frameworks/base/json/json_util.h"
25
26 namespace OHOS::Ace::Samples {
27 namespace {
28 const std::map<std::string, Platform::AceVersion> ACE_VERSION_MAP = {
29 {"1.0", Platform::AceVersion::ACE_1_0},
30 {"2.0", Platform::AceVersion::ACE_2_0},
31 };
32
33 const std::map<std::string, Platform::ProjectModel> PROJECT_MODEL_MAP = {
34 {"FA", Platform::ProjectModel::FA},
35 {"STAGE", Platform::ProjectModel::STAGE},
36 };
37
38 const std::map<std::string, DeviceType> DEVICE_TYPE_MAP = {
39 {"default", DeviceType::PHONE},
40 {"tablet", DeviceType::TABLET},
41 {"unknown", DeviceType::UNKNOWN},
42 };
43
44 const std::map<std::string, DeviceOrientation> DEVICE_ORIENTATION_MAP = {
45 {"portrait", DeviceOrientation::PORTRAIT},
46 {"landscape", DeviceOrientation::LANDSCAPE},
47 {"orientation_undefined", DeviceOrientation::ORIENTATION_UNDEFINED},
48 };
49
50 const std::map<std::string, ColorMode> COLOR_MODE_MAP = {
51 {"light", ColorMode::LIGHT},
52 {"dark", ColorMode::DARK},
53 {"color_mode_undefined", ColorMode::COLOR_MODE_UNDEFINED},
54 };
55 }
56
Parse(const std::string & contents)57 bool RunArgsParser::Parse(const std::string& contents)
58 {
59 auto rootJson = JsonUtil::ParseJsonString(contents);
60 if (!rootJson || !rootJson->IsValid()) {
61 LOGE("The run args config is illegal");
62 return false;
63 }
64 aceRunArgs_.assetPath = rootJson->GetString("assetPath", "");
65 if (aceRunArgs_.assetPath == "") {
66 LOGE("The assetPath is null.");
67 return false;
68 }
69 aceRunArgs_.systemResourcesPath = rootJson->GetString("systemResourcesPath", "");
70 aceRunArgs_.appResourcesPath = rootJson->GetString("appResourcesPath", "");
71 aceRunArgs_.windowTitle = rootJson->GetString("windowTitle", "ACE Demo");
72 aceRunArgs_.pageProfile = rootJson->GetString("pageProfile", "main_page");
73 aceRunArgs_.url = rootJson->GetString("url", "");
74 aceRunArgs_.language = rootJson->GetString("language", "zh");
75 aceRunArgs_.region = rootJson->GetString("region", "CN");
76 aceRunArgs_.script = rootJson->GetString("script", "");
77 aceRunArgs_.configChanges = rootJson->GetString("configChanges", "");
78 aceRunArgs_.isRound = rootJson->GetBool("isRound", false);
79 aceRunArgs_.formsEnabled = rootJson->GetBool("formsEnabled", false);
80 aceRunArgs_.containerSdkPath = rootJson->GetString("containerSdkPath", "");
81 aceRunArgs_.themeId = rootJson->GetUInt("themeId", Platform::THEME_ID_LIGHT);
82 aceRunArgs_.viewWidth = rootJson->GetInt("viewWidth", 0);
83 aceRunArgs_.viewHeight = rootJson->GetInt("viewHeight", 0);
84 aceRunArgs_.deviceWidth = rootJson->GetInt("deviceWidth", 0);
85 aceRunArgs_.deviceHeight = rootJson->GetInt("deviceHeight", 0);
86 aceRunArgs_.aceVersion = Platform::AceVersion::ACE_1_0;
87 std::string aceVersion = rootJson->GetString("aceVersion", "1.0");
88 auto iterVersion = ACE_VERSION_MAP.find(aceVersion);
89 if (iterVersion != ACE_VERSION_MAP.end()) {
90 aceRunArgs_.aceVersion = iterVersion->second;
91 }
92 aceRunArgs_.projectModel = Platform::ProjectModel::FA;
93 std::string projectModel = rootJson->GetString("projectModel", "FA");
94 auto iterModel = PROJECT_MODEL_MAP.find(projectModel);
95 if (iterModel != PROJECT_MODEL_MAP.end()) {
96 aceRunArgs_.projectModel = iterModel->second;
97 }
98 aceRunArgs_.deviceConfig = {
99 .orientation = DeviceOrientation::PORTRAIT,
100 .density = 1.0,
101 .deviceType = DeviceType::PHONE,
102 .colorMode = ColorMode::LIGHT,
103 };
104 auto deviceConfig = rootJson->GetValue("deviceConfig");
105 if (deviceConfig) {
106 std::string orientation = deviceConfig->GetString("orientation", "portrait");
107 auto iterOrientation = DEVICE_ORIENTATION_MAP.find(orientation);
108 if (iterOrientation != DEVICE_ORIENTATION_MAP.end()) {
109 aceRunArgs_.deviceConfig.orientation = iterOrientation->second;
110 }
111 std::string deviceType = deviceConfig->GetString("deviceType", "default");
112 auto iterDevice = DEVICE_TYPE_MAP.find(deviceType);
113 if (iterDevice != DEVICE_TYPE_MAP.end()) {
114 aceRunArgs_.deviceConfig.deviceType = iterDevice->second;
115 }
116 std::string colorMode = deviceConfig->GetString("colorMode", "light");
117 auto iterColor = COLOR_MODE_MAP.find(colorMode);
118 if (iterColor != COLOR_MODE_MAP.end()) {
119 aceRunArgs_.deviceConfig.colorMode = iterColor->second;
120 }
121 aceRunArgs_.deviceConfig.density = deviceConfig->GetDouble("density", 1.0);
122 }
123 return true;
124 }
125
GetAceRunArgs() const126 const Platform::AceRunArgs& RunArgsParser::GetAceRunArgs() const
127 {
128 return aceRunArgs_;
129 }
130 } // namespace OHOS::Ace::Samples