1/* 2 * Copyright (c) 2022-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 promptAction from '@ohos.promptAction' 17import router from '@ohos.router' 18import faultLogger from '@ohos.faultLogger' 19import Logger from '../model/Logger' 20 21const INDEX: number = 0 // 初始索引值 22const DURATION: number = 100 // 定时器时间 23 24@Preview 25@CustomDialog 26export struct FaultDialog { 27 private controller: CustomDialogController 28 private faultTypes: string[] = ['JS_CRASH', 'CPP_CRASH', 'FREEZE_CRASH'] 29 @State selectIndex: number = INDEX 30 31 addFaultLog() { 32 switch (this.selectIndex) { 33 case 0: 34 promptAction.showToast({ message: $r('app.string.js_crash_building') }) 35 setTimeout(() => { 36 let tempList = ['0', '1'] 37 Logger.info(tempList[3].toString()) 38 }, DURATION) 39 break 40 case 1: 41 promptAction.showToast({ message: $r('app.string.cpp_crash_building') }) 42 setTimeout(() => { 43 // 此处是为了构造cpp_crash,故意写的有问题的接口调用,因此添加了@ts-ignore 44 let module = 'ohos.samples.faultlogger' 45 //@ts-ignore 46 faultLogger.addFaultLog(0, faultLogger.FaultType.CPP_CRASH, module, 'faultloggertestsummary02') 47 router.back() 48 }, DURATION) 49 break 50 case 2: 51 promptAction.showToast({ message: $r('app.string.app_freeze_building') }) 52 setTimeout(() => { 53 let index = 0 // 循环初始值 54 while (true) { 55 index++ 56 this.selectIndex = index % 2 57 } 58 }, DURATION) 59 break 60 default: 61 break 62 } 63 } 64 65 build() { 66 Column() { 67 Text($r('app.string.select_fault_type')) 68 .textAlign(TextAlign.Start) 69 .width('100%') 70 .fontSize(22) 71 72 Column() { 73 ForEach(this.faultTypes, (item, index) => { 74 Row() { 75 Image(this.selectIndex === index ? $r('app.media.checked') : $r('app.media.uncheck')) 76 .height(30).width(30) 77 .objectFit(ImageFit.Contain) 78 79 Text(item) 80 .padding(10) 81 .fontSize(20) 82 .fontColor(Color.Black) 83 } 84 .id(item) 85 .width('100%') 86 .onClick(() => { 87 this.selectIndex = index 88 }) 89 }, item => item) 90 91 Row() { 92 Button() { 93 Text($r('app.string.sure')) 94 .fontColor(Color.Red) 95 .width('90%') 96 .textAlign(TextAlign.Center) 97 .fontSize(20) 98 } 99 .id('OK') 100 .type(ButtonType.Capsule) 101 .backgroundColor(Color.White) 102 .layoutWeight(1) 103 .onClick(() => { 104 this.controller.close() 105 this.addFaultLog() 106 }) 107 108 Divider().size({ width: 1, height: 40 }).backgroundColor(Color.Gray) 109 110 Button() { 111 Text($r('app.string.cancel')) 112 .fontColor('#0D9FFB') 113 .width('90%') 114 .textAlign(TextAlign.Center) 115 .fontSize(20) 116 } 117 .id('cancel') 118 .type(ButtonType.Capsule) 119 .backgroundColor(Color.White) 120 .layoutWeight(1) 121 .onClick(() => { 122 this.controller.close() 123 }) 124 } 125 .width('100%') 126 } 127 } 128 .padding(16) 129 } 130}