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 FOUNDATION_ABILITYRUNTIME_OHOS_RINGTONE_COLUMN_DATASHARE_EXT_ABILITY_H
17 #define FOUNDATION_ABILITYRUNTIME_OHOS_RINGTONE_COLUMN_DATASHARE_EXT_ABILITY_H
18 
19 #include "datashare_ext_ability.h"
20 
21 namespace OHOS {
22 namespace AbilityRuntime {
23 using namespace DataShare;
24 /**
25  * @brief Basic datashare extension ability components.
26  */
27 #define EXPORT __attribute__ ((visibility ("default")))
28 class RingtoneDataShareExtension : public DataShareExtAbility {
29 public:
30     EXPORT RingtoneDataShareExtension(Runtime &runtime);
31     EXPORT virtual ~RingtoneDataShareExtension() override;
32 
33     /**
34      * @brief Create RingtoneDataShareExtension.
35      *
36      * @param runtime The runtime.
37      * @return The RingtoneDataShareExtension instance.
38      */
39     EXPORT static RingtoneDataShareExtension *Create(const std::unique_ptr<Runtime> &runtime);
40 
41     /**
42      * @brief Init the extension.
43      *
44      * @param record the extension record.
45      * @param application the application info.
46      * @param handler the extension handler.
47      * @param token the remote token.
48      */
49     EXPORT void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record,
50         const std::shared_ptr<AppExecFwk::OHOSApplication> &application,
51         std::shared_ptr<AppExecFwk::AbilityHandler> &handler,
52         const sptr<IRemoteObject> &token) override;
53 
54     /**
55      * @brief Called when this datashare extension ability is started. You must override this function if you want to
56      *        perform some initialization operations during extension startup.
57      *
58      * This function can be called only once in the entire lifecycle of an extension.
59      * @param Want Indicates the {@link Want} structure containing startup information about the extension.
60      */
61     EXPORT void OnStart(const AAFwk::Want &want) override;
62 
63     EXPORT void OnStop() override;
64 
65     /**
66      * @brief Called when this datashare extension ability is connected for the first time.
67      *
68      * You can override this function to implement your own processing logic.
69      *
70      * @param want Indicates the {@link Want} structure containing connection information about the datashare
71      * extension.
72      * @return Returns a pointer to the <b>sid</b> of the connected datashare extension ability.
73      */
74     EXPORT sptr<IRemoteObject> OnConnect(const AAFwk::Want &want) override;
75     /**
76      * @brief Inserts a single data record into the database.
77      *
78      * @param uri Indicates the path of the data to operate.
79      * @param value  Indicates the data record to insert. If this parameter is null, a blank row will be inserted.
80      *
81      * @return Returns the index of the inserted data record.
82      */
83     EXPORT int Insert(const Uri &uri, const DataShareValuesBucket &value) override;
84 
85     /**
86      * @brief Deletes one or more data records from the database.
87      *
88      * @param uri Indicates the path of the data to operate.
89      * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
90      *
91      * @return Returns the number of data records deleted.
92      */
93     EXPORT int Delete(const Uri &uri, const DataSharePredicates &predicates) override;
94 
95     /**
96      * @brief Updates data records in the database.
97      *
98      * @param uri Indicates the path of data to update.
99      * @param value Indicates the data to update. This parameter can be null.
100      * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
101      *
102      * @return Returns the number of data records updated.
103      */
104     EXPORT int Update(const Uri &uri, const DataSharePredicates &predicates,
105             const DataShareValuesBucket &value) override;
106 
107     /**
108      * @brief query one or more data records from the database.
109      *
110      * @param uri Indicates the path of data to query.
111      * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
112      * @param columns Indicates the columns to query. If this parameter is null, all columns are queried.
113      * @param businessError Indicates errorcode and message.
114 
115      * @return Returns the query result.
116      */
117     EXPORT std::shared_ptr<DataShareResultSet> Query(const Uri &uri, const DataSharePredicates &predicates,
118             std::vector<std::string> &columns, DatashareBusinessError &businessError) override;
119 
120     /**
121      * @brief Opens a file in a specified remote path.
122      *
123      * @param uri Indicates the path of the file to open.
124      * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
125      * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
126      * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing data,
127      *  or "rwt" for read and write access that truncates any existing file.
128      *
129      * @return Returns the file descriptor.
130      */
131     EXPORT int OpenFile(const Uri &uri, const std::string &mode) override;
132 private:
133     EXPORT static void DumpDataShareValueBucket(const std::vector<std::string> &tabFields,
134         const DataShareValuesBucket &value);
135     void RingtoneScanner();
136     Runtime &runtime_;
137 };
138 } // namespace AbilityRuntime
139 } // namespace OHOS
140 #endif // FOUNDATION_ABILITYRUNTIME_OHOS_RINGTONE_COLUMN_DATASHARE_EXT_ABILITY_H
141