• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15import { logger } from '../utils/Logger';
16import mechanicManager from '@ohos.distributedHardware.mechanicManager';
17import { BusinessError } from '@ohos.base';
18
19const TAG = '[MechControl_Page] : ';
20
21@Entry
22@Component
23struct MechControl {
24  @State isTrackingEnabled: boolean = false;
25  @State currentTrackingLayoutText: ResourceStr = $r('app.string.mech_center');
26  @State currentTrackingLayoutImage: ResourceStr = $r('app.media.track_layout_center');
27
28
29  aboutToAppear() {
30    logger.info(`${TAG} aboutToAppear in`);
31  }
32
33  @Builder
34  IntelligentTracking(){
35    Column(){
36      Button() {
37        Image(this.isTrackingEnabled ? $r('app.media.intelligent_tracking') :$r('app.media.intelligent_tracking_dark'))
38          .width(20)
39          .height(20)
40          .margin({left: 3, top: 3})
41      }
42      .width('40vp')
43      .height('40vp')
44      .backgroundColor(this.isTrackingEnabled ? $r('app.color.color_0A59F7_blue') : $r('sys.color.ohos_id_color_component_normal'))
45      .onClick(() => {
46       const originalState = this.isTrackingEnabled;
47        try {
48          if (!this.isTrackingEnabled) {
49            logger.info(`${TAG} enable camera tracking`);
50            mechanicManager.setCameraTrackingEnabled(true);
51            this.isTrackingEnabled = true;
52          } else {
53            logger.info(`${TAG} disable camera tracking`);
54            mechanicManager.setCameraTrackingEnabled(false);
55            this.isTrackingEnabled = false;
56          }
57        } catch (error) {
58          let code: number = (error as BusinessError).code;
59          let message: string = (error as BusinessError).message;
60          logger.error(`${TAG} setCameraTrackingEnabled failed. error.code: ${code}, message: ${message}`);
61          this.isTrackingEnabled = originalState;
62        }
63        logger.info(`${TAG} onClick end, isTrackingEnabled = ${this.isTrackingEnabled}`);
64      })
65
66      Text($r('app.string.mech_intelligent_tracking'))
67        .fontSize(12)
68        .lineHeight(16)
69        .fontWeight(FontWeight.Medium)
70        .fontColor($r('app.color.color_black'))
71        .margin({ left: 0, top: 4, right: 0 })
72        .textAlign(TextAlign.Center)
73
74      Text(this.isTrackingEnabled ? $r('app.string.mech_enable'): $r('app.string.mech_close'))
75        .fontSize(10)
76        .lineHeight(13)
77        .fontWeight(FontWeight.Regular)
78        .fontColor($r('app.color.color_black'))
79        .margin({ top: 2 })
80        .textAlign(TextAlign.Center)
81    }
82  }
83
84  @Builder
85  TrackingLayout(){
86    Column(){
87      Button() {
88        Image(this.currentTrackingLayoutImage)
89          .width(20)
90          .height(20)
91      }
92      .width('40vp')
93      .height('40vp')
94      .backgroundColor($r('app.color.color_0A59F7_blue'))
95      .onClick(() => {
96        logger.info(`${TAG} onClick TrackingLayout in`);
97      })
98
99      Text($r('app.string.mech_tracking_layout'))
100        .fontSize(12)
101        .lineHeight(16)
102        .fontWeight(FontWeight.Medium)
103        .fontColor($r('app.color.color_black'))
104        .margin({ left: 0, top: 4, right: 0 })
105        .textAlign(TextAlign.Center)
106
107      Text(this.currentTrackingLayoutText)
108        .fontSize(10)
109        .lineHeight(13)
110        .fontWeight(FontWeight.Regular)
111        .fontColor($r('app.color.color_black'))
112        .margin({ top: 2 })
113        .textAlign(TextAlign.Center)
114    }
115  }
116
117  build() {
118    Column() {
119
120      Flex({
121        direction: FlexDirection.Row,
122        justifyContent: FlexAlign.SpaceEvenly,
123        alignItems: ItemAlign.Center
124      }) {
125        this.IntelligentTracking();
126
127        if (this.isTrackingEnabled) {
128          this.TrackingLayout();
129        }
130      }
131      .width('100%')
132      .height('100%')
133
134    }
135    .width('100%')
136    .height('100%')
137    .justifyContent(FlexAlign.Center)
138    .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.BOTTOM])
139  }
140}