diff --git a/ability/EtsCommonEvent/README_zh.md b/ability/EtsCommonEvent/README_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..3def85013a1fc27084637757096f779e6caaac0e --- /dev/null +++ b/ability/EtsCommonEvent/README_zh.md @@ -0,0 +1,17 @@ +# Ets公共事件 + + + +### 简介 + +本示例展示了在eTS中如何使用CommonEvent的接口完成创建订阅者、订阅公共事件、发布公共事件、取消订阅的功能。 + +### 使用说明 + +1、页面中先点击**创建订阅者**、**订阅公共事件**,下方编辑框中显示执行结果;然后点击**发布公共事件**和**发布公共事件指定发布信息**,下方编辑框中展示公共事件发布成功后接收到的公共事件信息,点击**取消订阅**可以取消公共事件订阅。 + + + +### 约束与限制 + +本示例仅支持标准系统上运行。 diff --git a/ability/EtsCommonEvent/build.gradle b/ability/EtsCommonEvent/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..8469dbed391d35ea3560bcb82b1258e27bdeb09a --- /dev/null +++ b/ability/EtsCommonEvent/build.gradle @@ -0,0 +1,34 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. +apply plugin: 'com.huawei.ohos.app' + +// For instructions on signature configuration, see https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ide_debug_device-0000001053822404#section1112183053510 +ohos { + compileSdkVersion 7 + supportSystem "standard" +} + +buildscript { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + } + dependencies { + classpath 'com.huawei.ohos:hap:3.0.3.4' + classpath 'com.huawei.ohos:decctest:1.2.6.0' + } +} + +allprojects { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + } +} diff --git a/ability/EtsCommonEvent/entry/build.gradle b/ability/EtsCommonEvent/entry/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..895f5ab55babc59d9158eda9724930a93d1c9c72 --- /dev/null +++ b/ability/EtsCommonEvent/entry/build.gradle @@ -0,0 +1,21 @@ +apply plugin: 'com.huawei.ohos.hap' +// For instructions on signature configuration, see https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ide_debug_device-0000001053822404#section1112183053510 +ohos { + compileSdkVersion 7 + defaultConfig { + compatibleSdkVersion 7 + } + buildTypes { + release { + proguardOpt { + proguardEnabled false + rulesFiles 'proguard-rules.pro' + } + } + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar', '*.har']) + testImplementation 'junit:junit:4.13.1' +} diff --git a/ability/EtsCommonEvent/entry/src/main/config.json b/ability/EtsCommonEvent/entry/src/main/config.json new file mode 100644 index 0000000000000000000000000000000000000000..a2fcefd2832ad44176c77ef367c28cb40ee09797 --- /dev/null +++ b/ability/EtsCommonEvent/entry/src/main/config.json @@ -0,0 +1,66 @@ +{ + "app": { + "bundleName": "ohos.samples.etscommonevent", + "vendor": "samples", + "version": { + "code": 1000000, + "name": "1.0.0" + } + }, + "deviceConfig": {}, + "module": { + "package": "ohos.samples.etscommonevent", + "name": ".MyApplication", + "mainAbility": ".MainAbility", + "deviceType": [ + "phone" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "entry", + "moduleType": "entry", + "installationFree": false + }, + "abilities": [ + { + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + "orientation": "unspecified", + "visible": true, + "srcPath": "MainAbility", + "name": ".MainAbility", + "srcLanguage": "ets", + "icon": "$media:icon", + "description": "$string:description_mainability", + "formsEnabled": false, + "label": "$string:entry_MainAbility", + "type": "page", + "launchType": "standard" + } + ], + "js": [ + { + "mode": { + "syntax": "ets", + "type": "pageAbility" + }, + "pages": [ + "pages/index" + ], + "name": ".MainAbility", + "window": { + "designWidth": 720, + "autoDesignWidth": false + } + } + ] + } +} \ No newline at end of file diff --git a/ability/EtsCommonEvent/entry/src/main/ets/MainAbility/app.ets b/ability/EtsCommonEvent/entry/src/main/ets/MainAbility/app.ets new file mode 100644 index 0000000000000000000000000000000000000000..cfb9635413551f8ad3434468b138550cbad174d6 --- /dev/null +++ b/ability/EtsCommonEvent/entry/src/main/ets/MainAbility/app.ets @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2021 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. + */ +export default { + onCreate() { + console.info('Application onCreate') + }, + onDestroy() { + console.info('Application onDestroy') + }, +} \ No newline at end of file diff --git a/ability/EtsCommonEvent/entry/src/main/ets/MainAbility/common/publishEvent.ets b/ability/EtsCommonEvent/entry/src/main/ets/MainAbility/common/publishEvent.ets new file mode 100644 index 0000000000000000000000000000000000000000..28d63f06c4f336f04c1aabf0970b00a961bf1ea1 --- /dev/null +++ b/ability/EtsCommonEvent/entry/src/main/ets/MainAbility/common/publishEvent.ets @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2021 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'; + +@Component +export struct PublishEventView { + private pushlishOptions: Resource[] = [$r('app.string.publish'), $r('app.string.publish_specify_commonEvent')] + + publish() { + // Publish CommonEvent + commonEvent.publish("event", (err) => { + console.log('[CommonEvent]PublishCallBack err=' + err) + }); + } + + initialPublish() { + // CommonEvent related information + var options = { + code: 0, // Initial code for CommonEvent + data: "initial data", // Initial data for CommonEvent + isOrdered: true // Orderly CommonEvent + } + // Publish CommonEvent + commonEvent.publish("event", options, (err) => { + console.log('[CommonEvent]PublishCallBack' + err) + }); + } + + build(){ + Column(){ + ForEach(this.pushlishOptions, item => { + Button() { + Text(item) + .fontSize(19) + .fontWeight(FontWeight.Bold) + .padding({ left: 10, right: 10 }) + .width('100%') + .textAlign(TextAlign.Center) + }.type(ButtonType.Capsule) + .backgroundColor('#0D9FFB') + .margin({ top: 15 }) + .onClick(() => { + var index = this.pushlishOptions.indexOf(item) + switch (index) { + case 0: + console.log('[CommonEvent]Publish1 onClick') + this.publish() + break + case 1: + console.log('[CommonEvent]Unsubscribe onClick') + this.initialPublish() + break + default: + break + } + }) + }, item => JSON.stringify(item)) + } + } +} \ No newline at end of file diff --git a/ability/EtsCommonEvent/entry/src/main/ets/MainAbility/common/subsrcibeEvent.ets b/ability/EtsCommonEvent/entry/src/main/ets/MainAbility/common/subsrcibeEvent.ets new file mode 100644 index 0000000000000000000000000000000000000000..d6e737b87ea12ea3309456ea19402aaf97e3a2ea --- /dev/null +++ b/ability/EtsCommonEvent/entry/src/main/ets/MainAbility/common/subsrcibeEvent.ets @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2021 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'; +import prompt from '@system.prompt'; + +@Component +export struct SubsrcibeEventView { + private subsrcibeOptions: Resource[] = [$r('app.string.create_subscriber'), $r('app.string.subscribe'), $r('app.string.unsubscribe')] + private subscriber = null + @Link result:string + + createSubscriber() { + // Subscriber information + var subscribeInfo = { + events: ["event"], + priority: 100 + }; + // Create subscriber callback + commonEvent.createSubscriber(subscribeInfo, (err, data) => { + console.log('[CommonEvent]CreateSubscriberCallBack') + this.subscriber = data; + this.result = 'createSubscriber succeed' + }) + } + + subscribe() { + // Subscribe + if (this.subscriber != null) { + commonEvent.subscribe(this.subscriber, (err, data) => { + console.log('[CommonEvent]SubscribeCallBack' + JSON.stringify(data)) + this.result = 'receive ,event = ' + data.event + ',data = ' + data.data + }); + this.result = 'subscribe succeed' + } else { + prompt.showToast({ message: 'need createSubscriber' }) + } + } + + unSubscribe() { + // Unsubscribe CommonEvent + if (this.subscriber != null) { + commonEvent.unsubscribe(this.subscriber, () => { + console.log('[CommonEvent]UnsubscribeCallBack') + this.subscriber = null + this.result = 'unsubscribe succeed' + }) + } + } + + build() { + Column() { + ForEach(this.subsrcibeOptions, item => { + Button() { + Text(item) + .fontSize(19) + .fontWeight(FontWeight.Bold) + .padding({ left: 10, right: 10 }) + .width('100%') + .textAlign(TextAlign.Center) + }.type(ButtonType.Capsule) + .backgroundColor('#0D9FFB') + .margin({ top: 15 }) + .onClick(() => { + var index = this.subsrcibeOptions.indexOf(item) + switch (index) { + case 0: + console.log('[CommonEvent]CreateSubscriber onClick') + this.createSubscriber() + break + case 1: + console.log('[CommonEvent]Subscribe onClick') + this.subscribe() + break + case 2: + console.log('[CommonEvent]Publish onClick') + this.unSubscribe() + break + default: + break + } + }) + }, item => JSON.stringify(item)) + } + } +} \ No newline at end of file diff --git a/ability/EtsCommonEvent/entry/src/main/ets/MainAbility/pages/index.ets b/ability/EtsCommonEvent/entry/src/main/ets/MainAbility/pages/index.ets new file mode 100644 index 0000000000000000000000000000000000000000..0fbb61cfdf789272191dcf2d845657a25b307467 --- /dev/null +++ b/ability/EtsCommonEvent/entry/src/main/ets/MainAbility/pages/index.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2021 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 {PublishEventView} from '../common/publishEvent' +import {SubsrcibeEventView} from '../common/subsrcibeEvent' + +@Entry +@Component +struct Index { + @State result: string = '' + + build() { + Column() { + Text($r("app.string.entry_MainAbility")) + .width('100%') + .height(50) + .backgroundColor('#0D9FFB') + .fontColor(Color.White) + .fontSize(20) + .padding({ left: 15 }) + Scroll() { + Column() { + SubsrcibeEventView({ result: $result }) + + Line() + .width('100%').height(2) + .backgroundColor('#D5D5D5') + .margin({ top: 15}) + + PublishEventView() + + Text(this.result) + .width('100%') + .height(150) + .backgroundColor('#DFDFDF') + .fontSize(20) + .margin({ top: 20 }) + .textAlign(TextAlign.Start) + .padding(10) + }.constraintSize({ minHeight: '100%' }) + } + .width('100%').height('100%') + .padding({ left: 15, right: 15, bottom: 15 }) + } + } +} \ No newline at end of file diff --git a/ability/EtsCommonEvent/entry/src/main/resources/base/element/string.json b/ability/EtsCommonEvent/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..f1d269756199ed7d4d831b46461763ba2e6711ca --- /dev/null +++ b/ability/EtsCommonEvent/entry/src/main/resources/base/element/string.json @@ -0,0 +1,32 @@ +{ + "string": [ + { + "name": "entry_MainAbility", + "value": "EtsCommonEvent" + }, + { + "name": "description_mainability", + "value": "EtsCommonEvent Ability" + }, + { + "name": "create_subscriber", + "value": "Create subscriber" + }, + { + "name": "subscribe", + "value": "Subscribe" + }, + { + "name": "publish", + "value": "Publish commonEvent" + }, + { + "name": "publish_specify_commonEvent", + "value": "Publish specify commonEvent" + }, + { + "name": "unsubscribe", + "value": "Unsubscribe" + } + ] +} \ No newline at end of file diff --git a/ability/EtsCommonEvent/entry/src/main/resources/base/media/icon.png b/ability/EtsCommonEvent/entry/src/main/resources/base/media/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..ce307a8827bd75456441ceb57d530e4c8d45d36c Binary files /dev/null and b/ability/EtsCommonEvent/entry/src/main/resources/base/media/icon.png differ diff --git a/ability/EtsCommonEvent/entry/src/main/resources/en/element/string.json b/ability/EtsCommonEvent/entry/src/main/resources/en/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..f1d269756199ed7d4d831b46461763ba2e6711ca --- /dev/null +++ b/ability/EtsCommonEvent/entry/src/main/resources/en/element/string.json @@ -0,0 +1,32 @@ +{ + "string": [ + { + "name": "entry_MainAbility", + "value": "EtsCommonEvent" + }, + { + "name": "description_mainability", + "value": "EtsCommonEvent Ability" + }, + { + "name": "create_subscriber", + "value": "Create subscriber" + }, + { + "name": "subscribe", + "value": "Subscribe" + }, + { + "name": "publish", + "value": "Publish commonEvent" + }, + { + "name": "publish_specify_commonEvent", + "value": "Publish specify commonEvent" + }, + { + "name": "unsubscribe", + "value": "Unsubscribe" + } + ] +} \ No newline at end of file diff --git a/ability/EtsCommonEvent/entry/src/main/resources/zh/element/string.json b/ability/EtsCommonEvent/entry/src/main/resources/zh/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..d6cb3696bfc21824b7f93745a995bd80f231b483 --- /dev/null +++ b/ability/EtsCommonEvent/entry/src/main/resources/zh/element/string.json @@ -0,0 +1,32 @@ +{ + "string": [ + { + "name": "entry_MainAbility", + "value": "Ets公共事件" + }, + { + "name": "description_mainability", + "value": "EtsCommonEvent Ability" + }, + { + "name": "create_subscriber", + "value": "创建订阅者" + }, + { + "name": "subscribe", + "value": "订阅公共事件" + }, + { + "name": "publish", + "value": "发布公共事件" + }, + { + "name": "publish_specify_commonEvent", + "value": "发布公共事件指定发布信息" + }, + { + "name": "unsubscribe", + "value": "取消订阅" + } + ] +} \ No newline at end of file diff --git a/ability/EtsCommonEvent/screenshots/devices/main.PNG b/ability/EtsCommonEvent/screenshots/devices/main.PNG new file mode 100644 index 0000000000000000000000000000000000000000..f82693414bf1de889a7db9690da5c55bc436b467 Binary files /dev/null and b/ability/EtsCommonEvent/screenshots/devices/main.PNG differ diff --git a/ability/EtsCommonEvent/settings.gradle b/ability/EtsCommonEvent/settings.gradle new file mode 100644 index 0000000000000000000000000000000000000000..4773db73233a570c2d0c01a22e75321acfbf7a07 --- /dev/null +++ b/ability/EtsCommonEvent/settings.gradle @@ -0,0 +1 @@ +include ':entry'