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