From df42434551d650b0e7a860e2ade54875a172c679 Mon Sep 17 00:00:00 2001 From: dengxiaoyu Date: Sat, 12 Jul 2025 11:26:04 +0800 Subject: [PATCH 1/3] add mech ext Signed-off-by: dengxiaoyu --- application/AppScope/app.json | 4 +- application/AppScope/app.json5 | 4 +- .../ets/mechextability/MechExtAbility.ets | 55 +++++++ .../entry/src/main/ets/pages/MechControl.ets | 155 ++++++++++++++++++ application/entry/src/main/module.json | 8 + application/entry/src/main/module.json5 | 8 + .../main/resources/base/element/color.json | 8 + .../main/resources/base/element/string.json | 36 ++++ .../base/media/intelligent_tracking.svg | 4 + .../base/media/intelligent_tracking_dark.svg | 4 + .../base/media/track_layout_center.svg | 6 + .../base/media/track_layout_center_dark.svg | 6 + .../base/media/track_layout_left.svg | 5 + .../base/media/track_layout_left_dark.svg | 5 + .../base/media/track_layout_right.svg | 4 + .../base/media/track_layout_right_dark.svg | 4 + .../resources/base/profile/main_pages.json | 3 +- .../main/resources/zh_CN/element/string.json | 36 ++++ 18 files changed, 350 insertions(+), 5 deletions(-) create mode 100644 application/entry/src/main/ets/mechextability/MechExtAbility.ets create mode 100644 application/entry/src/main/ets/pages/MechControl.ets create mode 100644 application/entry/src/main/resources/base/media/intelligent_tracking.svg create mode 100644 application/entry/src/main/resources/base/media/intelligent_tracking_dark.svg create mode 100644 application/entry/src/main/resources/base/media/track_layout_center.svg create mode 100644 application/entry/src/main/resources/base/media/track_layout_center_dark.svg create mode 100644 application/entry/src/main/resources/base/media/track_layout_left.svg create mode 100644 application/entry/src/main/resources/base/media/track_layout_left_dark.svg create mode 100644 application/entry/src/main/resources/base/media/track_layout_right.svg create mode 100644 application/entry/src/main/resources/base/media/track_layout_right_dark.svg diff --git a/application/AppScope/app.json b/application/AppScope/app.json index 73903a84..6dd83f4f 100644 --- a/application/AppScope/app.json +++ b/application/AppScope/app.json @@ -2,8 +2,8 @@ "app": { "bundleName": "com.ohos.dhardwareui", "vendor": "example", - "versionCode": 10000041, - "versionName": "1.0.41", + "versionCode": 10000042, + "versionName": "1.0.42", "icon": "$media:app_icon", "label": "$string:app_name", "minAPIVersion": 12, diff --git a/application/AppScope/app.json5 b/application/AppScope/app.json5 index 6ff633fb..e43d9ffc 100644 --- a/application/AppScope/app.json5 +++ b/application/AppScope/app.json5 @@ -16,8 +16,8 @@ "app": { "bundleName": "com.ohos.dhardwareui", "vendor": "example", - "versionCode": 10000041, - "versionName": "1.0.41", + "versionCode": 10000042, + "versionName": "1.0.42", "icon": "$media:app_icon", "label": "$string:app_name", "minAPIVersion": 12, diff --git a/application/entry/src/main/ets/mechextability/MechExtAbility.ets b/application/entry/src/main/ets/mechextability/MechExtAbility.ets new file mode 100644 index 00000000..e6c237f3 --- /dev/null +++ b/application/entry/src/main/ets/mechextability/MechExtAbility.ets @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2025 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 UIExtensionAbility from '@ohos.app.ability.UIExtensionAbility'; +import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; +import Want from '@ohos.app.ability.Want'; +import { logger } from '../utils/Logger'; + +const TAG: string = '[MechExtAbility]'; + +export default class MechExtAbility extends UIExtensionAbility { + onCreate() { + logger.info(`${TAG} UIExtAbility onCreate`); + AppStorage.setOrCreate('currentColorMode', this.context.config.colorMode); + AppStorage.setOrCreate('currentFontSizeScale', this.context.config.fontSizeScale); + } + + onForeground() { + logger.info(`${TAG} UIExtAbility onForeground`); + } + + onBackground() { + logger.info(`${TAG} UIExtAbility onBackground`); + } + + onDestroy() { + logger.info(`${TAG} UIExtAbility onDestroy`); + } + + onSessionCreate(want: Want, session: UIExtensionContentSession) { + logger.info(`${TAG} UIExtAbility onSessionCreate.`); + let param: Record = { + 'session': session + }; + let storage: LocalStorage = new LocalStorage(param); + session.loadContent('pages/MechControl', storage); + session.setWindowBackgroundColor('#00000000'); + logger.info(`${TAG} onSessionCreate end.`); + } + + onSessionDestroy(session: UIExtensionContentSession) { + logger.info(`${TAG} UIExtAbility onSessionDestroy`); + } +} \ No newline at end of file diff --git a/application/entry/src/main/ets/pages/MechControl.ets b/application/entry/src/main/ets/pages/MechControl.ets new file mode 100644 index 00000000..2f9f788b --- /dev/null +++ b/application/entry/src/main/ets/pages/MechControl.ets @@ -0,0 +1,155 @@ +/* + * Copyright (c) 2025 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 { logger } from '../utils/Logger'; +import mechanicManager from '@ohos.distributedHardware.mechanicManager'; +import { BusinessError } from '@kit.BasicServicesKit'; + +const TAG = '[MechControl_Page] : '; + +@Entry +@Component +struct MechControl { + @State isTrackingEnabled: boolean = false; + @State currentTrackingLayoutText: ResourceStr = $r('app.string.mech_center'); + @State currentTrackingLayoutImage: ResourceStr = $r('app.media.track_layout_center'); + + + aboutToAppear() { + logger.info(`${TAG} aboutToAppear in`); + } + + @Builder + IntelligentTracking(){ + Column(){ + Button() { + Image(this.isTrackingEnabled ? $r("app.media.intelligent_tracking") :$r("app.media.intelligent_tracking_dark")) + .width(20) + .height(20) + .margin({left: 3, top: 3}) + } + .width('40vp') + .height('40vp') + .backgroundColor(this.isTrackingEnabled ? $r('app.color.color_0A59F7_blue') : $r('sys.color.ohos_id_color_component_normal')) + .onClick(() => { + this.isTrackingEnabled = !this.isTrackingEnabled; + try { + if (this.isTrackingEnabled) { + logger.info(`${TAG} enable camera tracking`); + mechanicManager.setCameraTrackingEnabled(true); + } else { + logger.info(`${TAG} disable camera tracking`); + mechanicManager.setCameraTrackingEnabled(false); + } + } catch (error) { + let code: number = (error as BusinessError).code; + let message: string = (error as BusinessError).message; + logger.error(`${TAG} setCameraTrackingEnabled failed. error.code: ${code}, message: ${message}`); + } + logger.info(`${TAG} onClick end, isTrackingEnabled = ${this.isTrackingEnabled}`); + }) + + Text($r('app.string.mech_intelligent_tracking')) + .fontSize(12) + .lineHeight(16) + .fontWeight(FontWeight.Medium) + .fontColor($r('app.color.color_black')) + .margin({ left: 0, top: 4, right: 0 }) + .textAlign(TextAlign.Center) + + Text(this.isTrackingEnabled ? $r('app.string.mech_enable'): $r('app.string.mech_close')) + .fontSize(10) + .lineHeight(13) + .fontWeight(FontWeight.Regular) + .fontColor($r('app.color.color_black')) + .margin({ top: 2 }) + .textAlign(TextAlign.Center) + } + } + + @Builder + TrackingLayout(){ + Column(){ + Button() { + Image(this.currentTrackingLayoutImage) + .width(20) + .height(20) + } + .width('40vp') + .height('40vp') + .backgroundColor($r('app.color.color_0A59F7_blue')) + .onClick(() => { + this.isTrackingEnabled = !this.isTrackingEnabled; + try { + if (this.isTrackingEnabled) { + logger.info(`${TAG} enable camera tracking`); + mechanicManager.setCameraTrackingEnabled(true); + + this.currentTrackingLayoutImage = $r('app.media.track_layout_center'); + } else { + logger.info(`${TAG} disable camera tracking`); + mechanicManager.setCameraTrackingEnabled(false); + + this.currentTrackingLayoutImage = $r('app.media.track_layout_center_dark'); + } + } catch (error) { + let code: number = (error as BusinessError).code; + let message: string = (error as BusinessError).message; + logger.error(`${TAG} setCameraTrackingEnabled failed. error.code: ${code}, message: ${message}`); + } + logger.info(`${TAG} onClick end, isTrackingEnabled = ${this.isTrackingEnabled}`); + }) + + Text($r('app.string.mech_tracking_layout')) + .fontSize(12) + .lineHeight(16) + .fontWeight(FontWeight.Medium) + .fontColor($r('app.color.color_black')) + .margin({ left: 0, top: 4, right: 0 }) + .textAlign(TextAlign.Center) + + Text(this.currentTrackingLayoutText) + .fontSize(10) + .lineHeight(13) + .fontWeight(FontWeight.Regular) + .fontColor($r('app.color.color_black')) + .margin({ top: 2 }) + .textAlign(TextAlign.Center) + } + } + + build() { + Column() { + + Flex({ + direction: FlexDirection.Row, + justifyContent: FlexAlign.SpaceEvenly, + alignItems: ItemAlign.Center + }) { + this.IntelligentTracking(); + + if (this.isTrackingEnabled) { + this.TrackingLayout(); + } + } + .width('100%') + .height('100%') + + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.Center) + .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.BOTTOM]) + } +} \ No newline at end of file diff --git a/application/entry/src/main/module.json b/application/entry/src/main/module.json index 9765740f..56bb9fa4 100644 --- a/application/entry/src/main/module.json +++ b/application/entry/src/main/module.json @@ -62,6 +62,14 @@ "label": "$string:ContinueSwitchAbility_label", "type": "sys/commonUI", "exported": true + }, + { + "name": "MechExtAbility", + "srcEntry": "./ets/mechextability/MechExtAbility.ets", + "description": "$string:MechExtAbility_desc", + "label": "$string:MechExtAbility_label", + "type": "sys/commonUI", + "exported": true } ], "metadata": [ diff --git a/application/entry/src/main/module.json5 b/application/entry/src/main/module.json5 index f36e6173..ddfce3b7 100644 --- a/application/entry/src/main/module.json5 +++ b/application/entry/src/main/module.json5 @@ -76,6 +76,14 @@ "label": "$string:ContinueSwitchAbility_label", "type": "sys/commonUI", "exported": true + }, + { + "name": "MechExtAbility", + "srcEntry": "./ets/mechextability/MechExtAbility.ets", + "description": "$string:MechExtAbility_desc", + "label": "$string:MechExtAbility_label", + "type": "sys/commonUI", + "exported": true } ], "metadata": [ diff --git a/application/entry/src/main/resources/base/element/color.json b/application/entry/src/main/resources/base/element/color.json index a502679e..0faa4256 100644 --- a/application/entry/src/main/resources/base/element/color.json +++ b/application/entry/src/main/resources/base/element/color.json @@ -7,6 +7,14 @@ { "name": "color_0A59F7_blue", "value": "#0A59F7" + }, + { + "name": "color_black", + "value": "#000000" + }, + { + "name": "color_image_enable", + "value": "#FFFFFF" } ] } \ No newline at end of file diff --git a/application/entry/src/main/resources/base/element/string.json b/application/entry/src/main/resources/base/element/string.json index 3bc025a8..e4251917 100644 --- a/application/entry/src/main/resources/base/element/string.json +++ b/application/entry/src/main/resources/base/element/string.json @@ -107,6 +107,42 @@ "attr": { "priority": "LT" } + }, + { + "name": "MechExtAbility_desc", + "value": "description" + }, + { + "name": "MechExtAbility_label", + "value": "label" + }, + { + "name": "mech_intelligent_tracking", + "value": "Intelligent Tracking" + }, + { + "name": "mech_tracking_layout", + "value": "Tracking Layout" + }, + { + "name": "mech_enable", + "value": "enable" + }, + { + "name": "mech_close", + "value": "close" + }, + { + "name": "mech_left", + "value": "left" + }, + { + "name": "mech_center", + "value": "center" + }, + { + "name": "mech_right", + "value": "right" } ] } \ No newline at end of file diff --git a/application/entry/src/main/resources/base/media/intelligent_tracking.svg b/application/entry/src/main/resources/base/media/intelligent_tracking.svg new file mode 100644 index 00000000..aee450d9 --- /dev/null +++ b/application/entry/src/main/resources/base/media/intelligent_tracking.svg @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/application/entry/src/main/resources/base/media/intelligent_tracking_dark.svg b/application/entry/src/main/resources/base/media/intelligent_tracking_dark.svg new file mode 100644 index 00000000..11e123ab --- /dev/null +++ b/application/entry/src/main/resources/base/media/intelligent_tracking_dark.svg @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/application/entry/src/main/resources/base/media/track_layout_center.svg b/application/entry/src/main/resources/base/media/track_layout_center.svg new file mode 100644 index 00000000..7abb97c0 --- /dev/null +++ b/application/entry/src/main/resources/base/media/track_layout_center.svg @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/application/entry/src/main/resources/base/media/track_layout_center_dark.svg b/application/entry/src/main/resources/base/media/track_layout_center_dark.svg new file mode 100644 index 00000000..6dc793cd --- /dev/null +++ b/application/entry/src/main/resources/base/media/track_layout_center_dark.svg @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/application/entry/src/main/resources/base/media/track_layout_left.svg b/application/entry/src/main/resources/base/media/track_layout_left.svg new file mode 100644 index 00000000..4efb202a --- /dev/null +++ b/application/entry/src/main/resources/base/media/track_layout_left.svg @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/application/entry/src/main/resources/base/media/track_layout_left_dark.svg b/application/entry/src/main/resources/base/media/track_layout_left_dark.svg new file mode 100644 index 00000000..e06939a7 --- /dev/null +++ b/application/entry/src/main/resources/base/media/track_layout_left_dark.svg @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/application/entry/src/main/resources/base/media/track_layout_right.svg b/application/entry/src/main/resources/base/media/track_layout_right.svg new file mode 100644 index 00000000..c7d0a930 --- /dev/null +++ b/application/entry/src/main/resources/base/media/track_layout_right.svg @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/application/entry/src/main/resources/base/media/track_layout_right_dark.svg b/application/entry/src/main/resources/base/media/track_layout_right_dark.svg new file mode 100644 index 00000000..343e3eb5 --- /dev/null +++ b/application/entry/src/main/resources/base/media/track_layout_right_dark.svg @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/application/entry/src/main/resources/base/profile/main_pages.json b/application/entry/src/main/resources/base/profile/main_pages.json index 3556def3..ad262871 100644 --- a/application/entry/src/main/resources/base/profile/main_pages.json +++ b/application/entry/src/main/resources/base/profile/main_pages.json @@ -1,6 +1,7 @@ { "src": [ "pages/DHardwareUI", - "pages/ContinueSwitch" + "pages/ContinueSwitch", + "pages/MechControl" ] } \ No newline at end of file diff --git a/application/entry/src/main/resources/zh_CN/element/string.json b/application/entry/src/main/resources/zh_CN/element/string.json index 36615956..544819ed 100644 --- a/application/entry/src/main/resources/zh_CN/element/string.json +++ b/application/entry/src/main/resources/zh_CN/element/string.json @@ -107,6 +107,42 @@ "attr": { "priority": "translate" } + }, + { + "name": "MechExtAbility_desc", + "value": "description" + }, + { + "name": "MechExtAbility_label", + "value": "label" + }, + { + "name": "mech_intelligent_tracking", + "value": "智能跟踪" + }, + { + "name": "mech_tracking_layout", + "value": "跟踪布局" + }, + { + "name": "mech_enable", + "value": "开启" + }, + { + "name": "mech_close", + "value": "关闭" + }, + { + "name": "mech_left", + "value": "居左" + }, + { + "name": "mech_center", + "value": "居中" + }, + { + "name": "mech_right", + "value": "居右" } ] } \ No newline at end of file -- Gitee From 223c3e88da29acecfdf2da8eaa75de136c19bb6c Mon Sep 17 00:00:00 2001 From: dengxiaoyu Date: Sat, 12 Jul 2025 11:39:04 +0800 Subject: [PATCH 2/3] del str two Signed-off-by: dengxiaoyu --- application/entry/src/main/ets/pages/MechControl.ets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/entry/src/main/ets/pages/MechControl.ets b/application/entry/src/main/ets/pages/MechControl.ets index 2f9f788b..75cd4c08 100644 --- a/application/entry/src/main/ets/pages/MechControl.ets +++ b/application/entry/src/main/ets/pages/MechControl.ets @@ -34,7 +34,7 @@ struct MechControl { IntelligentTracking(){ Column(){ Button() { - Image(this.isTrackingEnabled ? $r("app.media.intelligent_tracking") :$r("app.media.intelligent_tracking_dark")) + Image(this.isTrackingEnabled ? $r('app.media.intelligent_tracking') :$r('app.media.intelligent_tracking_dark')) .width(20) .height(20) .margin({left: 3, top: 3}) -- Gitee From 05cea58c67efcd416987e794581a42f00ca591fe Mon Sep 17 00:00:00 2001 From: dengxiaoyu Date: Sat, 12 Jul 2025 14:11:45 +0800 Subject: [PATCH 3/3] add base Signed-off-by: dengxiaoyu --- application/entry/src/main/ets/pages/MechControl.ets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/entry/src/main/ets/pages/MechControl.ets b/application/entry/src/main/ets/pages/MechControl.ets index 75cd4c08..c5320487 100644 --- a/application/entry/src/main/ets/pages/MechControl.ets +++ b/application/entry/src/main/ets/pages/MechControl.ets @@ -14,7 +14,7 @@ */ import { logger } from '../utils/Logger'; import mechanicManager from '@ohos.distributedHardware.mechanicManager'; -import { BusinessError } from '@kit.BasicServicesKit'; +import { BusinessError } from '@ohos.base'; const TAG = '[MechControl_Page] : '; -- Gitee