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