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