• 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_ABILITY_RUNTIME_CJ_UI_ABILITY_H
17 #define OHOS_ABILITY_RUNTIME_CJ_UI_ABILITY_H
18 
19 #include "cj_ability_delegator_infos.h"
20 #include "freeze_util.h"
21 #include "ui_ability.h"
22 #ifdef SUPPORT_GRAPHICS
23 #include "window_stage_impl.h"
24 #include "cj_ability_object.h"
25 #endif
26 #include "cj_application_context.h"
27 
28 namespace OHOS {
29 
30 namespace AbilityRuntime {
31 class CJRuntime;
32 class CJAbilityObject;
33 struct InsightIntentExecutorInfo;
34 using AbilityHandler = AppExecFwk::AbilityHandler;
35 using AbilityInfo = AppExecFwk::AbilityInfo;
36 using OHOSApplication = AppExecFwk::OHOSApplication;
37 using Want = AppExecFwk::Want;
38 using AbilityStartSetting = AppExecFwk::AbilityStartSetting;
39 using Configuration = AppExecFwk::Configuration;
40 using InsightIntentExecuteResult = AppExecFwk::InsightIntentExecuteResult;
41 using InsightIntentExecuteParam = AppExecFwk::InsightIntentExecuteParam;
42 using InsightIntentExecutorAsyncCallback = AppExecFwk::InsightIntentExecutorAsyncCallback;
43 
44 class CJUIAbility : public UIAbility {
45 public:
46     /**
47      * @brief Create a JsUIAbility instance through the singleton pattern
48      * @param runtime The runtime of the ability
49      * @return Returns the JsUIability Instance point
50      */
51     static UIAbility *Create(const std::unique_ptr<Runtime> &runtime);
52 
53     explicit CJUIAbility(CJRuntime &cjRuntime);
54     ~CJUIAbility() override;
55 
56     /**
57      * @brief Init the UIability
58      * @param abilityInfo Indicate the Ability information
59      * @param ohosAppObject Indicates the main process
60      * @param handler the UIability EventHandler object
61      * @param token the remote token
62      */
63     void Init(std::shared_ptr<AppExecFwk::AbilityLocalRecord> record,
64         const std::shared_ptr<OHOSApplication> application,
65         std::shared_ptr<AbilityHandler> &handler, const sptr<IRemoteObject> &token) override;
66 
67     /**
68      * @brief OnStart,Start JsUIability
69      * @param want Indicates the {@link Want} structure containing startup information about the ability
70      * @param sessionInfo Indicates the sessionInfo
71      */
72     void OnStart(const Want &want, sptr<AAFwk::SessionInfo> sessionInfo = nullptr) override;
73 
74     /**
75      * @brief Called when this ability enters the <b>STATE_STOP</b> state.
76      * The ability in the <b>STATE_STOP</b> is being destroyed.
77      * You can override this function to implement your own processing logic.
78      */
79     void OnStop() override;
80 
81     /**
82      * @brief Called when this ability enters the <b>STATE_STOP</b> state.
83      * The ability in the <b>STATE_STOP</b> is being destroyed.
84      * You can override this function to implement your own processing logic.
85      * @param callbackInfo Indicates the lifecycle transaction callback information
86      * @param isAsyncCallback Indicates whether it is an asynchronous lifecycle callback
87      */
88     void OnStop(AppExecFwk::AbilityTransactionCallbackInfo<> *callbackInfo, bool &isAsyncCallback) override;
89 
90     /**
91      * @brief The callback of OnStop.
92      */
93     void OnStopCallback() override;
94 
95     /**
96      * @brief Prepare user data of local Ability.
97      * @param wantParams Indicates the user data to be saved.
98      * @return If the ability is willing to continue and data saved successfully, it returns 0;
99      * otherwise, it returns errcode.
100      */
101     int32_t OnContinue(WantParams &wantParams, bool &isAsyncOnContinue,
102         const AppExecFwk::AbilityInfo &abilityInfo) override;
103 
104     /**
105      * @brief Update configuration
106      * @param configuration Indicates the updated configuration information.
107      */
108     void OnConfigurationUpdated(const Configuration &configuration) override;
109 
110     /**
111      * @brief Update Contextconfiguration
112      */
113     void UpdateContextConfiguration() override;
114 
115     /**
116      * @brief Called when the system configuration is updated.
117      * @param level Indicates the memory trim level, which shows the current memory usage status.
118      */
119     void OnMemoryLevel(int level) override;
120 
121     /**
122      * @brief Called when the launch mode of an ability is set to singleInstance. This happens when you re-launch an
123      * ability that has been at the top of the ability stack.
124      * @param want Indicates the new Want containing information about the ability.
125      */
126     void OnNewWant(const Want &want) override;
127 
128     /**
129      * @brief Prepare user data of local Ability.
130      * @param reason the reason why framework invoke this function
131      * @param wantParams Indicates the user data to be saved.
132      * @return result code defined in abilityConstants
133      */
134     int32_t OnSaveState(int32_t reason, WantParams &wantParams) override;
135 
136     /**
137      * @brief Called when startAbilityForResult(ohos.aafwk.content.Want,int) is called to start an ability and the
138      * result is returned. This method is called only on Page abilities. You can start a new ability to perform some
139      * calculations and use setResult (int,ohos.aafwk.content.Want) to return the calculation result. Then the system
140      * calls back the current method to use the returned data to execute its own logic.
141      * @param requestCode Indicates the request code returned after the ability is started. You can define the request
142      * code to identify the results returned by abilities. The value ranges from 0 to 65535.
143      * @param resultCode Indicates the result code returned after the ability is started. You can define the result code
144      * to identify an error.
145      * @param want Indicates the data returned after the ability is started. You can define the data returned. The
146      * value can be null.
147      */
148     void OnAbilityResult(int requestCode, int resultCode, const Want &resultData) override;
149 
150     /**
151      * @brief request a remote object of callee from this ability.
152      * @return Returns the remote object of callee.
153      */
154     sptr<IRemoteObject> CallRequest() override;
155 
156     /**
157      * @brief dump ability info
158      * @param params dump params that indicate different dump targets
159      * @param info dump ability info
160      */
161     void Dump(const std::vector<std::string> &params, std::vector<std::string> &info) override;
162 
163     std::shared_ptr<CJAbilityObject> GetCJAbility();
164 
165     /**
166      * @brief Callback when the ability is shared.You can override this function to implement your own sharing logic.
167      * @param wantParams Indicates the user data to be saved.
168      * @return the result of OnShare
169      */
170     int32_t OnShare(WantParams &wantParams) override;
171 
172 #ifdef SUPPORT_GRAPHICS
173 #ifdef SUPPORT_SCREEN
174 public:
175     /**
176      * @brief Called after instantiating WindowScene.
177      * You can override this function to implement your own processing logic.
178      */
179     void OnSceneCreated() override;
180 
181     /**
182      * @brief Called after ability stoped.
183      * You can override this function to implement your own processing logic.
184      */
185     void OnSceneWillDestroy() override;
186 
187     /**
188      * @brief Called after ability stoped.
189      * You can override this function to implement your own processing logic.
190      */
191     void onSceneDestroyed() override;
192 
193     /**
194      * @brief Called after ability restored.
195      * You can override this function to implement your own processing logic.
196      */
197     void OnSceneRestored() override;
198 
199     /**
200      * @brief Called when this ability enters the <b>STATE_FOREGROUND</b> state.
201      * The ability in the <b>STATE_FOREGROUND</b> state is visible.
202      * You can override this function to implement your own processing logic.
203      */
204     void OnForeground(const Want &want) override;
205 
206     /**
207      * @brief Call "onForeground" js function barely.
208      *
209      * @param want Want
210      */
211     void CallOnForegroundFunc(const Want &want) override;
212 
213     /**
214      * @brief Request focus for current window, can be override.
215      *
216      * @param want Want
217      */
218     void RequestFocus(const Want &want) override;
219 
220     /**
221      * @brief Called when this ability enters the <b>STATE_BACKGROUND</b> state.
222      * The ability in the <b>STATE_BACKGROUND</b> state is invisible.
223      * You can override this function to implement your own processing logic.
224      */
225     void OnBackground() override;
226 
227     /**
228      * @brief Called after window stage focused or unfocused
229      * You can override this function to implement your own processing logic.
230      */
231     void OnAfterFocusedCommon(bool isFocused) override;
232 
233     /**
234      * Called when back press is dispatched.
235      * Return true if ability will be moved to background; return false if will be terminated
236      */
237     bool OnBackPress() override;
238 
239     /**
240      * @brief Called when ability prepare terminate.
241      * @return Return true if ability need to stop terminating; return false if ability need to terminate.
242      */
243     bool OnPrepareTerminate() override;
244 
245     /**
246      * @brief Get CJRuntime
247      * @return Returns the current CJRuntime
248      */
249     const CJRuntime &GetCJRuntime();
250 
251     /**
252      * @brief Execute insight intention when an ability is in foreground, schedule it to foreground repeatly.
253      *
254      * @param want Want.
255      * @param executeParam insight intention execute param.
256      * @param callback insight intention async callback.
257      */
258     void ExecuteInsightIntentRepeateForeground(const Want &want,
259         const std::shared_ptr<InsightIntentExecuteParam> &executeParam,
260         std::unique_ptr<InsightIntentExecutorAsyncCallback> callback) override;
261 
262     /**
263      * @brief Execute insight intention when an ability didn't started or in background, schedule it to foreground.
264      *
265      * @param want Want.
266      * @param executeParam insight intention execute param.
267      * @param callback insight intention async callback.
268      */
269     void ExecuteInsightIntentMoveToForeground(const Want &want,
270         const std::shared_ptr<InsightIntentExecuteParam> &executeParam,
271         std::unique_ptr<InsightIntentExecutorAsyncCallback> callback) override;
272 
273 protected:
274     void DoOnForeground(const Want &want) override;
275     void ContinuationRestore(const Want &want) override;
276 
277 private:
278     bool IsRestorePageStack(const Want &want);
279     void RestorePageStack(const Want &want);
280     void GetPageStackFromWant(const Want &want, std::string &pageStack);
281     void AbilityContinuationOrRecover(const Want &want);
282     inline bool GetInsightIntentExecutorInfo(const Want &want,
283         const std::shared_ptr<InsightIntentExecuteParam> &executeParam,
284         InsightIntentExecutorInfo& executeInfo);
285 
286     sptr<Rosen::CJWindowStageImpl> cjWindowStage_;
287     int32_t windowMode_ = 0;
288 #endif
289 #endif
290 private:
291     std::shared_ptr<AppExecFwk::ACJDelegatorAbilityProperty> CreateADelegatorAbilityProperty();
292     sptr<IRemoteObject> SetNewRuleFlagToCallee(int64_t remoteJsObj);
293     void SetAbilityContext(const std::shared_ptr<AbilityInfo> &abilityInfo);
294     void InitSceneDoOnForeground(std::shared_ptr<Rosen::WindowScene> scene, const Want &want);
295     void AddLifecycleEventBeforeCall(FreezeUtil::TimeoutState state, const std::string &methodName) const;
296     void AddLifecycleEventAfterCall(FreezeUtil::TimeoutState state, const std::string &methodName) const;
297     bool CheckSatisfyTargetAPIVersion(int32_t targetAPIVersion);
298     bool BackPressDefaultValue();
299     void CallAppCtxFunc(CjAppCtxFuncType type, int64_t id);
300     void CallAppCtxWindowFunc(CjAppCtxFuncType type, int64_t id, sptr<Rosen::CJWindowStageImpl> window);
301 
302     void* cjAppHandle_ = nullptr;
303 
304     CJRuntime &cjRuntime_;
305     std::shared_ptr<CJAbilityObject> cjAbilityObj_;
306     sptr<IRemoteObject> remoteCallee_;
307 };
308 } // namespace AbilityRuntime
309 } // namespace OHOS
310 #endif // OHOS_ABILITY_RUNTIME_CJ_UI_ABILITY_H
311