• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright 2020, 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 {Size, toRect, WindowLayoutParams, WindowState} from '../common';
18import {shortenName} from '../mixin';
19import {WindowContainer} from './WindowContainer';
20
21WindowState.fromProto = (
22  proto: any,
23  isActivityInTree: boolean,
24  nextSeq: () => number
25): WindowState => {
26  if (proto == null) {
27    return null;
28  } else {
29    const windowParams = createWindowLayoutParams(proto.attributes);
30    const identifierName = getIdentifier(proto);
31    const windowType = getWindowType(proto, identifierName);
32    const name = getName(identifierName);
33    const windowContainer = WindowContainer.fromProto(
34      /* proto */ proto.windowContainer,
35      /* protoChildren */ proto.windowContainer?.children ?? [],
36      /* isActivityInTree */ isActivityInTree,
37      /* computedZ */ nextSeq,
38      /* nameOverride */ name,
39      /* identifierOverride */ proto.identifier
40    );
41
42    const entry = new WindowState(
43      windowParams,
44      proto.displayId,
45      proto.stackId,
46      proto.animator?.surface?.layer ?? 0,
47      proto.animator?.surface?.shown ?? false,
48      windowType,
49      new Size(proto.requestedWidth, proto.requestedHeight),
50      toRect(proto.surfacePosition),
51      toRect(proto.windowFrames?.frame ?? null),
52      toRect(proto.windowFrames?.containingFrame ?? null),
53      toRect(proto.windowFrames?.parentFrame ?? null),
54      toRect(proto.windowFrames?.contentFrame ?? null),
55      toRect(proto.windowFrames?.contentInsets ?? null),
56      toRect(proto.surfaceInsets),
57      toRect(proto.givenContentInsets),
58      toRect(proto.animator?.lastClipRect ?? null),
59      windowContainer,
60      /* isAppWindow */ isActivityInTree
61    );
62
63    addAttributes(entry, proto);
64    return entry;
65  }
66};
67
68function createWindowLayoutParams(proto: any): WindowLayoutParams {
69  return new WindowLayoutParams(
70    /* type */ proto?.type ?? 0,
71    /* x */ proto?.x ?? 0,
72    /* y */ proto?.y ?? 0,
73    /* width */ proto?.width ?? 0,
74    /* height */ proto?.height ?? 0,
75    /* horizontalMargin */ proto?.horizontalMargin ?? 0,
76    /* verticalMargin */ proto?.verticalMargin ?? 0,
77    /* gravity */ proto?.gravity ?? 0,
78    /* softInputMode */ proto?.softInputMode ?? 0,
79    /* format */ proto?.format ?? 0,
80    /* windowAnimations */ proto?.windowAnimations ?? 0,
81    /* alpha */ proto?.alpha ?? 0,
82    /* screenBrightness */ proto?.screenBrightness ?? 0,
83    /* buttonBrightness */ proto?.buttonBrightness ?? 0,
84    /* rotationAnimation */ proto?.rotationAnimation ?? 0,
85    /* preferredRefreshRate */ proto?.preferredRefreshRate ?? 0,
86    /* preferredDisplayModeId */ proto?.preferredDisplayModeId ?? 0,
87    /* hasSystemUiListeners */ proto?.hasSystemUiListeners ?? false,
88    /* inputFeatureFlags */ proto?.inputFeatureFlags ?? 0,
89    /* userActivityTimeout */ proto?.userActivityTimeout ?? 0,
90    /* colorMode */ proto?.colorMode ?? 0,
91    /* flags */ proto?.flags ?? 0,
92    /* privateFlags */ proto?.privateFlags ?? 0,
93    /* systemUiVisibilityFlags */ proto?.systemUiVisibilityFlags ?? 0,
94    /* subtreeSystemUiVisibilityFlags */ proto?.subtreeSystemUiVisibilityFlags ?? 0,
95    /* appearance */ proto?.appearance ?? 0,
96    /* behavior */ proto?.behavior ?? 0,
97    /* fitInsetsTypes */ proto?.fitInsetsTypes ?? 0,
98    /* fitInsetsSides */ proto?.fitInsetsSides ?? 0,
99    /* fitIgnoreVisibility */ proto?.fitIgnoreVisibility ?? false
100  );
101}
102
103function getWindowType(proto: any, identifierName: string): number {
104  if (identifierName.startsWith(WindowState.STARTING_WINDOW_PREFIX)) {
105    return WindowState.WINDOW_TYPE_STARTING;
106  } else if (proto.animatingExit) {
107    return WindowState.WINDOW_TYPE_EXITING;
108  } else if (identifierName.startsWith(WindowState.DEBUGGER_WINDOW_PREFIX)) {
109    return WindowState.WINDOW_TYPE_STARTING;
110  }
111
112  return 0;
113}
114
115function getName(identifierName: string): string {
116  let name = identifierName;
117
118  if (identifierName.startsWith(WindowState.STARTING_WINDOW_PREFIX)) {
119    name = identifierName.substring(WindowState.STARTING_WINDOW_PREFIX.length);
120  } else if (identifierName.startsWith(WindowState.DEBUGGER_WINDOW_PREFIX)) {
121    name = identifierName.substring(WindowState.DEBUGGER_WINDOW_PREFIX.length);
122  }
123
124  return name;
125}
126
127function getIdentifier(proto: any): string {
128  return proto.windowContainer.identifier?.title ?? proto.identifier?.title ?? '';
129}
130
131function addAttributes(entry: WindowState, proto: any) {
132  entry.kind = entry.constructor.name;
133  entry.rect = entry.frame;
134  entry.rect.ref = entry;
135  entry.rect.label = entry.name;
136  entry.proto = proto;
137  entry.proto.configurationContainer = proto.windowContainer?.configurationContainer;
138  entry.proto.surfaceControl = proto.windowContainer?.surfaceControl;
139  entry.shortName = shortenName(entry.name);
140}
141
142export {WindowState};
143