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.applicationtrace.bean; 17 18 import ohos.devtools.views.applicationtrace.AllData; 19 import ohos.devtools.views.applicationtrace.util.TimeUtils; 20 import ohos.devtools.views.trace.AbstractNode; 21 import ohos.devtools.views.trace.Common; 22 import ohos.devtools.views.trace.DField; 23 import ohos.devtools.views.trace.TracePanel; 24 import ohos.devtools.views.trace.util.Final; 25 import ohos.devtools.views.trace.util.Utils; 26 27 import java.awt.Color; 28 import java.awt.Graphics2D; 29 import java.awt.Rectangle; 30 import java.awt.event.MouseEvent; 31 import java.util.Arrays; 32 import java.util.List; 33 34 import static ohos.devtools.views.trace.TracePanel.root; 35 36 /** 37 * Thread 38 * 39 * @since 2021/5/17 12:25 40 */ 41 public class Thread extends AbstractNode { 42 private static Color runningColor = new Color(Final.RUNNING_COLOR); 43 private static Color rColor = new Color(Final.R_COLOR); 44 private static Color uninterruptibleSleepColor = new Color(Final.UNINTERRUPTIBLE_SLEEP_COLOR); 45 private static Color exitColor = new Color(Final.EXIT_COLOR); 46 private static Color sColor = new Color(Final.S_COLOR); 47 48 private final Integer padding1 = 5; 49 private final Integer padding2 = 10; 50 private final float alpha2 = 0.02f; 51 @DField(name = "upid") 52 private Integer uPid; 53 @DField(name = "utid") 54 private Integer uTid; 55 @DField(name = "pid") 56 private Integer pid; // Process id 57 @DField(name = "tid") 58 private Integer tid; // Thread id 59 @DField(name = "processName") 60 private String processName; 61 @DField(name = "threadName") 62 private String threadName; 63 @DField(name = "state") 64 private String state; 65 @DField(name = "startTime") 66 private Long startTime; 67 @DField(name = "dur") 68 private Long duration; 69 @DField(name = "cpu") 70 private Integer cpu; 71 private boolean isSelected; // Whether to be selected 72 73 /** 74 * Gets the value of uPid . 75 * 76 * @return the value of uPid . 77 */ getuPid()78 public Integer getuPid() { 79 return uPid; 80 } 81 82 /** 83 * Sets the uPid . 84 * <p>You can use getuPid() to get the value of uPid.</p> 85 * 86 * @param param param. 87 */ setuPid(final Integer param)88 public void setuPid(final Integer param) { 89 this.uPid = param; 90 } 91 92 /** 93 * Gets the value of uTid . 94 * 95 * @return the value of uTid . 96 */ getuTid()97 public Integer getuTid() { 98 return uTid; 99 } 100 101 /** 102 * Sets the uTid . 103 * <p>You can use getuTid() to get the value of uTid.</p> 104 * 105 * @param param . 106 */ setuTid(final Integer param)107 public void setuTid(final Integer param) { 108 this.uTid = param; 109 } 110 111 /** 112 * Gets the value of pid . 113 * 114 * @return the value of pid . 115 */ getPid()116 public Integer getPid() { 117 return pid; 118 } 119 120 /** 121 * Sets the pid . 122 * <p>You can use getPid() to get the value of pid.</p> 123 * 124 * @param param . 125 */ setPid(final Integer param)126 public void setPid(final Integer param) { 127 this.pid = param; 128 } 129 130 /** 131 * Gets the value of tid . 132 * 133 * @return the value of tid . 134 */ getTid()135 public Integer getTid() { 136 return tid; 137 } 138 139 /** 140 * Sets the tid . 141 * <p>You can use getTid() to get the value of tid.</p> 142 * 143 * @param param . 144 */ setTid(final Integer param)145 public void setTid(final Integer param) { 146 this.tid = param; 147 } 148 149 /** 150 * Gets the value of processName . 151 * 152 * @return the value of processName . 153 */ getProcessName()154 public String getProcessName() { 155 return processName; 156 } 157 158 /** 159 * Sets the processName . 160 * <p>You can use getProcessName() to get the value of processName.</p> 161 * 162 * @param param . 163 */ setProcessName(final String param)164 public void setProcessName(final String param) { 165 this.processName = param; 166 } 167 168 /** 169 * Gets the value of threadName . 170 * 171 * @return the value of threadName . 172 */ getThreadName()173 public String getThreadName() { 174 return threadName; 175 } 176 177 /** 178 * Sets the threadName . 179 * <p>You can use getThreadName() to get the value of threadName.</p> 180 * 181 * @param param param 182 */ setThreadName(final String param)183 public void setThreadName(final String param) { 184 this.threadName = param; 185 } 186 187 /** 188 * Gets the value of state . 189 * 190 * @return the value of state . 191 */ getState()192 public String getState() { 193 return state; 194 } 195 196 /** 197 * Sets the state . 198 * <p>You can use getState() to get the value of state.</p> 199 * 200 * @param param param 201 */ setState(final String param)202 public void setState(final String param) { 203 this.state = param; 204 } 205 206 /** 207 * Gets the value of startTime . 208 * 209 * @return the value of startTime . 210 */ getStartTime()211 public Long getStartTime() { 212 return startTime; 213 } 214 215 /** 216 * Sets the startTime . 217 * <p>You can use getStartTime() to get the value of startTime.</p> 218 * 219 * @param param param 220 */ setStartTime(final Long param)221 public void setStartTime(final Long param) { 222 this.startTime = param; 223 } 224 225 /** 226 * Gets the value of duration . 227 * 228 * @return the value of duration . 229 */ getDuration()230 public Long getDuration() { 231 if (duration == -1) { 232 duration = TracePanel.DURATION - startTime; 233 } 234 return duration; 235 } 236 237 /** 238 * Sets the duration . 239 * <p>You can use getDuration() to get the value of duration.</p> 240 * 241 * @param param param 242 */ setDuration(final Long param)243 public void setDuration(final Long param) { 244 this.duration = param; 245 } 246 247 /** 248 * Gets the value of cpu . 249 * 250 * @return the value of cpu . 251 */ getCpu()252 public Integer getCpu() { 253 return cpu; 254 } 255 256 /** 257 * Sets the cpu . 258 * <p>You can use getCpu() to get the value of cpu.</p> 259 * 260 * @param param param 261 */ setCpu(final Integer param)262 public void setCpu(final Integer param) { 263 this.cpu = param; 264 } 265 266 /** 267 * Sets the isSelected . 268 * <p>You can use getSelected() to get the value of isSelected.</p> 269 * 270 * @param param param 271 */ select(final boolean param)272 public void select(final boolean param) { 273 isSelected = param; 274 } 275 276 /** 277 * repaint. 278 */ repaint()279 public void repaint() { 280 root.repaint(Utils.getX(rect), Utils.getY(rect) - padding1, rect.width, rect.height + padding2); 281 } 282 283 /** 284 * Draw the corresponding shape according to the brush 285 * 286 * @param paint graphics 287 */ 288 @Override draw(Graphics2D paint)289 public void draw(Graphics2D paint) { 290 if (isMouseIn) { 291 Common.setAlpha(paint, 0.7F); 292 } else { 293 Common.setAlpha(paint, 1.0F); 294 } 295 paint.setFont(paint.getFont().deriveFont(9f)); 296 if (isSelected && !"S".equals(state)) { 297 paint.setColor(Color.BLACK); 298 paint.fillRect(Utils.getX(rect), Utils.getY(rect) - padding1, rect.width, rect.height + padding2); 299 drawSelected(paint); 300 } else { 301 drawUnSelected(paint); 302 } 303 paint.setFont(paint.getFont().deriveFont(12f)); 304 Common.setAlpha(paint, 1.0F); 305 } 306 drawSelected(final Graphics2D paint)307 private void drawSelected(final Graphics2D paint) { 308 if ("S".equals(state)) { 309 paint.setColor(sColor); 310 Common.setAlpha(paint, alpha2); // transparency 311 paint.fillRect(Utils.getX(rect) + padding1, Utils.getY(rect), rect.width - padding2, rect.height); 312 Common.setAlpha(paint, 1f); // transparency 313 } else if ("R".equals(state) || "R+".equals(state)) { 314 paint.setColor(rColor); 315 paint.fillRect(Utils.getX(rect) + padding1, Utils.getY(rect), rect.width - padding2, rect.height); 316 paint.setColor(Color.white); 317 Common.drawStringCenter(paint, Utils.getEndState(state), rect); 318 } else if ("D".equals(state)) { 319 paint.setColor(uninterruptibleSleepColor); 320 paint.fillRect(Utils.getX(rect) + padding1, Utils.getY(rect), rect.width - padding2, rect.height); 321 paint.setColor(Color.white); 322 Common.drawStringCenter(paint, Utils.getEndState(state), rect); 323 } else if ("Running".equals(state)) { 324 paint.setColor(runningColor); 325 paint.fillRect(Utils.getX(rect) + padding1, Utils.getY(rect), rect.width - padding2, rect.height); 326 paint.setColor(Color.white); 327 Rectangle rectangle = new Rectangle(); 328 rectangle.setRect(rect.getX() + padding1, rect.getY(), rect.getWidth() - padding2, rect.getHeight()); 329 Common.drawStringCenter(paint, Utils.getEndState(state), rectangle); 330 } else { 331 paint.setColor(exitColor); 332 paint.fillRect(Utils.getX(rect) + padding1, Utils.getY(rect), rect.width - padding2, rect.height); 333 paint.setColor(Color.white); 334 Rectangle rectangle = new Rectangle(); 335 rectangle.setRect(rect.getX() + padding1, rect.getY(), rect.getWidth() - padding2, rect.getHeight()); 336 Common.drawStringCenter(paint, Utils.getEndState(state), rectangle); 337 } 338 } 339 drawUnSelected(final Graphics2D paint)340 private void drawUnSelected(final Graphics2D paint) { 341 if ("S".equals(state)) { 342 paint.setColor(sColor); 343 Common.setAlpha(paint, alpha2); // transparency 344 paint.fillRect(Utils.getX(rect), Utils.getY(rect), rect.width, rect.height); 345 Common.setAlpha(paint, 1f); // transparency 346 } else if ("R".equals(state)) { 347 paint.setColor(rColor); 348 paint.fillRect(Utils.getX(rect), Utils.getY(rect), rect.width, rect.height); 349 paint.setColor(Color.white); 350 Common.drawStringCenter(paint, Utils.getEndState(state), rect); 351 } else if ("D".equals(state)) { 352 paint.setColor(uninterruptibleSleepColor); 353 paint.fillRect(Utils.getX(rect) + padding1, Utils.getY(rect), rect.width - padding2, rect.height); 354 paint.setColor(Color.white); 355 Common.drawStringCenter(paint, Utils.getEndState(state), rect); 356 } else if ("Running".equals(state)) { 357 paint.setColor(runningColor); 358 paint.fillRect(Utils.getX(rect), Utils.getY(rect), rect.width, rect.height); 359 paint.setColor(Color.white); 360 Common.drawStringCenter(paint, Utils.getEndState(state), rect); 361 } else { 362 paint.setColor(exitColor); 363 paint.fillRect(Utils.getX(rect), Utils.getY(rect), rect.width, rect.height); 364 paint.setColor(Color.white); 365 Common.drawStringCenter(paint, Utils.getEndState(state), rect); 366 } 367 } 368 369 @Override getStringList(String time)370 public List<String> getStringList(String time) { 371 return Arrays.asList( 372 time, 373 "Thread: " + (threadName == null || threadName.isEmpty() ? AllData.threadNames.get(tid) : threadName), 374 Utils.getEndState(state), 375 TimeUtils.getTimeWithUnit(duration) 376 ); 377 } 378 379 @Override onClick(MouseEvent event)380 public void onClick(MouseEvent event) { 381 } 382 } 383