• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/components/container_modal/container_modal_constants.h"
19 #include "core/pipeline/pipeline_base.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         case DeviceType::WEARABLE:
43             return "wearable";
44         default:
45             return "phone";
46     }
47 }
48 
GetSystemOrientation()49 std::string MediaQueryInfo::GetSystemOrientation()
50 {
51     switch (SystemProperties::GetDeviceOrientation()) {
52         case DeviceOrientation::PORTRAIT:
53             return "portrait";
54         case DeviceOrientation::LANDSCAPE:
55             return "landscape";
56         default:
57             break;
58     }
59     return "";
60 }
61 
GetOrientation(const RefPtr<OHOS::Ace::Container> & container)62 std::string MediaQueryInfo::GetOrientation(const RefPtr<OHOS::Ace::Container>& container)
63 {
64     CHECK_NULL_RETURN(container, GetSystemOrientation());
65     switch (container->GetCurrentDisplayOrientation()) {
66         case DisplayOrientation::PORTRAIT:
67         case DisplayOrientation::PORTRAIT_INVERTED:
68             return "portrait";
69         case DisplayOrientation::LANDSCAPE:
70         case DisplayOrientation::LANDSCAPE_INVERTED:
71             return "landscape";
72         default:
73             break;
74     }
75     return "";
76 }
77 
78 /* 1.0 info */
GetMediaQueryInfo() const79 std::string MediaQueryInfo::GetMediaQueryInfo() const
80 {
81     auto json = GetMediaQueryJsonInfo();
82     json->Put("isInit", false);
83     return json->ToString();
84 }
85 
86 /* 2.0 info */
GetMediaQueryJsonInfo()87 std::unique_ptr<JsonValue> MediaQueryInfo::GetMediaQueryJsonInfo()
88 {
89     CHECK_RUN_ON(JS);
90     auto json = JsonUtil::Create(true);
91     auto container = Container::Current();
92     int32_t width = container ? container->GetViewWidth() : 0;
93     int32_t height = container ? container->GetViewHeight() : 0;
94     auto pipeline = PipelineBase::GetCurrentContext();
95     if (pipeline && pipeline->GetWindowManager() &&
96         pipeline->GetWindowManager()->GetWindowMode() == WindowMode::WINDOW_MODE_FLOATING) {
97         if (Container::LessThanAPITargetVersion(PlatformVersion::VERSION_TWENTY)) {
98             width -= static_cast<int32_t>(2 * (CONTAINER_BORDER_WIDTH + CONTENT_PADDING).ConvertToPx());
99         }
100         height -= static_cast<int32_t>(
101             2 * CONTAINER_BORDER_WIDTH.ConvertToPx() + (CONTENT_PADDING + CONTAINER_TITLE_HEIGHT).ConvertToPx());
102     }
103     if (pipeline) {
104         width = pipeline->CalcPageWidth(width);
105     }
106     double aspectRatio = (height != 0) ? (static_cast<double>(width) / height) : 1.0;
107     json->Put("width", width);
108     json->Put("height", height);
109     json->Put("aspect-ratio", aspectRatio);
110     json->Put("round-screen", SystemProperties::GetIsScreenRound());
111     json->Put("device-width", SystemProperties::GetDeviceWidth());
112     json->Put("device-height", SystemProperties::GetDeviceHeight());
113     json->Put("resolution", PipelineBase::GetCurrentDensity());
114 #if defined(PREVIEW)
115     json->Put("orientation", GetSystemOrientation().c_str());
116 #else
117     json->Put("orientation", GetOrientation(container).c_str());
118 #endif
119     json->Put("device-type", GetDeviceType().c_str());
120     json->Put("dark-mode", PipelineBase::GetCurrentColorMode() == ColorMode::DARK);
121     json->Put("api-version", StringUtils::StringToInt(SystemProperties::GetApiVersion()));
122     return json;
123 }
124 
125 } // namespace OHOS::Ace::Framework
126