feat: show

Signed-off-by: zengsiyu <zengsiyu3@huawei.com>
Change-Id: Ib10f47f17146fb2ad12aa662c3d3a986910ac6e5
Signed-off-by: zengsiyu <zengsiyu3@huawei.com>
This commit is contained in:
zengsiyu
2022-04-08 11:08:17 +08:00
parent c40df5af47
commit 2d7fa4fe78
+195 -3
View File
@@ -15,9 +15,16 @@
#include "publish.h"
#include "want_agent_helper.h"
namespace OHOS {
namespace NotificationNapi {
static const int32_t PUBLISH_NOTIFICATION_MAX = 3;
using namespace AbilityRuntime::WantAgent;
namespace {
static const int8_t PUBLISH_NOTIFICATION_MAX = 3;
static const int8_t SHOW_NOTIFICATION_MAX = 1;
}
struct AsyncCallbackInfoPublish {
napi_env env = nullptr;
@@ -160,10 +167,195 @@ napi_value Publish(napi_env env, napi_callback_info info)
}
}
bool CheckProperty(const napi_env &env, const napi_value &content, const std::string &property)
{
ANS_LOGI("enter");
bool hasProperty = false;
NAPI_CALL(env, napi_has_named_property(env, content, property.data(), &hasProperty));
if (!hasProperty) {
ANS_LOGW("Property %{public}s expected.", property.c_str());
}
return hasProperty;
}
napi_value GetStringProperty(
const napi_env &env, const napi_value &content, const std::string &property, std::string &result)
{
ANS_LOGI("enter");
if (!CheckProperty(env, content, property)) {
return nullptr;
}
napi_valuetype valuetype = napi_undefined;
napi_value value = nullptr;
char str[STR_MAX_SIZE] = {0};
size_t strLen = 0;
napi_get_named_property(env, content, property.data(), &value);
NAPI_CALL(env, napi_typeof(env, value, &valuetype));
if (valuetype != napi_string) {
ANS_LOGW("Wrong argument type. String expected.");
return nullptr;
}
NAPI_CALL(env, napi_get_value_string_utf8(env, value, str, STR_MAX_SIZE - 1, &strLen));
ANS_LOGI("normal::%{public}s = %{public}s", property.c_str(), str);
result = str;
return Common::NapiGetNull(env);
}
napi_value GetObjectProperty(
const napi_env &env, const napi_value &content, const std::string &property, napi_value &result)
{
ANS_LOGI("enter");
if (!CheckProperty(env, content, property)) {
return nullptr;
}
napi_valuetype valuetype = napi_undefined;
napi_get_named_property(env, content, property.data(), &result);
NAPI_CALL(env, napi_typeof(env, result, &valuetype));
if (valuetype != napi_object) {
ANS_LOGW("Wrong argument type. Object expected.");
return nullptr;
}
return Common::NapiGetNull(env);
}
napi_value ParseShowOptions(const napi_env &env, const napi_callback_info &info, ParametersInfoPublish &params)
{
ANS_LOGI("enter");
size_t argc = SHOW_NOTIFICATION_MAX;
napi_value argv[SHOW_NOTIFICATION_MAX] = {nullptr};
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
if (argc == 0) {
ANS_LOGW("Wrong number of arguments.");
return nullptr;
}
// argv[0] : ShowNotificationOptions
napi_valuetype valuetype = napi_undefined;
NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
if (valuetype != napi_object) {
ANS_LOGW("Wrong argument type. Object expected.");
return nullptr;
}
std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
// contentTitle
std::string contentTitle;
if (GetStringProperty(env, argv[PARAM0], "contentTitle", contentTitle) != nullptr) {
normalContent->SetTitle(contentTitle);
}
// contentText
std::string contentText;
if (GetStringProperty(env, argv[PARAM0], "contentText", contentText) != nullptr) {
normalContent->SetText(contentText);
}
std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
params.request.SetContent(content);
// clickAction
napi_value clickAction = nullptr;
if (GetObjectProperty(env, argv[PARAM0], "clickAction", clickAction) != nullptr) {
ANS_LOGD("create wantagent");
// bundleName & abilityName
std::shared_ptr<AAFwk::Want> want = std::make_shared<AAFwk::Want>();
std::string bundleName;
std::string abilityName;
if (GetStringProperty(env, clickAction, "bundleName", bundleName) == nullptr) {
return nullptr;
}
if (GetStringProperty(env, clickAction, "abilityName", abilityName) == nullptr) {
return nullptr;
}
want->SetElementName(bundleName, abilityName);
// uri
std::string uri;
if (GetStringProperty(env, clickAction, "uri", uri) == nullptr) {
return nullptr;
}
want->SetUri(uri);
std::vector<std::shared_ptr<AAFwk::Want>> wants = {};
std::vector<WantAgentConstant::Flags> wantAgentFlags = {};
std::shared_ptr<AAFwk::WantParams> extraInfo = std::make_shared<AAFwk::WantParams>();
wants.emplace_back(want);
WantAgentInfo wantAgentInfo(-1, WantAgentConstant::OperationType::START_ABILITY, wantAgentFlags,
wants, extraInfo);
std::shared_ptr<AbilityRuntime::Context> context = AbilityRuntime::Context::GetApplicationContext();
std::shared_ptr<WantAgent> wantAgent =
WantAgentHelper::GetWantAgent(context, wantAgentInfo);
params.request.SetWantAgent(wantAgent);
}
ANS_LOGI("end");
return Common::NapiGetNull(env);
}
napi_value ShowNotification(napi_env env, napi_callback_info info)
{
ANS_LOGI("ShowNotification enter");
return Common::NapiGetNull(env);
ANS_LOGI("enter");
ParametersInfoPublish params;
if (ParseShowOptions(env, info, params) == nullptr) {
ANS_LOGW("parse showOptions failed");
return Common::NapiGetUndefined(env);
}
AsyncCallbackInfoPublish *asynccallbackinfo =
new (std::nothrow) AsyncCallbackInfoPublish {.env = env, .asyncWork = nullptr};
if (!asynccallbackinfo) {
ANS_LOGW("failed to create asynccallbackinfo");
return Common::JSParaError(env, params.callback);
}
asynccallbackinfo->request = params.request;
napi_value resourceName = nullptr;
napi_create_string_latin1(env, "show", NAPI_AUTO_LENGTH, &resourceName);
ANS_LOGI("before napi_create_async_work");
napi_create_async_work(env,
nullptr,
resourceName,
[](napi_env env, void *data) {
ANS_LOGI("Show napi_create_async_work start");
AsyncCallbackInfoPublish *asynccallbackinfo = static_cast<AsyncCallbackInfoPublish *>(data);
ANS_LOGI("Show napi_create_async_work start notificationId = %{public}d, contentType = "
"%{public}d",
asynccallbackinfo->request.GetNotificationId(),
asynccallbackinfo->request.GetContent()->GetContentType());
asynccallbackinfo->info.errorCode =
NotificationHelper::PublishNotification(asynccallbackinfo->request);
},
[](napi_env env, napi_status status, void *data) {
ANS_LOGI("Show napi_create_async_work complete start");
AsyncCallbackInfoPublish *asynccallbackinfo = static_cast<AsyncCallbackInfoPublish *>(data);
if (asynccallbackinfo) {
if (asynccallbackinfo->info.callback != nullptr) {
napi_delete_reference(env, asynccallbackinfo->info.callback);
}
napi_delete_async_work(env, asynccallbackinfo->asyncWork);
delete asynccallbackinfo;
asynccallbackinfo = nullptr;
}
ANS_LOGI("Show napi_create_async_work complete end");
},
(void *)asynccallbackinfo,
&asynccallbackinfo->asyncWork);
NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork));
return nullptr;
}
} // namespace NotificationNapi
} // namespace OHOS