目录

File Open Boost (C/C++)

Starting from version 5.0.3(15), the file open boost function has been added. Preview Kit provides APIs for registration and unregistration, and allows apps to register a series of callback functions. The file open boost function recommends files to apps for preloading by calling the callback API.

Starting from version 26.0.0, a mechanism has been introduced during the file open boost process, enabling apps to scan a file and synchronize the scanning result with the file open boost function to determine whether the file qualifies for boosting.

Starting from version 26.0.0, a dynamic parameter configuration feature has been added, allowing apps to customize file preloading policies (such as requirements for file extensions and size ranges) based on their specific needs. Additionally, the system now supports querying the availability of the file open boost function and reporting file operation events, which helps improve preloading accuracy.

API Description

For details about the APIs, please refer to the API document of file open boost.

Table 1 File preloading APIs (C API)

API Description
bool HMS_Preview_FileBoost_IsSupported (void) Checks whether the current device supports file open boost. It is recommended that you call this API to check whether the current device supports file open boost before using the file open boost function. After confirming that the device supports this function, you can use other file open boost APIs, such as HMS_OpenFileBoost_RegisterFilePreload and HMS_Preview_FileScanBoost_RegisterFileScan.

OpenFileBoost_ErrCode HMS_OpenFileBoost_RegisterFilePreload(

HMS_OpenFileBoost_QueryAppState queryAppState,

HMS_OpenFileBoost_OnFilePreload filePreload,

HMS_OpenFileBoost_OnFilePreload cancelFilePreload);

Registers the file preloading callback with the system.
typedef OpenFileBoost_AppState (*HMS_OpenFileBoost_QueryAppState)(void); Called when the system queries the app status.
typedef OpenFileBoost_CbErrCode (*HMS_OpenFileBoost_OnFilePreload)(void* fileInfo); Called when the system recommends or cancels the recommendation of a preloaded file to an app.

OpenFileBoost_ErrCode HMS_OpenFileBoost_GetFdFromPreloadFileInfo(

void* fileInfo, int32_t* fd);

Called when the app obtains the file descriptor information in the callback context for recommending or canceling the recommendation of a preloaded file to the app.

OpenFileBoost_ErrCode HMS_OpenFileBoost_GetSandboxPathFromPreloadFileInfo(

void* fileInfo, char* sandboxPath, int32_t pathLen);

Called when the app obtains the file sandbox path information in the callback context for recommending or canceling the recommendation of a preloaded file to the app.
OpenFileBoost_ErrCode HMS_OpenFileBoost_UnregisterFilePreload(void); Unregisters the preloading callback.

OpenFileBoost_ErrCode HMS_OpenFileBoost_NotifyPreloadHit(

int32_t fd, char* sandboxPath, int32_t pathLen);

Called to notify the system of the preloading hit when a user opens a preloaded file. This helps improve the prediction accuracy of file preloading.
FileScanBoost_ErrCode HMS_Preview_FileScanBoost_ScanOptionCreate (FileScanBoost_ScanOption **outOption) Creates a FileScanBoost_ScanOption instance.
void HMS_Preview_FileScanBoost_ScanOptionDestroy (FileScanBoost_ScanOption *option) Destroys a FileScanBoost_ScanOption instance.
FileScanBoost_ErrCode HMS_Preview_FileScanBoost_ScanOptionAddSupportFile (FileScanBoost_ScanOption *option, const char *suffix, uint32_t suffixLen) Adds file types supported by an app to the scanning option.
FileScanBoost_ErrCode HMS_Preview_FileScanBoost_ScanResultCreate (FileScanBoost_ScanResult **outResult) Creates a FileScanBoost_ScanResult instance.
void HMS_Preview_FileScanBoost_ScanResultDestroy (FileScanBoost_ScanResult *result) Destroys a FileScanBoost_ScanResult instance.
FileScanBoost_ErrCode HMS_Preview_FileScanBoost_ScanResultSetState (FileScanBoost_ScanResult *result, FileScanBoost_ScanState state) Sets the scanning status in the result.
FileScanBoost_ErrCode HMS_Preview_FileScanBoost_ScanResultSetMaxAtomicTime (FileScanBoost_ScanResult *result, int64_t maxAtomicTime) Sets the maximum atomic time in the result.
FileScanBoost_ErrCode HMS_Preview_FileScanBoost_ScanResultSetMemSize (FileScanBoost_ScanResult *result, int64_t memSize) Sets the memory size in the result.
FileScanBoost_ErrCode HMS_Preview_FileScanBoost_RegisterFileScan (HMS_Preview_FileScanBoost_OnFileScan fileScanCb, FileScanBoost_ScanOption *option) Registers a callback for multiple file types using extension-based filtering. Do not re-register before the previous registration is unregistered. Duplicate registrations will return the error code FILE_SCAN_BOOST_ERROR_ALREADY_REGISTERED, and only the information from the initial registration will take effect.
FileScanBoost_ErrCode HMS_Preview_FileScanBoost_UnregisterFileScan (void) Unregisters the registered file scanning callback.
FileScanBoost_ErrCode HMS_Preview_FileScanBoost_ReportScanResult (const char *path, uint32_t pathLen, FileScanBoost_ScanResult *result) Reports the file scanning result.
OpenFileBoost_ErrCode HMS_Preview_OpenFileBoost_SupportFileCreate (const char *suffix, uint32_t suffixLen, uint64_t lowerLimitKb, uint64_t upperLimitKb, OpenFileBoost_SupportFile **outSupportFile) Creates an OpenFileBoost_SupportFile object.
OpenFileBoost_ErrCode HMS_Preview_OpenFileBoost_SupportFileDestroy (OpenFileBoost_SupportFile *supportFile) Destroys an OpenFileBoost_SupportFile object.
OpenFileBoost_ErrCode HMS_Preview_OpenFileBoost_OptionsCreate (OpenFileBoost_Options **outOptions) Creates an empty OpenFileBoost_Options object. Call HMS_Preview_OpenFileBoost_OptionsAddSupportFile to add a file.
OpenFileBoost_ErrCode HMS_Preview_OpenFileBoost_OptionsAddSupportFile (OpenFileBoost_Options *options, const OpenFileBoost_SupportFile *supportFile) Adds a list of file types that support preloading to OpenFileBoost_Options.
OpenFileBoost_ErrCode HMS_Preview_OpenFileBoost_OptionsDestroy (OpenFileBoost_Options *options) Destroys an OpenFileBoost_Options object.
OpenFileBoost_ErrCode HMS_Preview_OpenFileBoost_RegisterFilePreloadWithOption (HMS_OpenFileBoost_QueryAppState queryAppState, HMS_OpenFileBoost_OnFilePreload filePreload, HMS_OpenFileBoost_OnFilePreload cancelFilePreload, OpenFileBoost_Options *options) Registers a preload callback, which allows an app to pass file information that supports preloading.
bool HMS_Preview_OpenFileBoost_IsEnabled (void) Checks whether the file open boost function is enabled for the app.
OpenFileBoost_ErrCode HMS_Preview_OpenFileBoost_FileOperationInfoCreate (const char *path, uint32_t pathLen, const char *operation, uint32_t operationLen, OpenFileBoost_FileOperationInfo **outFileOperationInfo) Creates an OpenFileBoost_FileOperationInfo object.
OpenFileBoost_ErrCode HMS_Preview_OpenFileBoost_FileOperationInfoDestroy (OpenFileBoost_FileOperationInfo *fileOperationInfo) Destroys an OpenFileBoost_FileOperationInfo object.
OpenFileBoost_ErrCode HMS_Preview_OpenFileBoost_NotifyFileOperation (OpenFileBoost_FileOperationInfo *fileOperationInfo) Called to notify the system of the file operation type when a user performs an operation on a file. This helps improve the prediction accuracy of file preloading.

Constraints

Supported device types: PCs/2-in-1 devices. Starting from version 26.0.0, tablets are also supported.

Development Procedure

  1. Apply for the permission required for the file opening acceleration service. Add the file preloading permission to the module.json5 file. Note that the ohos.permission.PRELOAD_FILE permission is a restricted permission. For details, see Requesting Restricted Permissions.

    "requestPermissions": [
      {
        "name": "ohos.permission.PRELOAD_FILE"
      }
    ],
  2. Add the corresponding header file.

    #include "PreviewKit/open_file_boost.h"
  3. Compile CMakeLists.txt and add the dependency on the file open boost function.

    find_library(
        OPEN_FILE_BOOST_LIBRARY
        NAMES libopen_file_boost.so
        PATHS ${CMAKE_FIND_ROOT_PATH}/lib/aarch64-linux-ohos
    )
    # ...
    target_link_libraries(entry PUBLIC ${OPEN_FILE_BOOST_LIBRARY})
  4. Register the file preloading callback. The app can pass the information about the files that support preloading. After the registration, the system calls the callback function to recommend files to an app when conditions are met.

    static napi_value RegisterPreload(napi_env env, napi_callback_info info) {
        napi_value resp;
        napi_create_int32(env, 0, &resp);
        OH_LOG_INFO(LOG_APP, "Register file preload start.");
        // Create options.
        OpenFileBoost_Options *options = nullptr;
        auto createCode = HMS_Preview_OpenFileBoost_OptionsCreate(&options);
        if (createCode != OPEN_FILE_BOOST_SUCCESS) {
            // Customize error handling.
            OH_LOG_ERROR(LOG_APP, "Failed to create Options, code: %{public}d", createCode);
            napi_create_int32(env, -1, &resp);
            return resp;
        }
        // Create a file type.
        OpenFileBoost_SupportFile *supportFile = nullptr;
        auto supportCode = HMS_Preview_OpenFileBoost_SupportFileCreate("xlsx", 4, 0, 100 * 1024 * 1024, &supportFile);
        if (supportCode != OPEN_FILE_BOOST_SUCCESS) {
            // Customize error handling.
            OH_LOG_ERROR(LOG_APP, "Failed to create SupportFile, code: %{public}d", supportCode);
            // Destroy the previously created object.
            HMS_Preview_OpenFileBoost_OptionsDestroy(options);
            napi_create_int32(env, -2, &resp);
            return resp;
        }
        // Add the file type to the options.
        auto addCode = HMS_Preview_OpenFileBoost_OptionsAddSupportFile(options, supportFile);
        HMS_Preview_OpenFileBoost_SupportFileDestroy(supportFile);
    
        if (addCode != OPEN_FILE_BOOST_SUCCESS) {
            // Adding fails. The app can customize error handling.
            OH_LOG_ERROR(LOG_APP, "Failed to add support file, code: %{public}d", addCode);
            HMS_Preview_OpenFileBoost_OptionsDestroy(options);
            napi_create_int32(env, -3, &resp);
            return resp;
        }
        // Register a file preload callback. The app passes the supported file name extensions and file size limits.
        auto code = HMS_Preview_OpenFileBoost_RegisterFilePreloadWithOption(AppQueryAppStateCb, AppOnFilePreload,
                                                                            AppCancelFilePreload, options);
        HMS_Preview_OpenFileBoost_OptionsDestroy(options);
    
        if (code != OPEN_FILE_BOOST_SUCCESS) {
            // Registration fails. You can customize error handling.
            OH_LOG_ERROR(LOG_APP, "Register failed, ret :%{public}d", code);
            napi_create_int32(env, code, &resp);
            return resp;
        }
    
        OH_LOG_INFO(LOG_APP, "Register file preload successfully.");
        napi_create_int32(env, 0, &resp);
        return resp;
    }
  5. Synchronously parse the preloaded file in the current callback context, or wait until the parsing is complete before returning the result. In this way, the system can evaluate the resource consumption of the preloaded file.

    // Callback for querying the current app status. Before recommending a file to an app, the system calls the status query callback function to query whether the app is suitable for file recommendation.
    OpenFileBoost_AppState AppQueryAppStateCb(void) {
        // If file preloading is allowed, OPEN_FILE_BOOST_APP_STATE_ALLOW_PRELOAD is returned. state is an example. Actual code that determines whether to allow file preloading may vary.
        int state = ConfigManager::getInstance().getAppState();
        OH_LOG_INFO(LOG_APP, "AppQueryAppStateCb, return state: %{public}d", state);
        return static_cast<OpenFileBoost_AppState>(state);
    }
    
    // File preloading callback.
    OpenFileBoost_CbErrCode AppOnFilePreload(void *fileInfo) {
        OH_LOG_INFO(LOG_APP, "AppOnFilePreload called");
    
        int32_t fileDescriptor = 0;
        // The fileInfo pointer is valid only in the current callback context. Call HMS_OpenFileBoost_GetFdFromPreloadFileInfo in the callback to obtain the file descriptor.
        OpenFileBoost_ErrCode ret = HMS_OpenFileBoost_GetFdFromPreloadFileInfo(fileInfo, &fileDescriptor);
        if (ret != OPEN_FILE_BOOST_SUCCESS) {
            return OPEN_FILE_BOOST_CALLBACK_FAILURE;
        }
    
        char sandboxPath[MAX_BUFFER_LENGTH] = {0};
        // The fileInfo pointer is valid only in the current callback context. Call HMS_OpenFileBoost_GetSandboxPathFromPreloadFileInfo in the callback to obtain the file path.
        ret = HMS_OpenFileBoost_GetSandboxPathFromPreloadFileInfo(fileInfo, sandboxPath, MAX_BUFFER_LENGTH);
        if (ret != OPEN_FILE_BOOST_SUCCESS) {
            return OPEN_FILE_BOOST_CALLBACK_FAILURE;
        }
    
        OH_LOG_INFO(LOG_APP, "Preload file: fd=%{public}d, path=%{public}s", fileDescriptor, sandboxPath);
        // You can save the file descriptor and file path so that the file preloading can be canceled when receiving a cancellation notification.
        // You can customize the logic for file preloading.
        {
            std::lock_guard<std::mutex> lock(g_preloadMutex);
            g_preloadTasks[std::string(sandboxPath)] = fileDescriptor;
        }
        ConfigManager::getInstance().addFileProcessTask(std::string(sandboxPath), fileDescriptor);
    
        int callbackResult = ConfigManager::getInstance().getPreloadCallbackResult();
        OH_LOG_INFO(LOG_APP, "AppOnFilePreload returning: %{public}d", callbackResult);
        return static_cast<OpenFileBoost_CbErrCode>(callbackResult);
    }
    
    // Callback for canceling preloading.
    OpenFileBoost_CbErrCode AppCancelFilePreload(void *fileInfo) {
        OH_LOG_INFO(LOG_APP, "AppCancelFilePreload called");
        // Obtain the file descriptor and sandbox path in the same way.
        int32_t fileDescriptor = 0;
        OpenFileBoost_ErrCode ret = HMS_OpenFileBoost_GetFdFromPreloadFileInfo(fileInfo, &fileDescriptor);
        if (ret != OPEN_FILE_BOOST_SUCCESS) {
            return OPEN_FILE_BOOST_CALLBACK_FAILURE;
        }
    
        char sandboxPath[MAX_BUFFER_LENGTH] = {0};
        ret = HMS_OpenFileBoost_GetSandboxPathFromPreloadFileInfo(fileInfo, sandboxPath, MAX_BUFFER_LENGTH);
        if (ret != OPEN_FILE_BOOST_SUCCESS) {
            return OPEN_FILE_BOOST_CALLBACK_FAILURE;
        }
        // You can customize the logic for canceling file preloading.
        OH_LOG_INFO(LOG_APP, "Cancel preload: fd=%{public}d, path=%{public}s", fileDescriptor, sandboxPath);
    
        std::string path(sandboxPath);
        std::lock_guard<std::mutex> lock(g_preloadMutex);
        auto it = g_preloadTasks.find(path);
        if (it != g_preloadTasks.end()) {
            g_preloadTasks.erase(it);
        }
    
        return OPEN_FILE_BOOST_CALLBACK_SUCCESS;
    }
  6. If you open a preloaded file, the app needs to call HMS_OpenFileBoost_NotifyPreloadHit to notify the system. This helps the system adjust the preloading status of the file.

    static napi_value NotifyPreloadHit(napi_env env, napi_callback_info info) {
        napi_value resp;
    
        std::lock_guard<std::mutex> lock(g_preloadMutex);
        if (g_preloadTasks.empty()) {
            OH_LOG_INFO(LOG_APP, "No preload tasks to notify");
            napi_create_int32(env, 0, &resp);
            return resp;
        }
    
        int notifyCount = 0;
        for (auto it = g_preloadTasks.begin(); it != g_preloadTasks.end(); ++it) {
            std::string notifyPath = it->first;
            std::vector<char> pathBuffer(notifyPath.size() + 1);
            std::copy(notifyPath.begin(), notifyPath.end(), pathBuffer.begin());
            pathBuffer[notifyPath.size()] = '\0';
            int fd = it->second;
            // Pass the descriptor, path, and length of the preloaded file you opened.
            OpenFileBoost_ErrCode ret = HMS_OpenFileBoost_NotifyPreloadHit(
                fd, pathBuffer.data(),static_cast<int32_t>(notifyPath.size()));
            if (ret == OPEN_FILE_BOOST_SUCCESS) {
                OH_LOG_INFO(LOG_APP, "Notify file(%{public}s) hit successfully.", notifyPath.c_str());
                notifyCount++;
            } else {
                // Notification fails. You can customize error handling.
                OH_LOG_ERROR(LOG_APP, "Notify file(%{public}s) failed, ret :%{public}d", notifyPath.c_str(), ret);
            }
        }
    
        g_preloadTasks.clear();
        OH_LOG_INFO(LOG_APP, "Notify %{public}d files hit successfully.", notifyCount);
        napi_create_int32(env, 0, &resp);
        return resp;
    }
  7. Query the system to check whether the boost function is enabled.

    static napi_value IsAccelerateEnabled(napi_env env, napi_callback_info info) {
        napi_value resp;
    
        bool isEnabled = HMS_Preview_OpenFileBoost_IsEnabled();
        if (isEnabled) {
            // The boost function is enabled. Your app can process the boost-related business logic.
            OH_LOG_INFO(LOG_APP, "Accelerate Enabled ");
        } else {
            // The boost function is disabled. Your app can process its own business logic.
            OH_LOG_INFO(LOG_APP, "Accelerate not Enabled");
        }
    
        napi_get_boolean(env, isEnabled, &resp);
        return resp;
    }
  8. Send the user's operation on the file to the system.

    static napi_value NotifyFileOperation(napi_env env, napi_callback_info info) {
        napi_value resp;
        napi_create_int32(env, 0, &resp);
    
        size_t argc = 2;
        napi_value args[2] = {nullptr};
        napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    
        char filePath[MAX_BUFFER_LENGTH] = {0};
        char operationType[MAX_BUFFER_LENGTH] = {0};
    
        GetCharString(env, args[0], filePath);
        GetCharString(env, args[1], operationType);
    
        OH_LOG_INFO(LOG_APP, "NotifyFileOperation: path=%{public}s, operation=%{public}s", filePath, operationType);
        // Create file operation information.
        OpenFileBoost_FileOperationInfo *fileOpInfo = nullptr;
        OpenFileBoost_ErrCode ret = HMS_Preview_OpenFileBoost_FileOperationInfoCreate(
            filePath, static_cast<uint32_t>(strlen(filePath)), operationType, static_cast<uint32_t>(strlen(operationType)),
            &fileOpInfo);
    
        if (ret != OPEN_FILE_BOOST_SUCCESS) {
            // Creation fails. You can customize error handling.
            OH_LOG_ERROR(LOG_APP, "Failed to create FileOperationInfo, code: %{public}d", ret);
            napi_create_int32(env, ret, &resp);
            return resp;
        }
    
        ret = HMS_Preview_OpenFileBoost_NotifyFileOperation(fileOpInfo);
        if (ret != OPEN_FILE_BOOST_SUCCESS) {
            // Transfer fails. You can customize error handling.
            OH_LOG_ERROR(LOG_APP, "NotifyFileOperation failed, ret :%{public}d", ret);
            HMS_Preview_OpenFileBoost_FileOperationInfoDestroy(fileOpInfo);
            napi_create_int32(env, ret, &resp);
            return resp;
        }
    
        HMS_Preview_OpenFileBoost_FileOperationInfoDestroy(fileOpInfo);
        OH_LOG_INFO(LOG_APP, "NotifyFileOperation successfully.");
        napi_create_int32(env, 0, &resp);
        return resp;
    }
  9. Register the file scanning callback to ensure that the system can call the callback to notify your app of the scanning task when scanning files.

    static napi_value RegisterFileScan(napi_env env, napi_callback_info info) {
        napi_value resp;
        napi_create_int32(env, 0, &resp);
        OH_LOG_INFO(LOG_APP, "Register file scan start.");
        // Create a scanning option and add the file types supported by the app.
        FileScanBoost_ScanOption *scanOption = nullptr;
        FileScanBoost_ErrCode res = HMS_Preview_FileScanBoost_ScanOptionCreate(&scanOption);
        if (res != FILE_SCAN_BOOST_SUCCESS) {
            // Customize error handling.
            OH_LOG_ERROR(LOG_APP, "Failed to create ScanOption, code: %{public}d", res);
            napi_create_int32(env, -1, &resp);
            return resp;
        }
    
        res = HMS_Preview_FileScanBoost_ScanOptionAddSupportFile(scanOption, "xlsx", 4);
        if (res != FILE_SCAN_BOOST_SUCCESS) {
            // Customize error handling.
            OH_LOG_ERROR(LOG_APP, "Failed to add .xlsx support, code: %{public}d", res);
            HMS_Preview_FileScanBoost_ScanOptionDestroy(scanOption);
            napi_create_int32(env, -2, &resp);
            return resp;
        }
    
        res = HMS_Preview_FileScanBoost_ScanOptionAddSupportFile(scanOption, "xls", 3);
        if (res != FILE_SCAN_BOOST_SUCCESS) {
            // Customize error handling.
            OH_LOG_ERROR(LOG_APP, "Failed to add .xls support, code: %{public}d", res);
            HMS_Preview_FileScanBoost_ScanOptionDestroy(scanOption);
            napi_create_int32(env, -3, &resp);
            return resp;
        }
        // Register the file scanning callback.
        res = HMS_Preview_FileScanBoost_RegisterFileScan(AppOnFileScan, scanOption);
        if (res != FILE_SCAN_BOOST_SUCCESS) {
            // Registration fails. You can customize error handling.
            OH_LOG_ERROR(LOG_APP, "Register file scan failed, ret :%{public}d", res);
            napi_create_int32(env, res, &resp);
            return resp;
        }
        // Destroy scanning options after the scanning is complete.
        HMS_Preview_FileScanBoost_ScanOptionDestroy(scanOption);
        OH_LOG_INFO(LOG_APP, "Register file scan successfully.");
        napi_create_int32(env, 0, &resp);
        return resp;
    }
    
    // File scanning callback function, which is called by the system to deliver a scanning task.
    // The app must complete file parsing within the current callback context, either by processing it synchronously or by blocking until it finishes.
    FileScanBoost_CbErrCode AppOnFileScan(int32_t fd, const char *path, uint32_t pathLen) {
        // Implement the specific file parsing logic in the callback.
        // The return value FILE_SCAN_BOOST_CALLBACK_SUCCESS indicates that the scanning task is accepted.
        // Other values indicate task rejection.
        OH_LOG_INFO(LOG_APP, "AppOnFileScan called, fd=%{public}d, path=%{public}s", fd, path);
    
        {
            std::lock_guard<std::mutex> lock(g_scanMutex);
            g_scanTasks[std::string(path)] = fd;
        }
        ConfigManager::getInstance().addScanTask(std::string(path), fd);
    
        int callbackResult = ConfigManager::getInstance().getScanCallbackResult();
        OH_LOG_INFO(LOG_APP, "AppOnFileScan returning: %{public}d", callbackResult);
        return static_cast<FileScanBoost_CbErrCode>(callbackResult);
    }
  10. Send the scanning result to the system to ensure that the system can receive the file scanning result after the scanning is complete.

    static napi_value ReportScanResult(napi_env env, napi_callback_info info) {
        napi_value resp;
        napi_create_int32(env, 0, &resp);
    
        std::lock_guard<std::mutex> lock(g_scanMutex);
        if (g_scanTasks.empty()) {
            OH_LOG_INFO(LOG_APP, "No scan tasks to report");
            napi_create_int32(env, 0, &resp);
            return resp;
        }
    
        int reportCount = 0;
        for (auto it = g_scanTasks.begin(); it != g_scanTasks.end();) {
            std::string reportPath = it->first;
            // Create a scanning result.
            FileScanBoost_ScanResult *scanResult = nullptr;
            FileScanBoost_ErrCode res = HMS_Preview_FileScanBoost_ScanResultCreate(&scanResult);
            if (res != FILE_SCAN_BOOST_SUCCESS) {
                // Customize error handling.
                OH_LOG_ERROR(LOG_APP, "Failed to create ScanResult, code: %{public}d", res);
                ++it;
                continue;
            }
            // Set the scanning status.
            int state = ConfigManager::getInstance().getScanResultState();
            res = HMS_Preview_FileScanBoost_ScanResultSetState(scanResult, static_cast<FileScanBoost_ScanState>(state));
            if (res != FILE_SCAN_BOOST_SUCCESS) {
                // Customize error handling.
                OH_LOG_INFO(LOG_APP, "ScanResultSetState error");
            }
    
            // Set the maximum atomic duration, in milliseconds.
            int64_t maxAtomicTime = ConfigManager::getInstance().getScanResultMaxAtomicTime();
            res = HMS_Preview_FileScanBoost_ScanResultSetMaxAtomicTime(scanResult, maxAtomicTime);
            if (res != FILE_SCAN_BOOST_SUCCESS) {
                // Customize error handling.
                OH_LOG_INFO(LOG_APP, "ScanResultSetMaxAtomicTime error");
            }
    
            // Set the memory usage, in MB.
            int64_t memSize = ConfigManager::getInstance().getScanResultMemSize();
            res = HMS_Preview_FileScanBoost_ScanResultSetMemSize(scanResult, memSize);
            if (res != FILE_SCAN_BOOST_SUCCESS) {
                // Customize error handling.
                OH_LOG_INFO(LOG_APP, "ScanResultSetMemSize error");
            }
    
            // Report the scanning result to the system.
            res = HMS_Preview_FileScanBoost_ReportScanResult(reportPath.c_str(),
                                                             static_cast<uint32_t>(strlen(reportPath.c_str())), scanResult);
    
            if (res == FILE_SCAN_BOOST_SUCCESS) {
                OH_LOG_INFO(LOG_APP, "ReportScanResult successfully for: %{public}s", reportPath.c_str());
                reportCount++;
            } else {
                // Report failure. You can customize error handling.
                OH_LOG_ERROR(LOG_APP, "ReportScanResult failed for %{public}s, ret :%{public}d", reportPath.c_str(), res);
            }
    
            // Destroy the scanning result after the scanning is complete.
            HMS_Preview_FileScanBoost_ScanResultDestroy(scanResult);
    
            it = g_scanTasks.erase(it);
            ConfigManager::getInstance().removeScanTaskByPath(reportPath);
        }
    
        OH_LOG_INFO(LOG_APP, "Report %{public}d scan results successfully.", reportCount);
        napi_create_int32(env, 0, &resp);
        return resp;
    }
  11. Deregister the file scanning callback to ensure that the app will no longer receive scanning task notifications.

    static napi_value UnregisterFileScan(napi_env env, napi_callback_info info) {
        napi_value resp;
        napi_create_int32(env, 0, &resp);
        OH_LOG_INFO(LOG_APP, "UnRegister file scan start.");
    
        auto res = HMS_Preview_FileScanBoost_UnregisterFileScan();
        if (res != FILE_SCAN_BOOST_SUCCESS) {
            // Deregistration fails. You can customize error handling.
            OH_LOG_ERROR(LOG_APP, "UnRegister file scan failed, ret :%{public}d", res);
            napi_create_int32(env, res, &resp);
            return resp;
        }
    
        {
            std::lock_guard<std::mutex> lock(g_scanMutex);
            g_scanTasks.clear();
        }
    
        OH_LOG_INFO(LOG_APP, "UnRegister file scan successfully.");
        napi_create_int32(env, 0, &resp);
        return resp;
    }