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 {
18    KeyguardControllerState,
19    RootWindowContainer,
20    WindowManagerPolicy,
21    WindowManagerState
22} from "./common"
23
24import WindowContainer from "./windows/WindowContainer"
25
26WindowManagerState.fromProto = function (proto: any, timestamp: number = 0, where: string = ""): WindowManagerState {
27    var inputMethodWIndowAppToken = "";
28    if (proto.inputMethodWindow != null) {
29        proto.inputMethodWindow.hashCode.toString(16)
30    };
31
32    const rootWindowContainer = createRootWindowContainer(proto.rootWindowContainer);
33    const keyguardControllerState = createKeyguardControllerState(
34        proto.rootWindowContainer.keyguardController);
35    const policy = createWindowManagerPolicy(proto.policy);
36
37    const entry = new WindowManagerState(
38        where,
39        policy,
40        proto.focusedApp,
41        proto.focusedDisplayId,
42        proto.focusedWindow?.title ?? "",
43        inputMethodWIndowAppToken,
44        proto.rootWindowContainer.isHomeRecentsComponent,
45        proto.displayFrozen,
46        proto.rootWindowContainer.pendingActivities.map(it => it.title),
47        rootWindowContainer,
48        keyguardControllerState,
49        /*timestamp */ `${timestamp}`
50    );
51
52    addAttributes(entry, proto);
53    console.warn("Created ", entry.kind, " stableId=", entry.stableId)
54    return entry
55}
56
57function addAttributes(entry: WindowManagerState, proto: any) {
58    entry.kind = entry.constructor.name;
59    // There no JVM/JS translation for Longs yet
60    entry.timestampMs = entry.timestamp.toString();
61    entry.rects = entry.windowStates.reverse().map(it => it.rect);
62    if (!entry.isComplete()) {
63        entry.isIncompleteReason = entry.getIsIncompleteReason();
64    }
65    entry.proto = proto;
66    entry.shortName = entry.name;
67    entry.chips = [];
68    entry.isVisible = true;
69}
70
71function createWindowManagerPolicy(proto: any): WindowManagerPolicy {
72    return new WindowManagerPolicy(
73        proto.focusedAppToken ?? "",
74        proto.forceStatusBar,
75        proto.forceStatusBarFromKeyguard,
76        proto.keyguardDrawComplete,
77        proto.keyguardOccluded,
78        proto.keyguardOccludedChanged,
79        proto.keyguardOccludedPending,
80        proto.lastSystemUiFlags,
81        proto.orientation,
82        proto.rotation,
83        proto.rotationMode,
84        proto.screenOnFully,
85        proto.windowManagerDrawComplete
86    );
87}
88
89function createRootWindowContainer(proto: any): RootWindowContainer {
90    const windowContainer = WindowContainer.fromProto(
91        /* proto */ proto.windowContainer,
92        /* childrenProto */ proto.windowContainer.children.reverse()
93    );
94
95    if (windowContainer == null) {
96        throw new Error(`Window container should not be null.\n${JSON.stringify(proto)}`);
97    }
98    const entry = new RootWindowContainer(windowContainer);
99    return entry;
100}
101
102function createKeyguardControllerState(proto: any): KeyguardControllerState {
103    const keyguardOccludedStates = {};
104
105    if (proto) {
106        proto.keyguardOccludedStates.forEach(it =>
107            keyguardOccludedStates[it.displayId] = it.keyguardOccluded);
108    }
109
110    return new KeyguardControllerState(
111        proto?.isAodShowing ?? false,
112        proto?.isKeyguardShowing ?? false,
113        keyguardOccludedStates
114    );
115}
116
117export default WindowManagerState;