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