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 #ifndef OHOS_ROSEN_FOLD_SCREEN_STATE_INTERNEL_H
17 #define OHOS_ROSEN_FOLD_SCREEN_STATE_INTERNEL_H
18 
19 #include <sstream>
20 #include <parameters.h>
21 #include <regex>
22 namespace OHOS {
23 namespace Rosen {
24 namespace {
25 static const std::string g_foldScreenType = system::GetParameter("const.window.foldscreen.type", "0,0,0,0");
26 static const std::string PHY_ROTATION_OFFSET = system::GetParameter("const.window.phyrotation.offset", "0");
27 static const std::string INVALID_DEVICE = "-1";
28 static const std::string PORTRAIT_DEVICE = "0";
29 static const std::string DEFAULT_OFFSET = "0";
30 static const std::string SINGLE_DISPLAY = "1";
31 static const std::string DUAL_DISPLAY = "2";
32 static const std::string SINGLE_POCKET_DISPLAY = "4";
33 }
34 class FoldScreenStateInternel {
35 public:
getFoldType()36     static std::string getFoldType()
37     {
38         if (!IsValidFoldType(g_foldScreenType)) {
39             return INVALID_DEVICE;
40         }
41         std::vector<std::string> foldTypes = StringSplit(g_foldScreenType, ',');
42         if (foldTypes.empty()) {
43             return INVALID_DEVICE;
44         }
45         return foldTypes[0];
46     }
47 
IsFoldScreenDevice()48     static bool IsFoldScreenDevice()
49     {
50         std::string foldType = getFoldType();
51         return foldType != INVALID_DEVICE && foldType != PORTRAIT_DEVICE;
52     }
53 
54     // is two logic screen device
IsDualDisplayFoldDevice()55     static bool IsDualDisplayFoldDevice()
56     {
57         return getFoldType() == DUAL_DISPLAY;
58     }
59 
60     // only one logic screen device
IsSingleDisplayFoldDevice()61     static bool IsSingleDisplayFoldDevice()
62     {
63         // ALTB ccm property conflict with the chip, waiting for chip conflict resolution
64         return !IsDualDisplayFoldDevice() && !IsSingleDisplayPocketFoldDevice();
65     }
66 
IsSingleDisplayPocketFoldDevice()67     static bool IsSingleDisplayPocketFoldDevice()
68     {
69         if (!IsValidFoldType(g_foldScreenType)) {
70             return false;
71         }
72         std::vector<std::string> foldTypes = StringSplit(g_foldScreenType, ',');
73         if (foldTypes.empty()) {
74             return false;
75         }
76         return foldTypes[0] == SINGLE_POCKET_DISPLAY;
77     }
78 
GetPhyRotationOffset()79     static std::vector<std::string> GetPhyRotationOffset()
80     {
81         static std::vector<std::string> phyOffsets;
82         if (phyOffsets.empty()) {
83             std::vector<std::string> elems = StringSplit(PHY_ROTATION_OFFSET, ';');
84             for (auto& num : elems) {
85                 if (IsNumber(num)) {
86                     phyOffsets.push_back(num);
87                 } else {
88                     phyOffsets.push_back(DEFAULT_OFFSET);
89                 }
90             }
91         }
92         return phyOffsets;
93     }
94 
IsNumber(const std::string & str)95     static bool IsNumber(const std::string& str)
96     {
97         int32_t length = static_cast<int32_t>(str.size());
98         if (length == 0) {
99             return false;
100         }
101         for (int32_t i = 0; i < length; i++) {
102             if (str.at(i) < '0' || str.at(i) > '9') {
103                 return false;
104             }
105         }
106         return true;
107     }
108 
IsOuterScreen(FoldDisplayMode foldDisplayMode)109     static bool IsOuterScreen(FoldDisplayMode foldDisplayMode)
110     {
111         if (IsDualDisplayFoldDevice()) {
112             return foldDisplayMode == FoldDisplayMode::SUB;
113         }
114         if (IsSingleDisplayFoldDevice() || IsSingleDisplayPocketFoldDevice()) {
115             return foldDisplayMode == FoldDisplayMode::MAIN;
116         }
117         return false;
118     }
119 
StringSplit(const std::string & str,char delim)120     static std::vector<std::string> StringSplit(const std::string& str, char delim)
121     {
122         std::size_t previous = 0;
123         std::size_t current = str.find(delim);
124         std::vector<std::string> elems;
125         while (current != std::string::npos) {
126             if (current > previous) {
127                 elems.push_back(str.substr(previous, current - previous));
128             }
129             previous = current + 1;
130             current = str.find(delim, previous);
131         }
132         if (previous != str.size()) {
133             elems.push_back(str.substr(previous));
134         }
135         return elems;
136     }
137 
IsValidFoldType(const std::string & foldTypeStr)138     static bool IsValidFoldType(const std::string& foldTypeStr)
139     {
140         std::regex reg("^([0-9],){3}[0-9]{1}$");
141         return std::regex_match(foldTypeStr, reg);
142     }
143 };
144 } // Rosen
145 } // OHOS
146 #endif // OHOS_ROSEN_FOLD_SCREEN_STATE_INTERNEL_H