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 {assertTrue} from 'common/assert_utils';
18import {Trace} from 'trace/trace';
19import {Traces} from 'trace/traces';
20import {TraceType, TraceTypeUtils} from 'trace/trace_type';
21import {Viewer} from './viewer';
22import {ViewerInputMethodClients} from './viewer_input_method_clients/viewer_input_method_clients';
23import {ViewerInputMethodManagerService} from './viewer_input_method_manager_service/viewer_input_method_manager_service';
24import {ViewerInputMethodService} from './viewer_input_method_service/viewer_input_method_service';
25import {ViewerProtoLog} from './viewer_protolog/viewer_protolog';
26import {ViewerScreenshot} from './viewer_screen_recording/viewer_screenshot';
27import {ViewerScreenRecording} from './viewer_screen_recording/viewer_screen_recording';
28import {ViewerSurfaceFlinger} from './viewer_surface_flinger/viewer_surface_flinger';
29import {ViewerTransactions} from './viewer_transactions/viewer_transactions';
30import {ViewerTransitions} from './viewer_transitions/viewer_transitions';
31import {ViewerViewCapture} from './viewer_view_capture/viewer_view_capture';
32import {ViewerWindowManager} from './viewer_window_manager/viewer_window_manager';
33
34class ViewerFactory {
35  static readonly VIEWERS = [
36    ViewerSurfaceFlinger,
37    ViewerWindowManager,
38    ViewerInputMethodClients,
39    ViewerInputMethodManagerService,
40    ViewerInputMethodService,
41    ViewerTransactions,
42    ViewerProtoLog,
43    ViewerScreenRecording,
44    ViewerScreenshot,
45    ViewerTransitions,
46  ];
47
48  createViewers(traces: Traces, storage: Storage): Viewer[] {
49    const viewers: Viewer[] = [];
50
51    // regular viewers
52    traces.forEachTrace((trace) => {
53      ViewerFactory.VIEWERS.forEach((Viewer) => {
54        assertTrue(Viewer.DEPENDENCIES.length === 1);
55        const isViewerDepSatisfied = trace.type === Viewer.DEPENDENCIES[0];
56        if (isViewerDepSatisfied) {
57          viewers.push(new Viewer(trace as Trace<any>, traces, storage));
58        }
59      });
60    });
61
62    // ViewCapture viewer (instantiate one viewer for N ViewCapture traces)
63    const availableTraceTypes = new Set(traces.mapTrace((trace) => trace.type));
64    const isViewerDepSatisfied = ViewerViewCapture.DEPENDENCIES.some(
65      (traceType: TraceType) => availableTraceTypes.has(traceType),
66    );
67    if (isViewerDepSatisfied) {
68      viewers.push(new ViewerViewCapture(traces, storage));
69    }
70
71    // Note:
72    // the final order of tabs/views in the UI corresponds the order of the
73    // respective viewers below
74    return viewers.sort((a, b) =>
75      TraceTypeUtils.compareByDisplayOrder(
76        a.getTraces()[0].type,
77        b.getTraces()[0].type,
78      ),
79    );
80  }
81}
82
83export {ViewerFactory};
84