13e5483f6Sopenharmony_ci/* 23e5483f6Sopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd. 33e5483f6Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43e5483f6Sopenharmony_ci * you may not use this file except in compliance with the License. 53e5483f6Sopenharmony_ci * You may obtain a copy of the License at 63e5483f6Sopenharmony_ci * 73e5483f6Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83e5483f6Sopenharmony_ci * 93e5483f6Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103e5483f6Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113e5483f6Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123e5483f6Sopenharmony_ci * See the License for the specific language governing permissions and 133e5483f6Sopenharmony_ci * limitations under the License. 143e5483f6Sopenharmony_ci */ 153e5483f6Sopenharmony_ci 163e5483f6Sopenharmony_ci#define MLOG_TAG "Scanner" 173e5483f6Sopenharmony_ci 183e5483f6Sopenharmony_ci#include "ringtone_scanner.h" 193e5483f6Sopenharmony_ci 203e5483f6Sopenharmony_ci#include "directory_ex.h" 213e5483f6Sopenharmony_ci#include "ringtone_default_setting.h" 223e5483f6Sopenharmony_ci#include "ringtone_file_utils.h" 233e5483f6Sopenharmony_ci#include "ringtone_log.h" 243e5483f6Sopenharmony_ci#include "ringtone_mimetype_utils.h" 253e5483f6Sopenharmony_ci#include "ringtone_rdbstore.h" 263e5483f6Sopenharmony_ci#include "ringtone_scanner_utils.h" 273e5483f6Sopenharmony_ci 283e5483f6Sopenharmony_cinamespace OHOS { 293e5483f6Sopenharmony_cinamespace Media { 303e5483f6Sopenharmony_ciusing namespace std; 313e5483f6Sopenharmony_ciusing namespace OHOS::AppExecFwk; 323e5483f6Sopenharmony_ciusing namespace OHOS::DataShare; 333e5483f6Sopenharmony_cistatic const int32_t SCANNER_WAIT_FOR_TIMEOUT = 10000; // ms 343e5483f6Sopenharmony_cistatic const std::string PATH_PLAY_MODE_SYNC = "/synchronized"; 353e5483f6Sopenharmony_cistatic const std::string PATH_PLAY_MODE_CLASSIC = "/non-synchronized"; 363e5483f6Sopenharmony_cistatic const std::string PATH_VIBRATE_TYPE_STANDARD = "/standard"; 373e5483f6Sopenharmony_cistatic const std::string PATH_VIBRATE_TYPE_GENTLE = "/gentle"; 383e5483f6Sopenharmony_cistatic const std::string ALARMS_TYPE = "alarms"; 393e5483f6Sopenharmony_cistatic const std::string RINGTONES_TYPE = "ringtones"; 403e5483f6Sopenharmony_cistatic const std::string NOTIFICATIONS_TYPE = "notifications"; 413e5483f6Sopenharmony_ci#ifndef OHOS_LOCAL_DEBUG_DISABLE 423e5483f6Sopenharmony_ci// liuxk just for debug 433e5483f6Sopenharmony_cistatic const std::string LOCAL_DIR = "/data/storage/el2/base/preload_data"; 443e5483f6Sopenharmony_ci#endif 453e5483f6Sopenharmony_cistatic std::unordered_map<std::string, std::pair<int32_t, int32_t>> g_typeMap = { 463e5483f6Sopenharmony_ci#ifndef OHOS_LOCAL_DEBUG_DISABLE 473e5483f6Sopenharmony_ci // liuxk just for debug 483e5483f6Sopenharmony_ci {LOCAL_DIR + "/alarms", {SOURCE_TYPE_CUSTOMISED, TONE_TYPE_ALARM}}, 493e5483f6Sopenharmony_ci {LOCAL_DIR + "/ringtones", {SOURCE_TYPE_PRESET, TONE_TYPE_RINGTONE}}, 503e5483f6Sopenharmony_ci {LOCAL_DIR + "/notifications", {SOURCE_TYPE_CUSTOMISED, TONE_TYPE_NOTIFICATION}}, 513e5483f6Sopenharmony_ci#endif 523e5483f6Sopenharmony_ci // customized tones map 533e5483f6Sopenharmony_ci {RINGTONE_CUSTOMIZED_ALARM_PATH, {SOURCE_TYPE_CUSTOMISED, TONE_TYPE_ALARM}}, 543e5483f6Sopenharmony_ci {RINGTONE_CUSTOMIZED_RINGTONE_PATH, {SOURCE_TYPE_CUSTOMISED, TONE_TYPE_RINGTONE}}, 553e5483f6Sopenharmony_ci {RINGTONE_CUSTOMIZED_NOTIFICATIONS_PATH, {SOURCE_TYPE_CUSTOMISED, TONE_TYPE_NOTIFICATION}}, 563e5483f6Sopenharmony_ci // customized tones map 573e5483f6Sopenharmony_ci {ROOT_TONE_PRELOAD_PATH_NOAH_PATH + "/alarms", {SOURCE_TYPE_PRESET, TONE_TYPE_ALARM}}, 583e5483f6Sopenharmony_ci {ROOT_TONE_PRELOAD_PATH_NOAH_PATH + "/ringtones", {SOURCE_TYPE_PRESET, TONE_TYPE_RINGTONE}}, 593e5483f6Sopenharmony_ci {ROOT_TONE_PRELOAD_PATH_NOAH_PATH + "/notifications", {SOURCE_TYPE_PRESET, TONE_TYPE_NOTIFICATION}}, 603e5483f6Sopenharmony_ci {ROOT_TONE_PRELOAD_PATH_CHINA_PATH + "/alarms", {SOURCE_TYPE_PRESET, TONE_TYPE_ALARM}}, 613e5483f6Sopenharmony_ci {ROOT_TONE_PRELOAD_PATH_CHINA_PATH + "/ringtones", {SOURCE_TYPE_PRESET, TONE_TYPE_RINGTONE}}, 623e5483f6Sopenharmony_ci {ROOT_TONE_PRELOAD_PATH_CHINA_PATH + "/notifications", {SOURCE_TYPE_PRESET, TONE_TYPE_NOTIFICATION}}, 633e5483f6Sopenharmony_ci {ROOT_TONE_PRELOAD_PATH_OVERSEA_PATH + "/alarms", {SOURCE_TYPE_PRESET, TONE_TYPE_ALARM}}, 643e5483f6Sopenharmony_ci {ROOT_TONE_PRELOAD_PATH_OVERSEA_PATH + "/ringtones", {SOURCE_TYPE_PRESET, TONE_TYPE_RINGTONE}}, 653e5483f6Sopenharmony_ci {ROOT_TONE_PRELOAD_PATH_OVERSEA_PATH + "/notifications", {SOURCE_TYPE_PRESET, TONE_TYPE_NOTIFICATION}}, 663e5483f6Sopenharmony_ci}; 673e5483f6Sopenharmony_ci// vibrate type map 683e5483f6Sopenharmony_cistatic std::unordered_map<std::string, std::pair<int32_t, int32_t>> g_vibrateTypeMap = { 693e5483f6Sopenharmony_ci {ROOT_VIBRATE_PRELOAD_PATH_NOAH_PATH + PATH_VIBRATE_TYPE_STANDARD, {SOURCE_TYPE_PRESET, VIBRATE_TYPE_STANDARD}}, 703e5483f6Sopenharmony_ci {ROOT_VIBRATE_PRELOAD_PATH_NOAH_PATH + PATH_VIBRATE_TYPE_GENTLE, {SOURCE_TYPE_PRESET, VIBRATE_TYPE_GENTLE}}, 713e5483f6Sopenharmony_ci {ROOT_VIBRATE_PRELOAD_PATH_CHINA_PATH + PATH_VIBRATE_TYPE_STANDARD, {SOURCE_TYPE_PRESET, VIBRATE_TYPE_STANDARD}}, 723e5483f6Sopenharmony_ci {ROOT_VIBRATE_PRELOAD_PATH_CHINA_PATH + PATH_VIBRATE_TYPE_GENTLE, {SOURCE_TYPE_PRESET, VIBRATE_TYPE_GENTLE}}, 733e5483f6Sopenharmony_ci {ROOT_VIBRATE_PRELOAD_PATH_OVERSEA_PATH + PATH_VIBRATE_TYPE_STANDARD, {SOURCE_TYPE_PRESET, VIBRATE_TYPE_STANDARD}}, 743e5483f6Sopenharmony_ci {ROOT_VIBRATE_PRELOAD_PATH_OVERSEA_PATH + PATH_VIBRATE_TYPE_GENTLE, {SOURCE_TYPE_PRESET, VIBRATE_TYPE_GENTLE}}, 753e5483f6Sopenharmony_ci}; 763e5483f6Sopenharmony_ci 773e5483f6Sopenharmony_cistatic std::unordered_map<std::string, std::pair<int32_t, int32_t>> g_vibratePlayModeMap = { 783e5483f6Sopenharmony_ci {ROOT_VIBRATE_PRELOAD_PATH_NOAH_PATH + PATH_VIBRATE_TYPE_STANDARD + PATH_PLAY_MODE_SYNC, 793e5483f6Sopenharmony_ci {SOURCE_TYPE_PRESET, VIBRATE_PLAYMODE_SYNC}}, 803e5483f6Sopenharmony_ci {ROOT_VIBRATE_PRELOAD_PATH_NOAH_PATH + PATH_VIBRATE_TYPE_STANDARD + PATH_PLAY_MODE_CLASSIC, 813e5483f6Sopenharmony_ci {SOURCE_TYPE_PRESET, VIBRATE_PLAYMODE_CLASSIC}}, 823e5483f6Sopenharmony_ci {ROOT_VIBRATE_PRELOAD_PATH_NOAH_PATH + PATH_VIBRATE_TYPE_GENTLE + PATH_PLAY_MODE_SYNC, 833e5483f6Sopenharmony_ci {SOURCE_TYPE_PRESET, VIBRATE_PLAYMODE_SYNC}}, 843e5483f6Sopenharmony_ci {ROOT_VIBRATE_PRELOAD_PATH_NOAH_PATH + PATH_VIBRATE_TYPE_GENTLE + PATH_PLAY_MODE_CLASSIC, 853e5483f6Sopenharmony_ci {SOURCE_TYPE_PRESET, VIBRATE_PLAYMODE_CLASSIC}}, 863e5483f6Sopenharmony_ci {ROOT_VIBRATE_PRELOAD_PATH_CHINA_PATH + PATH_VIBRATE_TYPE_STANDARD + PATH_PLAY_MODE_SYNC, 873e5483f6Sopenharmony_ci {SOURCE_TYPE_PRESET, VIBRATE_PLAYMODE_SYNC}}, 883e5483f6Sopenharmony_ci {ROOT_VIBRATE_PRELOAD_PATH_CHINA_PATH + PATH_VIBRATE_TYPE_STANDARD + PATH_PLAY_MODE_CLASSIC, 893e5483f6Sopenharmony_ci {SOURCE_TYPE_PRESET, VIBRATE_PLAYMODE_CLASSIC}}, 903e5483f6Sopenharmony_ci {ROOT_VIBRATE_PRELOAD_PATH_CHINA_PATH + PATH_VIBRATE_TYPE_GENTLE + PATH_PLAY_MODE_SYNC, 913e5483f6Sopenharmony_ci {SOURCE_TYPE_PRESET, VIBRATE_PLAYMODE_SYNC}}, 923e5483f6Sopenharmony_ci {ROOT_VIBRATE_PRELOAD_PATH_CHINA_PATH + PATH_VIBRATE_TYPE_GENTLE + PATH_PLAY_MODE_CLASSIC, 933e5483f6Sopenharmony_ci {SOURCE_TYPE_PRESET, VIBRATE_PLAYMODE_CLASSIC}}, 943e5483f6Sopenharmony_ci {ROOT_VIBRATE_PRELOAD_PATH_OVERSEA_PATH + PATH_VIBRATE_TYPE_STANDARD + PATH_PLAY_MODE_SYNC, 953e5483f6Sopenharmony_ci {SOURCE_TYPE_PRESET, VIBRATE_PLAYMODE_SYNC}}, 963e5483f6Sopenharmony_ci {ROOT_VIBRATE_PRELOAD_PATH_OVERSEA_PATH + PATH_VIBRATE_TYPE_STANDARD + PATH_PLAY_MODE_CLASSIC, 973e5483f6Sopenharmony_ci {SOURCE_TYPE_PRESET, VIBRATE_PLAYMODE_CLASSIC}}, 983e5483f6Sopenharmony_ci {ROOT_VIBRATE_PRELOAD_PATH_OVERSEA_PATH + PATH_VIBRATE_TYPE_GENTLE + PATH_PLAY_MODE_SYNC, 993e5483f6Sopenharmony_ci {SOURCE_TYPE_PRESET, VIBRATE_PLAYMODE_SYNC}}, 1003e5483f6Sopenharmony_ci {ROOT_VIBRATE_PRELOAD_PATH_OVERSEA_PATH + PATH_VIBRATE_TYPE_GENTLE + PATH_PLAY_MODE_CLASSIC, 1013e5483f6Sopenharmony_ci {SOURCE_TYPE_PRESET, VIBRATE_PLAYMODE_CLASSIC}}, 1023e5483f6Sopenharmony_ci}; 1033e5483f6Sopenharmony_ci 1043e5483f6Sopenharmony_ciRingtoneScannerObj::RingtoneScannerObj(const std::string &path, 1053e5483f6Sopenharmony_ci const std::shared_ptr<IRingtoneScannerCallback> &callback, 1063e5483f6Sopenharmony_ci RingtoneScannerObj::ScanType type) : type_(type), callback_(callback) 1073e5483f6Sopenharmony_ci{ 1083e5483f6Sopenharmony_ci if (type_ == DIRECTORY) { 1093e5483f6Sopenharmony_ci dir_ = path; 1103e5483f6Sopenharmony_ci } else if (type_ == FILE) { 1113e5483f6Sopenharmony_ci path_ = path; 1123e5483f6Sopenharmony_ci } 1133e5483f6Sopenharmony_ci // when path is /Photo, it means update or clone scene 1143e5483f6Sopenharmony_ci stopFlag_ = make_shared<bool>(false); 1153e5483f6Sopenharmony_ci} 1163e5483f6Sopenharmony_ci 1173e5483f6Sopenharmony_ciRingtoneScannerObj::RingtoneScannerObj(RingtoneScannerObj::ScanType type) : type_(type) 1183e5483f6Sopenharmony_ci{ 1193e5483f6Sopenharmony_ci stopFlag_ = make_shared<bool>(false); 1203e5483f6Sopenharmony_ci} 1213e5483f6Sopenharmony_ci 1223e5483f6Sopenharmony_civoid RingtoneScannerObj::SetStopFlag(std::shared_ptr<bool> &flag) 1233e5483f6Sopenharmony_ci{ 1243e5483f6Sopenharmony_ci stopFlag_ = flag; 1253e5483f6Sopenharmony_ci} 1263e5483f6Sopenharmony_ci 1273e5483f6Sopenharmony_ciint32_t RingtoneScannerObj::ScanFile() 1283e5483f6Sopenharmony_ci{ 1293e5483f6Sopenharmony_ci RINGTONE_DEBUG_LOG("scan file %{private}s", path_.c_str()); 1303e5483f6Sopenharmony_ci 1313e5483f6Sopenharmony_ci int32_t ret = ScanFileInternal(); 1323e5483f6Sopenharmony_ci if (ret != E_OK) { 1333e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("ScanFileInternal err %{public}d", ret); 1343e5483f6Sopenharmony_ci } 1353e5483f6Sopenharmony_ci 1363e5483f6Sopenharmony_ci (void)InvokeCallback(ret); 1373e5483f6Sopenharmony_ci 1383e5483f6Sopenharmony_ci return ret; 1393e5483f6Sopenharmony_ci} 1403e5483f6Sopenharmony_ci 1413e5483f6Sopenharmony_ciint32_t RingtoneScannerObj::InvokeCallback(int32_t err) 1423e5483f6Sopenharmony_ci{ 1433e5483f6Sopenharmony_ci if (callback_ == nullptr) { 1443e5483f6Sopenharmony_ci return E_OK; 1453e5483f6Sopenharmony_ci } 1463e5483f6Sopenharmony_ci 1473e5483f6Sopenharmony_ci return callback_->OnScanFinished(err, uri_, path_); 1483e5483f6Sopenharmony_ci} 1493e5483f6Sopenharmony_ci 1503e5483f6Sopenharmony_civoid RingtoneScannerObj::Scan() 1513e5483f6Sopenharmony_ci{ 1523e5483f6Sopenharmony_ci switch (type_) { 1533e5483f6Sopenharmony_ci case FILE: 1543e5483f6Sopenharmony_ci ScanFile(); 1553e5483f6Sopenharmony_ci break; 1563e5483f6Sopenharmony_ci case DIRECTORY: 1573e5483f6Sopenharmony_ci ScanDir(); 1583e5483f6Sopenharmony_ci break; 1593e5483f6Sopenharmony_ci case START: 1603e5483f6Sopenharmony_ci BootScan(); 1613e5483f6Sopenharmony_ci break; 1623e5483f6Sopenharmony_ci default: 1633e5483f6Sopenharmony_ci break; 1643e5483f6Sopenharmony_ci } 1653e5483f6Sopenharmony_ci} 1663e5483f6Sopenharmony_ci 1673e5483f6Sopenharmony_ciint32_t RingtoneScannerObj::BootScan() 1683e5483f6Sopenharmony_ci{ 1693e5483f6Sopenharmony_ci static const std::vector<std::string> preloadDirs = { 1703e5483f6Sopenharmony_ci {ROOT_TONE_PRELOAD_PATH_NOAH_PATH + "/alarms"}, 1713e5483f6Sopenharmony_ci {ROOT_TONE_PRELOAD_PATH_NOAH_PATH + "/ringtones"}, 1723e5483f6Sopenharmony_ci {ROOT_TONE_PRELOAD_PATH_NOAH_PATH + "/notifications"}, 1733e5483f6Sopenharmony_ci {ROOT_TONE_PRELOAD_PATH_CHINA_PATH + "/alarms"}, 1743e5483f6Sopenharmony_ci {ROOT_TONE_PRELOAD_PATH_CHINA_PATH + "/ringtones"}, 1753e5483f6Sopenharmony_ci {ROOT_TONE_PRELOAD_PATH_CHINA_PATH + "/notifications"}, 1763e5483f6Sopenharmony_ci {ROOT_TONE_PRELOAD_PATH_OVERSEA_PATH + "/alarms"}, 1773e5483f6Sopenharmony_ci {ROOT_TONE_PRELOAD_PATH_OVERSEA_PATH + "/ringtones"}, 1783e5483f6Sopenharmony_ci {ROOT_TONE_PRELOAD_PATH_OVERSEA_PATH + "/notifications"}, 1793e5483f6Sopenharmony_ci {ROOT_VIBRATE_PRELOAD_PATH_NOAH_PATH + "/standard"}, 1803e5483f6Sopenharmony_ci {ROOT_VIBRATE_PRELOAD_PATH_NOAH_PATH + "/gentle"}, 1813e5483f6Sopenharmony_ci {ROOT_VIBRATE_PRELOAD_PATH_CHINA_PATH + "/standard"}, 1823e5483f6Sopenharmony_ci {ROOT_VIBRATE_PRELOAD_PATH_CHINA_PATH + "/gentle"}, 1833e5483f6Sopenharmony_ci {ROOT_VIBRATE_PRELOAD_PATH_OVERSEA_PATH + "/standard"}, 1843e5483f6Sopenharmony_ci {ROOT_VIBRATE_PRELOAD_PATH_OVERSEA_PATH + "/gentle"}, 1853e5483f6Sopenharmony_ci }; 1863e5483f6Sopenharmony_ci 1873e5483f6Sopenharmony_ci int64_t scanStart = RingtoneFileUtils::UTCTimeMilliSeconds(); 1883e5483f6Sopenharmony_ci for (auto &dir : preloadDirs) { 1893e5483f6Sopenharmony_ci RINGTONE_INFO_LOG("start to scan realpath %{private}s", dir.c_str()); 1903e5483f6Sopenharmony_ci string realPath; 1913e5483f6Sopenharmony_ci if (!PathToRealPath(dir, realPath)) { 1923e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("failed to get realpath %{private}s, errno %{public}d", dir.c_str(), errno); 1933e5483f6Sopenharmony_ci continue; 1943e5483f6Sopenharmony_ci } 1953e5483f6Sopenharmony_ci 1963e5483f6Sopenharmony_ci RINGTONE_INFO_LOG("start to scan realpath %{private}s", dir.c_str()); 1973e5483f6Sopenharmony_ci callback_ = make_shared<ScanErrCallback>(dir); 1983e5483f6Sopenharmony_ci 1993e5483f6Sopenharmony_ci if (RingtoneScannerUtils::IsDirectory(realPath)) { 2003e5483f6Sopenharmony_ci dir_ = move(realPath); 2013e5483f6Sopenharmony_ci (void)ScanDir(); 2023e5483f6Sopenharmony_ci } else if (RingtoneScannerUtils::IsRegularFile(realPath)) { 2033e5483f6Sopenharmony_ci path_ = move(realPath); 2043e5483f6Sopenharmony_ci (void)ScanFile(); 2053e5483f6Sopenharmony_ci } 2063e5483f6Sopenharmony_ci } 2073e5483f6Sopenharmony_ci 2083e5483f6Sopenharmony_ci // reset ringtone default settings 2093e5483f6Sopenharmony_ci auto rawRdb = RingtoneRdbStore::GetInstance()->GetRaw(); 2103e5483f6Sopenharmony_ci RingtoneDefaultSetting::GetObj(rawRdb)->Update(); 2113e5483f6Sopenharmony_ci 2123e5483f6Sopenharmony_ci int64_t scanEnd = RingtoneFileUtils::UTCTimeMilliSeconds(); 2133e5483f6Sopenharmony_ci RINGTONE_INFO_LOG("total preload tone files count:%{public}d, scanned: %{public}d, costed-time:%{public}" 2143e5483f6Sopenharmony_ci PRId64 " ms", tonesScannedCount_, tonesScannedCount_, scanEnd - scanStart); 2153e5483f6Sopenharmony_ci unique_lock<mutex> lock(scannerLock_); 2163e5483f6Sopenharmony_ci scannerCv_.notify_one(); 2173e5483f6Sopenharmony_ci return E_OK; 2183e5483f6Sopenharmony_ci} 2193e5483f6Sopenharmony_ci 2203e5483f6Sopenharmony_civoid RingtoneScannerObj::WaitFor() 2213e5483f6Sopenharmony_ci{ 2223e5483f6Sopenharmony_ci unique_lock<mutex> lock(scannerLock_); 2233e5483f6Sopenharmony_ci scannerCv_.wait_for(lock, chrono::milliseconds(SCANNER_WAIT_FOR_TIMEOUT)); 2243e5483f6Sopenharmony_ci} 2253e5483f6Sopenharmony_ci 2263e5483f6Sopenharmony_ciint32_t RingtoneScannerObj::ScanDir() 2273e5483f6Sopenharmony_ci{ 2283e5483f6Sopenharmony_ci RINGTONE_INFO_LOG("scan dir %{private}s", dir_.c_str()); 2293e5483f6Sopenharmony_ci 2303e5483f6Sopenharmony_ci int32_t ret = ScanDirInternal(); 2313e5483f6Sopenharmony_ci if (ret != E_OK) { 2323e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("ScanDirInternal err %{public}d", ret); 2333e5483f6Sopenharmony_ci } 2343e5483f6Sopenharmony_ci 2353e5483f6Sopenharmony_ci (void)InvokeCallback(ret); 2363e5483f6Sopenharmony_ci 2373e5483f6Sopenharmony_ci return ret; 2383e5483f6Sopenharmony_ci} 2393e5483f6Sopenharmony_ci 2403e5483f6Sopenharmony_ciint32_t RingtoneScannerObj::ScanDirInternal() 2413e5483f6Sopenharmony_ci{ 2423e5483f6Sopenharmony_ci if (RingtoneScannerUtils::IsDirHiddenRecursive(dir_)) { 2433e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("the dir %{private}s is hidden", dir_.c_str()); 2443e5483f6Sopenharmony_ci return E_DIR_HIDDEN; 2453e5483f6Sopenharmony_ci } 2463e5483f6Sopenharmony_ci 2473e5483f6Sopenharmony_ci /* no further operation when stopped */ 2483e5483f6Sopenharmony_ci auto err = WalkFileTree(dir_); 2493e5483f6Sopenharmony_ci if (err != E_OK) { 2503e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("walk file tree err %{public}d", err); 2513e5483f6Sopenharmony_ci return err; 2523e5483f6Sopenharmony_ci } 2533e5483f6Sopenharmony_ci err = CommitTransaction(); 2543e5483f6Sopenharmony_ci if (err != E_OK) { 2553e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("commit transaction err %{public}d", err); 2563e5483f6Sopenharmony_ci return err; 2573e5483f6Sopenharmony_ci } 2583e5483f6Sopenharmony_ci 2593e5483f6Sopenharmony_ci err = CommitVibrateTransaction(); 2603e5483f6Sopenharmony_ci if (err != E_OK) { 2613e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("commit vibrate transaction err %{public}d", err); 2623e5483f6Sopenharmony_ci return err; 2633e5483f6Sopenharmony_ci } 2643e5483f6Sopenharmony_ci err = CleanupDirectory(); 2653e5483f6Sopenharmony_ci if (err != E_OK) { 2663e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("clean up dir err %{public}d", err); 2673e5483f6Sopenharmony_ci return err; 2683e5483f6Sopenharmony_ci } 2693e5483f6Sopenharmony_ci 2703e5483f6Sopenharmony_ci return E_OK; 2713e5483f6Sopenharmony_ci} 2723e5483f6Sopenharmony_ci 2733e5483f6Sopenharmony_ciint32_t RingtoneScannerObj::CleanupDirectory() 2743e5483f6Sopenharmony_ci{ 2753e5483f6Sopenharmony_ci return E_OK; 2763e5483f6Sopenharmony_ci} 2773e5483f6Sopenharmony_ci 2783e5483f6Sopenharmony_ciint32_t RingtoneScannerObj::CommitTransaction() 2793e5483f6Sopenharmony_ci{ 2803e5483f6Sopenharmony_ci unique_ptr<RingtoneMetadata> data; 2813e5483f6Sopenharmony_ci string tableName = RINGTONE_TABLE; 2823e5483f6Sopenharmony_ci 2833e5483f6Sopenharmony_ci // will begin a transaction in later pr 2843e5483f6Sopenharmony_ci for (uint32_t i = 0; i < dataBuffer_.size(); i++) { 2853e5483f6Sopenharmony_ci data = move(dataBuffer_[i]); 2863e5483f6Sopenharmony_ci if (data->GetToneId() != FILE_ID_DEFAULT) { 2873e5483f6Sopenharmony_ci RingtoneScannerDb::UpdateMetadata(*data, tableName); 2883e5483f6Sopenharmony_ci } else { 2893e5483f6Sopenharmony_ci RingtoneScannerDb::InsertMetadata(*data, tableName); 2903e5483f6Sopenharmony_ci } 2913e5483f6Sopenharmony_ci } 2923e5483f6Sopenharmony_ci 2933e5483f6Sopenharmony_ci if (dataBuffer_.size() > 0) { 2943e5483f6Sopenharmony_ci tonesScannedCount_ += dataBuffer_.size(); 2953e5483f6Sopenharmony_ci } 2963e5483f6Sopenharmony_ci dataBuffer_.clear(); 2973e5483f6Sopenharmony_ci 2983e5483f6Sopenharmony_ci return E_OK; 2993e5483f6Sopenharmony_ci} 3003e5483f6Sopenharmony_ci 3013e5483f6Sopenharmony_ciint32_t RingtoneScannerObj::CommitVibrateTransaction() 3023e5483f6Sopenharmony_ci{ 3033e5483f6Sopenharmony_ci unique_ptr<VibrateMetadata> vibrateData; 3043e5483f6Sopenharmony_ci string vibrateTableName = VIBRATE_TABLE; 3053e5483f6Sopenharmony_ci 3063e5483f6Sopenharmony_ci for (uint32_t i = 0; i < vibrateDataBuffer_.size(); i++) { 3073e5483f6Sopenharmony_ci vibrateData = move(vibrateDataBuffer_[i]); 3083e5483f6Sopenharmony_ci if (vibrateData->GetVibrateId() != FILE_ID_DEFAULT) { 3093e5483f6Sopenharmony_ci RingtoneScannerDb::UpdateVibrateMetadata(*vibrateData, vibrateTableName); 3103e5483f6Sopenharmony_ci } else { 3113e5483f6Sopenharmony_ci RingtoneScannerDb::InsertVibrateMetadata(*vibrateData, vibrateTableName); 3123e5483f6Sopenharmony_ci } 3133e5483f6Sopenharmony_ci } 3143e5483f6Sopenharmony_ci 3153e5483f6Sopenharmony_ci if (vibrateDataBuffer_.size() > 0) { 3163e5483f6Sopenharmony_ci tonesScannedCount_ += vibrateDataBuffer_.size(); 3173e5483f6Sopenharmony_ci } 3183e5483f6Sopenharmony_ci vibrateDataBuffer_.clear(); 3193e5483f6Sopenharmony_ci 3203e5483f6Sopenharmony_ci return E_OK; 3213e5483f6Sopenharmony_ci} 3223e5483f6Sopenharmony_ci 3233e5483f6Sopenharmony_ciint32_t RingtoneScannerObj::WalkFileTree(const string &path) 3243e5483f6Sopenharmony_ci{ 3253e5483f6Sopenharmony_ci int err = E_OK; 3263e5483f6Sopenharmony_ci DIR *dirPath = nullptr; 3273e5483f6Sopenharmony_ci struct dirent *ent = nullptr; 3283e5483f6Sopenharmony_ci size_t len = path.length(); 3293e5483f6Sopenharmony_ci struct stat statInfo; 3303e5483f6Sopenharmony_ci if (len >= FILENAME_MAX - 1) { 3313e5483f6Sopenharmony_ci return ERR_INCORRECT_PATH; 3323e5483f6Sopenharmony_ci } 3333e5483f6Sopenharmony_ci auto fName = (char *)calloc(FILENAME_MAX, sizeof(char)); 3343e5483f6Sopenharmony_ci if (fName == nullptr) { 3353e5483f6Sopenharmony_ci return E_NO_MEMORY; 3363e5483f6Sopenharmony_ci } 3373e5483f6Sopenharmony_ci if (strcpy_s(fName, FILENAME_MAX, path.c_str()) != ERR_SUCCESS) { 3383e5483f6Sopenharmony_ci free(fName); 3393e5483f6Sopenharmony_ci return E_ERR; 3403e5483f6Sopenharmony_ci } 3413e5483f6Sopenharmony_ci fName[len++] = '/'; 3423e5483f6Sopenharmony_ci if ((dirPath = opendir(path.c_str())) == nullptr) { 3433e5483f6Sopenharmony_ci free(fName); 3443e5483f6Sopenharmony_ci return E_PERMISSION_DENIED; 3453e5483f6Sopenharmony_ci } 3463e5483f6Sopenharmony_ci while ((ent = readdir(dirPath)) != nullptr) { 3473e5483f6Sopenharmony_ci if (*stopFlag_) { 3483e5483f6Sopenharmony_ci err = E_STOP; 3493e5483f6Sopenharmony_ci break; 3503e5483f6Sopenharmony_ci } 3513e5483f6Sopenharmony_ci if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) { 3523e5483f6Sopenharmony_ci continue; 3533e5483f6Sopenharmony_ci } 3543e5483f6Sopenharmony_ci if (strncpy_s(fName + len, FILENAME_MAX - len, ent->d_name, FILENAME_MAX - len)) { 3553e5483f6Sopenharmony_ci continue; 3563e5483f6Sopenharmony_ci } 3573e5483f6Sopenharmony_ci if (lstat(fName, &statInfo) == -1) { 3583e5483f6Sopenharmony_ci continue; 3593e5483f6Sopenharmony_ci } 3603e5483f6Sopenharmony_ci string currentPath = fName; 3613e5483f6Sopenharmony_ci if (S_ISDIR(statInfo.st_mode)) { 3623e5483f6Sopenharmony_ci if (RingtoneScannerUtils::IsDirHidden(currentPath)) { 3633e5483f6Sopenharmony_ci continue; 3643e5483f6Sopenharmony_ci } 3653e5483f6Sopenharmony_ci (void)WalkFileTree(currentPath); 3663e5483f6Sopenharmony_ci } else { 3673e5483f6Sopenharmony_ci (void)ScanFileInTraversal(currentPath); 3683e5483f6Sopenharmony_ci } 3693e5483f6Sopenharmony_ci } 3703e5483f6Sopenharmony_ci closedir(dirPath); 3713e5483f6Sopenharmony_ci free(fName); 3723e5483f6Sopenharmony_ci return err; 3733e5483f6Sopenharmony_ci} 3743e5483f6Sopenharmony_ci 3753e5483f6Sopenharmony_ciint32_t RingtoneScannerObj::ScanFileInTraversal(const string &path) 3763e5483f6Sopenharmony_ci{ 3773e5483f6Sopenharmony_ci path_ = path; 3783e5483f6Sopenharmony_ci if (RingtoneScannerUtils::IsFileHidden(path_)) { 3793e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("the file is hidden"); 3803e5483f6Sopenharmony_ci return E_FILE_HIDDEN; 3813e5483f6Sopenharmony_ci } 3823e5483f6Sopenharmony_ci 3833e5483f6Sopenharmony_ci bool flag = (path_.find(ROOT_VIBRATE_PRELOAD_PATH_NOAH_PATH) != std::string::npos) ? true : false; 3843e5483f6Sopenharmony_ci flag |= (path_.find(ROOT_VIBRATE_PRELOAD_PATH_CHINA_PATH) != std::string::npos); 3853e5483f6Sopenharmony_ci flag |= (path_.find(ROOT_VIBRATE_PRELOAD_PATH_OVERSEA_PATH) != std::string::npos); 3863e5483f6Sopenharmony_ci std::string extension = RingtoneScannerUtils::GetFileExtension(path_); 3873e5483f6Sopenharmony_ci 3883e5483f6Sopenharmony_ci if (flag) { 3893e5483f6Sopenharmony_ci if (extension.compare("json") == 0) { 3903e5483f6Sopenharmony_ci isVibrateFile_ = true; 3913e5483f6Sopenharmony_ci return ScanVibrateFile(); 3923e5483f6Sopenharmony_ci } 3933e5483f6Sopenharmony_ci return E_OK; 3943e5483f6Sopenharmony_ci } 3953e5483f6Sopenharmony_ci 3963e5483f6Sopenharmony_ci int32_t err = GetFileMetadata(); 3973e5483f6Sopenharmony_ci if (err != E_OK) { 3983e5483f6Sopenharmony_ci if (err != E_SCANNED) { 3993e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("failed to get file metadata"); 4003e5483f6Sopenharmony_ci } 4013e5483f6Sopenharmony_ci return err; 4023e5483f6Sopenharmony_ci } 4033e5483f6Sopenharmony_ci 4043e5483f6Sopenharmony_ci err = BuildFileInfo(); 4053e5483f6Sopenharmony_ci if (err != E_OK) { 4063e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("failed to get other file metadata"); 4073e5483f6Sopenharmony_ci return err; 4083e5483f6Sopenharmony_ci } 4093e5483f6Sopenharmony_ci 4103e5483f6Sopenharmony_ci return E_OK; 4113e5483f6Sopenharmony_ci} 4123e5483f6Sopenharmony_ci 4133e5483f6Sopenharmony_ciint32_t RingtoneScannerObj::GetFileMetadata() 4143e5483f6Sopenharmony_ci{ 4153e5483f6Sopenharmony_ci if (path_.empty()) { 4163e5483f6Sopenharmony_ci return E_INVALID_ARGUMENTS; 4173e5483f6Sopenharmony_ci } 4183e5483f6Sopenharmony_ci struct stat statInfo = { 0 }; 4193e5483f6Sopenharmony_ci if (stat(path_.c_str(), &statInfo) != 0) { 4203e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("stat syscall err %{public}d", errno); 4213e5483f6Sopenharmony_ci return E_SYSCALL; 4223e5483f6Sopenharmony_ci } 4233e5483f6Sopenharmony_ci 4243e5483f6Sopenharmony_ci int errCode = 0; 4253e5483f6Sopenharmony_ci if (isVibrateFile_) { 4263e5483f6Sopenharmony_ci errCode = BuildVibrateData(statInfo); 4273e5483f6Sopenharmony_ci if (errCode != E_OK) { 4283e5483f6Sopenharmony_ci return errCode; 4293e5483f6Sopenharmony_ci } 4303e5483f6Sopenharmony_ci } else { 4313e5483f6Sopenharmony_ci errCode = BuildData(statInfo); 4323e5483f6Sopenharmony_ci if (errCode != E_OK) { 4333e5483f6Sopenharmony_ci return errCode; 4343e5483f6Sopenharmony_ci } 4353e5483f6Sopenharmony_ci } 4363e5483f6Sopenharmony_ci 4373e5483f6Sopenharmony_ci return E_OK; 4383e5483f6Sopenharmony_ci} 4393e5483f6Sopenharmony_ci 4403e5483f6Sopenharmony_ciint32_t RingtoneScannerObj::BuildFileInfo() 4413e5483f6Sopenharmony_ci{ 4423e5483f6Sopenharmony_ci auto err = GetMediaInfo(); 4433e5483f6Sopenharmony_ci if (err != E_OK) { 4443e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("failed to get media info"); 4453e5483f6Sopenharmony_ci } 4463e5483f6Sopenharmony_ci 4473e5483f6Sopenharmony_ci err = AddToTransaction(); 4483e5483f6Sopenharmony_ci if (err != E_OK) { 4493e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("failed to add to transaction err %{public}d", err); 4503e5483f6Sopenharmony_ci return err; 4513e5483f6Sopenharmony_ci } 4523e5483f6Sopenharmony_ci 4533e5483f6Sopenharmony_ci return E_OK; 4543e5483f6Sopenharmony_ci} 4553e5483f6Sopenharmony_ci 4563e5483f6Sopenharmony_ciint32_t RingtoneScannerObj::AddToTransaction() 4573e5483f6Sopenharmony_ci{ 4583e5483f6Sopenharmony_ci if (isVibrateFile_) { 4593e5483f6Sopenharmony_ci vibrateDataBuffer_.emplace_back(move(vibrateData_)); 4603e5483f6Sopenharmony_ci if (vibrateDataBuffer_.size() >= MAX_BATCH_SIZE) { 4613e5483f6Sopenharmony_ci return CommitVibrateTransaction(); 4623e5483f6Sopenharmony_ci } 4633e5483f6Sopenharmony_ci } else { 4643e5483f6Sopenharmony_ci dataBuffer_.emplace_back(move(data_)); 4653e5483f6Sopenharmony_ci if (dataBuffer_.size() >= MAX_BATCH_SIZE) { 4663e5483f6Sopenharmony_ci return CommitTransaction(); 4673e5483f6Sopenharmony_ci } 4683e5483f6Sopenharmony_ci } 4693e5483f6Sopenharmony_ci 4703e5483f6Sopenharmony_ci return E_OK; 4713e5483f6Sopenharmony_ci} 4723e5483f6Sopenharmony_ci 4733e5483f6Sopenharmony_ciint32_t RingtoneScannerObj::GetMediaInfo() 4743e5483f6Sopenharmony_ci{ 4753e5483f6Sopenharmony_ci#ifdef ENABLE_METADATA_EXTRACTOR 4763e5483f6Sopenharmony_ci auto pos = data_->GetMimeType().find_first_of("/"); 4773e5483f6Sopenharmony_ci std::string mimePrefix = data_->GetMimeType().substr(0, pos) + "/*"; 4783e5483f6Sopenharmony_ci if (find(EXTRACTOR_SUPPORTED_MIME.begin(), EXTRACTOR_SUPPORTED_MIME.end(), mimePrefix) != 4793e5483f6Sopenharmony_ci EXTRACTOR_SUPPORTED_MIME.end()) { 4803e5483f6Sopenharmony_ci return RingtoneMetadataExtractor::Extract(data_); 4813e5483f6Sopenharmony_ci } 4823e5483f6Sopenharmony_ci#endif // ENABLE_METADATA_EXTRACTOR 4833e5483f6Sopenharmony_ci return E_OK; 4843e5483f6Sopenharmony_ci} 4853e5483f6Sopenharmony_ci 4863e5483f6Sopenharmony_ciint32_t RingtoneScannerObj::BuildData(const struct stat &statInfo) 4873e5483f6Sopenharmony_ci{ 4883e5483f6Sopenharmony_ci data_ = make_unique<RingtoneMetadata>(); 4893e5483f6Sopenharmony_ci if (data_ == nullptr) { 4903e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("failed to make unique ptr for metadata"); 4913e5483f6Sopenharmony_ci return E_DATA; 4923e5483f6Sopenharmony_ci } 4933e5483f6Sopenharmony_ci 4943e5483f6Sopenharmony_ci if (S_ISDIR(statInfo.st_mode)) { 4953e5483f6Sopenharmony_ci return E_INVALID_ARGUMENTS; 4963e5483f6Sopenharmony_ci } 4973e5483f6Sopenharmony_ci 4983e5483f6Sopenharmony_ci int32_t err = RingtoneScannerDb::GetFileBasicInfo(path_, data_); 4993e5483f6Sopenharmony_ci if (err != E_OK) { 5003e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("failed to get file basic info"); 5013e5483f6Sopenharmony_ci return err; 5023e5483f6Sopenharmony_ci } 5033e5483f6Sopenharmony_ci 5043e5483f6Sopenharmony_ci for (const auto& pair : g_typeMap) { 5053e5483f6Sopenharmony_ci if (path_.find(pair.first) == 0) { 5063e5483f6Sopenharmony_ci data_->SetSourceType(pair.second.first); 5073e5483f6Sopenharmony_ci data_->SetToneType(pair.second.second); 5083e5483f6Sopenharmony_ci } 5093e5483f6Sopenharmony_ci } 5103e5483f6Sopenharmony_ci 5113e5483f6Sopenharmony_ci // file path 5123e5483f6Sopenharmony_ci data_->SetData(path_); 5133e5483f6Sopenharmony_ci auto dispName = RingtoneScannerUtils::GetFileNameFromUri(path_); 5143e5483f6Sopenharmony_ci data_->SetDisplayName(dispName); 5153e5483f6Sopenharmony_ci if (data_->GetTitle() == TITLE_DEFAULT) { 5163e5483f6Sopenharmony_ci data_->SetTitle(RingtoneScannerUtils::GetFileTitle(data_->GetDisplayName())); 5173e5483f6Sopenharmony_ci } 5183e5483f6Sopenharmony_ci 5193e5483f6Sopenharmony_ci // statinfo 5203e5483f6Sopenharmony_ci data_->SetSize(statInfo.st_size); 5213e5483f6Sopenharmony_ci data_->SetDateModified(static_cast<int64_t>(RingtoneFileUtils::Timespec2Millisecond(statInfo.st_mtim))); 5223e5483f6Sopenharmony_ci 5233e5483f6Sopenharmony_ci // extension and type 5243e5483f6Sopenharmony_ci std::string extension = RingtoneScannerUtils::GetFileExtension(path_); 5253e5483f6Sopenharmony_ci std::string mimeType = RingtoneMimeTypeUtils::GetMimeTypeFromExtension(extension); 5263e5483f6Sopenharmony_ci data_->SetMimeType(mimeType); 5273e5483f6Sopenharmony_ci int32_t mime = RingtoneMimeTypeUtils::GetMediaTypeFromMimeType(mimeType); 5283e5483f6Sopenharmony_ci data_->SetMediaType(mime); 5293e5483f6Sopenharmony_ci 5303e5483f6Sopenharmony_ci return E_OK; 5313e5483f6Sopenharmony_ci} 5323e5483f6Sopenharmony_ci 5333e5483f6Sopenharmony_ciint32_t RingtoneScannerObj::BuildVibrateData(const struct stat &statInfo) 5343e5483f6Sopenharmony_ci{ 5353e5483f6Sopenharmony_ci vibrateData_ = make_unique<VibrateMetadata>(); 5363e5483f6Sopenharmony_ci if (vibrateData_ == nullptr) { 5373e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("failed to make unique ptr for metadata"); 5383e5483f6Sopenharmony_ci return E_DATA; 5393e5483f6Sopenharmony_ci } 5403e5483f6Sopenharmony_ci 5413e5483f6Sopenharmony_ci if (S_ISDIR(statInfo.st_mode)) { 5423e5483f6Sopenharmony_ci return E_INVALID_ARGUMENTS; 5433e5483f6Sopenharmony_ci } 5443e5483f6Sopenharmony_ci 5453e5483f6Sopenharmony_ci int32_t err = RingtoneScannerDb::GetVibrateFileBasicInfo(path_, vibrateData_); 5463e5483f6Sopenharmony_ci if (err != E_OK) { 5473e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("failed to get file basic info"); 5483e5483f6Sopenharmony_ci return err; 5493e5483f6Sopenharmony_ci } 5503e5483f6Sopenharmony_ci 5513e5483f6Sopenharmony_ci for (const auto &pair : g_vibrateTypeMap) { 5523e5483f6Sopenharmony_ci if (path_.find(pair.first) == 0) { 5533e5483f6Sopenharmony_ci vibrateData_->SetSourceType(pair.second.first); 5543e5483f6Sopenharmony_ci vibrateData_->SetVibrateType(pair.second.second); 5553e5483f6Sopenharmony_ci int32_t ntype = 0; 5563e5483f6Sopenharmony_ci if (pair.second.second == VIBRATE_TYPE_STANDARD) { 5573e5483f6Sopenharmony_ci ntype = (path_.find(ALARMS_TYPE) != string::npos) ? VIBRATE_TYPE_SALARM : VIBRATE_TYPE_STANDARD; 5583e5483f6Sopenharmony_ci ntype = (path_.find(RINGTONES_TYPE) != string::npos) ? VIBRATE_TYPE_SRINGTONE : ntype; 5593e5483f6Sopenharmony_ci ntype = (path_.find(NOTIFICATIONS_TYPE) != string::npos) ? \ 5603e5483f6Sopenharmony_ci VIBRATE_TYPE_SNOTIFICATION : ntype; 5613e5483f6Sopenharmony_ci vibrateData_->SetVibrateType(ntype); 5623e5483f6Sopenharmony_ci } else { 5633e5483f6Sopenharmony_ci ntype = (path_.find(ALARMS_TYPE) != string::npos) ? VIBRATE_TYPE_GALARM : VIBRATE_TYPE_GENTLE; 5643e5483f6Sopenharmony_ci ntype = (path_.find(RINGTONES_TYPE) != string::npos) ? VIBRATE_TYPE_GRINGTONE : ntype; 5653e5483f6Sopenharmony_ci ntype = (path_.find(NOTIFICATIONS_TYPE) != string::npos) ? \ 5663e5483f6Sopenharmony_ci VIBRATE_TYPE_GNOTIFICATION : ntype; 5673e5483f6Sopenharmony_ci vibrateData_->SetVibrateType(ntype); 5683e5483f6Sopenharmony_ci } 5693e5483f6Sopenharmony_ci } 5703e5483f6Sopenharmony_ci } 5713e5483f6Sopenharmony_ci 5723e5483f6Sopenharmony_ci for (const auto &pair : g_vibratePlayModeMap) { 5733e5483f6Sopenharmony_ci if (path_.find(pair.first) == 0) { 5743e5483f6Sopenharmony_ci vibrateData_->SetPlayMode(pair.second.second); 5753e5483f6Sopenharmony_ci } 5763e5483f6Sopenharmony_ci } 5773e5483f6Sopenharmony_ci 5783e5483f6Sopenharmony_ci // file path 5793e5483f6Sopenharmony_ci vibrateData_->SetData(path_); 5803e5483f6Sopenharmony_ci auto dispName = RingtoneScannerUtils::GetFileNameFromUri(path_); 5813e5483f6Sopenharmony_ci vibrateData_->SetDisplayName(dispName); 5823e5483f6Sopenharmony_ci if (vibrateData_->GetTitle() == TITLE_DEFAULT) { 5833e5483f6Sopenharmony_ci vibrateData_->SetTitle(RingtoneScannerUtils::GetFileTitle(vibrateData_->GetDisplayName())); 5843e5483f6Sopenharmony_ci } 5853e5483f6Sopenharmony_ci 5863e5483f6Sopenharmony_ci // statinfo 5873e5483f6Sopenharmony_ci vibrateData_->SetSize(statInfo.st_size); 5883e5483f6Sopenharmony_ci vibrateData_->SetDateModified(static_cast<int64_t>(RingtoneFileUtils::Timespec2Millisecond(statInfo.st_mtim))); 5893e5483f6Sopenharmony_ci 5903e5483f6Sopenharmony_ci return E_OK; 5913e5483f6Sopenharmony_ci} 5923e5483f6Sopenharmony_ci 5933e5483f6Sopenharmony_ciint32_t RingtoneScannerObj::ScanFileInternal() 5943e5483f6Sopenharmony_ci{ 5953e5483f6Sopenharmony_ci if (RingtoneScannerUtils::IsFileHidden(path_)) { 5963e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("the file is hidden"); 5973e5483f6Sopenharmony_ci return E_FILE_HIDDEN; 5983e5483f6Sopenharmony_ci } 5993e5483f6Sopenharmony_ci 6003e5483f6Sopenharmony_ci bool flag = (path_.find(ROOT_VIBRATE_PRELOAD_PATH_NOAH_PATH) != std::string::npos) ? true : false; 6013e5483f6Sopenharmony_ci flag |= (path_.find(ROOT_VIBRATE_PRELOAD_PATH_CHINA_PATH) != std::string::npos); 6023e5483f6Sopenharmony_ci flag |= (path_.find(ROOT_VIBRATE_PRELOAD_PATH_OVERSEA_PATH) != std::string::npos); 6033e5483f6Sopenharmony_ci std::string extension = RingtoneScannerUtils::GetFileExtension(path_); 6043e5483f6Sopenharmony_ci 6053e5483f6Sopenharmony_ci if (flag) { 6063e5483f6Sopenharmony_ci if (extension.compare("json") == 0) { 6073e5483f6Sopenharmony_ci isVibrateFile_ = true; 6083e5483f6Sopenharmony_ci return ScanVibrateFile(); 6093e5483f6Sopenharmony_ci } 6103e5483f6Sopenharmony_ci return E_INVALID_PATH; 6113e5483f6Sopenharmony_ci } 6123e5483f6Sopenharmony_ci 6133e5483f6Sopenharmony_ci int32_t err = GetFileMetadata(); 6143e5483f6Sopenharmony_ci if (err != E_OK) { 6153e5483f6Sopenharmony_ci if (err != E_SCANNED) { 6163e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("failed to get file metadata"); 6173e5483f6Sopenharmony_ci } 6183e5483f6Sopenharmony_ci return err; 6193e5483f6Sopenharmony_ci } 6203e5483f6Sopenharmony_ci err = GetMediaInfo(); 6213e5483f6Sopenharmony_ci if (err != E_OK) { 6223e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("failed to get ringtone info"); 6233e5483f6Sopenharmony_ci } 6243e5483f6Sopenharmony_ci 6253e5483f6Sopenharmony_ci err = Commit(); 6263e5483f6Sopenharmony_ci if (err != E_OK) { 6273e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("failed to commit err %{public}d", err); 6283e5483f6Sopenharmony_ci return err; 6293e5483f6Sopenharmony_ci } 6303e5483f6Sopenharmony_ci 6313e5483f6Sopenharmony_ci return E_OK; 6323e5483f6Sopenharmony_ci} 6333e5483f6Sopenharmony_ci 6343e5483f6Sopenharmony_ciint32_t RingtoneScannerObj::ScanVibrateFile() 6353e5483f6Sopenharmony_ci{ 6363e5483f6Sopenharmony_ci int32_t err = GetFileMetadata(); 6373e5483f6Sopenharmony_ci if (err != E_OK) { 6383e5483f6Sopenharmony_ci if (err != E_SCANNED) { 6393e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("failed to get vibrate file metadata"); 6403e5483f6Sopenharmony_ci } 6413e5483f6Sopenharmony_ci isVibrateFile_ = false; 6423e5483f6Sopenharmony_ci return err; 6433e5483f6Sopenharmony_ci } 6443e5483f6Sopenharmony_ci 6453e5483f6Sopenharmony_ci if (type_ == FILE) { 6463e5483f6Sopenharmony_ci err = Commit(); 6473e5483f6Sopenharmony_ci if (err != E_OK) { 6483e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("failed to commit err %{public}d", err); 6493e5483f6Sopenharmony_ci isVibrateFile_ = false; 6503e5483f6Sopenharmony_ci return err; 6513e5483f6Sopenharmony_ci } 6523e5483f6Sopenharmony_ci } else { 6533e5483f6Sopenharmony_ci err = AddToTransaction(); 6543e5483f6Sopenharmony_ci if (err != E_OK) { 6553e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("failed to add to transaction err %{public}d", err); 6563e5483f6Sopenharmony_ci isVibrateFile_ = false; 6573e5483f6Sopenharmony_ci return err; 6583e5483f6Sopenharmony_ci } 6593e5483f6Sopenharmony_ci } 6603e5483f6Sopenharmony_ci 6613e5483f6Sopenharmony_ci isVibrateFile_ = false; 6623e5483f6Sopenharmony_ci return E_OK; 6633e5483f6Sopenharmony_ci} 6643e5483f6Sopenharmony_ci 6653e5483f6Sopenharmony_ciint32_t RingtoneScannerObj::Commit() 6663e5483f6Sopenharmony_ci{ 6673e5483f6Sopenharmony_ci std::string tab = RINGTONE_TABLE; 6683e5483f6Sopenharmony_ci 6693e5483f6Sopenharmony_ci if (isVibrateFile_) { 6703e5483f6Sopenharmony_ci tab = VIBRATE_TABLE; 6713e5483f6Sopenharmony_ci 6723e5483f6Sopenharmony_ci if (vibrateData_->GetVibrateId() != FILE_ID_DEFAULT) { 6733e5483f6Sopenharmony_ci uri_ = RingtoneScannerDb::UpdateVibrateMetadata(*vibrateData_, tab); 6743e5483f6Sopenharmony_ci } else { 6753e5483f6Sopenharmony_ci uri_ = RingtoneScannerDb::InsertVibrateMetadata(*vibrateData_, tab); 6763e5483f6Sopenharmony_ci } 6773e5483f6Sopenharmony_ci } else { 6783e5483f6Sopenharmony_ci if (data_->GetToneId() != FILE_ID_DEFAULT) { 6793e5483f6Sopenharmony_ci uri_ = RingtoneScannerDb::UpdateMetadata(*data_, tab); 6803e5483f6Sopenharmony_ci } else { 6813e5483f6Sopenharmony_ci uri_ = RingtoneScannerDb::InsertMetadata(*data_, tab); 6823e5483f6Sopenharmony_ci } 6833e5483f6Sopenharmony_ci } 6843e5483f6Sopenharmony_ci 6853e5483f6Sopenharmony_ci return E_OK; 6863e5483f6Sopenharmony_ci} 6873e5483f6Sopenharmony_ci} // namespace Media 6883e5483f6Sopenharmony_ci} // namespace OHOS 689