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