• 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 
23 namespace OHOS {
24 namespace Rosen {
25 namespace {
26 static const std::string INVALID_DEVICE = "-1";
27 static const std::string g_foldScreenType = system::GetParameter("const.window.foldscreen.type", "0,0,0,0");
28 static const std::string PHY_ROTATION_OFFSET = system::GetParameter("const.window.phyrotation.offset", "0");
29 static const  std::string SINGLE_DISPLAY = "1";
30 static const std::string DUAL_DISPLAY = "2";
31 static const std::string SINGLE_POCKET_DISPLAY = "4";
32 static const std::string SUPER_FOLD_DISPLAY = "5";
33 static const std::string SECONDARY_FOLD_DISPLAY = "6";
34 static const std::string DEFAULT_OFFSET = "0";
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         return g_foldScreenType != "";
54     }
55 
IsDualDisplayFoldDevice()56     static bool IsDualDisplayFoldDevice()
57     {
58         if (!IsValidFoldType(g_foldScreenType)) {
59             return false;
60         }
61         std::vector<std::string> foldTypes = StringSplit(g_foldScreenType, ',');
62         if (foldTypes.empty()) {
63             return false;
64         }
65         return foldTypes[0] == DUAL_DISPLAY;
66     }
67 
IsSingleDisplayFoldDevice()68     static bool IsSingleDisplayFoldDevice()
69     {
70         // ALTB ccm property conflict with the chip, waiting for chip conflict resolution
71         return !IsDualDisplayFoldDevice() && !IsSingleDisplayPocketFoldDevice() &&
72             !IsSuperFoldDisplayDevice() && !IsSecondaryDisplayFoldDevice();
73     }
74 
IsSingleDisplayPocketFoldDevice()75     static bool IsSingleDisplayPocketFoldDevice()
76     {
77         if (!IsValidFoldType(g_foldScreenType)) {
78             return false;
79         }
80         std::vector<std::string> foldTypes = StringSplit(g_foldScreenType, ',');
81         if (foldTypes.empty()) {
82             return false;
83         }
84         return foldTypes[0] == SINGLE_POCKET_DISPLAY;
85     }
86 
IsSuperFoldDisplayDevice()87     static bool IsSuperFoldDisplayDevice()
88     {
89         if (!IsValidFoldType(g_foldScreenType)) {
90             return false;
91         }
92         std::vector<std::string> foldTypes = StringSplit(g_foldScreenType, ',');
93         if (foldTypes.empty()) {
94             return false;
95         }
96         return foldTypes[0] == SUPER_FOLD_DISPLAY;
97     }
98 
IsSecondaryDisplayFoldDevice()99     static bool IsSecondaryDisplayFoldDevice()
100     {
101         return GetFoldType() == SECONDARY_FOLD_DISPLAY;
102     }
103 
IsOuterScreen(FoldDisplayMode foldDisplayMode)104     static bool IsOuterScreen(FoldDisplayMode foldDisplayMode)
105     {
106         if (IsDualDisplayFoldDevice()) {
107             return foldDisplayMode == FoldDisplayMode::SUB;
108         }
109         if (IsSingleDisplayFoldDevice() || IsSingleDisplayPocketFoldDevice()) {
110             return foldDisplayMode == FoldDisplayMode::MAIN;
111         }
112         return false;
113     }
114 
StringSplit(const std::string & str,char delim)115     static std::vector<std::string> StringSplit(const std::string& str, char delim)
116     {
117         std::size_t previous = 0;
118         std::size_t current = str.find(delim);
119         std::vector<std::string> elems;
120         while (current != std::string::npos) {
121             if (current > previous) {
122                 elems.push_back(str.substr(previous, current - previous));
123             }
124             previous = current + 1;
125             current = str.find(delim, previous);
126         }
127         if (previous != str.size()) {
128             elems.push_back(str.substr(previous));
129         }
130         return elems;
131     }
132 
GetPhyRotationOffset()133     static std::vector<std::string> GetPhyRotationOffset()
134     {
135         static std::vector<std::string> phyOffsets;
136         if (phyOffsets.empty()) {
137             std::vector<std::string> elems = StringSplit(PHY_ROTATION_OFFSET, ';');
138             for (auto& num : elems) {
139                 if (IsNumber(num)) {
140                     phyOffsets.push_back(num);
141                 } else {
142                     phyOffsets.push_back(DEFAULT_OFFSET);
143                 }
144             }
145         }
146         return phyOffsets;
147     }
148 
IsNumber(const std::string & str)149     static bool IsNumber(const std::string& str)
150     {
151         int32_t length = static_cast<int32_t>(str.size());
152         if (length == 0) {
153             return false;
154         }
155         for (int32_t i = 0; i < length; i++) {
156             if (str.at(i) < '0' || str.at(i) > '9') {
157                 return false;
158             }
159         }
160         return true;
161     }
162 
IsValidFoldType(const std::string & foldTypeStr)163     static bool IsValidFoldType(const std::string& foldTypeStr)
164     {
165         std::regex reg("^([0-9],){3}[0-9]{1}$");
166         return std::regex_match(foldTypeStr, reg);
167     }
168 
169     template<typename T>
TransVec2Str(const std::vector<T> & vec,const std::string & name)170     static std::string TransVec2Str(const std::vector<T> &vec, const std::string &name)
171     {
172         std::stringstream strs;
173         for (uint32_t i = 0; i < vec.size(); i++) {
174             auto str = vec[i];
175             strs << name;
176             if (i == 0) {
177                 strs << "_bc";
178             } else if (i == 1) {
179                 strs << "_ab";
180             } else if (i == THIRD_ANGLE) {
181                 strs << "_ab_anti";
182             }
183             strs << ": ";
184             strs << std::to_string(str) << " ";
185         }
186         return strs.str();
187     }
188 };
189 } // Rosen
190 } // OHOS
191 #endif // OHOS_ROSEN_FOLD_SCREEN_STATE_INTERNEL_H