1 /* 2 * Copyright (c) 2021 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_ABILITY_IMPL_H 17 #define OHOS_ABILITY_RUNTIME_ABILITY_IMPL_H 18 19 #include "ability.h" 20 #include "iability_lifecycle_callback.h" 21 #include "context.h" 22 #include "application_impl.h" 23 #include "ability_local_record.h" 24 #include "ability_handler.h" 25 #include "ability_manager_client.h" 26 #include "ability_manager_interface.h" 27 #include "dummy_component_container.h" 28 #ifdef SUPPORT_GRAPHICS 29 #include "foundation/multimodalinput/input/interfaces/native/innerkits/event/include/i_input_event_consumer.h" 30 #endif 31 namespace OHOS { 32 namespace AppExecFwk { 33 class Ability; 34 class AbilityHandler; 35 class ApplicationImpl; 36 class AbilityLocalRecord; 37 class AbilityLifecycleCallbacks; 38 class OHOSApplication; 39 class AbilityImpl : public std::enable_shared_from_this<AbilityImpl> { 40 public: 41 AbilityImpl() = default; 42 virtual ~AbilityImpl() = default; 43 virtual void Init(std::shared_ptr<OHOSApplication> &application, const std::shared_ptr<AbilityLocalRecord> &record, 44 std::shared_ptr<Ability> &ability, std::shared_ptr<AbilityHandler> &handler, const sptr<IRemoteObject> &token, 45 std::shared_ptr<ContextDeal> &contextDeal); 46 47 /** 48 * @brief Connect the ability. and Calling information back to Ability. 49 * 50 * @param want The Want object to connect to. 51 * 52 */ 53 sptr<IRemoteObject> ConnectAbility(const Want &want); 54 55 /** 56 * @brief Disconnects the connected object. 57 * 58 * @param want The Want object to disconnect to. 59 */ 60 void DisconnectAbility(const Want &want); 61 62 /** 63 * @brief Command the ability. and Calling information back to Ability. 64 * 65 * @param want The Want object to command to. 66 * 67 * * @param restart Indicates the startup mode. The value true indicates that Service is restarted after being 68 * destroyed, and the value false indicates a normal startup. 69 * 70 * @param startId Indicates the number of times the Service ability has been started. The startId is incremented 71 * by 1 every time the ability is started. For example, if the ability has been started for six times, the value 72 * of startId is 6. 73 */ 74 void CommandAbility(const Want &want, bool restart, int startId); 75 76 /** 77 * @brief Gets the current Ability status. 78 * 79 */ 80 int GetCurrentState(); 81 82 /** 83 * @brief Save data and states of an ability when it is restored by the system. and Calling information back to 84 * Ability. This method should be implemented by a Page ability. 85 * 86 */ 87 void DispatchSaveAbilityState(); 88 89 /** 90 * @brief Restores data and states of an ability when it is restored by the system. and Calling information back 91 * to Ability. This method should be implemented by a Page ability. 92 * @param instate The Want object to connect to. 93 * 94 */ 95 void DispatchRestoreAbilityState(const PacMap &inState); 96 97 // Page Service Ability has different AbilityTransaction 98 virtual void HandleAbilityTransaction(const Want &want, const AAFwk::LifeCycleStateInfo &targetState); 99 100 /** 101 * @brief The life cycle callback. 102 * @param state The life cycle state to switch to. 103 */ 104 virtual void AbilityTransactionCallback(const AbilityLifeCycleState &state); 105 106 /** 107 * @brief Send the result code and data to be returned by this Page ability to the caller. 108 * When a Page ability is destroyed, the caller overrides the AbilitySlice#onAbilityResult(int, int, Want) 109 * method to receive the result set in the current method. This method can be called only after the ability has 110 * been initialized. 111 * 112 * @param requestCode Indicates the request code. 113 * @param resultCode Indicates the result code returned after the ability is destroyed. You can define the 114 * result code to identify an error. 115 * @param resultData Indicates the data returned after the ability is destroyed. You can define the data 116 * returned. This parameter can be null. 117 */ 118 void SendResult(int requestCode, int resultCode, const Want &resultData); 119 120 /** 121 * @brief Called when the launch mode of an ability is set to singleInstance. This happens when you re-launch 122 * an ability that has been at the top of the ability stack. 123 * 124 * @param want Indicates the new Want containing information about the ability. 125 */ 126 void NewWant(const Want &want); 127 128 /** 129 * @brief Obtains the MIME types of files supported. 130 * 131 * @param uri Indicates the path of the files to obtain. 132 * @param mimeTypeFilter Indicates the MIME types of the files to obtain. This parameter cannot be null. 133 * 134 * @return Returns the matched MIME types. If there is no match, null is returned. 135 */ 136 virtual std::vector<std::string> GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter); 137 138 /** 139 * @brief Opens a file in a specified remote path. 140 * 141 * @param uri Indicates the path of the file to open. 142 * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access 143 * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file, 144 * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing 145 * data, or "rwt" for read and write access that truncates any existing file. 146 * 147 * @return Returns the file descriptor. 148 */ 149 virtual int OpenFile(const Uri &uri, const std::string &mode); 150 151 /** 152 * @brief This is like openFile, open a file that need to be able to return sub-sections of files,often assets 153 * inside of their .hap. 154 * 155 * @param uri Indicates the path of the file to open. 156 * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access 157 * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file, 158 * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing 159 * data, or "rwt" for read and write access that truncates any existing file. 160 * 161 * @return Returns the RawFileDescriptor object containing file descriptor. 162 */ 163 virtual int OpenRawFile(const Uri &uri, const std::string &mode); 164 165 /** 166 * @brief Inserts a single data record into the database. 167 * 168 * @param uri Indicates the path of the data to operate. 169 * @param value Indicates the data record to insert. If this parameter is null, a blank row will be inserted. 170 * 171 * @return Returns the index of the inserted data record. 172 */ 173 virtual int Insert(const Uri &uri, const NativeRdb::ValuesBucket &value); 174 175 virtual std::shared_ptr<AppExecFwk::PacMap> Call( 176 const Uri &uri, const std::string &method, const std::string &arg, const AppExecFwk::PacMap &pacMap); 177 178 /** 179 * @brief Updates data records in the database. 180 * 181 * @param uri Indicates the path of data to update. 182 * @param value Indicates the data to update. This parameter can be null. 183 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is 184 * null. 185 * 186 * @return Returns the number of data records updated. 187 */ 188 virtual int Update(const Uri &uri, const NativeRdb::ValuesBucket &value, 189 const NativeRdb::DataAbilityPredicates &predicates); 190 191 /** 192 * @brief Deletes one or more data records from the database. 193 * 194 * @param uri Indicates the path of the data to operate. 195 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is 196 * null. 197 * 198 * @return Returns the number of data records deleted. 199 */ 200 virtual int Delete(const Uri &uri, const NativeRdb::DataAbilityPredicates &predicates); 201 202 /** 203 * @brief Deletes one or more data records from the database. 204 * 205 * @param uri Indicates the path of data to query. 206 * @param columns Indicates the columns to query. If this parameter is null, all columns are queried. 207 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is 208 * null. 209 * 210 * @return Returns the query result. 211 */ 212 virtual std::shared_ptr<NativeRdb::AbsSharedResultSet> Query( 213 const Uri &uri, std::vector<std::string> &columns, const NativeRdb::DataAbilityPredicates &predicates); 214 215 /** 216 * @brief Obtains the MIME type matching the data specified by the URI of the Data ability. This method should 217 * be implemented by a Data ability. Data abilities supports general data types, including text, HTML, and JPEG. 218 * 219 * @param uri Indicates the URI of the data. 220 * 221 * @return Returns the MIME type that matches the data specified by uri. 222 */ 223 virtual std::string GetType(const Uri &uri); 224 225 /** 226 * @brief Reloads data in the database. 227 * 228 * @param uri Indicates the position where the data is to reload. This parameter is mandatory. 229 * @param extras Indicates the PacMap object containing the additional parameters to be passed in this call. 230 * This parameter can be null. If a custom Sequenceable object is put in the PacMap object and will be 231 * transferred across processes, you must call BasePacMap.setClassLoader(ClassLoader) to set a class loader for 232 * the custom object. 233 * 234 * @return Returns true if the data is successfully reloaded; returns false otherwise. 235 */ 236 virtual bool Reload(const Uri &uri, const PacMap &extras); 237 238 /** 239 * @brief Inserts multiple data records into the database. 240 * 241 * @param uri Indicates the path of the data to operate. 242 * @param values Indicates the data records to insert. 243 * 244 * @return Returns the number of data records inserted. 245 */ 246 virtual int BatchInsert(const Uri &uri, const std::vector<NativeRdb::ValuesBucket> &values); 247 248 /** 249 * @brief Set deviceId/bundleName/abilityName of the calling ability 250 * 251 * @param deviceId deviceId of the calling ability 252 * 253 * @param deviceId bundleName of the calling ability 254 * 255 * @param deviceId abilityName of the calling ability 256 */ 257 void SetCallingContext(const std::string &deviceId, const std::string &bundleName, 258 const std::string &abilityName, const std::string &moduleName); 259 260 /** 261 * @brief Converts the given uri that refer to the Data ability into a normalized URI. A normalized URI can be used 262 * across devices, persisted, backed up, and restored. It can refer to the same item in the Data ability even if the 263 * context has changed. If you implement URI normalization for a Data ability, you must also implement 264 * denormalizeUri(ohos.utils.net.Uri) to enable URI denormalization. After this feature is enabled, URIs passed to 265 * any method that is called on the Data ability must require normalization verification and denormalization. The 266 * default implementation of this method returns null, indicating that this Data ability does not support URI 267 * normalization. 268 * 269 * @param uri Indicates the Uri object to normalize. 270 * 271 * @return Returns the normalized Uri object if the Data ability supports URI normalization; returns null otherwise. 272 */ 273 virtual Uri NormalizeUri(const Uri &uri); 274 275 /** 276 * @brief Converts the given normalized uri generated by normalizeUri(ohos.utils.net.Uri) into a denormalized one. 277 * The default implementation of this method returns the original URI passed to it. 278 * 279 * @param uri uri Indicates the Uri object to denormalize. 280 * 281 * @return Returns the denormalized Uri object if the denormalization is successful; returns the original Uri passed 282 * to this method if there is nothing to do; returns null if the data identified by the original Uri cannot be found 283 * in the current environment. 284 */ 285 virtual Uri DenormalizeUri(const Uri &uri); 286 287 /** 288 * @brief ScheduleUpdateConfiguration, scheduling update configuration. 289 */ 290 void ScheduleUpdateConfiguration(const Configuration &config); 291 292 /** 293 * @brief Create a PostEvent timeout task. The default delay is 5000ms 294 * 295 * @return Return a smart pointer to a timeout object 296 */ 297 std::shared_ptr<AbilityPostEventTimeout> CreatePostEventTimeouter(std::string taskstr); 298 299 virtual std::vector<std::shared_ptr<DataAbilityResult>> ExecuteBatch( 300 const std::vector<std::shared_ptr<DataAbilityOperation>> &operations); 301 302 /** 303 * @brief continue ability to target device. 304 * 305 * @param deviceId target deviceId 306 * @param versionCode Target bundle version. 307 * @return 308 */ 309 void ContinueAbility(const std::string& deviceId, uint32_t versionCode); 310 311 /** 312 * @brief Notify continuation result to ability. 313 * 314 * @param result Continuaton result. 315 * 316 * @return 317 */ 318 virtual void NotifyContinuationResult(int32_t result); 319 320 /** 321 * @brief Notify current memory level to ability. 322 * 323 * @param level Current memory level. 324 * 325 * @return 326 */ 327 virtual void NotifyMemoryLevel(int32_t level); 328 329 bool IsStageBasedModel() const; 330 331 #ifdef SUPPORT_GRAPHICS 332 public: 333 /** 334 * @brief Execution the KeyDown callback of the ability 335 * @param keyEvent Indicates the key-down event. 336 * 337 * @return Returns true if this event is handled and will not be passed further; returns false if this event is 338 * not handled and should be passed to other handlers. 339 * 340 */ 341 virtual void DoKeyDown(const std::shared_ptr<MMI::KeyEvent>& keyEvent); 342 343 /** 344 * @brief Execution the KeyUp callback of the ability 345 * @param keyEvent Indicates the key-up event. 346 * 347 * @return Returns true if this event is handled and will not be passed further; returns false if this event is 348 * not handled and should be passed to other handlers. 349 * 350 */ 351 virtual void DoKeyUp(const std::shared_ptr<MMI::KeyEvent>& keyEvent); 352 353 /** 354 * @brief Called when a touch event is dispatched to this ability. The default implementation of this callback 355 * does nothing and returns false. 356 * @param touchEvent Indicates information about the touch event. 357 * 358 * @return Returns true if the event is handled; returns false otherwise. 359 * 360 */ 361 virtual void DoPointerEvent(std::shared_ptr<MMI::PointerEvent>& pointerEvent); 362 363 void AfterUnFocused(); 364 void AfterFocused(); 365 366 protected: 367 /** 368 * @brief Toggles the lifecycle status of Ability to AAFwk::ABILITY_STATE_INACTIVE. And notifies the application 369 * that it belongs to of the lifecycle status. 370 * 371 * @param want The Want object to switch the life cycle. 372 */ 373 void Foreground(const Want &want); 374 375 /** 376 * @brief Toggles the lifecycle status of Ability to AAFwk::ABILITY_STATE_BACKGROUND. And notifies the 377 * application that it belongs to of the lifecycle status. 378 * 379 */ 380 void Background(); 381 #endif 382 383 protected: 384 /** 385 * @brief Toggles the lifecycle status of Ability to AAFwk::ABILITY_STATE_INACTIVE. And notifies the application 386 * that it belongs to of the lifecycle status. 387 * 388 * @param want The Want object to switch the life cycle. 389 */ 390 void Start(const Want &want); 391 392 /** 393 * @brief Toggles the lifecycle status of Ability to AAFwk::ABILITY_STATE_INITIAL. And notifies the application 394 * that it belongs to of the lifecycle status. 395 * 396 */ 397 void Stop(); 398 399 /** 400 * @brief Toggles the lifecycle status of Ability to AAFwk::ABILITY_STATE_INITIAL. And notifies the application 401 * that it belongs to of the lifecycle status. 402 * @param isAsyncCallback Indicates whether it is an asynchronous lifecycle callback 403 */ 404 void Stop(bool &isAsyncCallback); 405 406 /** 407 * @brief Toggles the lifecycle status of Ability to AAFwk::ABILITY_STATE_INITIAL. And notifies the application 408 * that it belongs to of the lifecycle status. 409 */ 410 void StopCallback(); 411 412 /** 413 * @brief Toggles the lifecycle status of Ability to AAFwk::ABILITY_STATE_ACTIVE. And notifies the application 414 * that it belongs to of the lifecycle status. 415 * 416 */ 417 void Active(); 418 419 /** 420 * @brief Toggles the lifecycle status of Ability to AAFwk::ABILITY_STATE_INACTIVE. And notifies the application 421 * that it belongs to of the lifecycle status. 422 * 423 */ 424 void Inactive(); 425 426 /** 427 * @brief SerUriString 428 */ 429 void SerUriString(const std::string &uri); 430 431 /** 432 * @brief Set the LifeCycleStateInfo to the deal. 433 * 434 * @param info the info to set. 435 */ 436 void SetLifeCycleStateInfo(const AAFwk::LifeCycleStateInfo &info); 437 438 /** 439 * @brief Check if it needs to restore the data to the ability. 440 * 441 * @return Return true if success, otherwise return false. 442 */ 443 bool CheckAndRestore(); 444 445 /** 446 * @brief Check if it needs to save the data to the ability. 447 * 448 * @return Return true if success, otherwise return false. 449 */ 450 bool CheckAndSave(); 451 452 PacMap &GetRestoreData(); 453 454 bool isStageBasedModel_ = false; 455 int lifecycleState_ = AAFwk::ABILITY_STATE_INITIAL; 456 sptr<IRemoteObject> token_; 457 std::shared_ptr<Ability> ability_; 458 std::shared_ptr<AbilityHandler> handler_; 459 bool notifyForegroundByWindow_ = false; 460 bool notifyForegroundByAbility_ = false; 461 std::mutex notifyForegroundLock_; 462 463 private: 464 typedef enum { 465 START, 466 INACTIVE, 467 ACTIVE, 468 BACKGROUND, 469 FOREGROUND, 470 STOP, 471 } Action; 472 473 std::shared_ptr<AbilityLifecycleCallbacks> abilityLifecycleCallbacks_; 474 std::shared_ptr<ApplicationImpl> applicationImpl_; 475 std::shared_ptr<ContextDeal> contextDeal_; 476 bool hasSaveData_ = false; 477 bool needSaveDate_ = false; 478 PacMap restoreData_; 479 480 private: 481 void AfterFocusedCommon(bool isFocused); 482 483 #ifdef SUPPORT_GRAPHICS 484 private: 485 class WindowLifeCycleImpl : public Rosen::IWindowLifeCycle { 486 public: WindowLifeCycleImpl(const sptr<IRemoteObject> & token,const std::shared_ptr<AbilityImpl> & owner)487 WindowLifeCycleImpl(const sptr<IRemoteObject>& token, const std::shared_ptr<AbilityImpl>& owner) 488 : token_(token), owner_(owner) {} ~WindowLifeCycleImpl()489 virtual ~WindowLifeCycleImpl() {} 490 void AfterForeground() override; 491 void AfterBackground() override; 492 void AfterFocused() override; 493 void AfterUnfocused() override; 494 void ForegroundFailed(int32_t type) override; 495 private: 496 sptr<IRemoteObject> token_ = nullptr; 497 std::weak_ptr<AbilityImpl> owner_; 498 }; 499 500 class InputEventConsumerImpl : public MMI::IInputEventConsumer { 501 public: InputEventConsumerImpl(const std::shared_ptr<AbilityImpl> & abilityImpl)502 explicit InputEventConsumerImpl(const std::shared_ptr<AbilityImpl>& abilityImpl) : abilityImpl_(abilityImpl) {} 503 ~InputEventConsumerImpl() = default; 504 void OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const override; 505 void OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const override; OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent)506 void OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const override {} 507 private: 508 std::shared_ptr<AbilityImpl> abilityImpl_; 509 }; 510 #endif 511 }; 512 } // namespace AppExecFwk 513 } // namespace OHOS 514 #endif // OHOS_ABILITY_RUNTIME_ABILITY_IMPL_H 515