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 {FixedStringFormatter} from 'trace/tree_node/formatters';
18import {Operation} from 'trace/tree_node/operations/operation';
19import {PropertyTreeNode} from 'trace/tree_node/property_tree_node';
20
21export class UpdateTransitionChangesNames
22  implements Operation<PropertyTreeNode>
23{
24  constructor(
25    private readonly layerIdToName: Map<number, string>,
26    private readonly windowTokenToTitle: Map<string, string>,
27  ) {}
28
29  apply(node: PropertyTreeNode): void {
30    node
31      .getChildByName('wmData')
32      ?.getChildByName('targets')
33      ?.getAllChildren()
34      .forEach((target) => {
35        const layerId = target.getChildByName('layerId');
36        if (layerId) {
37          const layerIdValue = Number(layerId.getValue());
38          const layerName = this.layerIdToName.get(layerIdValue);
39          if (layerName) {
40            layerId.setFormatter(
41              new FixedStringFormatter(`${layerIdValue} (${layerName})`),
42            );
43          }
44        }
45
46        const windowId = target.getChildByName('windowId');
47        if (windowId) {
48          const windowIdString = windowId.getValue().toString(16);
49          const windowTitle = this.windowTokenToTitle.get(windowIdString);
50          windowId.setFormatter(
51            new FixedStringFormatter(
52              windowTitle
53                ? `0x${windowIdString} (${windowTitle})`
54                : `0x${windowIdString}`,
55            ),
56          );
57        }
58      });
59  }
60}
61