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 hiAppEvent from '@ohos.hiAppEvent'; 17import prompt from '@ohos.prompt'; 18import Logger from '../model/Logger'; 19 20const TAG = 'DotTestPage'; 21 22@Entry 23@Component 24struct DotTestPage { 25 @State contentText: Resource = $r('app.string.initContentText'); 26 @State isOn: boolean = false; 27 28 private writeEvent() { 29 hiAppEvent.write('test_event', hiAppEvent.EventType.STATISTIC, { 30 'int_data': 100, 'str_data': 'strValue' 31 }, (err, value) => { 32 if (err) { 33 Logger.error(TAG, `failed to write event because ${err.code}`); 34 this.contentText = $r('app.string.writeFailed'); 35 return; 36 } 37 prompt.showToast({ 38 message: 'success to write event' 39 }); 40 this.contentText = $r('app.string.writeSuccess'); 41 Logger.info(TAG, `success to write event: ${value}`); 42 }); 43 } 44 45 private changeEvent(isOn: boolean) { 46 this.isOn = isOn; 47 if (isOn) { 48 hiAppEvent.configure({ disable: isOn }); 49 this.contentText = $r('app.string.appDisabled'); 50 } else { 51 hiAppEvent.configure({ disable: isOn }); 52 this.contentText = $r('app.string.appAble'); 53 } 54 } 55 56 build() { 57 Column() { 58 Row() { 59 Text($r('app.string.appName')) 60 .fontSize('20vp') 61 .fontColor(Color.White) 62 } 63 .width('100%') 64 .height('10%') 65 .backgroundColor($r('app.color.title_bgColor')) 66 .padding({ left: '10vp' }) 67 .alignItems(VerticalAlign.Center) 68 69 Column() { 70 Row() { 71 Text(this.contentText) 72 .fontSize('15vp') 73 .margin({ left: '4vp' }) 74 } 75 .width('380vp') 76 .height('200vp') 77 .border({ width: '2vp' }) 78 .margin({ bottom: '10vp' }) 79 80 Button($r('app.string.event')) 81 .id('eventInto') 82 .width('150vp') 83 .height('50vp') 84 .fontSize('16vp') 85 .onClick(() => { 86 this.writeEvent(); 87 }) 88 } 89 .width('100%') 90 .height('40%') 91 .justifyContent(FlexAlign.Center) 92 93 Row() { 94 Text($r('app.string.disable')) 95 .fontSize('16vp') 96 Toggle({ type: ToggleType.Switch, isOn: this.isOn }) 97 .id('eventSwitch') 98 .selectedColor($r('app.color.toggle_selectedColor')) 99 .switchPointColor($r('app.color.toggle_switchPointColor')) 100 .onChange((isOn: boolean) => { 101 this.changeEvent(isOn) 102 }) 103 } 104 .width('100%') 105 .height('10%') 106 .backgroundColor($r('app.color.operation_bgColor')) 107 .justifyContent(FlexAlign.SpaceAround) 108 } 109 .width('100%') 110 .height('100%') 111 } 112} 113