1 /*
2 * Copyright (C) 2022 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 <unistd.h>
18 #include "accesstoken_kit.h"
19 #include "datashare_helper.h"
20 #include "get_self_permissions.h"
21 #include "iservice_registry.h"
22 #include "media_log.h"
23 #include "medialibrary_db_const.h"
24 #include "medialibrary_type_const.h"
25 #include "nativetoken_kit.h"
26 #include "token_setproc.h"
27
28 using namespace std;
29 using namespace OHOS;
30 using namespace OHOS::Media;
31 using namespace OHOS::DataShare;
32
33 namespace OHOS {
34 namespace Media {
35 namespace {
36 constexpr int STORAGE_MANAGER_MANAGER_ID = 5003;
37 } // namespace
38 } // namespace Media
39 } // namespace OHOS
40
CreateDataShareHelper(int32_t systemAbilityId)41 static std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper(int32_t systemAbilityId)
42 {
43 MEDIA_INFO_LOG("CreateDataShareHelper::CreateFileExtHelper ");
44 auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
45 if (saManager == nullptr) {
46 MEDIA_INFO_LOG("CreateFileExtHelper Get system ability mgr failed.");
47 return nullptr;
48 }
49 auto remoteObj = saManager->GetSystemAbility(systemAbilityId);
50 if (remoteObj == nullptr) {
51 MEDIA_INFO_LOG("CreateDataShareHelper GetSystemAbility Service Failed.");
52 return nullptr;
53 }
54 return DataShare::DataShareHelper::Creator(remoteObj, MEDIALIBRARY_DATA_URI);
55 }
56
57 /*
58 * Feature: MediaScanner
59 * Function: Strat scanner
60 * SubFunction: NA
61 * FunctionPoints: NA
62 * EnvConditions: NA
63 */
main()64 int32_t main()
65 {
66 vector<string> perms;
67 perms.push_back("ohos.permission.READ_MEDIA");
68 perms.push_back("ohos.permission.WRITE_MEDIA");
69 perms.push_back("ohos.permission.FILE_ACCESS_MANAGER");
70 perms.push_back("ohos.permission.GET_BUNDLE_INFO_PRIVILEGED");
71 uint64_t tokenId = 0;
72 PermissionUtilsUnitTest::SetAccessTokenPermission("MediaLibraryScan", perms, tokenId);
73 if (tokenId == 0) {
74 MEDIA_ERR_LOG("Set Access Token Permisson Failed.");
75 return 0;
76 }
77
78 auto mediaDataShareHelper = CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID);
79 if (mediaDataShareHelper == nullptr) {
80 MEDIA_ERR_LOG("mediaDataShareHelper fail");
81 return 0;
82 }
83 Uri scanUri(URI_SCANNER);
84 DataShareValuesBucket valuesBucket;
85 valuesBucket.Put(MEDIA_DATA_DB_FILE_PATH, ROOT_MEDIA_DIR);
86 mediaDataShareHelper->Insert(scanUri, valuesBucket);
87 // Waiting for scanner to complete.
88 constexpr int32_t estimatedScanTime = 10;
89 sleep(estimatedScanTime);
90 return 0;
91 }
92