【基础能力】支持上传下载任务状态变化事件通知

Signed-off-by: 王柄涵 <wangbinghan1@huawei.com>
This commit is contained in:
王柄涵 2023-10-18 16:15:31 +08:00
parent 948223660f
commit fd2dec7d69
6 changed files with 81 additions and 0 deletions

View File

@ -30,6 +30,7 @@
"components": [
"samgr",
"ipc",
"common_event_service",
"netmanager_base",
"eventhandler",
"ability_runtime",

View File

@ -115,6 +115,7 @@ ohos_shared_library("request_service_c") {
"${request_path}/services/service/rust/src/c_wrapper/source/c_request_database.cpp",
"${request_path}/services/service/rust/src/c_wrapper/source/c_string_wrapper.cpp",
"${request_path}/services/service/rust/src/c_wrapper/source/c_task_info.cpp",
"${request_path}/services/service/rust/src/c_wrapper/source/common_event_notify.cpp",
"${request_path}/services/service/rust/src/c_wrapper/source/get_calling_bundle.cpp",
"${request_path}/services/service/rust/src/c_wrapper/source/get_top_bundle.cpp",
"${request_path}/services/service/rust/src/c_wrapper/source/network_adapter.cpp",
@ -130,6 +131,7 @@ ohos_shared_library("request_service_c") {
"access_token:libaccesstoken_sdk",
"access_token:libtokenid_sdk",
"c_utils:utils",
"common_event_service:cesfwk_innerkits",
"distributed_notification_service:ans_innerkits",
"eventhandler:libeventhandler",
"hilog:libhilog",

View File

@ -0,0 +1,31 @@
/*
* Copyright (C) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef COMMON_EVENT_NOTIFY_H
#define COMMON_EVENT_NOTIFY_H
#include <cstdint>
#ifdef __cplusplus
extern "C" {
#endif
constexpr const char *EVENT_NAME_COMPLETE = "ohos.request.event.COMPLETE";
void PublishStateChangeEvents(const char* bundleName, uint32_t len, uint32_t taskId, int32_t state);
#ifdef __cplusplus
}
#endif
#endif // COMMON_EVENT_NOTIFY_H

View File

@ -0,0 +1,43 @@
/*
* Copyright (C) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <want.h>
#include "common_event_data.h"
#include "common_event_manager.h"
#include "common_event_notify.h"
#include "common_event_publish_info.h"
#include "log.h"
using namespace OHOS::EventFwk;
void PublishStateChangeEvents(const char* bundleName, uint32_t len, uint32_t taskId, int32_t state)
{
REQUEST_HILOGD("PublishStateChangeEvents in.");
static std::string eventAction = "ohos.request.event.COMPLETE";
std::string bundle (bundleName, len);
Want want;
want.SetAction(eventAction);
want.SetBundle(bundle);
std::string data = std::to_string(taskId);
CommonEventData commonData (want, state, data);
bool res = CommonEventManager::PublishCommonEvent(commonData);
if (!res) {
REQUEST_HILOGE("PublishStateChangeEvents failed!");
}
}

View File

@ -51,4 +51,5 @@ extern "C" {
pub fn Search(filter: CFilter) -> CVectorWrapper;
pub fn RequestIsSystemAPI(tokenId: u64) -> bool;
pub fn GetCallingBundle(tokenId: u64) -> CStringWrapper;
pub fn PublishStateChangeEvents(bundleName: *const c_char, bundleNameLen: u32, taskId: u32, state: i32);
}

View File

@ -840,12 +840,15 @@ impl RequestTask {
let mode = self.conf.common_data.mode;
if version == Version::API9 || mode == Mode::FRONTEND {
let notify_data = self.build_notify_data();
let bundle = self.conf.bundle.clone();
TaskManager::get_instance().front_notify("progress".into(), &notify_data);
match state {
State::COMPLETED => {
unsafe { PublishStateChangeEvents(CString::new(bundle.as_str()).unwrap().as_ptr(), bundle.len() as u32, self.task_id, State::COMPLETED as i32); }
TaskManager::get_instance().front_notify("complete".into(), &notify_data)
}
State::FAILED => {
unsafe { PublishStateChangeEvents(CString::new(bundle.as_str()).unwrap().as_ptr(), bundle.len() as u32, self.task_id, State::FAILED as i32); }
TaskManager::get_instance().front_notify("fail".into(), &notify_data)
}
State::PAUSED | State::WAITING => {