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 
16 #ifndef RINGTONE_SCANNER_OBJ_H
17 #define RINGTONE_SCANNER_OBJ_H
18 
19 #include <dirent.h>
20 #include <sys/stat.h>
21 
22 #include "iringtone_scanner_callback.h"
23 #include "ringtone_db_const.h"
24 #include "ringtone_errno.h"
25 #include "ringtone_metadata_extractor.h"
26 #include "ringtone_scanner_db.h"
27 
28 namespace OHOS {
29 namespace Media {
30 #define EXPORT __attribute__ ((visibility ("default")))
31 /**
32  * Ringtone Scanner class for scanning files and folders in RingtoneLibrary Database
33  * and updating the metadata for each Ringtone file
34  *
35  * @since 1.0
36  * @version 1.0
37  */
38 class RingtoneScannerObj {
39 public:
40     enum ScanType {
41         FILE,
42         DIRECTORY,
43         START
44     };
45     EXPORT RingtoneScannerObj(const std::string &path, const std::shared_ptr<IRingtoneScannerCallback> &callback,
46         RingtoneScannerObj::ScanType type);
47     EXPORT RingtoneScannerObj(RingtoneScannerObj::ScanType type);
48     EXPORT virtual ~RingtoneScannerObj() = default;
49 
50     EXPORT void Scan();
51 
52     /* stop */
53     EXPORT void SetStopFlag(std::shared_ptr<bool> &stopFlag);
54 
55     /* wait for scanning finished*/
56     EXPORT void WaitFor();
57 
58     /* set is Force Scan */
SetForceScan(bool isForceScan)59     EXPORT void SetForceScan(bool isForceScan)
60     {
61         isForceScan_ = isForceScan;
62     }
63 private:
64     /* boot scan */
65     EXPORT int32_t BootScan();
66 
67     /* file */
68     EXPORT int32_t ScanFile();
69     EXPORT int32_t ScanFileInternal();
70     EXPORT int32_t ScanVibrateFile();
71     EXPORT int32_t BuildFileInfo();
72     EXPORT int32_t BuildData(const struct stat &statInfo);
73     EXPORT int32_t BuildVibrateData(const struct stat &statInfo);
74     EXPORT int32_t GetFileMetadata();
75     EXPORT int32_t GetMediaInfo();
76 
77     /* dir */
78     EXPORT int32_t ScanDir();
79     EXPORT int32_t ScanDirInternal();
80     EXPORT int32_t ScanFileInTraversal(const std::string &path);
81     EXPORT int32_t WalkFileTree(const std::string &path);
82     EXPORT int32_t CleanupDirectory();
83 
84     /* db ops */
85     EXPORT int32_t Commit();
86     EXPORT int32_t AddToTransaction();
87     EXPORT int32_t CommitTransaction();
88     EXPORT int32_t CommitVibrateTransaction();
89     EXPORT int32_t UpdateToneTypeSettings();
90 
91     /* callback */
92     EXPORT int32_t InvokeCallback(int32_t code);
93 
94     ScanType type_;
95     std::string path_;
96     std::string dir_;
97     std::string uri_;
98     std::shared_ptr<IRingtoneScannerCallback> callback_;
99     std::shared_ptr<bool> stopFlag_;
100 
101     std::unique_ptr<RingtoneMetadata> data_;
102     std::vector<std::unique_ptr<RingtoneMetadata>> dataBuffer_;
103     std::unique_ptr<VibrateMetadata> vibrateData_;
104     std::vector<std::unique_ptr<VibrateMetadata>> vibrateDataBuffer_;
105     bool isVibrateFile_ = false;
106     bool isForceScan_ = false;
107     uint32_t tonesScannedCount_ = 0;
108     std::mutex scannerLock_;
109     std::condition_variable scannerCv_;
110 };
111 
112 class ScanErrCallback : public IRingtoneScannerCallback {
113 public:
ScanErrCallback(const std::string & err)114     ScanErrCallback(const std::string &err) : err_(err) {};
115     ~ScanErrCallback() = default;
116 
OnScanFinished(const int32_t status,const std::string & uri,const std::string & path)117     int32_t OnScanFinished(const int32_t status, const std::string &uri, const std::string &path) override
118     {
119         return E_OK;
120     }
121 
122 private:
123     std::string err_;
124 };
125 } // namespace Media
126 } // namespace OHOS
127 
128 #endif // RINGTONE_SCANNER_OBJ_H
129