feat: add device online and offline event

Signed-off-by: caochuan <caochuan@huawei.com>
This commit is contained in:
caochuan 2023-08-25 08:38:01 +08:00
parent 4d6ffc0843
commit 905740b17d
7 changed files with 128 additions and 32 deletions

View File

@ -214,11 +214,40 @@ void InitNotifyType(napi_env env, napi_value exports)
DECLARE_NAPI_STATIC_PROPERTY("NOTIFY_MOVED_TO", NVal::CreateInt32(env, MOVED_TO).val_),
DECLARE_NAPI_STATIC_PROPERTY("NOTIFY_MOVED_FROM", NVal::CreateInt32(env, MOVED_FROM).val_),
DECLARE_NAPI_STATIC_PROPERTY("NOTIFY_MOVE_SELF", NVal::CreateInt32(env, MOVED_SELF).val_),
DECLARE_NAPI_STATIC_PROPERTY("NOTIFY_DEVICE_ONLINE", NVal::CreateInt32(env, DEVICE_ONLINE).val_),
DECLARE_NAPI_STATIC_PROPERTY("NOTIFY_DEVICE_OFFLINE", NVal::CreateInt32(env, DEVICE_OFFLINE).val_),
};
napi_value obj = nullptr;
napi_define_class(env, className, NAPI_AUTO_LENGTH, RootInfoConstructor, nullptr,
sizeof(desc) / sizeof(*desc), desc, &obj);
napi_set_named_property(env, exports, className, obj);
}
void InitDeviceUri(napi_env env, napi_value exports)
{
napi_value deviceUri = nullptr;
napi_create_string_utf8(env, DEVICE_URI.c_str(), DEVICE_URI.length(), &deviceUri);
napi_set_named_property(env, exports, "DEVICE_URI", deviceUri);
}
void InitDeviceRoots(napi_env env, napi_value exports)
{
napi_value deviceRoots = nullptr;
napi_status status = napi_create_array_with_length(env, DEVICE_ROOTS.size(), &deviceRoots);
if (status != napi_ok) {
HILOG_ERROR("Create array failed, error: %{public}d", status);
}
int elementIndex = 0;
for (auto deviceRoot : DEVICE_ROOTS) {
napi_value deviceRootRet = nullptr;
napi_create_string_utf8(env, deviceRoot.c_str(), NAPI_AUTO_LENGTH, &deviceRootRet);
status = napi_set_element(env, deviceRoots, elementIndex++, deviceRootRet);
if (status != napi_ok) {
HILOG_ERROR("Set lite item failed, error: %{public}d", status);
break;
}
}
napi_set_named_property(env, exports, "DeviceRoots", deviceRoots);
}
} // namespace FileAccessFwk
} // namespace OHOS

View File

@ -29,6 +29,8 @@ void InitOpenFlags(napi_env env, napi_value exports);
void InitQueryFlags(napi_env env, napi_value exports);
void InitCopyResult(napi_env env, napi_value exports);
void InitNotifyType(napi_env env, napi_value exports);
void InitDeviceUri(napi_env env, napi_value exports);
void InitDeviceRoots(napi_env env, napi_value exports);
} // namespace FileAccessFwk
} // namespace OHOS
#endif // FILE_EXTENSION_INFO_NAPI_H

View File

@ -45,6 +45,8 @@ static napi_value Init(napi_env env, napi_value exports)
InitQueryFlags(env, exports);
InitCopyResult(env, exports);
InitNotifyType(env, exports);
InitDeviceUri(env, exports);
InitDeviceRoots(env, exports);
std::vector<std::unique_ptr<NExporter>> products;
products.emplace_back(std::make_unique<NapiRootIteratorExporter>(env, exports));

View File

@ -79,6 +79,18 @@ constexpr int32_t DELETE_EVENT = 1;
constexpr int32_t MOVED_TO = 2;
constexpr int32_t MOVED_FROM = 3;
constexpr int32_t MOVED_SELF = 4;
constexpr int32_t DEVICE_ONLINE = 5;
constexpr int32_t DEVICE_OFFLINE = 6;
//Indicates the device root uris
const std::string DEVICE_URI = "file://docs";
//Indicates the root uri of the External device.
const std::string EXTERNAL_ROOT_URI = "file://docs/storage/External";
/**
* Indicates the root uri for all device.
*/
static const std::vector<std::string> DEVICE_ROOTS = {EXTERNAL_ROOT_URI};
struct FileInfo : public virtual OHOS::Parcelable {
public:

View File

@ -31,7 +31,9 @@ enum NotifyType {
NOTIFY_DELETE,
NOTIFY_MOVE_TO,
NOTIFY_MOVE_FROM,
NOTIFY_MOVE_SELE
NOTIFY_MOVE_SELE,
NOTIFY_DEVICE_ONLINE,
NOTIFY_DEVICE_OFFLINE
};
struct NotifyMessage : public OHOS::Parcelable {

View File

@ -45,6 +45,8 @@ std::vector<AAFwk::Want> FileAccessHelper::wants_;
sptr<IFileAccessExtBase> g_sourceExtProxy;
sptr<IFileAccessExtBase> g_destExtProxy;
std::vector<Uri> deviceUris(DEVICE_ROOTS.begin(), DEVICE_ROOTS.end());
static int GetUserId()
{
int uid = IPCSkeleton::GetCallingUid();
@ -1131,6 +1133,15 @@ int FileAccessHelper::StopWatcher(Uri &uri, bool isUnregisterAll)
return ERR_OK;
}
static void convertUris(Uri uri, std::vector<Uri> &uris) {
std::string uriString = uri.ToString();
if (uriString == DEVICE_URI) {
uris = deviceUris;
} else {
uris.push_back(uri);
}
}
int FileAccessHelper::RegisterNotify(Uri uri, bool notifyForDescendants, sptr<IFileAccessObserver> &observer)
{
UserAccessTracer trace;
@ -1151,17 +1162,22 @@ int FileAccessHelper::RegisterNotify(Uri uri, bool notifyForDescendants, sptr<IF
return E_LOAD_SA;
}
int ret = proxy->RegisterNotify(uri, notifyForDescendants, observer);
if (ret != ERR_OK) {
HILOG_ERROR("RegisterNotify error ret = %{public}d", ret);
return ret;
}
std::vector<Uri> uris;
convertUris(uri, uris);
for (auto eachUri : uris) {
int ret = proxy->RegisterNotify(eachUri, notifyForDescendants, observer);
if (ret != ERR_OK) {
HILOG_ERROR("RegisterNotify error ret = %{public}d", ret);
return ret;
}
ret = StartWatcher(uri);
if (ret != ERR_OK) {
HILOG_ERROR("StartWatcher error ret = %{public}d", ret);
ret = StartWatcher(eachUri);
if (ret != ERR_OK) {
HILOG_ERROR("StartWatcher error ret = %{public}d", ret);
return ret;
}
}
return ret;
return ERR_OK;
}
int FileAccessHelper::UnregisterNotify(Uri uri, sptr<IFileAccessObserver> &observer)
@ -1184,18 +1200,24 @@ int FileAccessHelper::UnregisterNotify(Uri uri, sptr<IFileAccessObserver> &obser
return E_LOAD_SA;
}
int ret = proxy->UnregisterNotify(uri, observer);
if (ret != ERR_OK) {
HILOG_ERROR("UnregisterNotify error ret = %{public}d", ret);
return ret;
std::vector<Uri> uris;
convertUris(uri, uris);
for (auto eachUri : uris) {
int ret = proxy->UnregisterNotify(eachUri, observer);
if (ret != ERR_OK) {
HILOG_ERROR("UnregisterNotify error ret = %{public}d", ret);
return ret;
}
bool isUnregisterAll = false;
ret = StopWatcher(eachUri, isUnregisterAll);
if (ret != ERR_OK) {
HILOG_ERROR("StopWatcher error ret = %{public}d", ret);
return ret;
}
}
bool isUnregisterAll = false;
ret = StopWatcher(uri, isUnregisterAll);
if (ret != ERR_OK) {
HILOG_ERROR("StopWatcher error ret = %{public}d", ret);
}
return ret;
return ERR_OK;
}
int FileAccessHelper::UnregisterNotify(Uri uri)
@ -1218,18 +1240,24 @@ int FileAccessHelper::UnregisterNotify(Uri uri)
}
sptr<IFileAccessObserver> observer = nullptr;
int ret = proxy->UnregisterNotify(uri, observer);
if (ret != ERR_OK) {
HILOG_ERROR("UnregisterNotify error ret = %{public}d", ret);
return ret;
}
bool isUnregisterAll = true;
ret = StopWatcher(uri, isUnregisterAll);
if (ret != ERR_OK) {
HILOG_ERROR("StopWatcher error ret = %{public}d", ret);
std::vector<Uri> uris;
convertUris(uri, uris);
for (auto eachUri : uris) {
int ret = proxy->UnregisterNotify(eachUri, observer);
if (ret != ERR_OK) {
HILOG_ERROR("UnregisterNotify error ret = %{public}d", ret);
return ret;
}
bool isUnregisterAll = true;
ret = StopWatcher(eachUri, isUnregisterAll);
if (ret != ERR_OK) {
HILOG_ERROR("StopWatcher error ret = %{public}d", ret);
return ret;
}
}
return ret;
return ERR_OK;
}
void FileAccessDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)

View File

@ -16,6 +16,7 @@
import Extension from '@ohos.application.FileAccessExtensionAbility';
import fs from '@ohos.file.fs';
import type { Filter } from '@ohos.file.fs';
import fileAccess from '@ohos.file.fileAccess';
import fileExtensionInfo from '@ohos.file.fileExtensionInfo';
import hilog from '@ohos.hilog';
import { getFileInfos } from './ListScanFileInfo';
@ -54,6 +55,8 @@ const DELETE_EVENT = 1;
const MOVED_TO = 2;
const MOVED_FROM = 3;
const MOVED_SELF = 4;
const DEVICE_ONLINE = 5;
const DEVICE_OFFLINE = 6;
const TRASH_PATH = '/storage/.Trash/Users/100/';
const TRASH_SUB_FODER = '/oh_trash_content';
const EXTERNAL_PATH = '/storage/External';
@ -68,6 +71,11 @@ let eventMap = new Map([
[MOVE_SELF_CODE, MOVED_SELF]
]);
let deviceOnlineMap = new Map([
[CREATE_EVENT_CODE, DEVICE_ONLINE],
[IN_DELETE_EVENT_CODE, DEVICE_OFFLINE]
]);
// ['IN_ACCESS', 0x00000001],
// ['IN_MODIFY', 0x00000002],
// ['IN_ATTRIB', 0x00000004],
@ -774,6 +782,15 @@ export default class FileExtAbility extends Extension {
return resultsResultObject(queryResults, ERR_OK);
}
isDeviceUri(uri): boolean {
let tempUri = uri.slice(0, uri.lastIndexOf('/'))
let deviceUris = fileAccess.DeviceRoots
if (deviceUris.indexOf(tempUri) != -1) {
return true;
}
return false;
}
startWatcher(uri, callback): number {
uri = decodeUri(uri);
if (!checkUri(uri)) {
@ -786,12 +803,16 @@ export default class FileExtAbility extends Extension {
MOVE_TO_CODE | MOVED_FROM_CODE | MOVE_SELF_CODE, (data) => {
try {
let eventCode = -1;
eventMap.forEach((value, key) => {
let targetUri = FILE_PREFIX_NAME + BUNDLE_NAME + data.fileName;
let tempMap = eventMap;
if (this.isDeviceUri(targetUri)) {
tempMap = deviceOnlineMap;
}
tempMap.forEach((value, key) => {
if (data.event & key) {
eventCode = value;
}
});
let targetUri = FILE_PREFIX_NAME + BUNDLE_NAME + data.fileName;
targetUri = encodePathOfUri(targetUri);
if (eventCode >= 0) {
callback(targetUri, eventCode);