• 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         valuesBucket.Put(RINGTONE_COLUMN_SCANNER_FLAG, static_cast<int>(0));
99 
100         g_dataShareHelper->Insert(ringtoneUri, valuesBucket);
101     }
102 }
103 
RingtoneDeleteAndUpdate(int32_t toneId)104 static int RingtoneDeleteAndUpdate(int32_t toneId)
105 {
106     Uri ringtoneUri(RINGTONE_PATH_URI);
107     // Ringtone library Delete api demo
108     int32_t deleteId = toneId;
109     DataShare::DataSharePredicates deletePredicates;
110     deletePredicates.SetWhereClause(RINGTONE_COLUMN_TONE_ID + " < ? ");
111     deletePredicates.SetWhereArgs({ to_string(deleteId) });
112 
113     // Ringtone library Update api demo
114     DataSharePredicates updatePredicates;
115 
116     vector<string> updateIds;
117     updateIds.push_back(to_string(toneId - 1));
118     deletePredicates.In(RINGTONE_COLUMN_TONE_ID, updateIds);
119 
120     DataShareValuesBucket updateValuesBucket;
121     updateValuesBucket.Put(RINGTONE_COLUMN_SHOT_TONE_TYPE, 0);
122     updateValuesBucket.Put(RINGTONE_COLUMN_SHOT_TONE_SOURCE_TYPE, 0);
123 
124     int32_t changedRows = g_dataShareHelper->Update(ringtoneUri, deletePredicates, updateValuesBucket);
125     if (changedRows < 0) {
126         cout << "Update error" << endl;
127         return E_ERR;
128     }
129     cout << "Update " << changedRows << " rows from ringtone library." << endl;
130 
131     changedRows = g_dataShareHelper->Delete(ringtoneUri, deletePredicates);
132     cout << "delete " << changedRows << " rows from ringtone library." << endl;
133     return E_SUCCESS;
134 }
135 
RingtoneQuery()136 static int32_t RingtoneQuery()
137 {
138     // Ringtone library Query api demo
139     Uri ringtoneUri(RINGTONE_PATH_URI);
140     int errCode = 0;
141     DatashareBusinessError businessError;
142     DataSharePredicates queryPredicates;
143     queryPredicates.EqualTo(RINGTONE_COLUMN_SHOT_TONE_TYPE, to_string(1));
144     vector<string> columns = {};
145     auto resultSet = g_dataShareHelper->Query(ringtoneUri, queryPredicates, columns, &businessError);
146     errCode = businessError.GetCode();
147     cout << "Query errCode=" << errCode << endl;
148 
149     auto results = make_unique<RingtoneFetchResult<RingtoneAsset>>(move(resultSet));
150     cout << "query count = " << to_string(results->GetCount()) << endl;
151     unique_ptr<RingtoneAsset> ringtoneAsset = results->GetFirstObject();
152     do {
153         cout << "GetId()                            = " << to_string(ringtoneAsset->GetId()) << endl;
154         cout << "GetSize()                          = " << to_string(ringtoneAsset->GetSize()) << endl;
155         cout << "GetPath()                          = " << ringtoneAsset->GetPath() << endl;
156         cout << "GetDisplayName()                   = " << ringtoneAsset->GetDisplayName() << endl;
157         cout << "GetTitle()                         = " << ringtoneAsset->GetTitle() << endl;
158         cout << "GetMimeType()                      = " << ringtoneAsset->GetMimeType() << endl;
159         cout << "GetSourceType()                    = " << to_string(ringtoneAsset->GetSourceType()) << endl;
160         cout << "GetDateAdded()                     = " << to_string(ringtoneAsset->GetDateAdded()) << endl;
161         cout << "GetDateModified()                  = " << to_string(ringtoneAsset->GetDateModified()) << endl;
162         cout << "GetDateTaken()                     = " << to_string(ringtoneAsset->GetDateTaken()) << endl;
163         cout << "GetDuration()                      = " << to_string(ringtoneAsset->GetDuration()) << endl;
164         cout << "GetShottoneType()                  = " << to_string(ringtoneAsset->GetShottoneType()) << endl;
165         cout << "GetShottoneSourceType()            = " << to_string(ringtoneAsset->GetShottoneSourceType()) << endl;
166         cout << "GetNotificationtoneType()          = " << to_string(ringtoneAsset->GetNotificationtoneType()) << endl;
167         cout << "GetNotificationtoneSourceType()    = " << to_string(ringtoneAsset->GetNotificationtoneSourceType())
168             << endl;
169         cout << "GetRingtoneType()                  = " << to_string(ringtoneAsset->GetRingtoneType()) << endl;
170         cout << "GetRingtoneSourceType()            = " << to_string(ringtoneAsset->GetRingtoneSourceType()) << endl;
171         cout << "GetAlarmtoneType()                 = " << to_string(ringtoneAsset->GetAlarmtoneType()) << endl;
172         cout << "GetAlarmtoneSourceType()           = " << to_string(ringtoneAsset->GetAlarmtoneSourceType()) << endl;
173         cout << "GetScannerFlag()                   = " << to_string(ringtoneAsset->GetScannerFlag()) << endl;
174         ringtoneAsset = results->GetNextObject();
175     } while (ringtoneAsset != nullptr);
176     ringtoneAsset = results->GetLastObject();
177     return ringtoneAsset->GetId();
178 }
179 
180 /*
181  * Feature: RingtoneScanner
182  * Function: Strat scanner
183  * SubFunction: NA
184  * FunctionPoints: NA
185  * EnvConditions: NA
186  */
main()187 int32_t main()
188 {
189     vector<string> perms;
190     perms.push_back("ohos.permission.WRITE_RINGTONE");
191 
192     uint64_t tokenId = 0;
193     RingtonePermissionUtilsUnitTest::SetAccessTokenPermission("RingtoneScan", perms, tokenId);
194     if (tokenId == 0) {
195         RINGTONE_ERR_LOG("Set Access Token Permisson Failed.");
196         return -1;
197     }
198 
199     g_dataShareHelper = CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID);
200     if (g_dataShareHelper == nullptr) {
201         RINGTONE_ERR_LOG("g_dataShareHelper fail");
202         return -1;
203     }
204 
205     RingtoneIndex();
206     int32_t toneId = RingtoneQuery();
207     cout << "query last tone id = " << toneId << endl;
208 
209     string uriStr = RINGTONE_PATH_URI + "/" + to_string(toneId);
210     Uri ofUri(uriStr);
211     auto fd = g_dataShareHelper->OpenFile(ofUri, "rw");
212     cout << "OpenFile fd = " << fd << endl;
213 
214     if (fd >= 0) {
215         string dataBuffer = "hello world";
216         ssize_t written = write(fd, dataBuffer.c_str(), dataBuffer.size());
217         if (written < 0) {
218             cout << "Failed to copy data buffer, return " << static_cast<int>(written) << endl;
219             return -1;
220         }
221     } else {
222         cout << "OpenFile error" << endl;
223         return -fd;
224     }
225     close(fd);
226 
227     int ret = RingtoneDeleteAndUpdate(toneId);
228     if (ret != E_SUCCESS) {
229         return -1;
230     }
231     system("rm -rf /storage/media/100/local/data/com.ohos.ringtonelibrary.ringtonelibrarydata/*");
232     return 0;
233 }
234