• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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 */
15
16import accessibility from '@ohos.accessibility'
17import ResourceUtils from '../utils/ResourceUtils'
18import Logger from '../utils/Logger'
19
20@Entry
21@Component
22struct Index {
23  private context: Context = getContext(this)
24  @State connectState: Resource = $r("app.string.state_unconnected")
25  @State accessibilityTest: Resource = $r("app.string.accessibility_test")
26  @State currentState: Resource = $r("app.string.current_state")
27  @State buttonClickState: Resource = $r("app.string.accessibility_not_clicked")
28
29  aboutToAppear(): void {
30    Logger.info("Index aboutToAppear()")
31    try {
32      accessibility.isOpenAccessibility().then((data) => {
33        console.info("Index success in isOpenAccessibility. data: " + JSON.stringify(data))
34        this.connectState = data ? $r("app.string.state_connected") : $r("app.string.state_unconnected")
35      }).catch((err) => {
36        console.error("Index Failed to isOpenAccessibility. Cause: " + JSON.stringify(err))
37      })
38    } catch (error) {
39      Logger.error("Index Failed to isOpenAccessibility. Cause: " + JSON.stringify(error))
40    }
41    try {
42      accessibility.on("accessibilityStateChange", (data) => {
43        this.connectState = data ? $r("app.string.state_connected") : $r("app.string.state_unconnected")
44        Logger.info("Index Subscribe accessibility state change. Result: " + JSON.stringify(data))
45      })
46    } catch (error) {
47      Logger.error("Index Failed to subscribe accessibility state change. Cause: " + JSON.stringify(error))
48    }
49  }
50
51  aboutToDisappear(): void {
52    Logger.info("Index aboutToDisappear()")
53    try {
54      accessibility.off("accessibilityStateChange", (data) => {
55        Logger.info("Index Unsubscribe accessibility state change. Result: " + JSON.stringify(data))
56      })
57    } catch (error) {
58      Logger.error("Index Failed to unsubscribe accessibility state change. Cause: " + JSON.stringify(error))
59    }
60  }
61
62  build() {
63    Row() {
64      Column() {
65        Row() {
66          Text(this.accessibilityTest)
67            .fontSize(30)
68            .fontWeight(FontWeight.Bold)
69            .margin(20)
70        }
71
72        Row() {
73          Text(this.currentState)
74            .fontSize(25)
75            .fontWeight(FontWeight.Bold)
76          Text(": ")
77            .fontSize(25)
78            .fontWeight(FontWeight.Bold)
79          Text(this.connectState)
80            .fontSize(25)
81            .fontWeight(FontWeight.Bold)
82        }
83        .margin(20)
84
85        Row() {
86          Button($r("app.string.accessibility_focus"))
87            .fontSize(20)
88            .fontWeight(FontWeight.Bold)
89            .type(ButtonType.Capsule)
90            .margin({
91              top: 5,
92              bottom: 5
93            })
94            .backgroundColor("#0D9FFB")
95            .width("60%")
96            .height("5%")
97        }
98        .margin(10)
99
100        Row() {
101          Button(this.buttonClickState)
102            .fontSize(20)
103            .fontWeight(FontWeight.Bold)
104            .type(ButtonType.Capsule)
105            .margin({
106              top: 5,
107              bottom: 5
108            })
109            .backgroundColor("#0D9FFB")
110            .width("60%")
111            .height("5%")
112            .onClick(() => {
113              let currentClickState = ResourceUtils.getStringByResource(this.context, this.buttonClickState)
114              Logger.info("currentClickState: " + currentClickState)
115              if (currentClickState == ResourceUtils.getStringByName(this.context, "accessibility_not_clicked")) {
116                this.buttonClickState = $r("app.string.accessibility_clicked")
117              } else {
118                this.buttonClickState = $r("app.string.accessibility_not_clicked")
119              }
120            })
121        }
122        .margin(10)
123      }
124      .width('100%')
125    }
126    .height('100%')
127  }
128}