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 "frameworks/bridge/common/media_query/media_query_info.h"
17
18 #include "core/common/container.h"
19 #include "core/components/container_modal/container_modal_constants.h"
20
21 namespace OHOS::Ace::Framework {
22
GetDeviceType()23 std::string MediaQueryInfo::GetDeviceType()
24 {
25 if (SystemProperties::GetParamDeviceType() == "tablet") {
26 return "tablet";
27 }
28 if (SystemProperties::GetParamDeviceType() == "2in1") {
29 return "2in1";
30 }
31 switch (SystemProperties::GetDeviceType()) {
32 case DeviceType::TV:
33 return "tv";
34 case DeviceType::CAR:
35 return "car";
36 case DeviceType::WATCH:
37 return "wearable";
38 case DeviceType::TABLET:
39 return "tablet";
40 case DeviceType::TWO_IN_ONE:
41 return "2in1";
42 default:
43 return "phone";
44 }
45 }
46
GetOrientation()47 std::string MediaQueryInfo::GetOrientation()
48 {
49 switch (SystemProperties::GetDeviceOrientation()) {
50 case DeviceOrientation::PORTRAIT:
51 return "portrait";
52 case DeviceOrientation::LANDSCAPE:
53 return "landscape";
54 default:
55 break;
56 }
57 return "";
58 }
59
60 /* 1.0 info */
GetMediaQueryInfo() const61 std::string MediaQueryInfo::GetMediaQueryInfo() const
62 {
63 auto json = GetMediaQueryJsonInfo();
64 json->Put("isInit", false);
65 return json->ToString();
66 }
67
68 /* 2.0 info */
GetMediaQueryJsonInfo()69 std::unique_ptr<JsonValue> MediaQueryInfo::GetMediaQueryJsonInfo()
70 {
71 CHECK_RUN_ON(JS);
72 auto json = JsonUtil::Create(true);
73 auto container = Container::Current();
74 int32_t width = container ? container->GetViewWidth() : 0;
75 int32_t height = container ? container->GetViewHeight() : 0;
76 auto pipeline = PipelineContext::GetCurrentContext();
77 if (pipeline) {
78 auto windowManager = pipeline->GetWindowManager();
79 if (windowManager) {
80 auto mode = windowManager->GetWindowMode();
81 if (mode == WindowMode::WINDOW_MODE_FLOATING) {
82 width -= static_cast<int32_t>(2 * (CONTAINER_BORDER_WIDTH + CONTENT_PADDING).ConvertToPx());
83 height -= static_cast<int32_t>(2 * CONTAINER_BORDER_WIDTH.ConvertToPx() +
84 (CONTENT_PADDING + CONTAINER_TITLE_HEIGHT).ConvertToPx());
85 }
86 }
87 }
88 double aspectRatio = (height != 0) ? (static_cast<double>(width) / height) : 1.0;
89 json->Put("width", width);
90 json->Put("height", height);
91 json->Put("aspect-ratio", aspectRatio);
92 json->Put("round-screen", SystemProperties::GetIsScreenRound());
93 json->Put("device-width", SystemProperties::GetDeviceWidth());
94 json->Put("device-height", SystemProperties::GetDeviceHeight());
95 json->Put("resolution", PipelineBase::GetCurrentDensity());
96 json->Put("orientation", GetOrientation().c_str());
97 json->Put("device-type", GetDeviceType().c_str());
98 json->Put("dark-mode", SystemProperties::GetColorMode() == ColorMode::DARK);
99 json->Put("api-version", StringUtils::StringToInt(SystemProperties::GetApiVersion()));
100 return json;
101 }
102
103 } // namespace OHOS::Ace::Framework
104