1 /* 2 * Copyright (c) 2022 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 "display_manager_adapter_impl.h" 17 18 #include "nweb_log.h" 19 #include "syspara/parameters.h" 20 21 using namespace OHOS::Rosen; 22 using namespace OHOS::NWeb; 23 24 namespace OHOS::NWeb { DisplayListenerAdapterImpl(std::shared_ptr<DisplayListenerAdapter> listener)25DisplayListenerAdapterImpl::DisplayListenerAdapterImpl( 26 std::shared_ptr<DisplayListenerAdapter> listener) : listener_(listener) {} 27 OnCreate(DisplayId id)28void DisplayListenerAdapterImpl::OnCreate(DisplayId id) 29 { 30 if (listener_ != nullptr) { 31 listener_->OnCreate(id); 32 } 33 } OnDestroy(DisplayId id)34void DisplayListenerAdapterImpl::OnDestroy(DisplayId id) 35 { 36 if (listener_ != nullptr) { 37 listener_->OnDestroy(id); 38 } 39 } OnChange(DisplayId id)40void DisplayListenerAdapterImpl::OnChange(DisplayId id) 41 { 42 if (listener_ != nullptr) { 43 listener_->OnChange(id); 44 } 45 } 46 DisplayAdapterImpl(sptr<OHOS::Rosen::Display> display)47DisplayAdapterImpl::DisplayAdapterImpl(sptr<OHOS::Rosen::Display> display) 48 : display_(display) {} 49 ConvertRotationType(OHOS::Rosen::Rotation type)50OHOS::NWeb::RotationType DisplayAdapterImpl::ConvertRotationType(OHOS::Rosen::Rotation type) 51 { 52 switch (type) { 53 case OHOS::Rosen::Rotation::ROTATION_0: 54 return OHOS::NWeb::RotationType::ROTATION_0; 55 case OHOS::Rosen::Rotation::ROTATION_90: 56 return OHOS::NWeb::RotationType::ROTATION_90; 57 case OHOS::Rosen::Rotation::ROTATION_180: 58 return OHOS::NWeb::RotationType::ROTATION_180; 59 case OHOS::Rosen::Rotation::ROTATION_270: 60 return OHOS::NWeb::RotationType::ROTATION_270; 61 default: 62 return OHOS::NWeb::RotationType::ROTATION_BUTT; 63 } 64 } 65 ConvertOrientationType(OHOS::Rosen::Orientation type)66OHOS::NWeb::OrientationType DisplayAdapterImpl::ConvertOrientationType(OHOS::Rosen::Orientation type) 67 { 68 switch (type) { 69 case OHOS::Rosen::Orientation::UNSPECIFIED: 70 return OHOS::NWeb::OrientationType::UNSPECIFIED; 71 case OHOS::Rosen::Orientation::VERTICAL: 72 return OHOS::NWeb::OrientationType::VERTICAL; 73 case OHOS::Rosen::Orientation::HORIZONTAL: 74 return OHOS::NWeb::OrientationType::HORIZONTAL; 75 case OHOS::Rosen::Orientation::REVERSE_VERTICAL: 76 return OHOS::NWeb::OrientationType::REVERSE_VERTICAL; 77 case OHOS::Rosen::Orientation::REVERSE_HORIZONTAL: 78 return OHOS::NWeb::OrientationType::REVERSE_HORIZONTAL; 79 case OHOS::Rosen::Orientation::SENSOR: 80 return OHOS::NWeb::OrientationType::SENSOR; 81 case OHOS::Rosen::Orientation::SENSOR_VERTICAL: 82 return OHOS::NWeb::OrientationType::SENSOR_VERTICAL; 83 case OHOS::Rosen::Orientation::SENSOR_HORIZONTAL: 84 return OHOS::NWeb::OrientationType::SENSOR_HORIZONTAL; 85 default: 86 return OHOS::NWeb::OrientationType::BUTT; 87 } 88 } 89 GetId()90DisplayId DisplayAdapterImpl::GetId() 91 { 92 if (display_ != nullptr) { 93 return display_->GetId(); 94 } 95 return static_cast<DisplayId>(-1); 96 } 97 GetWidth()98int32_t DisplayAdapterImpl::GetWidth() 99 { 100 if (display_ != nullptr) { 101 return display_->GetWidth(); 102 } 103 return -1; 104 } 105 GetHeight()106int32_t DisplayAdapterImpl::GetHeight() 107 { 108 if (display_ != nullptr) { 109 return display_->GetHeight(); 110 } 111 return -1; 112 } 113 GetVirtualPixelRatio()114float DisplayAdapterImpl::GetVirtualPixelRatio() 115 { 116 if (display_ != nullptr) { 117 return display_->GetVirtualPixelRatio(); 118 } 119 return -1; 120 } 121 GetRotation()122RotationType DisplayAdapterImpl::GetRotation() 123 { 124 if (display_ != nullptr) { 125 return ConvertRotationType(display_->GetRotation()); 126 } 127 return RotationType::ROTATION_BUTT; 128 } 129 GetOrientation()130OrientationType DisplayAdapterImpl::GetOrientation() 131 { 132 if (display_ != nullptr) { 133 return ConvertOrientationType(display_->GetOrientation()); 134 } 135 return OrientationType::BUTT; 136 } 137 GetDefaultDisplayId()138DisplayId DisplayManagerAdapterImpl::GetDefaultDisplayId() 139 { 140 return DisplayManager::GetInstance().GetDefaultDisplayId(); 141 } 142 GetDefaultDisplay()143std::shared_ptr<DisplayAdapter> DisplayManagerAdapterImpl::GetDefaultDisplay() 144 { 145 sptr<Display> display = DisplayManager::GetInstance().GetDefaultDisplay(); 146 return std::make_shared<DisplayAdapterImpl>(display); 147 } 148 RegisterDisplayListener(std::shared_ptr<DisplayListenerAdapter> listener)149bool DisplayManagerAdapterImpl::RegisterDisplayListener( 150 std::shared_ptr<DisplayListenerAdapter> listener) 151 { 152 sptr<DisplayListenerAdapterImpl> reg = 153 new (std::nothrow) DisplayListenerAdapterImpl(listener); 154 if (reg == nullptr) { 155 return false; 156 } 157 reg_.emplace(std::make_pair(listener.get(), reg)); 158 return DisplayManager::GetInstance().RegisterDisplayListener(reg) == DMError::DM_OK; 159 } 160 UnregisterDisplayListener(std::shared_ptr<DisplayListenerAdapter> listener)161bool DisplayManagerAdapterImpl::UnregisterDisplayListener( 162 std::shared_ptr<DisplayListenerAdapter> listener) 163 { 164 ListenerMap::iterator iter = reg_.find(listener.get()); 165 if (iter == reg_.end()) { 166 return false; 167 } 168 if (DisplayManager::GetInstance().UnregisterDisplayListener(iter->second) == DMError::DM_OK) { 169 reg_.erase(iter); 170 return true; 171 } 172 return false; 173 } 174 IsDefaultPortrait()175bool DisplayManagerAdapterImpl::IsDefaultPortrait() 176 { 177 std::string deviceType = OHOS::system::GetDeviceType(); 178 return deviceType == "phone" || deviceType == "default"; 179 } 180 }