From 6f118d2e0d42d738095e7a65778fac8de8ad8368 Mon Sep 17 00:00:00 2001 From: zhangxingxia Date: Fri, 17 Jun 2022 10:39:08 +0800 Subject: [PATCH 1/2] hap devicemanager Signed-off-by: zhangxingxia --- .../src/main/ets/Application/AbilityStage.ts | 2 - .../FileExtensionAbility.ts | 66 +++++++++-------- .../ets/FileExtensionAbility/Subcriber.ts | 34 +++++++++ .../ets/FileExtensionAbility/VolumeManager.ts | 74 +++++++++++++++++++ 4 files changed, 145 insertions(+), 31 deletions(-) create mode 100644 services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/Subcriber.ts create mode 100644 services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/VolumeManager.ts diff --git a/services/file_extension_hap/entry/src/main/ets/Application/AbilityStage.ts b/services/file_extension_hap/entry/src/main/ets/Application/AbilityStage.ts index 18d98c8e..c4851529 100644 --- a/services/file_extension_hap/entry/src/main/ets/Application/AbilityStage.ts +++ b/services/file_extension_hap/entry/src/main/ets/Application/AbilityStage.ts @@ -13,10 +13,8 @@ * limitations under the License. */ import AbilityStage from "@ohos.application.AbilityStage" -import hilog from '@ohos.hilog' export default class MyAbilityStage extends AbilityStage { onCreate() { - hilog.info(0x0001, 'js server tag dsa', "MyAbilityStage onCreate") } } \ No newline at end of file diff --git a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts index 32c6778f..39697209 100644 --- a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts +++ b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts @@ -16,19 +16,40 @@ import Extension from '@ohos.application.FileExtensionAbility' import uriClass from '@ohos.uri' import fileio from '@ohos.fileio' import hilog from '@ohos.hilog' +import { init, addVolumeInfo, delVolumeInfo, path2uri, getVolumeInfoList } from './VolumeManager' +import { onReceiveEvent } from './Subcriber' import fileExtensionInfo from "@ohos.fileExtensionInfo" const FLAG = fileExtensionInfo.FLAG; export default class FileExtAbility extends Extension { onCreate(want) { - hilog.info(0x0001, 'js server tag dsa', 'FileExtAbility onCreate, want:' + want.abilityName); + init(); + onReceiveEvent(function (data) { + let parameters = data.parameters; + let flag = FLAG.SUPPORTS_WRITE | FLAG.SUPPORTS_DELETE | FLAG.SUPPORTS_RENAME | FLAG.SUPPORTS_COPY + | FLAG.SUPPORTS_MOVE | FLAG.SUPPORTS_REMOVE | FLAG.DIR_SUPPORTS_CREATE | FLAG.DIR_PREFERS_LAST_MODIFIED; + if (data.event == 'usual.event.data.VOLUME_MOUNTED') { + let volumeInfo = { + 'volumeId': parameters.id, + 'fsUuid': parameters.fsUuid, + 'path': parameters.path, + 'uri': path2uri(parameters.id, parameters.path), + 'flags': flag, + 'deviceId': '', + 'displayName': parameters.id, + 'type': 'SD' + } + addVolumeInfo(volumeInfo); + } else { + delVolumeInfo(parameters.id); + } + }); } getPath(uri) { let uriObj = new uriClass.URI(uri); let path = uriObj.path; - hilog.info(0x0001, 'js server tag dsa', 'getPath is ' + path); return path; } @@ -40,7 +61,6 @@ export default class FileExtAbility extends Extension { } else { newFileUri += '/' + displayName; } - hilog.info(0x0001, 'js server tag dsa', 'genNewFileUri is ' + newFileUri); return newFileUri; } @@ -72,12 +92,10 @@ export default class FileExtAbility extends Extension { newFileUri += arr[index] + '/'; } } - hilog.info(0x0001, 'js server tag dsa', 'renameUri is ' + newFileUri); return newFileUri; } listDir(path, cb) { - hilog.info(0x0001, 'js server tag dsa', 'listDir: ' + path); try { let stat = fileio.statSync(path); if (stat.isDirectory()) { @@ -88,7 +106,7 @@ export default class FileExtAbility extends Extension { let dirent = dir.readSync(); this.listDir(path + '/' + dirent.name, cb); } catch (e) { - hilog.info(0x0001, 'js server tag dsa', 'listDir dir.readSync catch' + e); + hilog.debug(0x0001, 'jsserver', 'listDir dir.readSync catch' + e); hasNextFile = false; cb(path); } @@ -97,19 +115,18 @@ export default class FileExtAbility extends Extension { cb(path); } } catch (e) { - hilog.info(0x0001, 'js server tag dsa', 'listDir catch ' + e + ' ' + path); + hilog.debug(0x0001, 'jsserver', 'listDir catch ' + e + ' ' + path); cb(path); } } openFile(sourceFileUri, flags) { - hilog.info(0x0001, 'js server tag dsa', 'openFile, uri:' + sourceFileUri + ',flags:' + flags); let fd = 0; try { let path = this.getPath(sourceFileUri); fd = fileio.openSync(path, flags, 0o666); } catch (e) { - hilog.info(0x0001, 'js server tag dsa', 'openFile catch' + e); + hilog.debug(0x0001, 'jsserver', 'openFile catch' + e); fd = -1; } @@ -121,46 +138,43 @@ export default class FileExtAbility extends Extension { fileio.closeSync(fd); return true; } catch (e) { - hilog.info(0x0001, 'js server tag dsa', 'closeFile catch' + e); + hilog.debug(0x0001, 'jsserver', 'closeFile catch' + e); return false; } } createFile(parentUri, displayName) { - hilog.info(0x0001, 'js server tag dsa', 'createFile, uri:' + parentUri + ', displayName:' + displayName); try { let newFileUri = this.genNewFileUri(parentUri, displayName); let path = this.getPath(newFileUri); fileio.openSync(path, 0o100, 0o666); return newFileUri; } catch (e) { - hilog.info(0x0001, 'js server tag dsa', 'createFile catch' + e); + hilog.debug(0x0001, 'jsserver', 'createFile catch' + e); return ''; } } mkdir(parentUri, displayName) { - hilog.info(0x0001, 'js server tag dsa', 'mkdir, uri:' + parentUri + ', displayName:' + displayName); try { let newFileUri = this.genNewFileUri(parentUri, displayName); let path = this.getPath(newFileUri); fileio.mkdirSync(path); return newFileUri; } catch (e) { - hilog.info(0x0001, 'js server tag dsa', 'mkdir catch' + e); + hilog.debug(0x0001, 'jsserver', 'mkdir catch' + e); return ''; } } delete(selectFileUri) { - hilog.info(0x0001, 'js server tag dsa', 'delete, uri:' + selectFileUri); let path = this.getPath(selectFileUri); let code = 0; this.listDir(path, function (filePath) { try { fileio.unlinkSync(filePath); } catch (e) { - hilog.info(0x0001, 'js server tag dsa', 'delete catch' + e); + hilog.debug(0x0001, 'jsserver', 'delete catch' + e); code = -1; } }); @@ -169,7 +183,6 @@ export default class FileExtAbility extends Extension { } move(sourceFileUri, targetParentUri) { - hilog.info(0x0001, 'js server tag dsa', 'move, sourceFileUri:' + sourceFileUri + ', targetParentUri:' + targetParentUri); try { let displayName = this.getFileName(sourceFileUri); let newFileUri = this.genNewFileUri(targetParentUri, displayName); @@ -178,13 +191,12 @@ export default class FileExtAbility extends Extension { fileio.renameSync(oldPath, newPath); return newFileUri; } catch (e) { - hilog.info(0x0001, 'js server tag dsa', 'rename catch' + e); + hilog.debug(0x0001, 'jsserver', 'rename catch' + e); return ''; } } rename(sourceFileUri, displayName) { - hilog.info(0x0001, 'js server tag dsa', 'rename, sourceFileUri:' + sourceFileUri + ', displayName:' + displayName); try { let newFileUri = this.renameUri(sourceFileUri, displayName); let oldPath = this.getPath(sourceFileUri); @@ -192,13 +204,12 @@ export default class FileExtAbility extends Extension { fileio.renameSync(oldPath, newPath); return newFileUri; } catch (e) { - hilog.info(0x0001, 'js server tag dsa', 'rename catch' + e); + hilog.debug(0x0001, 'jsserver', 'rename catch' + e); return ''; } } query(sourceFileUri) { - hilog.info(0x0001, 'js server tag dsa', 'query, sourceFileUri:' + sourceFileUri); try { let path = this.getPath(sourceFileUri); let stat = fileio.statSync(path); @@ -211,13 +222,12 @@ export default class FileExtAbility extends Extension { mimiType: '', }; } catch (e) { - hilog.info(0x0001, 'js server tag dsa', 'query catch' + e); + hilog.debug(0x0001, 'jsserver', 'query catch' + e); return null; } } listFile(sourceFileUri) { - hilog.info(0x0001, 'js server tag dsa', 'listFile, sourceFileUri: ' + sourceFileUri); let infos = []; try { @@ -237,27 +247,25 @@ export default class FileExtAbility extends Extension { mimiType: '', }); } catch (e) { - hilog.info(0x0001, 'js server tag dsa', 'dir.readSync catch' + e); + hilog.debug(0x0001, 'jsserver', 'dir.readSync catch' + e); hasNextFile = false; } } } catch (e) { - hilog.info(0x0001, 'js server tag dsa', 'listFile catch' + e); + hilog.debug(0x0001, 'jsserver', 'listFile catch' + e); } - hilog.info(0x0001, 'js server tag dsa', 'listFile return: ' + JSON.stringify(infos)); return infos; } getRoots() { - hilog.info(0x0001, 'js server tag dsa', 'getRoots'); - let roots = [{ + let roots = getVolumeInfoList().concat({ uri: 'fileAccess:///data/storage/el1/bundle/storage_daemon', displayName: 'storage_daemon', deviceId: '', flags: FLAG.SUPPORTS_WRITE | FLAG.SUPPORTS_DELETE | FLAG.SUPPORTS_RENAME | FLAG.SUPPORTS_COPY | FLAG.SUPPORTS_MOVE | FLAG.SUPPORTS_REMOVE | FLAG.DIR_SUPPORTS_CREATE | FLAG.DIR_PREFERS_LAST_MODIFIED, - }]; + }); return roots; } }; \ No newline at end of file diff --git a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/Subcriber.ts b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/Subcriber.ts new file mode 100644 index 00000000..c68446f4 --- /dev/null +++ b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/Subcriber.ts @@ -0,0 +1,34 @@ +/* +* Copyright (c) 2022 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. +*/ +import CommonEvent from '@ohos.commonevent' +async function onReceiveEvent(callBack) { + //订阅者信息 + var subscribeInfo = { + events: [ + "usual.event.data.VOLUME_REMOVED", + "usual.event.data.VOLUME_UNMOUNTED", + "usual.event.data.VOLUME_MOUNTED", + "usual.event.data.VOLUME_BAD_REMOVAL", + "usual.event.data.VOLUME_EJECT" + ] + }; + let subscriber = await CommonEvent.createSubscriber(subscribeInfo); + //订阅公共事件 + CommonEvent.subscribe(subscriber, function (err, data) { + callBack(data) + }) +} + +export { onReceiveEvent } \ No newline at end of file diff --git a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/VolumeManager.ts b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/VolumeManager.ts new file mode 100644 index 00000000..10100910 --- /dev/null +++ b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/VolumeManager.ts @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2022 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. + */ +import volumeManager from '@ohos.volumeManager' +import hilog from '@ohos.hilog' +import fileExtensionInfo from "@ohos.fileExtensionInfo" +if (!globalThis.volumeInfoList) { + globalThis.volumeInfoList = []; +} + +const FLAG = fileExtensionInfo.FLAG; + +function init() { + try { + volumeManager.getAllVolumes().then((volumes) => { + let flag = FLAG.SUPPORTS_WRITE | FLAG.SUPPORTS_DELETE | FLAG.SUPPORTS_RENAME | FLAG.SUPPORTS_COPY + | FLAG.SUPPORTS_MOVE | FLAG.SUPPORTS_REMOVE | FLAG.DIR_SUPPORTS_CREATE | FLAG.DIR_PREFERS_LAST_MODIFIED; + for (let i = 0; i < volumes.length; i++) { + let volume = volumes[i]; + let volumeInfo = { + 'volumeId': volume.id, + 'fsUuid': volume.uuid, + 'path': volume.path, + 'uri': path2uri(volume.id, volume.path), + 'displayName': volume.id, + 'deviceId': '', + 'flags': flag, + 'type': 'SD' + } + globalThis.volumeInfoList.push(volumeInfo); + } + }); + + } catch (err) { + hilog.debug(0x0001, 'jsserver', "err============>" + err); + } +} + +function addVolumeInfo(volumeInfo) { + globalThis.volumeInfoList.push(volumeInfo); +} + +function path2uri(id, path) { + return `fileAccess://${id}/${path}`; +} + +function delVolumeInfo(volumeId) { + globalThis.volumeInfoList = globalThis.volumeInfoList.filter((volume) => volume.volumeId !== volumeId); +} + +function getVolumeInfo(volumeId) { + let volumeInfo = globalThis.volumeInfoList.filter((volume) => volume.volumeId === volumeId); + return volumeInfo; +} + +function getVolumeInfoList() { + return globalThis.volumeInfoList; +} + +function notifyChange() { + +} +export { init, addVolumeInfo, delVolumeInfo, getVolumeInfoList, path2uri } \ No newline at end of file -- Gitee From b7e778e4dd0010b532ab520c5bad8fa6c42739fc Mon Sep 17 00:00:00 2001 From: zhangxingxia Date: Fri, 17 Jun 2022 10:40:21 +0800 Subject: [PATCH 2/2] hap devicemanager Signed-off-by: zhangxingxia --- .../entry/src/main/ets/FileExtensionAbility/Subcriber.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/Subcriber.ts b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/Subcriber.ts index c68446f4..a2e8acec 100644 --- a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/Subcriber.ts +++ b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/Subcriber.ts @@ -14,7 +14,6 @@ */ import CommonEvent from '@ohos.commonevent' async function onReceiveEvent(callBack) { - //订阅者信息 var subscribeInfo = { events: [ "usual.event.data.VOLUME_REMOVED", @@ -25,7 +24,6 @@ async function onReceiveEvent(callBack) { ] }; let subscriber = await CommonEvent.createSubscriber(subscribeInfo); - //订阅公共事件 CommonEvent.subscribe(subscriber, function (err, data) { callBack(data) }) -- Gitee