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