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