• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_JS_WINDOW_UTILS_H
17 #define OHOS_JS_WINDOW_UTILS_H
18 #include <map>
19 #include "js_runtime_utils.h"
20 #include "native_engine/native_engine.h"
21 #include "native_engine/native_value.h"
22 #include "window.h"
23 
24 #ifndef WINDOW_PREVIEW
25 #include "window_manager.h"
26 #else
27 #include "mock/window_manager.h"
28 #endif
29 
30 #include "window_option.h"
31 #include "wm_common.h"
32 namespace OHOS {
33 namespace Rosen {
34 constexpr int32_t RGB_LENGTH = 6;
35 constexpr int32_t RGBA_LENGTH = 8;
36 enum class ApiWindowType : uint32_t {
37     TYPE_BASE,
38     TYPE_APP = TYPE_BASE,
39     TYPE_SYSTEM_ALERT,
40     TYPE_INPUT_METHOD,
41     TYPE_STATUS_BAR,
42     TYPE_PANEL,
43     TYPE_KEYGUARD,
44     TYPE_VOLUME_OVERLAY,
45     TYPE_NAVIGATION_BAR,
46     TYPE_FLOAT,
47     TYPE_WALLPAPER,
48     TYPE_DESKTOP,
49     TYPE_LAUNCHER_RECENT,
50     TYPE_LAUNCHER_DOCK,
51     TYPE_VOICE_INTERACTION,
52     TYPE_POINTER,
53     TYPE_FLOAT_CAMERA,
54     TYPE_DIALOG,
55     TYPE_SCREENSHOT,
56     TYPE_END
57 };
58 
59 enum class LifeCycleEventType : uint32_t {
60     FOREGROUND = 1,
61     ACTIVE,
62     INACTIVE,
63     BACKGROUND,
64 };
65 
66 const std::map<WindowType, ApiWindowType> NATIVE_JS_TO_WINDOW_TYPE_MAP {
67     { WindowType::WINDOW_TYPE_APP_SUB_WINDOW,      ApiWindowType::TYPE_APP               },
68     { WindowType::WINDOW_TYPE_DIALOG,              ApiWindowType::TYPE_DIALOG            },
69     { WindowType::WINDOW_TYPE_SYSTEM_ALARM_WINDOW, ApiWindowType::TYPE_SYSTEM_ALERT      },
70     { WindowType::WINDOW_TYPE_INPUT_METHOD_FLOAT,  ApiWindowType::TYPE_INPUT_METHOD      },
71     { WindowType::WINDOW_TYPE_STATUS_BAR,          ApiWindowType::TYPE_STATUS_BAR        },
72     { WindowType::WINDOW_TYPE_PANEL,               ApiWindowType::TYPE_PANEL             },
73     { WindowType::WINDOW_TYPE_KEYGUARD,            ApiWindowType::TYPE_KEYGUARD          },
74     { WindowType::WINDOW_TYPE_VOLUME_OVERLAY,      ApiWindowType::TYPE_VOLUME_OVERLAY    },
75     { WindowType::WINDOW_TYPE_NAVIGATION_BAR,      ApiWindowType::TYPE_NAVIGATION_BAR    },
76     { WindowType::WINDOW_TYPE_FLOAT,               ApiWindowType::TYPE_FLOAT             },
77     { WindowType::WINDOW_TYPE_FLOAT_CAMERA,        ApiWindowType::TYPE_FLOAT_CAMERA      },
78     { WindowType::WINDOW_TYPE_WALLPAPER,           ApiWindowType::TYPE_WALLPAPER         },
79     { WindowType::WINDOW_TYPE_DESKTOP,             ApiWindowType::TYPE_DESKTOP           },
80     { WindowType::WINDOW_TYPE_LAUNCHER_RECENT,     ApiWindowType::TYPE_LAUNCHER_RECENT   },
81     { WindowType::WINDOW_TYPE_LAUNCHER_DOCK,       ApiWindowType::TYPE_LAUNCHER_DOCK     },
82     { WindowType::WINDOW_TYPE_VOICE_INTERACTION,   ApiWindowType::TYPE_VOICE_INTERACTION },
83     { WindowType::WINDOW_TYPE_POINTER,             ApiWindowType::TYPE_POINTER           },
84     { WindowType::WINDOW_TYPE_SCREENSHOT,          ApiWindowType::TYPE_SCREENSHOT        },
85 };
86 
87 const std::map<ApiWindowType, WindowType> JS_TO_NATIVE_WINDOW_TYPE_MAP {
88     { ApiWindowType::TYPE_APP,                 WindowType::WINDOW_TYPE_APP_SUB_WINDOW      },
89     { ApiWindowType::TYPE_DIALOG,              WindowType::WINDOW_TYPE_DIALOG              },
90     { ApiWindowType::TYPE_SYSTEM_ALERT,        WindowType::WINDOW_TYPE_SYSTEM_ALARM_WINDOW },
91     { ApiWindowType::TYPE_INPUT_METHOD,        WindowType::WINDOW_TYPE_INPUT_METHOD_FLOAT  },
92     { ApiWindowType::TYPE_STATUS_BAR,          WindowType::WINDOW_TYPE_STATUS_BAR          },
93     { ApiWindowType::TYPE_PANEL,               WindowType::WINDOW_TYPE_PANEL               },
94     { ApiWindowType::TYPE_KEYGUARD,            WindowType::WINDOW_TYPE_KEYGUARD            },
95     { ApiWindowType::TYPE_VOLUME_OVERLAY,      WindowType::WINDOW_TYPE_VOLUME_OVERLAY      },
96     { ApiWindowType::TYPE_NAVIGATION_BAR,      WindowType::WINDOW_TYPE_NAVIGATION_BAR      },
97     { ApiWindowType::TYPE_FLOAT,               WindowType::WINDOW_TYPE_FLOAT               },
98     { ApiWindowType::TYPE_FLOAT_CAMERA,        WindowType::WINDOW_TYPE_FLOAT_CAMERA        },
99     { ApiWindowType::TYPE_WALLPAPER,           WindowType::WINDOW_TYPE_WALLPAPER           },
100     { ApiWindowType::TYPE_DESKTOP,             WindowType::WINDOW_TYPE_DESKTOP             },
101     { ApiWindowType::TYPE_LAUNCHER_RECENT,     WindowType::WINDOW_TYPE_LAUNCHER_RECENT     },
102     { ApiWindowType::TYPE_LAUNCHER_DOCK,       WindowType::WINDOW_TYPE_LAUNCHER_DOCK       },
103     { ApiWindowType::TYPE_VOICE_INTERACTION,   WindowType::WINDOW_TYPE_VOICE_INTERACTION   },
104     { ApiWindowType::TYPE_POINTER,             WindowType::WINDOW_TYPE_POINTER             },
105     { ApiWindowType::TYPE_SCREENSHOT,          WindowType::WINDOW_TYPE_SCREENSHOT          },
106 };
107 
108 enum class ApiWindowMode : uint32_t {
109     UNDEFINED = 1,
110     FULLSCREEN,
111     PRIMARY,
112     SECONDARY,
113     FLOATING,
114     MODE_END = FLOATING
115 };
116 
117 const std::map<WindowMode, ApiWindowMode> NATIVE_TO_JS_WINDOW_MODE_MAP {
118     { WindowMode::WINDOW_MODE_UNDEFINED,       ApiWindowMode::UNDEFINED  },
119     { WindowMode::WINDOW_MODE_FULLSCREEN,      ApiWindowMode::FULLSCREEN },
120     { WindowMode::WINDOW_MODE_SPLIT_PRIMARY,   ApiWindowMode::PRIMARY    },
121     { WindowMode::WINDOW_MODE_SPLIT_SECONDARY, ApiWindowMode::SECONDARY  },
122     { WindowMode::WINDOW_MODE_FLOATING,        ApiWindowMode::FLOATING   },
123 };
124 
125 const std::map<ApiWindowMode, WindowMode> JS_TO_NATIVE_WINDOW_MODE_MAP {
126     {ApiWindowMode::UNDEFINED,  WindowMode::WINDOW_MODE_UNDEFINED       },
127     {ApiWindowMode::FULLSCREEN, WindowMode::WINDOW_MODE_FULLSCREEN      },
128     {ApiWindowMode::PRIMARY,    WindowMode::WINDOW_MODE_SPLIT_PRIMARY   },
129     {ApiWindowMode::SECONDARY,  WindowMode::WINDOW_MODE_SPLIT_SECONDARY },
130     {ApiWindowMode::FLOATING,   WindowMode::WINDOW_MODE_FLOATING        },
131 };
132 
133 enum class ApiOrientation : uint32_t {
134     UNSPECIFIED = 0,
135     PORTRAIT = 1,
136     LANDSCAPE = 2,
137     PORTRAIT_INVERTED = 3,
138     LANDSCAPE_INVERTED = 4,
139     AUTO_ROTATION = 5,
140     AUTO_ROTATION_PORTRAIT = 6,
141     AUTO_ROTATION_LANDSCAPE = 7,
142     AUTO_ROTATION_RESTRICTED = 8,
143     AUTO_ROTATION_PORTRAIT_RESTRICTED = 9,
144     AUTO_ROTATION_LANDSCAPE_RESTRICTED = 10,
145     LOCKED = 11,
146 };
147 
148 const std::map<ApiOrientation, Orientation> JS_TO_NATIVE_ORIENTATION_MAP {
149     {ApiOrientation::UNSPECIFIED,                           Orientation::UNSPECIFIED                        },
150     {ApiOrientation::PORTRAIT,                              Orientation::VERTICAL                           },
151     {ApiOrientation::LANDSCAPE,                             Orientation::HORIZONTAL                         },
152     {ApiOrientation::PORTRAIT_INVERTED,                     Orientation::REVERSE_VERTICAL                   },
153     {ApiOrientation::LANDSCAPE_INVERTED,                    Orientation::REVERSE_HORIZONTAL                 },
154     {ApiOrientation::AUTO_ROTATION,                         Orientation::SENSOR                             },
155     {ApiOrientation::AUTO_ROTATION_PORTRAIT,                Orientation::SENSOR_VERTICAL                    },
156     {ApiOrientation::AUTO_ROTATION_LANDSCAPE,               Orientation::SENSOR_HORIZONTAL                  },
157     {ApiOrientation::AUTO_ROTATION_RESTRICTED,              Orientation::AUTO_ROTATION_RESTRICTED           },
158     {ApiOrientation::AUTO_ROTATION_PORTRAIT_RESTRICTED,     Orientation::AUTO_ROTATION_PORTRAIT_RESTRICTED  },
159     {ApiOrientation::AUTO_ROTATION_LANDSCAPE_RESTRICTED,    Orientation::AUTO_ROTATION_LANDSCAPE_RESTRICTED },
160     {ApiOrientation::LOCKED,                                Orientation::LOCKED                             },
161 };
162 
163     NativeValue* GetRectAndConvertToJsValue(NativeEngine& engine, const Rect& rect);
164     NativeValue* CreateJsWindowPropertiesObject(NativeEngine& engine, sptr<Window>& window);
165     bool SetSystemBarPropertiesFromJs(NativeEngine& engine, NativeObject* jsObject,
166         std::map<WindowType, SystemBarProperty>& properties, sptr<Window>& window);
167     bool GetSystemBarStatus(std::map<WindowType, SystemBarProperty>& systemBarProperties,
168         NativeEngine& engine, NativeCallbackInfo& info, sptr<Window>& window);
169     NativeValue* CreateJsSystemBarRegionTintArrayObject(NativeEngine& engine,
170         const SystemBarRegionTints& tints);
171     NativeValue* ConvertAvoidAreaToJsValue(NativeEngine& engine, const AvoidArea& avoidArea, AvoidAreaType type);
172     bool CheckCallingPermission(std::string permission);
173     NativeValue* WindowTypeInit(NativeEngine* engine);
174     NativeValue* AvoidAreaTypeInit(NativeEngine* engine);
175     NativeValue* WindowModeInit(NativeEngine* engine);
176     NativeValue* ColorSpaceInit(NativeEngine* engine);
177     NativeValue* OrientationInit(NativeEngine* engine);
178     NativeValue* WindowStageEventTypeInit(NativeEngine* engine);
179     NativeValue* WindowLayoutModeInit(NativeEngine* engine);
180     NativeValue* BlurStyleInit(NativeEngine* engine);
181     NativeValue* WindowErrorCodeInit(NativeEngine* engine);
182     NativeValue* WindowErrorInit(NativeEngine* engine);
183     bool GetAPI7Ability(NativeEngine& engine, AppExecFwk::Ability* &ability);
184     template<class T>
ParseJsValue(NativeObject * jsObject,NativeEngine & engine,const std::string & name,T & data)185     bool ParseJsValue(NativeObject* jsObject, NativeEngine& engine, const std::string& name, T& data)
186     {
187         NativeValue* value = jsObject->GetProperty(name.c_str());
188         if (value->TypeOf() != NATIVE_UNDEFINED) {
189             if (!AbilityRuntime::ConvertFromJsValue(engine, value, data)) {
190                 return false;
191             }
192         } else {
193             return false;
194         }
195         return true;
196     }
197     template<class T>
ConvertNativeValueToVector(NativeEngine & engine,NativeValue * nativeValue,std::vector<T> & out)198     inline bool ConvertNativeValueToVector(NativeEngine& engine, NativeValue* nativeValue, std::vector<T>& out)
199     {
200         NativeArray* nativeArray = AbilityRuntime::ConvertNativeValueTo<NativeArray>(nativeValue);
201         if (nativeArray == nullptr) {
202             return false;
203         }
204         T value;
205         for (uint32_t i = 0; i < nativeArray->GetLength(); i++) {
206             if (!AbilityRuntime::ConvertFromJsValue(engine, nativeArray->GetElement(i), value)) {
207                 return false;
208             }
209             out.emplace_back(value);
210         }
211         return true;
212     }
213 }
214 }
215 #endif