• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static const std::string SUPER_FOLD_DISPLAY = "5";
34 static const std::string SECONDARY_FOLD_DISPLAY = "6";
35 static const size_t THIRD_ANGLE = 2;
36 }
37 class FoldScreenStateInternel {
38 public:
getFoldType()39     static std::string getFoldType()
40     {
41         if (!IsValidFoldType(g_foldScreenType)) {
42             return INVALID_DEVICE;
43         }
44         std::vector<std::string> foldTypes = StringSplit(g_foldScreenType, ',');
45         if (foldTypes.empty()) {
46             return INVALID_DEVICE;
47         }
48         return foldTypes[0];
49     }
50 
IsFoldScreenDevice()51     static bool IsFoldScreenDevice()
52     {
53         std::string foldType = getFoldType();
54         return foldType != INVALID_DEVICE && foldType != PORTRAIT_DEVICE;
55     }
56 
57     // is two logic screen device
IsDualDisplayFoldDevice()58     static bool IsDualDisplayFoldDevice()
59     {
60         return getFoldType() == DUAL_DISPLAY;
61     }
62 
63     // only one logic screen device
IsSingleDisplayFoldDevice()64     static bool IsSingleDisplayFoldDevice()
65     {
66         // ALTB ccm property conflict with the chip, waiting for chip conflict resolution
67         return !IsDualDisplayFoldDevice() && !IsSingleDisplayPocketFoldDevice() && !IsSecondaryDisplayFoldDevice();
68     }
69 
IsSingleDisplayPocketFoldDevice()70     static bool IsSingleDisplayPocketFoldDevice()
71     {
72         if (!IsValidFoldType(g_foldScreenType)) {
73             return false;
74         }
75         std::vector<std::string> foldTypes = StringSplit(g_foldScreenType, ',');
76         if (foldTypes.empty()) {
77             return false;
78         }
79         return foldTypes[0] == SINGLE_POCKET_DISPLAY;
80     }
81 
IsSuperFoldDisplayDevice()82     static bool IsSuperFoldDisplayDevice()
83     {
84         if (!IsValidFoldType(g_foldScreenType)) {
85             return false;
86         }
87         std::vector<std::string> foldTypes = StringSplit(g_foldScreenType, ',');
88         if (foldTypes.empty()) {
89             return false;
90         }
91         return foldTypes[0] == SUPER_FOLD_DISPLAY;
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 
IsOuterScreen(FoldDisplayMode foldDisplayMode)124     static bool IsOuterScreen(FoldDisplayMode foldDisplayMode)
125     {
126         if (IsDualDisplayFoldDevice()) {
127             return foldDisplayMode == FoldDisplayMode::SUB;
128         }
129         if (IsSingleDisplayFoldDevice() || IsSingleDisplayPocketFoldDevice()) {
130             return foldDisplayMode == FoldDisplayMode::MAIN;
131         }
132         return false;
133     }
134 
IsSecondaryDisplayFoldDevice()135     static bool IsSecondaryDisplayFoldDevice()
136     {
137         return getFoldType() == SECONDARY_FOLD_DISPLAY;
138     }
139 
StringSplit(const std::string & str,char delim)140     static std::vector<std::string> StringSplit(const std::string& str, char delim)
141     {
142         std::size_t previous = 0;
143         std::size_t current = str.find(delim);
144         std::vector<std::string> elems;
145         while (current != std::string::npos) {
146             if (current > previous) {
147                 elems.push_back(str.substr(previous, current - previous));
148             }
149             previous = current + 1;
150             current = str.find(delim, previous);
151         }
152         if (previous != str.size()) {
153             elems.push_back(str.substr(previous));
154         }
155         return elems;
156     }
157 
IsValidFoldType(const std::string & foldTypeStr)158     static bool IsValidFoldType(const std::string& foldTypeStr)
159     {
160         std::regex reg("^([0-9],){3}[0-9]{1}$");
161         return std::regex_match(foldTypeStr, reg);
162     }
163 
164     template<typename T>
TransVec2Str(const std::vector<T> & vec,const std::string & name)165     static std::string TransVec2Str(const std::vector<T> &vec, const std::string &name)
166     {
167         std::stringstream strs;
168         for (uint32_t i = 0; i < vec.size(); i++) {
169             auto str = vec[i];
170             strs << name;
171             if (i == 0) {
172                 strs << "_bc";
173             } else if (i == 1) {
174                 strs << "_ab";
175             } else if (i == THIRD_ANGLE) {
176                 strs << "_ab_anti";
177             }
178             strs << ": ";
179             strs << std::to_string(str) << " ";
180         }
181         return strs.str();
182     }
183 };
184 } // Rosen
185 } // OHOS
186 #endif // OHOS_ROSEN_FOLD_SCREEN_STATE_INTERNEL_H