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