1 /*
2 * Copyright (c) 2023 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 "screen_rotation_property.h"
17 #include "screen_session_manager.h"
18
19 namespace OHOS {
20 namespace Rosen {
21 bool ScreenRotationProperty::isDeviceHorizontal_ = false;
22
isDeviceHorizontal()23 bool ScreenRotationProperty::isDeviceHorizontal()
24 {
25 return isDeviceHorizontal_;
26 }
27
HandleSensorEventInput(DeviceRotation deviceRotation)28 void ScreenRotationProperty::HandleSensorEventInput(DeviceRotation deviceRotation)
29 {
30 static DeviceRotation lastSensorRotationConverted_ = DeviceRotation::INVALID;
31 TLOGI(WmsLogTag::DMS, "DeviceRotation: %{public}d, "
32 "lastSensorRotationConverted: %{public}d", deviceRotation, lastSensorRotationConverted_);
33
34 if (deviceRotation != DeviceRotation::INVALID && lastSensorRotationConverted_ != deviceRotation) {
35 lastSensorRotationConverted_ = deviceRotation;
36 }
37 auto screenSession = ScreenSessionManager::GetInstance().GetDefaultScreenSession();
38 if (!screenSession) {
39 TLOGW(WmsLogTag::DMS, "screenSession is null, sensor rotation status handle failed");
40 return;
41 }
42 screenSession->HandleSensorRotation(ConvertDeviceToFloat(deviceRotation));
43 }
44
ConvertDeviceToFloat(DeviceRotation deviceRotation)45 float ScreenRotationProperty::ConvertDeviceToFloat(DeviceRotation deviceRotation)
46 {
47 float sensorRotation = -1.0f; // -1 mean keep before degree
48 switch (deviceRotation) {
49 case DeviceRotation::ROTATION_PORTRAIT:
50 sensorRotation = 0.0f; // degree 0
51 isDeviceHorizontal_ = false;
52 break;
53 case DeviceRotation::ROTATION_LANDSCAPE:
54 sensorRotation = 90.0f; // degree 90
55 isDeviceHorizontal_ = true;
56 break;
57 case DeviceRotation::ROTATION_PORTRAIT_INVERTED:
58 sensorRotation = 180.0f; // degree 180
59 isDeviceHorizontal_ = false;
60 break;
61 case DeviceRotation::ROTATION_LANDSCAPE_INVERTED:
62 sensorRotation = 270.0f; // degree 270
63 isDeviceHorizontal_ = true;
64 break;
65 case DeviceRotation::INVALID:
66 sensorRotation = -1.0f; // keep before degree
67 break;
68 default:
69 TLOGW(WmsLogTag::DMS, "invalid device rotation: %{public}d", deviceRotation);
70 }
71 return sensorRotation;
72 }
73
HandleHoverStatusEventInput(DeviceHoverStatus hoverStatus,bool needRotate)74 void ScreenRotationProperty::HandleHoverStatusEventInput(DeviceHoverStatus hoverStatus, bool needRotate)
75 {
76 static DeviceHoverStatus lastHoverStatusConverted_ = DeviceHoverStatus::INVALID;
77 TLOGI(WmsLogTag::DMS, "DeviceHoverStatus: %{public}d, "
78 "lastHoverStatusConverted: %{public}d", hoverStatus, lastHoverStatusConverted_);
79
80 if (hoverStatus != DeviceHoverStatus::INVALID) {
81 lastHoverStatusConverted_ = hoverStatus;
82 }
83 auto screenSession = ScreenSessionManager::GetInstance().GetDefaultScreenSession();
84 if (!screenSession) {
85 TLOGW(WmsLogTag::DMS, "screenSession is null, sensor rotation status handle failed");
86 return;
87 }
88 screenSession->HandleHoverStatusChange(static_cast<int32_t>(hoverStatus), needRotate);
89 }
90 } // Rosen
91 } // OHOS