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 "bridge/common/plugin_adapter/plugin_bridge.h"
17 
18 #include "core/common/container.h"
19 
20 namespace OHOS::Ace::Framework {
ProcessSystemParam(std::unique_ptr<JsonValue> & infoList)21 void PluginBridge::ProcessSystemParam(std::unique_ptr<JsonValue>& infoList)
22 {
23     std::string tmp = SystemProperties::GetBrand();
24     if (tmp != SystemProperties::INVALID_PARAM) {
25         infoList->Put("brand", tmp.c_str());
26     }
27     tmp = SystemProperties::GetManufacturer();
28     if (tmp != SystemProperties::INVALID_PARAM) {
29         infoList->Put("manufacturer", tmp.c_str());
30     }
31     tmp = SystemProperties::GetModel();
32     if (tmp != SystemProperties::INVALID_PARAM) {
33         infoList->Put("model", tmp.c_str());
34     }
35     tmp = SystemProperties::GetProduct();
36     if (tmp != SystemProperties::INVALID_PARAM) {
37         infoList->Put("product", tmp.c_str());
38     }
39     tmp = SystemProperties::GetApiVersion();
40     if (tmp != SystemProperties::INVALID_PARAM) {
41         char* tmpEnd = nullptr;
42         infoList->Put(
43             "apiVersion", static_cast<int32_t>(std::strtol(SystemProperties::GetApiVersion().c_str(), &tmpEnd, 10)));
44     }
45     tmp = SystemProperties::GetReleaseType();
46     if (tmp != SystemProperties::INVALID_PARAM) {
47         infoList->Put("releaseType", tmp.c_str());
48     }
49     tmp = SystemProperties::GetParamDeviceType();
50     if (tmp != SystemProperties::INVALID_PARAM) {
51         infoList->Put("deviceType", tmp.c_str());
52     }
53 
54     tmp = SystemProperties::GetLanguage();
55     if (tmp != SystemProperties::INVALID_PARAM) {
56         infoList->Put("language", tmp.c_str());
57     }
58 
59     tmp = SystemProperties::GetRegion();
60     if (tmp != SystemProperties::INVALID_PARAM) {
61         infoList->Put("region", tmp.c_str());
62     }
63 }
64 
GetDeviceInfo()65 std::pair<std::string, bool> PluginBridge::GetDeviceInfo()
66 {
67     static constexpr uint8_t paramNumber = 13;
68     auto infoList = JsonUtil::Create(true);
69     ProcessSystemParam(infoList);
70 
71     auto container = Container::Current();
72     int32_t width = container ? container->GetViewWidth() : 0;
73     if (width != 0) {
74         infoList->Put("windowWidth", width);
75     }
76     int32_t height = container ? container->GetViewHeight() : 0;
77     if (height != 0) {
78         infoList->Put("windowHeight", height);
79     }
80     infoList->Put("screenDensity", PipelineBase::GetCurrentDensity());
81 
82     bool isRound = SystemProperties::GetIsScreenRound();
83     if (isRound) {
84         infoList->Put("screenShape", "circle");
85     } else {
86         infoList->Put("screenShape", "rect");
87     }
88 
89     bool isParamValid = false;
90     uint8_t count = 0;
91     auto child = infoList->GetChild();
92     while (child->IsValid()) {
93         child = child->GetNext();
94         ++count;
95     }
96     if (count == paramNumber) {
97         isParamValid = true;
98     }
99     return std::make_pair<std::string, bool>(infoList->ToString(), std::move(isParamValid));
100 }
101 } // namespace OHOS::Ace::Framework
102