• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 CJ_INPUTMETHOD_EXTENSION_H
17 #define CJ_INPUTMETHOD_EXTENSION_H
18 
19 #include "cj_inputmethod_extension_context.h"
20 #include "cj_inputmethod_extension_object.h"
21 #include "configuration.h"
22 #include "display_manager.h"
23 #include "inputmethod_extension.h"
24 #include "system_ability_status_change_stub.h"
25 
26 namespace OHOS {
27 namespace AbilityRuntime {
28 /**
29  * @brief Basic inputmethod components.
30  */
31 class CjInputMethodExtension : public InputMethodExtension {
32     struct CacheDisplay {
33         int32_t displayWidth = 0;
34         int32_t displayHeight = 0;
35         Rosen::Rotation displayRotation = Rosen::Rotation::ROTATION_0;
36         Rosen::FoldStatus displayFoldStatus = Rosen::FoldStatus::UNKNOWN;
IsEmptyCacheDisplay37         bool IsEmpty()
38         {
39             return displayWidth == 0 && displayHeight == 0 && displayRotation == Rosen::Rotation::ROTATION_0 &&
40                 displayFoldStatus == Rosen::FoldStatus::UNKNOWN;
41         };
SetCacheDisplayCacheDisplay42         void SetCacheDisplay(int32_t width, int32_t height, Rosen::Rotation rotation, Rosen::FoldStatus foldStatus)
43         {
44             displayWidth = width;
45             displayHeight = height;
46             displayRotation = rotation;
47             displayFoldStatus = foldStatus;
48         };
49     };
50 
51 public:
52     CjInputMethodExtension();
53     ~CjInputMethodExtension() override;
54     static CjInputMethodExtension *cjInputMethodExtension;
55     /**
56      * @brief Create CjInputMethodExtension.
57      *
58      * @param runtime The runtime.
59      * @return The CjInputMethodExtension instance.
60      */
61     static CjInputMethodExtension *Create(const std::unique_ptr<Runtime> &runtime);
62 
63     /**
64      * @brief Init the extension.
65      *
66      * @param record the extension record.
67      * @param application the application info.
68      * @param handler the extension handler.
69      * @param token the remote token.
70      */
71     void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record,
72         const std::shared_ptr<AppExecFwk::OHOSApplication> &application,
73         std::shared_ptr<AppExecFwk::AbilityHandler> &handler, const sptr<IRemoteObject> &token) override;
74 
75     /**
76      * @brief Called when this extension is started. You must override this function if you want to perform some
77      *        initialization operations during extension startup.
78      *
79      * This function can be called only once in the entire lifecycle of an extension.
80      * @param Want Indicates the {@link Want} structure containing startup information about the extension.
81      */
82     void OnStart(const AAFwk::Want &want) override;
83 
84     /**
85      * @brief Called when this InputMethod extension is connected for the first time.
86      *
87      * You can override this function to implement your own processing logic.
88      *
89      * @param want Indicates the {@link Want} structure containing connection information about the InputMethod
90      *        extension.
91      *
92      * @return Returns a pointer to the <b>sid</b> of the connected InputMethod extension.
93      */
94     sptr<IRemoteObject> OnConnect(const AAFwk::Want &want) override;
95 
96     /**
97      * @brief Called when all abilities connected to this InputMethod extension are disconnected.
98      *
99      * You can override this function to implement your own processing logic.
100      *
101      */
102     void OnDisconnect(const AAFwk::Want &want) override;
103 
104     /**
105      * @brief Called back when InputMethod is started.
106      * This method can be called only by InputMethod. You can use the StartAbility(ohos.aafwk.content.Want) method
107      *        to start InputMethod. Then the system calls back the current method to use the transferred want parameter
108      *        to execute its own logic.
109      *
110      * @param want Indicates the want of InputMethod to start.
111      * @param restart Indicates the startup mode. The value true indicates that InputMethod is restarted after being
112      *        destroyed, and the value false indicates a normal startup.
113      * @param startId Indicates the number of times the InputMethod extension has been started. The startId is
114      *        incremented.
115      * by 1 every time the extension is started. For example, if the extension has been started for six times, the
116      * value of startId is 6.
117      */
118     void OnCommand(const AAFwk::Want &want, bool restart, int startId) override;
119 
120     /**
121      * @brief Called when this extension enters the <b>STATE_STOP</b> state.
122      *
123      * The extension in the <b>STATE_STOP</b> is being destroyed.
124      * You can override this function to implement your own processing logic.
125      */
126     void OnStop() override;
127 
128     /**
129      * @brief Called when the system configuration is updated.
130      *
131      * @param configuration Indicates the updated configuration information.
132      */
133     void OnConfigurationUpdated(const AppExecFwk::Configuration &config) override;
134 
135     /**
136      * @brief Called when configuration changed, including system configuration and window configuration.
137      *
138      */
139     void ConfigurationUpdated();
140 
SetCjContext(sptr<CjInputMethodExtensionContext> cjContext)141     void SetCjContext(sptr<CjInputMethodExtensionContext> cjContext)
142     {
143         cjContext_ = cjContext;
144     }
145 
146 private:
147     void ListenWindowManager();
148 
149     void InitDisplayCache();
150 
151     CjInputMethodExtensionObject cjObj_;
152     sptr<CjInputMethodExtensionContext> cjContext_ = nullptr;
153     std::shared_ptr<AbilityHandler> handler_ = nullptr;
154     CacheDisplay cacheDisplay_;
155 
156 protected:
157     class CjInputMethodExtensionDisplayListener : public Rosen::DisplayManager::IDisplayListener {
158     public:
CjInputMethodExtensionDisplayListener(const std::weak_ptr<CjInputMethodExtension> & extension)159         explicit CjInputMethodExtensionDisplayListener(const std::weak_ptr<CjInputMethodExtension> &extension)
160         {
161             cjInputMethodExtension_ = extension;
162         }
163 
OnCreate(Rosen::DisplayId displayId)164         void OnCreate(Rosen::DisplayId displayId) override
165         {
166             auto inputMethodSptr = cjInputMethodExtension_.lock();
167             if (inputMethodSptr != nullptr) {
168                 inputMethodSptr->OnCreate(displayId);
169             }
170         }
171 
OnDestroy(Rosen::DisplayId displayId)172         void OnDestroy(Rosen::DisplayId displayId) override
173         {
174             auto inputMethodSptr = cjInputMethodExtension_.lock();
175             if (inputMethodSptr != nullptr) {
176                 inputMethodSptr->OnDestroy(displayId);
177             }
178         }
179 
OnChange(Rosen::DisplayId displayId)180         void OnChange(Rosen::DisplayId displayId) override
181         {
182             auto inputMethodSptr = cjInputMethodExtension_.lock();
183             if (inputMethodSptr != nullptr) {
184                 inputMethodSptr->CheckNeedAdjustKeyboard(displayId);
185                 inputMethodSptr->OnChange(displayId);
186             }
187         }
188 
189     private:
190         std::weak_ptr<CjInputMethodExtension> cjInputMethodExtension_;
191     };
192 
193     void OnCreate(Rosen::DisplayId displayId);
194     void OnDestroy(Rosen::DisplayId displayId);
195     void OnChange(Rosen::DisplayId displayId);
196     void CheckNeedAdjustKeyboard(Rosen::DisplayId displayId);
197 
198 private:
199     class SystemAbilityStatusChangeListener : public OHOS::SystemAbilityStatusChangeStub {
200     public:
SystemAbilityStatusChangeListener(sptr<CjInputMethodExtensionDisplayListener> displayListener)201         SystemAbilityStatusChangeListener(sptr<CjInputMethodExtensionDisplayListener> displayListener)
202             : listener_(displayListener) { };
~SystemAbilityStatusChangeListener()203         ~SystemAbilityStatusChangeListener()
204         {
205             if (listener_ != nullptr) {
206                 Rosen::DisplayManager::GetInstance().RegisterDisplayListener(listener_);
207             }
208         }
209         void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)210         void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override { }
211 
212     private:
213         sptr<CjInputMethodExtensionDisplayListener> listener_ = nullptr;
214     };
215 
216     sptr<CjInputMethodExtensionDisplayListener> displayListener_ = nullptr;
217 };
218 } // namespace AbilityRuntime
219 } // namespace OHOS
220 #endif // CJ_INPUTMETHOD_EXTENSION_H