• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define MLOG_TAG "scan_demo"
16 
17 #include <iostream>
18 #include <unistd.h>
19 
20 #include "accesstoken_kit.h"
21 #include "datashare_helper.h"
22 #include "get_self_permissions.h"
23 #include "iservice_registry.h"
24 #include "nativetoken_kit.h"
25 #include "ringtone_asset.h"
26 #include "ringtone_db_const.h"
27 #include "ringtone_errno.h"
28 #include "ringtone_fetch_result.h"
29 #include "ringtone_log.h"
30 #include "token_setproc.h"
31 
32 using namespace std;
33 using namespace OHOS;
34 using namespace OHOS::Media;
35 using namespace OHOS::DataShare;
36 
37 namespace OHOS {
38 namespace Media {
39 namespace {
40 constexpr int STORAGE_MANAGER_MANAGER_ID = 5003;
41 static const string RINGTONE_LIBRARY_PATH =
42     "/storage/media/local/data/com.ohos.ringtonelibrary.ringtonelibrarydata";
43 std::shared_ptr<DataShare::DataShareHelper> g_dataShareHelper = nullptr;
44 } // namespace
45 } // namespace Media
46 } // namespace OHOS
47 
CreateDataShareHelper(int32_t systemAbilityId)48 static shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper(int32_t systemAbilityId)
49 {
50     RINGTONE_INFO_LOG("CreateDataShareHelper::CreateFileExtHelper ");
51     auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
52     if (saManager == nullptr) {
53         RINGTONE_INFO_LOG("CreateFileExtHelper Get system ability mgr failed.");
54         return nullptr;
55     }
56     auto remoteObj = saManager->GetSystemAbility(systemAbilityId);
57     if (remoteObj == nullptr) {
58         RINGTONE_INFO_LOG("CreateDataShareHelper GetSystemAbility Service Failed.");
59         return nullptr;
60     }
61     return DataShare::DataShareHelper::Creator(remoteObj, RINGTONE_URI);
62 }
63 
RingtoneIndex()64 static void RingtoneIndex()
65 {
66     Uri ringtoneUri(RINGTONE_PATH_URI);
67     int32_t index = 0;
68     const int32_t count = 10;
69     const int64_t ringtoneSize = 1022;
70     const int type = 2;
71     const int64_t addedTime = 1559276453;
72     const int64_t modifiedTime = 1559276455;
73     const int64_t takenTime = 1559276457;
74     const int durationTime = 112;
75     while (index++ < count) {
76         DataShareValuesBucket valuesBucket;
77         valuesBucket.Put(RINGTONE_COLUMN_DATA, static_cast<string>(RINGTONE_LIBRARY_PATH + "/" +
78             "test_insert_ringtone_library" + to_string(index) + ".ogg"));
79         valuesBucket.Put(RINGTONE_COLUMN_SIZE, static_cast<int64_t>(ringtoneSize));
80         valuesBucket.Put(RINGTONE_COLUMN_DISPLAY_NAME, static_cast<string>("rainning") + ".ogg");
81         valuesBucket.Put(RINGTONE_COLUMN_TITLE, static_cast<string>("rainning"));
82         valuesBucket.Put(RINGTONE_COLUMN_MEDIA_TYPE, static_cast<int>(type));
83         valuesBucket.Put(RINGTONE_COLUMN_TONE_TYPE, static_cast<int>(type));
84         valuesBucket.Put(RINGTONE_COLUMN_MIME_TYPE, static_cast<string>("ogg"));
85         valuesBucket.Put(RINGTONE_COLUMN_SOURCE_TYPE, static_cast<int>(1));
86         valuesBucket.Put(RINGTONE_COLUMN_DATE_ADDED, static_cast<int64_t>(addedTime));
87         valuesBucket.Put(RINGTONE_COLUMN_DATE_MODIFIED, static_cast<int64_t>(modifiedTime));
88         valuesBucket.Put(RINGTONE_COLUMN_DATE_TAKEN, static_cast<int64_t>(takenTime));
89         valuesBucket.Put(RINGTONE_COLUMN_DURATION, static_cast<int>(durationTime));
90         valuesBucket.Put(RINGTONE_COLUMN_SHOT_TONE_TYPE, static_cast<int>(1));
91         valuesBucket.Put(RINGTONE_COLUMN_SHOT_TONE_SOURCE_TYPE, static_cast<int>(type));
92         valuesBucket.Put(RINGTONE_COLUMN_NOTIFICATION_TONE_TYPE, static_cast<int>(1));
93         valuesBucket.Put(RINGTONE_COLUMN_NOTIFICATION_TONE_SOURCE_TYPE, static_cast<int>(type));
94         valuesBucket.Put(RINGTONE_COLUMN_RING_TONE_TYPE, static_cast<int>(1));
95         valuesBucket.Put(RINGTONE_COLUMN_RING_TONE_SOURCE_TYPE, static_cast<int>(type));
96         valuesBucket.Put(RINGTONE_COLUMN_ALARM_TONE_TYPE, static_cast<int>(1));
97         valuesBucket.Put(RINGTONE_COLUMN_ALARM_TONE_SOURCE_TYPE, static_cast<int>(type));
98 
99         g_dataShareHelper->Insert(ringtoneUri, valuesBucket);
100     }
101 }
102 
RingtoneDeleteAndUpdate(int32_t toneId)103 static int RingtoneDeleteAndUpdate(int32_t toneId)
104 {
105     Uri ringtoneUri(RINGTONE_PATH_URI);
106     // Ringtone library Delete api demo
107     int32_t deleteId = toneId;
108     DataShare::DataSharePredicates deletePredicates;
109     deletePredicates.SetWhereClause(RINGTONE_COLUMN_TONE_ID + " < ? ");
110     deletePredicates.SetWhereArgs({ to_string(deleteId) });
111 
112     // Ringtone library Update api demo
113     DataSharePredicates updatePredicates;
114 
115     vector<string> updateIds;
116     updateIds.push_back(to_string(toneId - 1));
117     deletePredicates.In(RINGTONE_COLUMN_TONE_ID, updateIds);
118 
119     DataShareValuesBucket updateValuesBucket;
120     updateValuesBucket.Put(RINGTONE_COLUMN_SHOT_TONE_TYPE, 0);
121     updateValuesBucket.Put(RINGTONE_COLUMN_SHOT_TONE_SOURCE_TYPE, 0);
122 
123     int32_t changedRows = g_dataShareHelper->Update(ringtoneUri, deletePredicates, updateValuesBucket);
124     if (changedRows < 0) {
125         cout << "Update error" << endl;
126         return E_ERR;
127     }
128     cout << "Update " << changedRows << " rows from ringtone library." << endl;
129 
130     changedRows = g_dataShareHelper->Delete(ringtoneUri, deletePredicates);
131     cout << "delete " << changedRows << " rows from ringtone library." << endl;
132     return E_SUCCESS;
133 }
134 
RingtoneQuery()135 static int32_t RingtoneQuery()
136 {
137     // Ringtone library Query api demo
138     Uri ringtoneUri(RINGTONE_PATH_URI);
139     int errCode = 0;
140     DatashareBusinessError businessError;
141     DataSharePredicates queryPredicates;
142     queryPredicates.EqualTo(RINGTONE_COLUMN_SHOT_TONE_TYPE, to_string(1));
143     vector<string> columns = {};
144     auto resultSet = g_dataShareHelper->Query(ringtoneUri, queryPredicates, columns, &businessError);
145     errCode = businessError.GetCode();
146     cout << "Query errCode=" << errCode << endl;
147 
148     auto results = make_unique<RingtoneFetchResult<RingtoneAsset>>(move(resultSet));
149     cout << "query count = " << to_string(results->GetCount()) << endl;
150     unique_ptr<RingtoneAsset> ringtoneAsset = results->GetFirstObject();
151     do {
152         cout << "GetId()                            = " << to_string(ringtoneAsset->GetId()) << endl;
153         cout << "GetSize()                          = " << to_string(ringtoneAsset->GetSize()) << endl;
154         cout << "GetPath()                          = " << ringtoneAsset->GetPath() << endl;
155         cout << "GetDisplayName()                   = " << ringtoneAsset->GetDisplayName() << endl;
156         cout << "GetTitle()                         = " << ringtoneAsset->GetTitle() << endl;
157         cout << "GetMimeType()                      = " << ringtoneAsset->GetMimeType() << endl;
158         cout << "GetSourceType()                    = " << to_string(ringtoneAsset->GetSourceType()) << endl;
159         cout << "GetDateAdded()                     = " << to_string(ringtoneAsset->GetDateAdded()) << endl;
160         cout << "GetDateModified()                  = " << to_string(ringtoneAsset->GetDateModified()) << endl;
161         cout << "GetDateTaken()                     = " << to_string(ringtoneAsset->GetDateTaken()) << endl;
162         cout << "GetDuration()                      = " << to_string(ringtoneAsset->GetDuration()) << endl;
163         cout << "GetShottoneType()                  = " << to_string(ringtoneAsset->GetShottoneType()) << endl;
164         cout << "GetShottoneSourceType()            = " << to_string(ringtoneAsset->GetShottoneSourceType()) << endl;
165         cout << "GetNotificationtoneType()          = " << to_string(ringtoneAsset->GetNotificationtoneType()) << endl;
166         cout << "GetNotificationtoneSourceType()    = " << to_string(ringtoneAsset->GetNotificationtoneSourceType())
167             << endl;
168         cout << "GetRingtoneType()                  = " << to_string(ringtoneAsset->GetRingtoneType()) << endl;
169         cout << "GetRingtoneSourceType()            = " << to_string(ringtoneAsset->GetRingtoneSourceType()) << endl;
170         cout << "GetAlarmtoneType()                 = " << to_string(ringtoneAsset->GetAlarmtoneType()) << endl;
171         cout << "GetAlarmtoneSourceType()           = " << to_string(ringtoneAsset->GetAlarmtoneSourceType()) << endl;
172         ringtoneAsset = results->GetNextObject();
173     } while (ringtoneAsset != nullptr);
174     ringtoneAsset = results->GetLastObject();
175     return ringtoneAsset->GetId();
176 }
177 
178 /*
179  * Feature: RingtoneScanner
180  * Function: Strat scanner
181  * SubFunction: NA
182  * FunctionPoints: NA
183  * EnvConditions: NA
184  */
main()185 int32_t main()
186 {
187     vector<string> perms;
188     perms.push_back("ohos.permission.WRITE_RINGTONE");
189 
190     uint64_t tokenId = 0;
191     RingtonePermissionUtilsUnitTest::SetAccessTokenPermission("RingtoneScan", perms, tokenId);
192     if (tokenId == 0) {
193         RINGTONE_ERR_LOG("Set Access Token Permisson Failed.");
194         return -1;
195     }
196 
197     g_dataShareHelper = CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID);
198     if (g_dataShareHelper == nullptr) {
199         RINGTONE_ERR_LOG("g_dataShareHelper fail");
200         return -1;
201     }
202 
203     RingtoneIndex();
204     int32_t toneId = RingtoneQuery();
205     cout << "query last tone id = " << toneId << endl;
206 
207     string uriStr = RINGTONE_PATH_URI + "/" + to_string(toneId);
208     Uri ofUri(uriStr);
209     auto fd = g_dataShareHelper->OpenFile(ofUri, "rw");
210     cout << "OpenFile fd = " << fd << endl;
211 
212     if (fd >= 0) {
213         string dataBuffer = "hello world";
214         ssize_t written = write(fd, dataBuffer.c_str(), dataBuffer.size());
215         if (written < 0) {
216             cout << "Failed to copy data buffer, return " << static_cast<int>(written) << endl;
217             return -1;
218         }
219     } else {
220         cout << "OpenFile error" << endl;
221         return -fd;
222     }
223     close(fd);
224 
225     int ret = RingtoneDeleteAndUpdate(toneId);
226     if (ret != E_SUCCESS) {
227         return -1;
228     }
229     system("rm -rf /storage/media/100/local/data/com.ohos.ringtonelibrary.ringtonelibrarydata/*");
230     return 0;
231 }
232