1/* 2 * Copyright (C) 2022 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 { secToTime } from '../common/utils/TimeUtils'; 16import { TaskStatus } from '../common/profiler/base/ProfilerConstant'; 17import { ProfilerTask } from '../common/profiler/ProfilerTask'; 18import { destoryAllFloatWindow } from '../common/ui/floatwindow/utils/FloatWindowUtils'; 19import WorkerHandler from '../common/profiler/WorkerHandler'; 20import worker from '@ohos.worker'; 21let mainWorker = globalThis.MainWorker 22import { CollectorType } from '../common/profiler/base/ProfilerConstant' 23 24mainWorker.onmessage = function (result) { 25 WorkerHandler.socketHandler(result) 26} 27@Entry 28@Component 29struct FloatBall { 30 @State playerState: number = TaskStatus.task_init 31 @State timerNum: number = 0 32 timerId: number = -1 33 @State windShowState: boolean = false 34 offsetX: number = -1 35 offsetY: number = -1 36 //解決手势失效的问题 37 COUNTS = 2 // 点击次数 38 DURATION: number = 300 // 规定有效时间 39 mHits = Array(this.COUNTS) // 数组 40 isDoubleFlag = false // 是否是双击 41 singleClickId = 0 // 单击事件ID 42 43 aboutToAppear() { 44 ProfilerTask.getInstance().initModule() 45 ProfilerTask.getInstance().taskInit() 46 console.log('cm-floatBall-CreateTitleWindow1') 47 globalThis.CreateTitleWindow() 48 console.log('cm-floatBall-CreateTitleWindow2') 49 globalThis.task_status = TaskStatus.task_init 50 console.log('cm-floatBall-CreateTitleWindow3') 51 } 52 53 initAllCollect() { 54 console.log('collectIntervalCollect initAllCollect....'); 55 if (globalThis.collectConfigs != -1 && globalThis.collectPkg != -1) { 56 if (globalThis.collectConfigs.screenCapture) { 57 mainWorker.postMessage({'screenCapture':true}) 58 } 59 if (globalThis.collectConfigs.trace) { 60 mainWorker.postMessage({'catchTraceStart':true}) 61 } 62 globalThis.collectIntervalCollect = setInterval(() => { 63 if (this.playerState == TaskStatus.task_running) { 64 ProfilerTask.getInstance().taskStart() 65 this.timerNum++ 66 } 67 }, 1000) 68 69 globalThis.collectPowerCollect = setInterval(() => { 70 if (this.playerState == TaskStatus.task_running) { 71 ProfilerTask.getInstance().taskSingleItemStart(CollectorType.TYPE_POWER) 72 } 73 }, 250) 74 } 75 globalThis.task_status = TaskStatus.task_running 76 this.playerState = TaskStatus.task_running 77 console.log('collectIntervalCollect initAllCollect finished....'); 78 } 79 80 singleEvent() { 81 console.log('cm-floatBall-singleEvent') 82 if (this.playerState == TaskStatus.task_running) { 83 globalThis.task_status = TaskStatus.task_pause 84 this.playerState = TaskStatus.task_pause 85 } else if (this.playerState == TaskStatus.task_pause) { 86 globalThis.task_status = TaskStatus.task_running 87 this.playerState = TaskStatus.task_running 88 } 89 } 90 91 doubleEvent() { 92 console.log('cm-floatBall-doubleEvent' + this.windShowState) 93 // 双击启动悬浮TITLE 94 if (this.windShowState) { 95 globalThis.HideTitleWindow() 96 this.windShowState = false 97 } else { 98 globalThis.ShowTitleWindow() 99 this.windShowState = true 100 } 101 } 102 103 longEvent() { 104 console.log('cm-floatBall-longEvent') 105 this.playerState = TaskStatus.task_stop 106 ProfilerTask.getInstance().taskStop() 107 setTimeout(() => { 108 this.destroyAllWindow() 109 this.clearAllInterVal() 110 ProfilerTask.getInstance().taskGetDubai() 111 }, 5000) 112 } 113 async dubai_data_to_disk() { 114 mainWorker.postMessage({'setDuBaiDb': true}) 115 } 116 destroyAllWindow() { 117 console.log('cm-floatBall-destroyAllWindow') 118 globalThis.DestroyFloatingWindow() 119 globalThis.DestroyTitleWindow() 120 destoryAllFloatWindow() 121 } 122 123 clearAllInterVal() { 124 console.log('cm-floatBall-clearAllInterVal') 125 if (globalThis.collectConfigs.trace) { 126 mainWorker.postMessage({'catchTraceEnd':true}) 127 } 128 clearInterval(globalThis.collectIntervalCollect) 129 clearInterval(globalThis.collectPowerCollect) 130 } 131 132 MoveWindow(offsetX: number, offsetY: number) { 133 globalThis.MoveFloatingWindow(offsetX, offsetY) 134 } 135 136 SetWindowPosition(offsetX: number, offsetY: number) { 137 globalThis.SetFloatingWindowPosition(offsetX, offsetY) 138 } 139 140 build() { 141 Stack({ alignContent: Alignment.Center }) { 142 if (this.playerState == TaskStatus.task_init) { 143 Circle() 144 .width('90vp') 145 .height('90vp') 146 .fill(Color.White) 147 .fillOpacity(0) 148 .opacity(0.8) 149 .border({ radius: '90vp', width: '0.5vp', color: $r('app.color.colorPrimary') }) 150 .linearGradient({ 151 angle: 135, 152 direction: GradientDirection.Left, 153 colors: [[$r('app.color.colorPrimary'), 1.0], [$r('app.color.colorPrimary'), 1.0]] 154 }) 155 Text('start') 156 .fontSize(18) 157 .textAlign(TextAlign.Center) 158 .fontColor($r('app.color.color_fff')) 159 .width('100%') 160 .height('100%') 161 .onClick(() => { 162 console.log('collectIntervalCollect single click ....'); 163 this.dubai_data_to_disk() 164 this.initAllCollect() 165 166 console.log('collectIntervalCollect single click finished....'); 167 }) 168 .gesture( 169 GestureGroup(GestureMode.Exclusive, 170 TapGesture({ count: 2 }) 171 .onAction(() => { 172 this.doubleEvent() 173 174 }), 175 PanGesture({}) 176 .onActionStart(() => { 177 }) 178 .onActionUpdate((event: GestureEvent) => { 179 this.offsetX = event.offsetX 180 this.offsetY = event.offsetY 181 }) 182 .onActionEnd(() => { 183 this.MoveWindow(this.offsetX, this.offsetY) 184 this.SetWindowPosition(this.offsetX, this.offsetY) 185 }) 186 )) 187 } 188 189 if (this.playerState == TaskStatus.task_running || this.playerState == TaskStatus.task_pause) { 190 if (this.playerState == TaskStatus.task_pause) { 191 Circle() 192 .width('90vp') 193 .height('90vp') 194 .fill(Color.White) 195 .fillOpacity(0) 196 .opacity(0.8) 197 .border({ radius: '90vp', width: '0.5vp', color: $r('app.color.color_666') }) 198 .linearGradient({ 199 angle: 135, 200 direction: GradientDirection.Left, 201 colors: [[$r('app.color.color_666'), 0.7], [$r('app.color.color_666'), 0.7]] 202 }) 203 } else { 204 Circle() 205 .width('90vp') 206 .height('90vp') 207 .fill(Color.White) 208 .fillOpacity(0) 209 .opacity(0.5) 210 .border({ radius: '90vp', width: '0.5vp', color: $r('app.color.colorPrimary') }) 211 .linearGradient({ 212 angle: 135, 213 direction: GradientDirection.Left, 214 colors: [[$r('app.color.colorPrimary'), 0.7], [$r('app.color.colorPrimary'), 0.7]] 215 }) 216 } 217 Text(secToTime(this.timerNum).toString()) 218 .fontSize('16fp') 219 .textAlign(TextAlign.Center) 220 .fontColor($r('app.color.color_fff')) 221 .width('100%') 222 .height('100%') 223 .onClick(res => { 224 this.isDoubleFlag = false 225 for (let i = 0; i < this.mHits.length - 1; i++) { 226 this.mHits[i] = this.mHits[i + 1] 227 } 228 this.mHits[this.mHits.length - 1] = new Date().getTime() 229 if (this.mHits[0] >= new Date().getTime() - this.DURATION) { 230 this.doubleEvent() 231 this.isDoubleFlag = true 232 this.mHits = Array(this.COUNTS) 233 } else { 234 this.singleClickId = setTimeout(()=>{ 235 if (!this.isDoubleFlag) { 236 this.singleEvent() 237 } 238 }, 300) 239 } 240 }) 241 .gesture( 242 GestureGroup(GestureMode.Exclusive, 243 LongPressGesture({ fingers: 1, repeat: false, duration: 1000 }) 244 .onAction(() => { 245 this.dubai_data_to_disk() 246 this.longEvent() 247 248 }), 249 PanGesture({}) 250 .onActionStart(() => { 251 }) 252 .onActionUpdate((event: GestureEvent) => { 253 this.offsetX = event.offsetX 254 this.offsetY = event.offsetY 255 }) 256 .onActionEnd(() => { 257 this.MoveWindow(this.offsetX, this.offsetY) 258 this.SetWindowPosition(this.offsetX, this.offsetY) 259 }) 260 )) 261 } 262 if (this.playerState == TaskStatus.task_stop) { 263 Circle() 264 .width('90vp') 265 .height('90vp') 266 .fill(Color.White) 267 .fillOpacity(0) 268 .opacity(0.8) 269 .border({ radius: '90vp', width: '0.5vp', color: $r('app.color.colorPrimary') }) 270 .linearGradient({ 271 angle: 135, 272 direction: GradientDirection.Left, 273 colors: [[$r('app.color.colorPrimary'), 1.0], [$r('app.color.colorPrimary'), 1.0]] 274 }) 275 Text('saving..') 276 .fontSize(12) 277 .textAlign(TextAlign.Center) 278 .fontColor($r('app.color.color_fff')) 279 .width('100%') 280 .height('100%') 281 } 282 }.width('100%').height('100%') 283 284 } 285}