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