• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 { initFloatWindow,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
45    ProfilerTask.getInstance().initModule()
46    ProfilerTask.getInstance().taskInit()
47    //创建TiTile窗
48    globalThis.CreateTitleWindow()
49    globalThis.task_status = TaskStatus.task_init
50    initFloatWindow()
51//    this.initCollectPid()
52  }
53
54//  initCollectPid(){
55//    //尝试循环刷新 当前running abilitysInfo 获取pid
56//    globalThis.collectIntervalPid = setInterval(()=>{
57//      //初始化采集目标进程pid
58//      getPidOfAbility(globalThis.collectPkg).then(pid=>{
59//        globalThis.processPid = pid
60//        if(pid != "-1"){
61//          clearInterval(globalThis.collectIntervalPid)
62//          SPLogger.DEBUG(TAG, "clear collectIntervalPid and cur pid:" + pid)
63//        }
64//      })
65//    },1000)
66//  }
67
68  initAllCollect() {
69    console.log("collectIntervalCollect initAllCollect....");
70    if (globalThis.collectConfigs != -1 && globalThis.collectPkg != -1) {
71      if(globalThis.collectConfigs.screen_capture){
72        MainWorker.postMessage({"screen_capture":true})
73      }
74      if(globalThis.collectConfigs.trace){
75        MainWorker.postMessage({"catch_trace_start":true})
76      }
77      globalThis.collectIntervalCollect = setInterval(() => {
78        console.log("collectIntervalCollect running....");
79        if (this.playerState == TaskStatus.task_running) {
80          ProfilerTask.getInstance().taskStart()
81          this.timerNum++
82        }
83        console.log("collectIntervalCollect running status::"+ this.playerState);
84      }, 1000)
85
86      globalThis.collectPowerCollect = setInterval(() => {
87        console.log("collectIntervalCollect running....");
88        if (this.playerState == TaskStatus.task_running) {
89          ProfilerTask.getInstance().taskSingleItemStart(CollectorType.TYPE_POWER)
90        }
91        console.log("collectIntervalCollect running status::"+ this.playerState);
92      }, 250)
93    }
94    globalThis.task_status = TaskStatus.task_running
95    this.playerState = TaskStatus.task_running
96    console.log("collectIntervalCollect initAllCollect finished....");
97  }
98
99  singleEvent() {
100    if (this.playerState == TaskStatus.task_running) {
101      globalThis.task_status = TaskStatus.task_pause
102      this.playerState = TaskStatus.task_pause
103    }
104    else if (this.playerState == TaskStatus.task_pause) {
105      globalThis.task_status = TaskStatus.task_running
106      this.playerState = TaskStatus.task_running
107    }
108  }
109
110  doubleEvent() {
111    // 双击启动悬浮TITLE
112    if (this.windShowState) {
113      globalThis.HideTitleWindow()
114      this.windShowState = false
115    } else {
116      globalThis.ShowTitleWindow()
117      this.windShowState = true
118    }
119  }
120
121  longEvent() {
122    ProfilerTask.getInstance().taskStop()
123    this.destroyAllWindow()
124    this.clearAllInterVal()
125    var now = new Date();
126    var exitTime = now.getTime() + 1000;
127    while (true) {
128      now = new Date();
129      if (now.getTime() > exitTime)
130      break;
131    }
132    ProfilerTask.getInstance().taskGetDubai()
133  }
134  async dubai_data_to_disk(){
135    MainWorker.postMessage({"setDuBaiDb": true})
136  }
137  destroyAllWindow(){
138    globalThis.DestroyFloatingWindow()
139    globalThis.DestroyTitleWindow()
140    destoryAllFloatWindow()
141  }
142
143  clearAllInterVal(){
144    if(globalThis.collectConfigs.trace){
145      MainWorker.postMessage({"catch_trace_end":true})
146    }
147    clearInterval(globalThis.collectIntervalCollect)
148    clearInterval(globalThis.collectPowerCollect)
149  }
150
151  MoveWindow(offsetX: number, offsetY: number) {
152    globalThis.MoveFloatingWindow(offsetX, offsetY)
153  }
154
155  SetWindowPosition(offsetX: number, offsetY: number) {
156    globalThis.SetFloatingWindowPosition(offsetX, offsetY)
157  }
158
159  build() {
160    Stack({ alignContent: Alignment.Center }) {
161      if (this.playerState == TaskStatus.task_init) {
162        Circle()
163          .width('90vp')
164          .height('90vp')
165          .fill(Color.White)
166          .fillOpacity(0)
167          .opacity(0.8)
168          .border({ radius: '90vp', width: '0.5vp', color: $r("app.color.colorPrimary") })
169          .linearGradient({
170            angle: 135,
171            direction: GradientDirection.Left,
172            colors: [[$r("app.color.colorPrimary"), 1.0], [$r("app.color.colorPrimary"), 1.0]]
173          })
174        Text('start')
175          .fontSize(18)
176          .textAlign(TextAlign.Center)
177          .fontColor($r("app.color.color_fff"))
178          .width('100%')
179          .height('100%')
180          .onClick(() => {
181            console.log("collectIntervalCollect  single click ....");
182            this.dubai_data_to_disk()
183            this.initAllCollect()
184
185            console.log("collectIntervalCollect  single click finished....");
186          })
187          .gesture(
188          GestureGroup(GestureMode.Exclusive,
189          TapGesture({ count: 2 })
190            .onAction(() => {
191              this.doubleEvent()
192
193            }),
194          PanGesture({})
195            .onActionStart(() => {
196            })
197            .onActionUpdate((event: GestureEvent) => {
198              this.offsetX = event.offsetX
199              this.offsetY = event.offsetY
200            })
201            .onActionEnd(() => {
202              this.MoveWindow(this.offsetX, this.offsetY)
203              this.SetWindowPosition(this.offsetX, this.offsetY)
204            })
205          ))
206      }
207
208      if (this.playerState == TaskStatus.task_running || this.playerState == TaskStatus.task_pause) {
209        if (this.playerState == TaskStatus.task_pause) {
210          Circle()
211            .width('90vp')
212            .height('90vp')
213            .fill(Color.White)
214            .fillOpacity(0)
215            .opacity(0.8)
216            .border({ radius: '90vp', width: '0.5vp', color: $r("app.color.color_666") })
217            .linearGradient({
218              angle: 135,
219              direction: GradientDirection.Left,
220              colors: [[$r("app.color.color_666"), 0.7], [$r("app.color.color_666"), 0.7]]
221            })
222        }else{
223          Circle()
224            .width('90vp')
225            .height('90vp')
226            .fill(Color.White)
227            .fillOpacity(0)
228            .opacity(0.5)
229            .border({ radius: '90vp', width: '0.5vp', color: $r("app.color.colorPrimary") })
230            .linearGradient({
231              angle: 135,
232              direction: GradientDirection.Left,
233              colors: [[$r("app.color.colorPrimary"), 0.7], [$r("app.color.colorPrimary"), 0.7]]
234            })
235        }
236        Text(secToTime(this.timerNum).toString())
237          .fontSize('16fp')
238          .textAlign(TextAlign.Center)
239          .fontColor($r("app.color.color_fff"))
240          .width('100%')
241          .height('100%')
242          .onClick(res => {
243            this.isDoubleFlag = false
244            for(let i = 0; i < this.mHits.length - 1; i++) {
245              this.mHits[i] = this.mHits[i + 1]
246            }
247            this.mHits[this.mHits.length - 1] = new Date().getTime()
248            if(this.mHits[0] >= new Date().getTime() - this.DURATION) {
249              this.doubleEvent()
250              this.isDoubleFlag = true
251              this.mHits = Array(this.COUNTS)
252            } else {
253              this.singleClickId = setTimeout(()=>{
254                if(!this.isDoubleFlag) {
255                  this.singleEvent()
256                }
257              }, 300)
258            }
259          })
260          .gesture(
261          GestureGroup(GestureMode.Exclusive,
262          LongPressGesture({ fingers: 1, repeat: false, duration: 1000 })
263            .onAction(() => {
264              this.dubai_data_to_disk()
265              this.longEvent()
266
267            }),
268          PanGesture({})
269            .onActionStart(() => {
270            })
271            .onActionUpdate((event: GestureEvent) => {
272              this.offsetX = event.offsetX
273              this.offsetY = event.offsetY
274            })
275            .onActionEnd(() => {
276              this.MoveWindow(this.offsetX, this.offsetY)
277              this.SetWindowPosition(this.offsetX, this.offsetY)
278            })
279          ))
280      }
281    }.width('100%').height('100%')
282
283  }
284}