• 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 
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     EXPORT int32_t UpdateDefaultTone();
67     EXPORT int32_t BootScanProcess();
68 
69     /* file */
70     EXPORT int32_t ScanFile();
71     EXPORT int32_t ScanFileInternal();
72     EXPORT int32_t ScanVibrateFile();
73     EXPORT int32_t BuildFileInfo();
74     EXPORT int32_t BuildData(const struct stat &statInfo);
75     EXPORT int32_t BuildVibrateData(const struct stat &statInfo);
76     EXPORT int32_t GetFileMetadata();
77     EXPORT int32_t GetMediaInfo();
78 
79     /* dir */
80     EXPORT int32_t ScanDir();
81     EXPORT int32_t ScanDirInternal();
82     EXPORT int32_t ScanFileInTraversal(const std::string &path);
83     EXPORT int32_t WalkFileTree(const std::string &path);
84     EXPORT int32_t CleanupDirectory();
85 
86     /* db ops */
87     EXPORT int32_t Commit();
88     EXPORT int32_t AddToTransaction();
89     EXPORT int32_t CommitTransaction();
90     EXPORT int32_t CommitVibrateTransaction();
91     EXPORT int32_t UpdateToneTypeSettings();
92 
93     /* callback */
94     EXPORT int32_t InvokeCallback(int32_t code);
95 
96     ScanType type_;
97     std::string path_;
98     std::string dir_;
99     std::string uri_;
100     std::shared_ptr<IRingtoneScannerCallback> callback_;
101     std::shared_ptr<bool> stopFlag_;
102 
103     std::unique_ptr<RingtoneMetadata> data_;
104     std::vector<std::unique_ptr<RingtoneMetadata>> dataBuffer_;
105     std::unique_ptr<VibrateMetadata> vibrateData_;
106     std::vector<std::unique_ptr<VibrateMetadata>> vibrateDataBuffer_;
107     bool isVibrateFile_ = false;
108     bool isForceScan_ = false;
109     uint32_t tonesScannedCount_ = 0;
110     std::mutex scannerLock_;
111     std::condition_variable scannerCv_;
112 };
113 
114 class ScanErrCallback : public IRingtoneScannerCallback {
115 public:
ScanErrCallback(const std::string & err)116     ScanErrCallback(const std::string &err) : err_(err) {};
117     ~ScanErrCallback() = default;
118 
OnScanFinished(const int32_t status,const std::string & uri,const std::string & path)119     int32_t OnScanFinished(const int32_t status, const std::string &uri, const std::string &path) override
120     {
121         return E_OK;
122     }
123 
124 private:
125     std::string err_;
126 };
127 } // namespace Media
128 } // namespace OHOS
129 
130 #endif // RINGTONE_SCANNER_OBJ_H
131