• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
16 package ohos.devtools.views.layout.dialog;
17 
18 import com.intellij.ui.components.JBLabel;
19 import com.intellij.ui.components.JBPanel;
20 import ohos.devtools.datasources.utils.profilerlog.ProfilerLogManager;
21 import ohos.devtools.datasources.utils.session.entity.SessionInfo;
22 import ohos.devtools.datasources.utils.session.service.SessionManager;
23 import ohos.devtools.views.common.LayoutConstants;
24 import ohos.devtools.views.layout.chartview.ProfilerChartsView;
25 import org.apache.logging.log4j.LogManager;
26 import org.apache.logging.log4j.Logger;
27 
28 import javax.swing.JButton;
29 import javax.swing.SwingWorker;
30 import java.awt.Dimension;
31 import java.awt.Font;
32 import java.awt.event.MouseAdapter;
33 import java.awt.event.MouseEvent;
34 import java.awt.event.WindowAdapter;
35 import java.awt.event.WindowEvent;
36 import java.io.PrintWriter;
37 import java.io.StringWriter;
38 import java.util.Date;
39 import java.util.Objects;
40 import java.util.concurrent.TimeUnit;
41 
42 /**
43  * record native dialog
44  *
45  * @since 2021/10/25
46  */
47 public class NativeRecordDialog {
48     private static final Logger LOGGER = LogManager.getLogger(NativeRecordDialog.class);
49 
50     /**
51      * record native dialog width
52      */
53     private static final int DIALOG_WIDTH = 428;
54 
55     /**
56      * record native dialog height
57      */
58     private static final int DIALOG_HEIGHT = 222;
59 
60     private final ProfilerChartsView bottomPanel;
61 
62     private final CustomDialog sampleDialog;
63 
64     private final JBPanel jPanel = new JBPanel(null);
65 
66     private final JBLabel status = new JBLabel("Status");
67     private final JBLabel duration = new JBLabel("Duration");
68     private final JBLabel statusValue = new JBLabel("Record native allocations");
69 
70     private final JBLabel timeJLabel = new JBLabel();
71 
72     private final JButton buttonCancel = new JButton("Cancel");
73 
74     private final JButton buttonStop = new JButton("Stop");
75 
76     private long sessionId = 1L;
77 
78     private boolean recordStatus = true;
79 
80     /**
81      * HosRecordDialog
82      *
83      * @param bottomJPanel bottomJPanel
84      * @param id Native Hook Session Id
85      */
NativeRecordDialog(ProfilerChartsView bottomJPanel, long id)86     public NativeRecordDialog(ProfilerChartsView bottomJPanel, long id) {
87         this.sessionId = id;
88         this.bottomPanel = bottomJPanel;
89         sampleDialog = new CustomDialog("Record native allocations", jPanel);
90         addButtonListeners();
91         jPanel.setPreferredSize(new Dimension(DIALOG_WIDTH, DIALOG_HEIGHT));
92         // set the style of jPanel and button
93         setFontAndBounds();
94         // Add components
95         timeJLabel.setText("00:00.000");
96         jPanel.add(buttonCancel);
97         jPanel.add(buttonStop);
98         jPanel.add(status);
99         jPanel.add(duration);
100         jPanel.add(statusValue);
101         new SwingWorker<>() {
102             @Override
103             protected Object doInBackground() {
104                 Date date = new Date();
105                 StringWriter stringWriter = new StringWriter();
106                 PrintWriter printWriter = new PrintWriter(stringWriter);
107                 long cur;
108                 long start = System.currentTimeMillis();
109                 while (recordStatus) {
110                     cur = System.currentTimeMillis() - start;
111                     if (cur >= LayoutConstants.ONE_HOURS_MILLISECONDS) {
112                         recordStatus = false;
113                     }
114                     date.setTime(cur);
115                     printWriter.format("%1$tM:%1$tS.%tL", date);
116                     printWriter.flush();
117                     StringBuffer stringBuffer = stringWriter.getBuffer();
118                     timeJLabel.setText(stringBuffer.toString());
119                     stringBuffer.setLength(0);
120                     try {
121                         TimeUnit.MILLISECONDS.sleep(6L);
122                     } catch (InterruptedException exception) {
123                         LOGGER.error("Thread sleep error: {}", exception.getMessage());
124                     }
125                 }
126                 return timeJLabel;
127             }
128 
129             @Override
130             protected void done() {
131                 sampleDialog.close(1);
132             }
133         }.execute();
134         jPanel.add(timeJLabel);
135         sampleDialog.setResizable(true);
136         sampleDialog.show();
137     }
138 
139     /**
140      * Set the style of J Panel and Button
141      */
setFontAndBounds()142     private void setFontAndBounds() {
143         Font fontUpText = new Font(Font.DIALOG, Font.PLAIN, LayoutConstants.FONT_SIZE);
144         status.setFont(fontUpText);
145         duration.setFont(fontUpText);
146         statusValue.setFont(fontUpText);
147         timeJLabel.setFont(fontUpText);
148 
149         status.setBounds(LayoutConstants.SIXTY, LayoutConstants.NUM_50, LayoutConstants.RECORD_LEFT_PANEL_WIDTH,
150             LayoutConstants.NUM_20);
151         duration.setBounds(LayoutConstants.SIXTY, LayoutConstants.RECORD_LEFT_POINT_NUMBER,
152             LayoutConstants.RECORD_LEFT_PANEL_WIDTH, LayoutConstants.NUM_20);
153         statusValue.setBounds(LayoutConstants.RECORD_UP_POINT_NUMBER, LayoutConstants.NUM_50,
154             LayoutConstants.RECORD_RIGHT_PANEL_WIDTH, LayoutConstants.NUM_20);
155         timeJLabel.setBounds(LayoutConstants.RECORD_UP_POINT_NUMBER, LayoutConstants.RECORD_LEFT_POINT_NUMBER,
156             LayoutConstants.RECORD_RIGHT_PANEL_WIDTH, LayoutConstants.NUM_20);
157 
158         Font fontUpButton = new Font(Font.DIALOG, Font.PLAIN, LayoutConstants.NUM_20);
159         buttonCancel.setFont(fontUpButton);
160         buttonStop.setFont(fontUpButton);
161 
162         buttonCancel.setBounds(LayoutConstants.NUM_110, LayoutConstants.NUM_142, LayoutConstants.NUM_96,
163             LayoutConstants.NUM_40);
164         buttonStop.setBounds(LayoutConstants.NUM_222, LayoutConstants.NUM_142, LayoutConstants.NUM_96,
165             LayoutConstants.NUM_40);
166     }
167 
addButtonListeners()168     private void addButtonListeners() {
169         addStopButtonListener();
170         sampleDialog.getWindow().addWindowListener(new WindowAdapter() {
171             @Override
172             public void windowClosed(WindowEvent windowEvent) {
173                 super.windowClosed(windowEvent);
174                 if (ProfilerLogManager.isInfoEnabled()) {
175                     LOGGER.info("windows closed");
176                 }
177                 LOGGER.info("windows closed");
178                 recordStatus = false;
179                 SessionInfo sessionInfo = SessionManager.getInstance().getSessionInfo(bottomPanel.getSessionId());
180                 if (Objects.isNull(sessionInfo)) {
181                     return;
182                 }
183                 SessionManager.getInstance().stopAndDestoryOperation(sessionInfo.getSecondSessionId());
184             }
185         });
186 
187         buttonCancel.addMouseListener(new MouseAdapter() {
188             @Override
189             public void mouseClicked(MouseEvent event) {
190                 super.mouseClicked(event);
191                 recordStatus = false;
192                 sampleDialog.close(1);
193                 SessionInfo sessionInfo = SessionManager.getInstance().getSessionInfo(bottomPanel.getSessionId());
194                 if (Objects.isNull(sessionInfo)) {
195                     return;
196                 }
197                 SessionManager.getInstance().stopAndDestoryOperation(sessionInfo.getSecondSessionId());
198             }
199         });
200     }
201 
addStopButtonListener()202     private void addStopButtonListener() {
203         buttonStop.addMouseListener(new MouseAdapter() {
204             @Override
205             public void mouseClicked(MouseEvent mouseEvent) {
206                 super.mouseClicked(mouseEvent);
207                 int endTime = bottomPanel.getTimeline().getEndTime();
208                 Date date = new Date();
209                 StringWriter stringWriter = new StringWriter();
210                 PrintWriter printWriter = new PrintWriter(stringWriter);
211                 date.setTime(endTime);
212                 printWriter.format("%1$tM:%1$tS.%tL", date);
213                 printWriter.flush();
214                 recordStatus = false;
215                 sampleDialog.close(1);
216                 long session = bottomPanel.getSessionId();
217                 SessionInfo sessionInfo = SessionManager.getInstance().getSessionInfo(session);
218                 if (Objects.isNull(sessionInfo)) {
219                     return;
220                 }
221                 SessionManager.getInstance().stopAndDestoryOperation(sessionInfo.getSecondSessionId());
222                 String timeText = stringWriter.toString();
223                 bottomPanel.getTaskScenePanelChart()
224                     .createSessionList(LayoutConstants.NATIVE_HOOK_RECORDING, timeText, session, null);
225             }
226         });
227     }
228 }
229