1/*
2 * Copyright 2017, 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 {transform, nanos_to_string, get_visible_chip} from './transform.js'
18
19function transform_window(entry) {
20  var chips = [];
21  var renderIdentifier = (id) => shortenComponentName(id.title) + "@" + id.hashCode;
22  var visible = entry.windowContainer.visible;
23  function transform_rect(rect, label) {
24    var r = rect || {};
25    return {
26        left: r.left || 0,
27        right: r.right || 0,
28        top: r.top || 0,
29        bottom: r.bottom || 0,
30        label,
31    }
32  }
33  var name = renderIdentifier(entry.identifier)
34  var rect = transform_rect((entry.windowFrames || entry).frame, name);
35
36  if (visible) {
37    chips.push(get_visible_chip());
38  } else {
39    rect = undefined;
40  }
41
42  return transform({
43    obj: entry,
44    kind: 'window',
45    name,
46    children: [
47      [entry.childWindows, transform_window]
48    ],
49    rect,
50    highlight: rect,
51    chips: chips,
52    visible: visible,
53  });
54}
55
56function transform_app_window_token(entry) {
57  return transform({
58    obj: entry,
59    kind: 'appWinToken',
60    name: entry.name,
61    children: [
62      [entry.windowToken.windows, transform_window],
63    ],
64  });
65}
66
67function transform_task(entry) {
68  return transform({
69    obj: entry,
70    kind: 'task',
71    name: entry.id || 0,
72    children: [
73      [entry.appWindowTokens, transform_app_window_token],
74    ],
75  });
76}
77
78function transform_stack(entry) {
79  return transform({
80    obj: entry,
81    kind: 'stack',
82    name: entry.id || 0,
83    children: [
84      [entry.tasks, transform_task],
85    ],
86  });
87}
88
89function transform_window_token(entry) {
90  return transform({
91    obj: entry,
92    kind: 'winToken',
93    name: '',
94    children: [
95      [entry.windows, transform_window],
96    ],
97  });
98}
99
100function transform_below(entry) {
101  return transform({
102    obj: entry,
103    kind: 'belowAppWindow',
104    name: '',
105    children: [
106      [entry.windows, transform_window],
107    ],
108  });
109}
110
111function transform_above(entry) {
112  return transform({
113    obj: entry,
114    kind: 'aboveAppWindow',
115    name: '',
116    children: [
117      [entry.windows, transform_window],
118    ],
119  });
120}
121
122function transform_ime(entry) {
123  return transform({
124    obj: entry,
125    kind: 'imeWindow',
126    name: '',
127    children: [
128      [entry.windows, transform_window],
129    ],
130  });
131}
132
133function transform_display(entry) {
134  var bounds = {
135    width: entry.displayInfo.logicalWidth || 0,
136    height: entry.displayInfo.logicalHeight || 0,
137  };
138
139  return transform({
140    obj: entry,
141    kind: 'display',
142    name: entry.id || 0,
143    children: [
144      [entry.aboveAppWindows, transform_above],
145      [entry.imeWindows, transform_ime],
146      [entry.stacks, transform_stack],
147      [entry.belowAppWindows, transform_below],
148    ],
149    bounds,
150  });
151}
152
153function transform_policy(entry) {
154  return transform({
155    obj: entry,
156    kind: 'policy',
157    name: 'policy',
158    children: [],
159  });
160}
161
162function transform_window_service(entry) {
163  return transform({
164    obj: entry,
165    kind: 'service',
166    name: '',
167    children: [
168      [entry.rootWindowContainer.displays, transform_display],
169      [[entry.policy], transform_policy],
170    ],
171    timestamp: entry.elapsedRealtimeNanos,
172  });
173}
174
175function transform_entry(entry) {
176  return transform({
177    obj: entry,
178    kind: 'entry',
179    name: nanos_to_string(entry.elapsedRealtimeNanos),
180    children: [
181      [entry.windowManagerService.rootWindowContainer.displays, transform_display],
182      [[entry.windowManagerService.policy], transform_policy],
183    ],
184    timestamp: entry.elapsedRealtimeNanos,
185    stableId: 'entry',
186  });
187}
188
189function transform_window_trace(entries) {
190  return transform({
191    obj: entries,
192    kind: 'entries',
193    name: 'entries',
194    children: [
195      [entries.entry, transform_entry],
196    ],
197  });
198}
199
200function shortenComponentName(name) {
201  if (!name.includes('/')) {
202    return name
203  }
204  var split = name.split('/');
205  var pkg = split[0];
206  var clazz = split.slice(1).join('/');
207  if (clazz.startsWith(pkg + '.')) {
208    clazz = clazz.slice(pkg.length + 1);
209    return [pkg, clazz].join('/');
210  }
211  return name;
212}
213
214export {transform_window_service, transform_window_trace};
215