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