xref: /aosp_15_r20/development/tools/winscope/src/viewers/viewer_media_based/presenter.ts (revision 90c8c64db3049935a07c6143d7fd006e26f8ecca)
1/*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import {WinscopeEvent, WinscopeEventType} from 'messaging/winscope_event';
18import {EmitEvent} from 'messaging/winscope_event_emitter';
19import {MediaBasedTraceEntry} from 'trace/media_based_trace_entry';
20import {Trace, TraceEntry} from 'trace/trace';
21import {TraceEntryFinder} from 'trace/trace_entry_finder';
22import {UiData} from './ui_data';
23
24export type NotifyHierarchyViewCallbackType<UiData> = (uiData: UiData) => void;
25
26export class Presenter {
27  private readonly uiData: UiData;
28
29  constructor(
30    private readonly traces: Array<Trace<MediaBasedTraceEntry>>,
31    private readonly notifyViewCallback: NotifyHierarchyViewCallbackType<UiData>,
32  ) {
33    this.uiData = new UiData(
34      this.traces.map((trace) => trace.getDescriptors().join(', ')),
35    );
36    this.notifyViewCallback(this.uiData);
37  }
38
39  setEmitEvent(callback: EmitEvent) {
40    // do nothing
41  }
42
43  async onAppEvent(event: WinscopeEvent) {
44    await event.visit(
45      WinscopeEventType.TRACE_POSITION_UPDATE,
46      async (event) => {
47        const traceEntries = this.traces
48          .map((trace) =>
49            TraceEntryFinder.findCorrespondingEntry(trace, event.position),
50          )
51          .filter((entry) => entry !== undefined) as Array<
52          TraceEntry<MediaBasedTraceEntry>
53        >;
54        const entries: MediaBasedTraceEntry[] = await Promise.all(
55          traceEntries.map((entry) => {
56            return entry.getValue();
57          }),
58        );
59        this.uiData.currentTraceEntries = entries;
60        this.notifyViewCallback(this.uiData);
61      },
62    );
63    await event.visit(
64      WinscopeEventType.EXPANDED_TIMELINE_TOGGLED,
65      async (event) => {
66        this.uiData.forceMinimize = event.isTimelineExpanded;
67        this.notifyViewCallback(this.uiData);
68      },
69    );
70  }
71}
72