1/*
2 * Copyright (C) 2022 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 {ScreenRecordingTraceEntry} from 'trace/screen_recording';
19import {Trace} from 'trace/trace';
20import {Traces} from 'trace/traces';
21import {TraceEntryFinder} from 'trace/trace_entry_finder';
22import {TraceType} from 'trace/trace_type';
23import {View, Viewer, ViewType} from 'viewers/viewer';
24import {ViewerScreenRecordingComponent} from './viewer_screen_recording_component';
25
26export class ViewerScreenRecording implements Viewer {
27  static readonly DEPENDENCIES: TraceType[] = [TraceType.SCREEN_RECORDING];
28
29  private readonly trace: Trace<ScreenRecordingTraceEntry>;
30  private readonly htmlElement: HTMLElement;
31  private readonly view: View;
32
33  constructor(trace: Trace<ScreenRecordingTraceEntry>, traces: Traces) {
34    this.trace = trace;
35    this.htmlElement = document.createElement('viewer-screen-recording');
36    this.view = new View(
37      ViewType.OVERLAY,
38      this.getTraces(),
39      this.htmlElement,
40      'ScreenRecording',
41    );
42  }
43
44  async onWinscopeEvent(event: WinscopeEvent) {
45    await event.visit(
46      WinscopeEventType.TRACE_POSITION_UPDATE,
47      async (event) => {
48        const entry = TraceEntryFinder.findCorrespondingEntry(
49          this.trace,
50          event.position,
51        );
52        (
53          this.htmlElement as unknown as ViewerScreenRecordingComponent
54        ).currentTraceEntry = await entry?.getValue();
55      },
56    );
57    await event.visit(
58      WinscopeEventType.EXPANDED_TIMELINE_TOGGLED,
59      async (event) => {
60        (
61          this.htmlElement as unknown as ViewerScreenRecordingComponent
62        ).forceMinimize = event.isTimelineExpanded;
63      },
64    );
65  }
66
67  setEmitEvent() {
68    // do nothing
69  }
70
71  getViews(): View[] {
72    return [this.view];
73  }
74
75  getTraces(): Array<Trace<ScreenRecordingTraceEntry>> {
76    return [this.trace];
77  }
78}
79