!335 新增微信打印ndk

Merge pull request !335 from 单海洋/myfeature
This commit is contained in:
openharmony_ci 2024-09-01 07:55:42 +00:00 committed by Gitee
commit faf1e26113
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 391 additions and 1 deletions

View File

@ -56,10 +56,12 @@ ohos_shared_library("ohprint") {
"ability_base:base",
"ability_base:want",
"ability_base:zuri",
"ability_runtime:ability_context_native",
"ability_runtime:ability_manager",
"ability_runtime:abilitykit_native",
"ability_runtime:data_ability_helper",
"ability_runtime:napi_base_context",
"ability_runtime:ui_extension",
"access_token:libaccesstoken_sdk",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",

View File

@ -280,6 +280,28 @@ typedef enum {
DOCUMENT_FORMAT_TEXT,
} Print_DocumentFormat;
/**
* @brief Indicates the print job doc adapter state.
*
* @since 13
*/
typedef enum {
/** Print job preview ability destroy. */
PRINT_DOC_ADAPTER_PREVIEW_ABILITY_DESTROY = 0,
/** Print job task succeed. */
PRINT_DOC_ADAPTER_PRINT_TASK_SUCCEED = 1,
/** Print job task failed. */
PRINT_DOC_ADAPTER_PRINT_TASK_FAIL = 2,
/** Print job task cancel. */
PRINT_DOC_ADAPTER_PRINT_TASK_CANCEL = 3,
/** Print job task block. */
PRINT_DOC_ADAPTER_PRINT_TASK_BLOCK = 4,
/** Print job task preview ability destroy for canceled. */
PRINT_DOC_ADAPTER_PREVIEW_ABILITY_DESTROY_FOR_CANCELED = 5,
/** Print job task preview ability destroy for started. */
PRINT_DOC_ADAPTER_PREVIEW_ABILITY_DESTROY_FOR_STARTED = 6,
} Print_JobDocAdapterState;
/**
* @brief Indicates printer capabilities.
*
@ -422,6 +444,96 @@ typedef struct {
char *advancedOptions;
} Print_PrintJob;
/**
* @brief Indicates print range structure.
*
* @since 13
*/
typedef struct {
/** Print start page. */
uint32_t startPage;
/** Print end page. */
uint32_t endPage;
/** Print page array length. */
uint32_t pagesArrayLen;
/** Print page array. */
uint32_t* pagesArray;
} Print_Range;
/**
* @brief Indicates print attributes structure.
*
* @since 13
*/
typedef struct {
/** Print ranges. */
Print_Range pageRange;
/** Print page size. */
Print_PageSize pageSize;
/** Print margin. */
Print_Margin pageMargin;
/** Copy numbers. */
uint32_t copyNumber;
/** Duplex mode. */
uint32_t duplexMode;
/** Color mode. */
uint32_t colorMode;
/** Print sequential. */
bool isSequential;
/** Print orient. */
bool isLandscape;
/** Print option flag. */
bool hasOption;
/** Print options. */
char options[256];
} Print_PrintAttributes;
/**
* @brief Write files result callback.
*
* @param jobId The print job id of one print task.
* @param code The result of write files.
* @since 13
*/
typedef void(*Print_WriteResultCallback)(const char *jobId, uint32_t code);
/**
* @brief Print start layout callback.
*
* @param jobId The print job id of one print task.
* @param fd The file descriptor to be written.
* @param oldAttrs The atrributes of last.
* @param newAttrs The atrributes of current.
* @param writeCallback The Write files result callback.
* @since 13
*/
typedef void(*Print_OnStartLayoutWrite)(const char *jobId,
uint32_t fd,
const Print_PrintAttributes *oldAttrs,
const Print_PrintAttributes *newAttrs,
Print_WriteResultCallback writeCallback);
/**
* @brief Print job state callback.
*
* @param jobId The print job id of one print task.
* @param state The state of current print job.
* @since 13
*/
typedef void(*Print_OnJobStateChanged)(const char *jobId, uint32_t state);
/**
* @brief Indicates print doc state callback structure.
*
* @since 13
*/
typedef struct {
/** Print start layout callback. */
Print_OnStartLayoutWrite startLayoutWriteCb;
/** Print job state callback. */
Print_OnJobStateChanged jobStateChangedCb;
} Print_PrintDocCallback;
/**
* @brief Printer discovery callback.
*
@ -659,6 +771,23 @@ Print_ErrorCode OH_Print_UpdatePrinterProperties(const char *printerId, const Pr
*/
Print_ErrorCode OH_Print_RestorePrinterProperties(const char *printerId, const Print_StringList *propertyKeyList);
/**
* @brief This API restores printer properties to default settings based on the list of property keywords.
*
* @permission {@code ohos.permission.PRINT}
* @param printJobName The name of this print job.
* @param printDocCallback The print doc state callback.
* @param context The context of caller app.
* @return Returns {@link Print_ErrorCode#PRINT_ERROR_NONE} if the execution is successful.
* {@link PRINT_ERROR_NO_PERMISSION} The permission {@code ohos.permission.PRINT} is needed.
* {@link PRINT_ERROR_RPC_FAILURE} Unable to connect to the print service.
* @syscap SystemCapability.Print.PrintFramework
* @since 13
*/
Print_ErrorCode OH_Print_StartPrintByNative(const char *printJobName,
Print_PrintDocCallback printDocCallback,
void *context);
#ifdef __cplusplus
}
#endif

View File

@ -14,6 +14,16 @@
*/
#include "ohprint.h"
#include <algorithm>
#include <map>
#include <memory>
#include <mutex>
#include <vector>
#include "ability_context.h"
#include "iprint_adapter.h"
#include "print_attributes.h"
#include "print_manager_client.h"
#include "print_constant.h"
#include "print_log.h"
@ -22,6 +32,8 @@
#include "print_callback.h"
#include "print_converter.h"
#include "print_helper.h"
#include "refbase.h"
#include "ui_extension_context.h"
using namespace OHOS::Print;
@ -65,6 +77,63 @@ static bool NativePrinterInfoFunction(uint32_t event, const PrinterInfo &info)
return true;
}
static OHOS::Ace::UIContent *GetUIContent(void *context)
{
if (context == nullptr) {
PRINT_HILOGE("input context is null.");
return nullptr;
}
auto weakContext = static_cast<std::weak_ptr<OHOS::AbilityRuntime::Context>*>(context);
if (weakContext == nullptr) {
PRINT_HILOGE("cast context to weak is null.");
return nullptr;
}
auto sharedContext = weakContext->lock();
auto abilityContext =
OHOS::AbilityRuntime::Context::ConvertTo<OHOS::AbilityRuntime::AbilityContext>(sharedContext);
if (abilityContext != nullptr) {
PRINT_HILOGD("get ability ui content.");
return abilityContext->GetUIContent();
}
auto uiExtensionContext =
OHOS::AbilityRuntime::Context::ConvertTo<OHOS::AbilityRuntime::UIExtensionContext>(sharedContext);
if (uiExtensionContext != nullptr) {
PRINT_HILOGD("get ui extension ui content.");
return uiExtensionContext->GetUIContent();
}
PRINT_HILOGE("get ui content failed.");
return nullptr;
}
static Print_PrintAttributes *NewPrintAttributes(void)
{
auto attributes = new (std::nothrow) Print_PrintAttributes;
if (attributes == nullptr) {
PRINT_HILOGE("OH_Print new attributes failed.");
return nullptr;
}
if (memset_s(attributes, sizeof(Print_PrintAttributes), 0, sizeof(Print_PrintAttributes)) != 0) {
PRINT_HILOGE("OH_Print attributes memset failed.");
delete attributes;
return nullptr;
}
return attributes;
}
static void ReleasePrintAttributes(Print_PrintAttributes *attr)
{
if (attr == nullptr) {
PRINT_HILOGE("OH_Print release attr is null.");
return;
}
if (attr->pageRange.pagesArray != nullptr) {
delete [] attr->pageRange.pagesArray;
attr->pageRange.pagesArray = nullptr;
}
delete attr;
}
// 初始化
Print_ErrorCode OH_Print_Init()
{
@ -311,4 +380,190 @@ Print_ErrorCode OH_Print_UpdatePrinterProperties(const char *printerId, const Pr
Print_ErrorCode OH_Print_RestorePrinterProperties(const char *printerId, const Print_StringList *propertyKeyList)
{
return PRINT_ERROR_NONE;
}
}
class PrintDocumentAdapterWrapper : public PrintDocumentAdapter {
public:
explicit PrintDocumentAdapterWrapper(Print_PrintDocCallback PrintCallback);
void onStartLayoutWrite(const std::string &jobId,
const PrintAttributes &oldAttrs,
const PrintAttributes &newAttrs,
uint32_t fd,
std::function<void(std::string, uint32_t)> writeResultCallback) override;
void onJobStateChanged(const std::string &jobId, uint32_t state) override;
void OnWriteResultCallback(const std::string &jobId, uint32_t code);
static void WriteResultCallback(const char *jobId, uint32_t code);
private:
Print_PrintAttributes *BuildPrintAttributes(const PrintAttributes &attrs);
Print_PrintDocCallback printCb_;
std::function<void(std::string, uint32_t)> writeResultCb_;
static std::mutex printDocMutex_;
static std::map<std::string, PrintDocumentAdapterWrapper*> printDocAdapterMap_;
};
std::mutex PrintDocumentAdapterWrapper::printDocMutex_;
std::map<std::string, PrintDocumentAdapterWrapper*> PrintDocumentAdapterWrapper::printDocAdapterMap_;
PrintDocumentAdapterWrapper::PrintDocumentAdapterWrapper(Print_PrintDocCallback PrintCallback)
{
printCb_ = PrintCallback;
}
void PrintDocumentAdapterWrapper::onStartLayoutWrite(const std::string &jobId,
const PrintAttributes &oldAttrs,
const PrintAttributes &newAttrs,
uint32_t fd,
std::function<void(std::string, uint32_t)> writeResultCallback)
{
if (printCb_.startLayoutWriteCb == nullptr) {
PRINT_HILOGE("OH_Print start layout callback is null.");
return;
}
{
std::lock_guard<std::mutex> lock(printDocMutex_);
if (printDocAdapterMap_.find(jobId) == printDocAdapterMap_.end()) {
printDocAdapterMap_.insert({ jobId, this });
}
}
writeResultCb_ = writeResultCallback;
auto oldAttrsPtr = BuildPrintAttributes(oldAttrs);
auto newAttrsPtr = BuildPrintAttributes(newAttrs);
printCb_.startLayoutWriteCb(jobId.c_str(),
fd,
oldAttrsPtr,
newAttrsPtr,
PrintDocumentAdapterWrapper::WriteResultCallback);
ReleasePrintAttributes(oldAttrsPtr);
ReleasePrintAttributes(newAttrsPtr);
oldAttrsPtr = nullptr;
newAttrsPtr = nullptr;
}
void PrintDocumentAdapterWrapper::onJobStateChanged(const std::string &jobId, uint32_t state)
{
if (printCb_.jobStateChangedCb == nullptr) {
PRINT_HILOGE("OH_Print job state callback is null.");
return;
}
if (state == PRINT_DOC_ADAPTER_PREVIEW_ABILITY_DESTROY_FOR_CANCELED
|| state == PRINT_DOC_ADAPTER_PREVIEW_ABILITY_DESTROY_FOR_STARTED) {
std::lock_guard<std::mutex> lock(printDocMutex_);
printDocAdapterMap_.erase(jobId);
}
printCb_.jobStateChangedCb(jobId.c_str(), state);
}
void PrintDocumentAdapterWrapper::WriteResultCallback(const char *jobId, uint32_t code)
{
if (jobId == nullptr) {
PRINT_HILOGE("OH_Print write result callback param is null.");
return;
}
std::string jobIdStr = jobId;
PrintDocumentAdapterWrapper* wrapper = nullptr;
{
std::lock_guard<std::mutex> lock(printDocMutex_);
auto iter = printDocAdapterMap_.find(jobId);
if (iter != printDocAdapterMap_.end()) {
wrapper = iter->second;
}
}
if (wrapper == nullptr) {
PRINT_HILOGE("OH_Print get wrapper failed.");
return;
}
wrapper->OnWriteResultCallback(jobIdStr, code);
}
void PrintDocumentAdapterWrapper::OnWriteResultCallback(const std::string &jobId, uint32_t code)
{
if (writeResultCb_ == nullptr) {
PRINT_HILOGE("OH_Print write callback is null.");
return;
}
writeResultCb_(jobId, code);
}
Print_PrintAttributes *PrintDocumentAdapterWrapper::BuildPrintAttributes(const PrintAttributes &attrs)
{
auto attributesRaw = NewPrintAttributes();
if (attributesRaw == nullptr) {
PRINT_HILOGE("OH_Print attribute raw is null.");
return nullptr;
}
std::unique_ptr<Print_PrintAttributes> attributes(attributesRaw);
attributes->copyNumber = attrs.GetCopyNumber();
Print_Range printRange;
PrintRange range;
attrs.GetPageRange(range);
printRange.startPage = range.GetStartPage();
printRange.endPage = range.GetEndPage();
std::vector<uint32_t> pages;
range.GetPages(pages);
auto pageArray = new (std::nothrow) uint32_t[pages.size()];
if (pageArray == nullptr) {
PRINT_HILOGE("OH_Print pages array is null.");
return nullptr;
}
std::copy(pages.begin(), pages.end(), pageArray);
printRange.pagesArray = pageArray;
printRange.pagesArrayLen = pages.size();
attributes->pageRange = printRange;
attributes->isSequential = attrs.GetIsSequential();
Print_PageSize printPageSize;
PrintPageSize pageSize;
attrs.GetPageSize(pageSize);
printPageSize.width = pageSize.GetWidth();
printPageSize.height = pageSize.GetHeight();
attributes->pageSize = printPageSize;
attributes->isLandscape = attrs.GetIsLandscape();
attributes->colorMode = attrs.GetColorMode();
attributes->duplexMode = attrs.GetDuplexMode();
Print_Margin printMargin;
PrintMargin margin;
attrs.GetMargin(margin);
printMargin.topMargin = margin.GetTop();
printMargin.bottomMargin = margin.GetBottom();
printMargin.leftMargin = margin.GetLeft();
printMargin.rightMargin = margin.GetRight();
attributes->pageMargin = printMargin;
return attributes.release();
}
Print_ErrorCode OH_Print_StartPrintByNative(const char *printJobName,
Print_PrintDocCallback printDocCallback,
void *context)
{
if (printJobName == nullptr) {
PRINT_HILOGE("OH_Print start print native print job name is null.");
return PRINT_ERROR_INVALID_PARAMETER;
}
auto uiContent = GetUIContent(context);
if (uiContent == nullptr) {
PRINT_HILOGE("OH_Print start print native ui content is null.");
return PRINT_ERROR_INVALID_PARAMETER;
}
auto wrapper = new (std::nothrow) PrintDocumentAdapterWrapper(printDocCallback);
if (wrapper == nullptr) {
PRINT_HILOGE("OH_Print start print print doc adapter is null.");
return PRINT_ERROR_GENERIC_FAILURE;
}
OHOS::sptr<IPrintCallback> printCb = new (std::nothrow) PrintCallback(wrapper);
if (printCb == nullptr) {
PRINT_HILOGE("OH_Print start print print callback is null.");
delete wrapper;
return PRINT_ERROR_GENERIC_FAILURE;
}
auto attributes = std::make_shared<PrintAttributes>();
std::string printJobNameStr = printJobName;
int32_t ret =
PrintManagerClient::GetInstance()->Print(printJobNameStr, printCb, attributes, uiContent);
if (ret != PRINT_ERROR_NONE) {
PRINT_HILOGE("OH_Print start print start failed.");
return PRINT_ERROR_GENERIC_FAILURE;
}
return PRINT_ERROR_NONE;
}

View File

@ -66,5 +66,9 @@
{
"first_introduced": "12",
"name": "OH_Print_RestorePrinterProperties"
},
{
"first_introduced": "13",
"name": "OH_Print_StartPrintByNative"
}
]