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