1 /* 2 * Copyright (c) 2023 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 INPUT_METHOD_PANEL_H 17 #define INPUT_METHOD_PANEL_H 18 19 #include <cstdint> 20 #include <functional> 21 #include <map> 22 #include <string> 23 24 #include "calling_window_info.h" 25 #include "display_manager.h" 26 #include "input_window_info.h" 27 #include "native_engine/native_engine.h" 28 #include "panel_common.h" 29 #include "panel_info.h" 30 #include "window_change_listener_impl.h" 31 #include "wm_common.h" 32 33 namespace OHOS { 34 namespace MiscServices { 35 constexpr int FOLD_TOP = 0; 36 constexpr int FOLD_LEFT = 0; 37 constexpr int FOLD_RIGHT = 0; 38 constexpr int FOLD_BOTTOM = 606; 39 constexpr int UNFOLD_TOP = 0; 40 constexpr int UNFOLD_LEFT = 0; 41 constexpr int UNFOLD_RIGHT = 0; 42 constexpr int UNFOLD_BOTTOM = 822; 43 constexpr int COMMON_BOTTOM = 809; 44 45 class InputMethodPanel { 46 public: 47 static constexpr uint32_t INVALID_WINDOW_ID = 0; 48 using CallbackFunc = std::function<void(uint32_t, PanelFlag)>; 49 InputMethodPanel() = default; 50 ~InputMethodPanel(); 51 int32_t SetUiContent(const std::string &contentInfo, napi_env env, std::shared_ptr<NativeReference> contentStorage); 52 int32_t CreatePanel(const std::shared_ptr<AbilityRuntime::Context> &context, const PanelInfo &panelInfo); 53 int32_t DestroyPanel(); 54 55 int32_t Resize(uint32_t width, uint32_t height); 56 int32_t MoveTo(int32_t x, int32_t y); 57 int32_t StartMoving(); 58 int32_t GetDisplayId(uint64_t &displayId); 59 int32_t AdjustKeyboard(); 60 int32_t AdjustPanelRect(const PanelFlag panelFlag, const LayoutParams &layoutParams, bool needUpdateRegion = true); 61 int32_t AdjustPanelRect(PanelFlag panelFlag, EnhancedLayoutParams params, HotAreas hotAreas); 62 int32_t UpdateRegion(std::vector<Rosen::Rect> region); 63 std::tuple<std::vector<std::string>, std::vector<std::string>> GetScreenStatus(const PanelFlag panelFlag); 64 int32_t ChangePanelFlag(PanelFlag panelFlag); 65 PanelType GetPanelType(); 66 PanelFlag GetPanelFlag(); 67 int32_t ShowPanel(); 68 int32_t HidePanel(); 69 int32_t SizeChange(const WindowSize &size); 70 WindowSize GetKeyboardSize(); 71 bool SetPanelStatusListener(std::shared_ptr<PanelStatusListener> statusListener, const std::string &type); 72 void ClearPanelListener(const std::string &type); 73 int32_t SetCallingWindow(uint32_t windowId); 74 int32_t GetCallingWindowInfo(CallingWindowInfo &windowInfo); 75 int32_t SetPrivacyMode(bool isPrivacyMode); 76 bool IsShowing(); 77 bool IsDisplayPortrait(); 78 int32_t SetTextFieldAvoidInfo(double positionY, double height); 79 void SetPanelHeightCallback(CallbackFunc heightCallback); 80 int32_t IsEnhancedParamValid(PanelFlag panelFlag, EnhancedLayoutParams ¶ms); 81 int32_t SetImmersiveMode(ImmersiveMode mode); 82 ImmersiveMode GetImmersiveMode(); 83 bool IsInMainDisplay(); 84 int32_t SetImmersiveEffect(const ImmersiveEffect &effect); 85 int32_t SetKeepScreenOn(bool isKeepScreenOn); 86 uint32_t windowId_ = INVALID_WINDOW_ID; 87 88 private: 89 class KeyboardPanelInfoChangeListener : public Rosen::IKeyboardPanelInfoChangeListener { 90 public: 91 using ChangeHandler = std::function<void(const Rosen::KeyboardPanelInfo &keyboardPanelInfo)>; KeyboardPanelInfoChangeListener(ChangeHandler handler)92 explicit KeyboardPanelInfoChangeListener(ChangeHandler handler) : handler_(std::move(handler)) { } 93 ~KeyboardPanelInfoChangeListener() = default; OnKeyboardPanelInfoChanged(const Rosen::KeyboardPanelInfo & keyboardPanelInfo)94 void OnKeyboardPanelInfoChanged(const Rosen::KeyboardPanelInfo &keyboardPanelInfo) override 95 { 96 if (handler_ == nullptr) { 97 return; 98 } 99 handler_(keyboardPanelInfo); 100 } 101 102 private: 103 ChangeHandler handler_ = nullptr; 104 }; 105 void RegisterKeyboardPanelInfoChangeListener(); 106 void UnregisterKeyboardPanelInfoChangeListener(); 107 void HandleKbPanelInfoChange(const Rosen::KeyboardPanelInfo &keyboardPanelInfo); 108 bool IsHidden(); 109 int32_t SetPanelProperties(); 110 std::string GeneratePanelName(); 111 void PanelStatusChange(const InputWindowStatus &status); 112 void PanelStatusChangeToImc(const InputWindowStatus &status, const Rosen::Rect &rect); 113 bool MarkListener(const std::string &type, bool isRegister); 114 bool SetPanelSizeChangeListener(std::shared_ptr<PanelStatusListener> statusListener); 115 std::shared_ptr<PanelStatusListener> GetPanelListener(); 116 static uint32_t GenerateSequenceId(); 117 bool IsSizeValid(uint32_t width, uint32_t height); 118 bool IsSizeValid(PanelFlag panelFlag, uint32_t width, uint32_t height, int32_t displayWidth, int32_t displayHeight); 119 bool IsRectValid(const Rosen::Rect &rect, const WindowSize &displaySize); 120 bool IsRectValid(PanelFlag panelFlag, const Rosen::Rect &rect, const WindowSize &displaySize); 121 int32_t GetResizeParams(Rosen::Rect &portrait, Rosen::Rect &landscape, uint32_t width, uint32_t height); 122 123 int32_t ParseEnhancedParams( 124 PanelFlag panelFlag, const FullPanelAdjustInfo &adjustInfo, EnhancedLayoutParams ¶ms); 125 int32_t RectifyRect(bool isFullScreen, EnhancedLayoutParam &layoutParam, const WindowSize &displaySize, 126 PanelFlag panelFlag, const PanelAdjustInfo &adjustInfo); 127 int32_t CalculateAvoidHeight(EnhancedLayoutParam &layoutParam, const WindowSize &displaySize, PanelFlag panelFlag, 128 const PanelAdjustInfo &adjustInfo); 129 130 int32_t ParseParams(PanelFlag panelFlag, const LayoutParams &input, Rosen::KeyboardLayoutParams &output); 131 void ParseParam(PanelFlag panelFlag, const PanelAdjustInfo &adjustInfo, const WindowSize &displaySize, 132 const Rosen::Rect &inputRect, EnhancedLayoutParam &outputParam); 133 134 void CalculateHotAreas(const EnhancedLayoutParams &enhancedParams, const Rosen::KeyboardLayoutParams ¶ms, 135 const FullPanelAdjustInfo &adjustInfo, HotAreas &hotAreas); 136 void CalculateDefaultHotArea(const Rosen::Rect &keyboard, const Rosen::Rect &panel, 137 const PanelAdjustInfo &adjustInfo, HotArea &hotArea, uint32_t changeY); 138 void CalculateHotArea(const Rosen::Rect &keyboard, const Rosen::Rect &panel, const PanelAdjustInfo &adjustInfo, 139 HotArea &hotArea, uint32_t changeY); 140 void CalculateEnhancedHotArea( 141 const EnhancedLayoutParam &layout, const PanelAdjustInfo &adjustInfo, HotArea &hotArea, uint32_t changeY); 142 void RectifyAreas(const std::vector<Rosen::Rect> &availableAreas, std::vector<Rosen::Rect> &areas); 143 Rosen::Rect GetRectIntersection(Rosen::Rect a, Rosen::Rect b); 144 uint32_t SafeSubtract(uint32_t minuend, uint32_t subtrahend); 145 146 int32_t ResizePanel(uint32_t width, uint32_t height); 147 int32_t ResizeWithoutAdjust(uint32_t width, uint32_t height); 148 int32_t ResizeEnhancedPanel(uint32_t width, uint32_t height); 149 void UpdateRectParams( 150 Rosen::Rect &portrait, Rosen::Rect &landscape, uint32_t width, uint32_t height, const LayoutParams &currParams); 151 void RectifyResizeParams(LayoutParams ¶ms, const DisplaySize &displaySize); 152 int32_t MovePanelRect(int32_t x, int32_t y); 153 int32_t MoveEnhancedPanelRect(int32_t x, int32_t y); 154 155 bool IsDisplayUnfolded(); 156 int32_t GetDisplaySize(DisplaySize &size); 157 int32_t GetDensityDpi(float &densityDpi); 158 159 int32_t InitAdjustInfo(); 160 int32_t GetAdjustInfo(PanelFlag panelFlag, FullPanelAdjustInfo &fullPanelAdjustInfo); 161 162 void UpdateResizeParams(); 163 void UpdateHotAreas(); 164 void UpdateLayoutInfo(PanelFlag panelFlag, const LayoutParams ¶ms, const EnhancedLayoutParams &enhancedParams, 165 const Rosen::KeyboardLayoutParams &wmsParams, bool isEnhanced); 166 Rosen::KeyboardLayoutParams ConvertToWMSParam(PanelFlag panelFlag, const EnhancedLayoutParams &layoutParams); 167 Rosen::KeyboardTouchHotAreas ConvertToWMSHotArea(const HotAreas &hotAreas); 168 void OnPanelHeightChange(const Rosen::KeyboardPanelInfo &keyboardPanelInfo); 169 int32_t GetKeyboardArea(PanelFlag panelFlag, const WindowSize &size, PanelAdjustInfo &keyboardArea); 170 int32_t GetWindowOrientation(PanelFlag panelFlag, uint32_t windowWidth, bool &isPortrait); 171 172 sptr<Rosen::Display> GetCurDisplay(); 173 uint64_t GetCurDisplayId(); 174 bool IsNeedConfig(); 175 int32_t IsValidParam(const ImmersiveEffect &effect, const Rosen::KeyboardLayoutParams &layoutParams); 176 int32_t AdjustLayout(const Rosen::KeyboardLayoutParams ¶m); 177 int32_t AdjustLayout(const Rosen::KeyboardLayoutParams ¶m, const ImmersiveEffect &effect); 178 int32_t FullScreenPrepare(Rosen::KeyboardLayoutParams ¶m, const ImmersiveEffect &effect); 179 int32_t NormalImePrepare(Rosen::KeyboardLayoutParams ¶m, const ImmersiveEffect &effect); 180 int32_t PrepareAdjustLayout(Rosen::KeyboardLayoutParams ¶m, const ImmersiveEffect &effect); 181 bool IsImmersiveEffectSupported(); 182 Rosen::KeyboardEffectOption ConvertToWmEffect(ImmersiveMode mode, const ImmersiveEffect &effect); 183 void SetImmersiveEffectToNone(); 184 void UpdateImmersiveHotArea(); 185 bool IsValidGradientHeight(uint32_t gradientHeight); 186 187 // Locked read and write functions for concurrent protection 188 Rosen::KeyboardLayoutParams GetKeyboardLayoutParams(); 189 void SetKeyboardLayoutParams(Rosen::KeyboardLayoutParams params); 190 EnhancedLayoutParams GetEnhancedLayoutParams(); 191 void SetEnhancedLayoutParams(EnhancedLayoutParams params); 192 std::vector<int32_t> GetIgnoreAdjustInputTypes(); 193 void SetIgnoreAdjustInputTypes(const std::vector<int32_t> &inputTypes); 194 ImmersiveEffect LoadImmersiveEffect(); 195 void StoreImmersiveEffect(ImmersiveEffect effect); 196 HotAreas GetHotAreas(); 197 void SetHotAreas(const HotAreas &hotAreas); 198 ChangeY GetChangeY(); 199 void SetChangeY(ChangeY changeY); 200 201 void StoreImmersiveMode(ImmersiveMode mode); 202 CallbackFunc GetPanelHeightCallback(); 203 204 sptr<OHOS::Rosen::Window> window_ = nullptr; 205 sptr<OHOS::Rosen::WindowOption> winOption_ = nullptr; 206 PanelType panelType_ = PanelType::SOFT_KEYBOARD; 207 PanelFlag panelFlag_ = PanelFlag::FLG_FIXED; 208 bool showRegistered_ = false; 209 bool hideRegistered_ = false; 210 bool sizeChangeRegistered_ = false; 211 bool sizeUpdateRegistered_ = false; 212 uint32_t invalidGravityPercent = 0; 213 std::shared_ptr<PanelStatusListener> panelStatusListener_ = nullptr; 214 215 static std::atomic<uint32_t> sequenceId_; 216 sptr<Rosen::IKeyboardPanelInfoChangeListener> kbPanelInfoListener_ { nullptr }; 217 bool isScbEnable_ { false }; 218 219 std::mutex panelAdjustLock_; 220 std::map<std::vector<std::string>, PanelAdjustInfo> panelAdjust_; 221 std::mutex adjustInfoInitLock_; 222 uint64_t adjustInfoDisplayId_ = 0; 223 std::atomic<bool> isAdjustInfoInitialized_{ false }; 224 std::atomic<bool> isIgnorePanelAdjustInitialized_{ false }; 225 std::mutex ignoreAdjustInputTypeLock_; 226 std::vector<int32_t> ignoreAdjustInputTypes_; 227 228 std::mutex hotAreasLock_; 229 HotAreas hotAreas_; 230 std::mutex enhancedLayoutParamMutex_; 231 EnhancedLayoutParams enhancedLayoutParams_; 232 std::mutex keyboardLayoutParamsMutex_; 233 Rosen::KeyboardLayoutParams keyboardLayoutParams_; 234 235 std::mutex keyboardSizeLock_; 236 WindowSize keyboardSize_ { 0, 0 }; 237 std::mutex windowListenerLock_; 238 sptr<Rosen::IWindowChangeListener> windowChangedListener_ = nullptr; 239 std::mutex heightCallbackMutex_; 240 CallbackFunc panelHeightCallback_ = nullptr; 241 242 std::mutex foldResizeParamMutex_; 243 LayoutParams resizePanelFoldParams_ { // FoldDefaultValue 244 { FOLD_TOP, FOLD_LEFT, FOLD_RIGHT, FOLD_BOTTOM }, 245 { FOLD_TOP, FOLD_LEFT, FOLD_RIGHT, COMMON_BOTTOM } 246 }; 247 248 std::mutex unfoldResizeParamMutex_; 249 LayoutParams resizePanelUnfoldParams_ { // UnfoldDefaultValue 250 { UNFOLD_TOP, UNFOLD_LEFT, UNFOLD_RIGHT, UNFOLD_BOTTOM }, 251 { UNFOLD_TOP, UNFOLD_LEFT, UNFOLD_RIGHT, COMMON_BOTTOM } 252 }; 253 std::atomic<bool> isWaitSetUiContent_ { true }; 254 std::atomic<bool> isInEnhancedAdjust_{ false }; 255 256 std::mutex immersiveModeMutex_; 257 ImmersiveMode immersiveMode_ { ImmersiveMode::NONE_IMMERSIVE }; 258 std::mutex immersiveEffectMutex_; 259 ImmersiveEffect immersiveEffect_ { 0, GradientMode::NONE, FluidLightMode::NONE }; 260 std::mutex changeYMutex_; 261 ChangeY changeY_; 262 }; 263 } // namespace MiscServices 264 } // namespace OHOS 265 266 #endif // INPUT_METHOD_PANEL_H 267