1 /**
2 * Copyright (c) 2024 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 "core/common/display_info_utils.h"
17
18 #include "display_manager.h"
19
20 #include "core/common/display_info.h"
21
22 namespace OHOS::Ace {
GetInstance()23 DisplayInfoUtils& DisplayInfoUtils::GetInstance()
24 {
25 static DisplayInfoUtils instance;
26 return instance;
27 }
28
GetDisplayInfo()29 RefPtr<DisplayInfo> DisplayInfoUtils::GetDisplayInfo()
30 {
31 auto displayManager = Rosen::DisplayManager::GetInstance().GetDefaultDisplay();
32 CHECK_NULL_RETURN(displayManager, nullptr);
33 auto dmRotation = displayManager->GetRotation();
34 auto isFoldable = Rosen::DisplayManager::GetInstance().IsFoldable();
35 auto dmFoldStatus = Rosen::DisplayManager::GetInstance().GetFoldStatus();
36 std::vector<Rect> rects;
37 auto foldCreaseRegion = Rosen::DisplayManager::GetInstance().GetCurrentFoldCreaseRegion();
38 if (foldCreaseRegion) {
39 auto creaseRects = foldCreaseRegion->GetCreaseRects();
40 if (!creaseRects.empty()) {
41 for (const auto& item : creaseRects) {
42 Rect rect;
43 rect.SetRect(item.posX_, item.posY_, item.width_, item.height_);
44 rects.insert(rects.end(), rect);
45 }
46 }
47 }
48 displayInfo_->SetWidth(displayManager->GetWidth());
49 displayInfo_->SetHeight(displayManager->GetHeight());
50 displayInfo_->SetDisplayId(displayManager->GetId());
51 displayInfo_->SetIsFoldable(isFoldable);
52 displayInfo_->SetFoldStatus(static_cast<FoldStatus>(static_cast<uint32_t>(dmFoldStatus)));
53 displayInfo_->SetRotation(static_cast<Rotation>(static_cast<uint32_t>(dmRotation)));
54 displayInfo_->SetCurrentFoldCreaseRegion(rects);
55 return displayInfo_;
56 }
57
InitIsFoldable()58 void DisplayInfoUtils::InitIsFoldable()
59 {
60 auto isFoldable = Rosen::DisplayManager::GetInstance().IsFoldable();
61 displayInfo_->SetIsFoldable(isFoldable);
62 }
63
IsFoldable()64 bool DisplayInfoUtils::IsFoldable()
65 {
66 if (hasInitIsFoldable) {
67 return displayInfo_->GetIsFoldable();
68 }
69 auto isFoldable = Rosen::DisplayManager::GetInstance().IsFoldable();
70 displayInfo_->SetIsFoldable(isFoldable);
71 hasInitIsFoldable = true;
72 return isFoldable;
73 }
74
GetCurrentFoldStatus()75 FoldStatus DisplayInfoUtils::GetCurrentFoldStatus()
76 {
77 auto dmFoldStatus = Rosen::DisplayManager::GetInstance().GetFoldStatus();
78 displayInfo_->SetFoldStatus(static_cast<FoldStatus>(static_cast<uint32_t>(dmFoldStatus)));
79 return displayInfo_->GetFoldStatus();
80 }
81 } // namespace OHOS::Ace::DisplayInfoUtils
82