diff --git a/ability/ability_runtime/stage/actsuripermission/actsassistanthap/entry/src/main/ets/Hap2MainAbility4/Hap2MainAbility4.ts b/ability/ability_runtime/stage/actsuripermission/actsassistanthap/entry/src/main/ets/Hap2MainAbility4/Hap2MainAbility4.ts index 0000e698d7813ba8aed8adae82c9b8d5cf624c1f..aab0e3f47f1c2252cbca8962fb3dc95b8cac82e5 100755 --- a/ability/ability_runtime/stage/actsuripermission/actsassistanthap/entry/src/main/ets/Hap2MainAbility4/Hap2MainAbility4.ts +++ b/ability/ability_runtime/stage/actsuripermission/actsassistanthap/entry/src/main/ets/Hap2MainAbility4/Hap2MainAbility4.ts @@ -15,97 +15,101 @@ import Ability from '@ohos.app.ability.UIAbility' import commonEvent from '@ohos.commonEvent'; +function sleep(time) { + return new Promise((resolve) => setTimeout(resolve, time)) +} export default class Hap2MainAbility4 extends Ability { - onCreate(want, launchParam) { - console.log('[Demo] Hap2MainAbility4 onCreate') - } + onCreate(want, launchParam) { + console.log('[Demo] Hap2MainAbility4 onCreate') + } - onDestroy() { - console.log('[Demo] Hap2MainAbility4 onDestroy') - } + onDestroy() { + console.log('[Demo] Hap2MainAbility4 onDestroy') + } + + onWindowStageCreate(windowStage) { + console.log('[Demo] Hap2MainAbility4 onWindowStageCreate') + windowStage.setUIContent(this.context, 'pages/Hap2MainAbility4_pages', null) + } + + onWindowStageDestroy() { + console.log('[Demo] Hap2MainAbility4 onWindowStageDestroy') + } - onWindowStageCreate(windowStage) { - console.log('[Demo] Hap2MainAbility4 onWindowStageCreate') - windowStage.setUIContent(this.context, 'pages/Hap2MainAbility4_pages', null) + onForeground() { + console.log('[Demo] Hap2MainAbility4 onForeground') + let uri : string = globalThis.abilityWant3.uri; + if (uri == null) { + console.log('[Demo] Hap2MainAbility1 uri is invalid'); + return; } + let eventName1 : string = 'Temp_UriPermissionTest_0300_Read_Successfully'; + let eventName2 : string = 'Temp_UriPermissionTest_0300_Read_Failed'; + globalThis.fs.open(uri, globalThis.fs.OpenMode.READ_ONLY).then((file) => { + console.info('file fd: ' + file.fd); + commonEvent.publish(eventName1, (err) => { + console.log('Hap2MainAbility4' + eventName1); + }); + }).catch((err) => { + console.info('open file failed with error message: ' + err.message + ', error code: ' + err.code); + commonEvent.publish(eventName2, (err) => { + console.log('Hap2MainAbility4' + eventName2); + }); + }); - onWindowStageDestroy() { - console.log('[Demo] Hap2MainAbility4 onWindowStageDestroy') + let eventName3 : string = 'Temp_UriPermissionTest_0300_Write_Successfully'; + let eventName4 : string = 'Temp_UriPermissionTest_0300_Write_Failed'; + globalThis.fs.open(uri, globalThis.fs.OpenMode.READ_WRITE).then((file) => { + console.info('file fd: ' + file.fd); + commonEvent.publish(eventName3, (err) => { + console.log('Hap2MainAbility4' + eventName3); + }); + }).catch((err) => { + console.info('open file failed with error message: ' + err.message + ', error code: ' + err.code); + commonEvent.publish(eventName4, (err) => { + console.log('Hap2MainAbility4' + eventName4); + }); + }); + + let eventName5 : string = 'TerminateDone_Hap2MainAbility3'; + let subscribeInfo = { + events: [eventName5] } - onForeground() { - console.log('[Demo] Hap2MainAbility4 onForeground') - let uri : string = globalThis.abilityWant3.uri; - if (uri == null) { - console.log('[Demo] Hap2MainAbility1 uri is invalid'); - return; - } - let eventName1 : string = 'Temp_UriPermissionTest_0300_Read_Successfully'; - let eventName2 : string = 'Temp_UriPermissionTest_0300_Read_Failed'; + let eventName6 : string = 'Temp_UriPermissionTest_0300_Read_Successfully_'; + let eventName7 : string = 'Temp_UriPermissionTest_0300_Read_Failed_'; + async function SubscribeCallBackContext(err, data) { + console.log(' Subscribe CallBack data:' + JSON.stringify(data)); + if (data.event === eventName5) { + await sleep(1000); globalThis.fs.open(uri, globalThis.fs.OpenMode.READ_ONLY).then((file) => { - console.info('file fd: ' + file.fd); - commonEvent.publish(eventName1, (err) => { - console.log('Hap2MainAbility4' + eventName1); - }); - }).catch((err) => { - console.info('open file failed with error message: ' + err.message + ', error code: ' + err.code); - commonEvent.publish(eventName2, (err) => { - console.log('Hap2MainAbility4' + eventName2); - }); - }); - - let eventName3 : string = 'Temp_UriPermissionTest_0300_Write_Successfully'; - let eventName4 : string = 'Temp_UriPermissionTest_0300_Write_Failed'; - globalThis.fs.open(uri, globalThis.fs.OpenMode.READ_WRITE).then((file) => { - console.info('file fd: ' + file.fd); - commonEvent.publish(eventName3, (err) => { - console.log('Hap2MainAbility4' + eventName3); - }); + console.info('file fd: ' + file.fd); + commonEvent.publish(eventName6, (err) => { + console.log('Hap2MainAbility4' + eventName6); + }); }).catch((err) => { - console.info('open file failed with error message: ' + err.message + ', error code: ' + err.code); - commonEvent.publish(eventName4, (err) => { - console.log('Hap2MainAbility4' + eventName4); - }); + console.info('open file failed with error message: ' + err.message + ', error code: ' + err.code); + commonEvent.publish(eventName7, (err) => { + console.log('Hap2MainAbility4' + eventName7); + }); }); + } + } - let eventName5 : string = 'TerminateDone_Hap2MainAbility3'; - let subscribeInfo = { - events: [eventName5] - } - - let eventName6 : string = 'Temp_UriPermissionTest_0300_Read_Successfully_'; - let eventName7 : string = 'Temp_UriPermissionTest_0300_Read_Failed_'; - function SubscribeCallBackContext(err, data) { - console.log(' Subscribe CallBack data:' + JSON.stringify(data)); - if (data.event === eventName5) { - globalThis.fs.open(uri, globalThis.fs.OpenMode.READ_ONLY).then((file) => { - console.info('file fd: ' + file.fd); - commonEvent.publish(eventName6, (err) => { - console.log('Hap2MainAbility4' + eventName6); - }); - }).catch((err) => { - console.info('open file failed with error message: ' + err.message + ', error code: ' + err.code); - commonEvent.publish(eventName7, (err) => { - console.log('Hap2MainAbility4' + eventName7); - }); - }); - } - } + let subscriber__1; + commonEvent.createSubscriber(subscribeInfo).then(async (data) => { + subscriber__1 = data; + console.debug('====>Create Subscriber====>'); + await commonEvent.subscribe(subscriber__1, SubscribeCallBackContext); + }); - let subscriber__1; - commonEvent.createSubscriber(subscribeInfo).then(async (data) => { - subscriber__1 = data; - console.debug('====>Create Subscriber====>'); - await commonEvent.subscribe(subscriber__1, SubscribeCallBackContext); - }); + let eventName : string = 'TerminateSelf_Hap2MainAbility3'; + commonEvent.publish(eventName, (err) => { + console.log('Hap2MainAbility4' + eventName); + }); + } - let eventName : string = 'TerminateSelf_Hap2MainAbility3'; - commonEvent.publish(eventName, (err) => { - console.log('Hap2MainAbility4' + eventName); - }); - } - - onBackground() { - console.log('[Demo] Hap2MainAbility4 onBackground') - } + onBackground() { + console.log('[Demo] Hap2MainAbility4 onBackground') + } }; diff --git a/ability/ability_runtime/stage/actsuripermission/actsassistanthap2/entry/src/main/ets/Hap3MainAbility1/Hap3MainAbility1.ts b/ability/ability_runtime/stage/actsuripermission/actsassistanthap2/entry/src/main/ets/Hap3MainAbility1/Hap3MainAbility1.ts index 70bfa3b642551c219846fc795968ef1d3cca738f..203a29a95c58e984e8ab27608a73a07971e5094d 100644 --- a/ability/ability_runtime/stage/actsuripermission/actsassistanthap2/entry/src/main/ets/Hap3MainAbility1/Hap3MainAbility1.ts +++ b/ability/ability_runtime/stage/actsuripermission/actsassistanthap2/entry/src/main/ets/Hap3MainAbility1/Hap3MainAbility1.ts @@ -17,89 +17,94 @@ import Ability from '@ohos.app.ability.UIAbility' import commonEvent from '@ohos.commonEvent'; import fs from '@ohos.file.fs' +function sleep(time) { + return new Promise((resolve) => setTimeout(resolve, time)) +} + export default class Hap3MainAbility1 extends Ability { - onCreate(want, launchParam) { - console.log('[Demo] Hap3MainAbility1 onCreate') - globalThis.abilityWant = want; - } + onCreate(want, launchParam) { + console.log('[Demo] Hap3MainAbility1 onCreate') + globalThis.abilityWant = want; + } - onDestroy() { - console.log('[Demo] Hap3MainAbility1 onDestroy') - } + onDestroy() { + console.log('[Demo] Hap3MainAbility1 onDestroy') + } - onWindowStageCreate(windowStage) { - console.log('[Demo] Hap3MainAbility1 onWindowStageCreate') - windowStage.setUIContent(this.context, 'pages/Hap3MainAbility1_pages', null) - } + onWindowStageCreate(windowStage) { + console.log('[Demo] Hap3MainAbility1 onWindowStageCreate') + windowStage.setUIContent(this.context, 'pages/Hap3MainAbility1_pages', null) + } - onWindowStageDestroy() { - console.log('[Demo] Hap3MainAbility1 onWindowStageDestroy') - } - - onForeground() { - console.log('[Demo] Hap3MainAbility1 onForeground') - let eventName : string = 'TerminateNotify_Hap3MainAbility1'; - let subscribeInfo = { - events: [eventName] - } - let subscriber_1; - function UnSubscribeInfoCallback_1(err, data) { - console.info("Hap2MainAbility1 ===UnSubscribeInfoCallback_1===") - } + onWindowStageDestroy() { + console.log('[Demo] Hap3MainAbility1 onWindowStageDestroy') + } - function SubscribeCallBackContext(err, data) { - console.log('Hap3MainAbility1 Subscribe CallBack data:' + JSON.stringify(data)); - if (data.event === eventName) { - let uri : string = globalThis.abilityWant.uri; - if (uri == null) { - console.log('[Demo] Hap3MainAbility1 uri is invalid'); - return; - } - console.info('[Demo] Hap3MainAbility1 uri is ' + JSON.stringify(uri)); - let eventName13_1 : string = 'Temp_UriPermissionTest_1300_Read_Successfully'; - let eventName13_2 : string = 'Temp_UriPermissionTest_1300_Read_Failed'; - fs.open(uri, fs.OpenMode.READ_ONLY).then((file) => { - console.info('file fd: ' + file.fd); - commonEvent.publish(eventName13_1, (err) => { - console.log('Hap3MainAbility1' + eventName13_1); - }); - }).catch((err) => { - console.info('open file failed with error message: ' + err.message + ', error code: ' + err.code); - commonEvent.publish(eventName13_2, (err) => { - console.log('Hap3MainAbility1' + eventName13_2); - }); - }); + onForeground() { + console.log('[Demo] Hap3MainAbility1 onForeground') + let eventName : string = 'TerminateNotify_Hap3MainAbility1'; + let subscribeInfo = { + events: [eventName] + } + let subscriber_1; + function UnSubscribeInfoCallback_1(err, data) { + console.info("Hap2MainAbility1 ===UnSubscribeInfoCallback_1===") + } - let eventName13_3 : string = 'Temp_UriPermissionTest_1300_Write_Successfully'; - let eventName13_4 : string = 'Temp_UriPermissionTest_1300_Write_Failed'; - fs.open(uri, fs.OpenMode.READ_WRITE).then((file) => { - console.info('file fd: ' + file.fd); - commonEvent.publish(eventName13_3, (err) => { - console.log('Hap3MainAbility1' + eventName13_3); - }); - }).catch((err) => { - console.info('open file failed with error message: ' + err.message + ', error code: ' + err.code); - commonEvent.publish(eventName13_4, (err) => { - console.log('Hap3MainAbility1' + eventName13_4); - }); - }); - } - commonEvent.unsubscribe(subscriber_1, UnSubscribeInfoCallback_1) + async function SubscribeCallBackContext(err, data) { + console.log('Hap3MainAbility1 Subscribe CallBack data:' + JSON.stringify(data)); + if (data.event === eventName) { + await sleep(1000); + let uri : string = globalThis.abilityWant.uri; + if (uri == null) { + console.log('[Demo] Hap3MainAbility1 uri is invalid'); + return; } - - commonEvent.createSubscriber(subscribeInfo).then(async (data) => { - subscriber_1 = data; - console.debug('====>Create Subscriber====>'); - await commonEvent.subscribe(subscriber_1, SubscribeCallBackContext); + console.info('[Demo] Hap3MainAbility1 uri is ' + JSON.stringify(uri)); + let eventName13_1 : string = 'Temp_UriPermissionTest_1300_Read_Successfully'; + let eventName13_2 : string = 'Temp_UriPermissionTest_1300_Read_Failed'; + fs.open(uri, fs.OpenMode.READ_ONLY).then((file) => { + console.info('file fd: ' + file.fd); + commonEvent.publish(eventName13_1, (err) => { + console.log('Hap3MainAbility1' + eventName13_1); + }); + }).catch((err) => { + console.info('open file failed with error message: ' + err.message + ', error code: ' + err.code); + commonEvent.publish(eventName13_2, (err) => { + console.log('Hap3MainAbility1' + eventName13_2); + }); }); - let eventName1 : string = 'RevicedFrom_Hap3MainAbility1'; - commonEvent.publish(eventName1, (err) => { - console.log('Hap3MainAbility1' + eventName1); + let eventName13_3 : string = 'Temp_UriPermissionTest_1300_Write_Successfully'; + let eventName13_4 : string = 'Temp_UriPermissionTest_1300_Write_Failed'; + fs.open(uri, fs.OpenMode.READ_WRITE).then((file) => { + console.info('file fd: ' + file.fd); + commonEvent.publish(eventName13_3, (err) => { + console.log('Hap3MainAbility1' + eventName13_3); + }); + }).catch((err) => { + console.info('open file failed with error message: ' + err.message + ', error code: ' + err.code); + commonEvent.publish(eventName13_4, (err) => { + console.log('Hap3MainAbility1' + eventName13_4); + }); }); + } + commonEvent.unsubscribe(subscriber_1, UnSubscribeInfoCallback_1) } - onBackground() { - console.log('[Demo] Hap3MainAbility1 onBackground') - } + commonEvent.createSubscriber(subscribeInfo).then(async (data) => { + subscriber_1 = data; + console.debug('====>Create Subscriber====>'); + await commonEvent.subscribe(subscriber_1, SubscribeCallBackContext); + }); + + let eventName1 : string = 'RevicedFrom_Hap3MainAbility1'; + commonEvent.publish(eventName1, (err) => { + console.log('Hap3MainAbility1' + eventName1); + }); + } + + onBackground() { + console.log('[Demo] Hap3MainAbility1 onBackground') + } };