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.trace; 17 18 import java.util.Iterator; 19 import java.util.List; 20 import java.util.Objects; 21 import java.util.concurrent.CopyOnWriteArrayList; 22 23 /** 24 * event dispatcher class 25 * 26 * @since 2021/5/24 12:00 27 */ 28 public class EventDispatcher { 29 private static List<IFuncClick> clickEvents = new CopyOnWriteArrayList<>(); 30 private static List<ITimeRange> rangeEvents = new CopyOnWriteArrayList<>(); 31 private static List<IThreadRange> threadEvents = new CopyOnWriteArrayList<>(); 32 private static List<IFuncChange> funcChanges = new CopyOnWriteArrayList<>(); 33 34 /** 35 * remove current RangeListener 36 * 37 * @param event event 38 */ removeRangeListener(ITimeRange event)39 public static void removeRangeListener(ITimeRange event) { 40 rangeEvents.remove(event); 41 } 42 43 /** 44 * remove current thread RangeListener 45 * 46 * @param event event 47 */ removeThreadRangeListener(IThreadRange event)48 public static void removeThreadRangeListener(IThreadRange event) { 49 threadEvents.remove(event); 50 } 51 52 /** 53 * dispatcher the time Range 54 * 55 * @param startNS startNS 56 * @param endNS endNS 57 * @param scale scale 58 */ dispatcherRange(long startNS, long endNS, long scale)59 public static void dispatcherRange(long startNS, long endNS, long scale) { 60 Iterator<ITimeRange> iterator = rangeEvents.iterator(); 61 long endTimeNs = endNS; 62 while (iterator.hasNext()) { 63 ITimeRange event = iterator.next(); 64 if (endNS == 0) { 65 endTimeNs = TracePanel.endNS; 66 } 67 event.change(startNS, endTimeNs, scale); 68 } 69 } 70 71 /** 72 * dispatcherThreadRange 73 * 74 * @param startNS startNS 75 * @param endNS endNS 76 * @param threadIds threadIds 77 */ dispatcherThreadRange(long startNS, long endNS, List<Integer> threadIds)78 public static void dispatcherThreadRange(long startNS, long endNS, List<Integer> threadIds) { 79 Iterator<IThreadRange> iterator = threadEvents.iterator(); 80 while (iterator.hasNext()) { 81 IThreadRange event = iterator.next(); 82 event.change(startNS, endNS, threadIds); 83 } 84 } 85 86 /** 87 * dispatcherClickListener 88 * 89 * @param node node 90 */ dispatcherClickListener(AbstractNode node)91 public static void dispatcherClickListener(AbstractNode node) { 92 Iterator<IFuncClick> iterator = clickEvents.iterator(); 93 while (iterator.hasNext()) { 94 IFuncClick event = iterator.next(); 95 event.change(node); 96 } 97 } 98 99 /** 100 * addRangeListener 101 * 102 * @param listener listener 103 */ addRangeListener(ITimeRange listener)104 public static void addRangeListener(ITimeRange listener) { 105 if (!rangeEvents.contains(listener)) { 106 rangeEvents.add(listener); 107 } 108 } 109 110 /** 111 * addThreadRangeListener 112 * 113 * @param listener listener 114 */ addThreadRangeListener(IThreadRange listener)115 public static void addThreadRangeListener(IThreadRange listener) { 116 if (!threadEvents.contains(listener)) { 117 threadEvents.add(listener); 118 } 119 } 120 121 /** 122 * addClickListener 123 * 124 * @param listener listener 125 */ addClickListener(IFuncClick listener)126 public static void addClickListener(IFuncClick listener) { 127 if (!clickEvents.contains(listener)) { 128 clickEvents.add(listener); 129 } 130 } 131 132 /** 133 * clearData 134 */ clearData()135 public static void clearData() { 136 if (Objects.nonNull(clickEvents)) { 137 clickEvents.clear(); 138 } 139 if (Objects.nonNull(rangeEvents)) { 140 rangeEvents.clear(); 141 } 142 if (Objects.nonNull(threadEvents)) { 143 threadEvents.clear(); 144 } 145 if (Objects.nonNull(funcChanges)) { 146 funcChanges.clear(); 147 } 148 } 149 150 /** 151 * add func select change listener 152 * 153 * @param listener select range change listener 154 */ addFuncSelectChange(IFuncChange listener)155 public static void addFuncSelectChange(IFuncChange listener) { 156 if (!funcChanges.contains(listener)) { 157 funcChanges.add(listener); 158 } 159 } 160 161 /** 162 * dispatcherClickListener 163 * 164 * @param id id 165 */ dispatcherFuncChangeListener(Integer id)166 public static void dispatcherFuncChangeListener(Integer id) { 167 Iterator<IFuncChange> iterator = funcChanges.iterator(); 168 while (iterator.hasNext()) { 169 IFuncChange event = iterator.next(); 170 event.change(id); 171 } 172 } 173 } 174